diff --git a/packages/quantum-js-cli/runner.js b/packages/quantum-js-cli/runner.js index 4d7f16e..b1ccae9 100644 --- a/packages/quantum-js-cli/runner.js +++ b/packages/quantum-js-cli/runner.js @@ -9,17 +9,17 @@ function prepareCircuit() { console.clear(); let circuit; while(!selection) { - console.log("Please select a method to begin circuit creation: "); + //the following prompt requires the user to select between a number of options to create a circuit. they will enter the NUMBER that corresponds with the action they'd like. + console.log("Please select a method (number value) to begin circuit creation: "); console.log("1. From Scratch\n" + - "2. From Text Diagram\n" - /*+ "3. From Table Transposed\n"*/); - selection = +prompt(mark); + "2. From Text Diagram\n"); + selection = Number(prompt(mark)); switch(selection) { case 1: let num_registers = NaN; while(!num_registers) { console.log("Enter the number of qubits you would like to start out with.\n"); - num_registers = +prompt(mark); + num_registers = Number(prompt(mark)); } circuit = Q(num_registers, 8); break; @@ -27,7 +27,7 @@ function prepareCircuit() { circuit = prepareCircuitFromTable(); break; default: - selection = NaN + selection = NaN; } } if(!(circuit instanceof Circuit)) { @@ -83,14 +83,16 @@ function evaluateOperation(input, circuit) { //Syntax: GateSymbol(momentIndex, [registerIndex0, registerIndex1,...]) //Regex explanation: looks for the first INTEGER of the from "(digit0digit1digit2..." and removes the "(" at the beginning. let momentIndex = +(/\(\s*\d+/).exec(input)[0].substring(1); + if(momentIndex > circuit.timewidth || momentIndex < 0) return logger.error("Moment index out of bounds"); if(momentIndex === undefined) { return logger.error("Invalid gate set operation syntax"); } - // + let registerIndices; - let re = /\[(\s*\d+\s*,{0,1}\s*)+\]/g; + //This is a regex that selects an array of integers from a string, i.e. any substring of the form "[integer1, integer2, integer3...]" + let arrayRegex = /\[(\s*\d+\s*,{0,1}\s*)+\]/g; try { - registerIndices = (re) + registerIndices = (arrayRegex) .exec(input)[0] .slice(1, -1) .split(',') @@ -99,12 +101,15 @@ function evaluateOperation(input, circuit) { catch(e) { return logger.error("Invalid gate set operation syntax"); } + if(!registerIndices.every(index => { + return index > 0 && index < circuit.bandwidth; + })) return logger.error("Register index out of bounds"); let newParameters = {}; - input = input.substring(re.lastIndex); - re = /\d+\.{0,1}\d*/g + input = input.substring(arrayRegex.lastIndex); + let commaSeparatedDecimalRegex = /\d+\.{0,1}\d*/g let input_params = []; - while(value = re.exec(input)) { - input_params.push(+value[0]); + while(value = commaSeparatedDecimalRegex.exec(input)) { + input_params.push(Number(value[0])); } input_params.reverse(); if(gate.has_parameters) { @@ -116,7 +121,7 @@ function evaluateOperation(input, circuit) { } }); } - else if(input_params.length !== 0) return logger.error("c Invalid gate set operation syntax"); + else if(input_params.length !== 0) return logger.error("Invalid gate set operation syntax"); return circuit[functionName](momentIndex, registerIndices, newParameters); } @@ -128,18 +133,18 @@ function removeOperation(input, circuit) { } // let registerIndices; - let re = /\[(\s*\d+\s*,{0,1}\s*)+\]/g; + let arrayRegex = /\[(\s*\d+\s*,{0,1}\s*)+\]/g; try { - registerIndices = (re) + registerIndices = (arrayRegex) .exec(input)[0] .slice(1, -1) .split(',') - .map(index => +index); + .map(index => Number(index)); } catch(e) { return logger.error("Invalid gate set operation syntax"); } - if(input.substring(re.lastIndex).trim() != ")") { + if(input.substring(arrayRegex.lastIndex).trim() != ")") { return logger.error("Invalid gate set operation syntax"); } let operationToRemove = circuit.get(momentIndex, registerIndices[0]); @@ -182,16 +187,16 @@ function evaluateInput(input, circuit) { else circuit = prepareCircuit(); break; default: - let circuitBackup = circuit.clone(); - let re = /((\w+-*)+\(\s*\d+\s*,\s*\[(\s*\d+\s*,{0,1}\s*)+\]\s*(,\s*\d+\.{0,1}\d*\s*)*\))/g; - while(functionCall = re.exec(input)) { + let circuitBackup = circuit.toText(); + let functionCallRegex = /((\w+-*)+\(\s*\d+\s*,\s*\[(\s*\d+\s*,{0,1}\s*)+\]\s*(,\s*\d+\.{0,1}\d*\s*)*\))/g; + while(functionCall = functionCallRegex.exec(input)) { functionCall = functionCall[0]; let functionName = (/[^\s,\[\]()]+/g).exec(functionCall)[0]; //checks that the function call is gate set$ operation and not another circuit operation. //Syntax: GateSymbol(momentIndex, [registerIndex0, registerIndex1,...]) if(circuit[functionName] instanceof Function && (Gate.findBySymbol(functionName) instanceof Gate || Gate.findByNameCss(functionName) instanceof Gate)) { if(evaluateOperation(functionCall, circuit) === "(error)") { - circuit = circuitBackup; + circuit = Q(circuitBackup); break; } } @@ -213,7 +218,7 @@ function run() { let circuit = prepareCircuit(); let input = prompt(mark); while(input !== "quit" && input !== "q") { - evaluateInput(input, circuit); + circuit = evaluateInput(input, circuit); input = prompt(mark) } diff --git a/packages/quantum-js-util/Q-Circuit.js b/packages/quantum-js-util/Q-Circuit.js index 8d1c4b6..a6e4dc2 100644 --- a/packages/quantum-js-util/Q-Circuit.js +++ b/packages/quantum-js-util/Q-Circuit.js @@ -1615,16 +1615,15 @@ print(task.result().measurement_counts)` const original = this let { - registerFirstIndex, - registerRange, - registerLastIndex, + qubitFirstIndex, + qubitRange, + qubitLastIndex, momentFirstIndex, momentRange, momentLastIndex } = this.determineRanges( options ) - - const copy = new Circuit( registerRange, momentRange ) + const copy = new Circuit( qubitRange, momentRange ) original.operations .filter( function( operation ){ @@ -1635,8 +1634,8 @@ print(task.result().measurement_counts)` operation.momentIndex >= momentFirstIndex && operation.momentIndex < momentLastIndex && - operation.registerIndex >= registerFirstIndex && - operation.registerIndex < registerLastIndex + operation.registerIndex >= qubitFirstIndex && + operation.registerIndex < qubitLastIndex ) })) }) @@ -1948,7 +1947,10 @@ Object.entries( Gate.constants ).forEach( function( entry ){ this.set$( gate, momentIndex, registerIndexOrIndices ) return this } - Circuit.prototype[ gateConstantName ] = set$ + Circuit.prototype[ gate.name ] = set$, + Circuit.prototype[ gate.name.toLowerCase() ] = set$, + Circuit.prototype[ gate.nameCss ] = set$, + Circuit.prototype[ gate.nameCss.toLowerCase() ] = set$, Circuit.prototype[ gate.symbol ] = set$ Circuit.prototype[ gate.symbol.toLowerCase() ] = set$ }) diff --git a/packages/quantum-js-util/Q-Gate.js b/packages/quantum-js-util/Q-Gate.js index 43190ff..488a666 100644 --- a/packages/quantum-js-util/Q-Gate.js +++ b/packages/quantum-js-util/Q-Gate.js @@ -98,6 +98,9 @@ Object.assign( Gate, { findByName: function( name ){ return Gate.findBy( 'name', name ) + }, + findByNameCss: function( nameCss ) { + return Gate.findBy( 'nameCss', nameCss ) } })