Skip to content

Commit

Permalink
RClarify regex usage, user input prompts, and correct circuitBackup g…
Browse files Browse the repository at this point in the history
…eneration in evaluateInput() method
  • Loading branch information
Altan Nagji committed Aug 13, 2021
1 parent 4e46ec2 commit 0a4aec8
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions packages/quantum-js-cli/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ 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;
case 2:
circuit = prepareCircuitFromTable();
break;
default:
selection = NaN
selection = NaN;
}
}
if(!(circuit instanceof Circuit)) {
Expand Down Expand Up @@ -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(',')
Expand All @@ -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) {
Expand All @@ -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);
}

Expand All @@ -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]);
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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)
}

Expand Down

0 comments on commit 0a4aec8

Please sign in to comment.