Skip to content

Commit

Permalink
Merge pull request #7 from Altanali/quantum-js-cli
Browse files Browse the repository at this point in the history
Quantum js cli
  • Loading branch information
Altanali authored Aug 13, 2021
2 parents 1c99533 + 21ef0ab commit ade84d7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
51 changes: 28 additions & 23 deletions packages/quantum-js-cli/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ 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;
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
18 changes: 10 additions & 8 deletions packages/quantum-js-util/Q-Circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ){
Expand All @@ -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
)
}))
})
Expand Down Expand Up @@ -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$
})
Expand Down
3 changes: 3 additions & 0 deletions packages/quantum-js-util/Q-Gate.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ Object.assign( Gate, {
findByName: function( name ){

return Gate.findBy( 'name', name )
},
findByNameCss: function( nameCss ) {
return Gate.findBy( 'nameCss', nameCss )
}
})

Expand Down

0 comments on commit ade84d7

Please sign in to comment.