diff --git a/packages/quantum-js-cli/runner.js b/packages/quantum-js-cli/runner.js index 4d7f16e..b8c7359 100644 --- a/packages/quantum-js-cli/runner.js +++ b/packages/quantum-js-cli/runner.js @@ -13,13 +13,13 @@ function prepareCircuit() { console.log("1. From Scratch\n" + "2. From Text Diagram\n" /*+ "3. From Table Transposed\n"*/); - selection = +prompt(mark); + 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) }