-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Quantum js cli #34
Open
Altanali
wants to merge
32
commits into
ltnln-npm-package
Choose a base branch
from
quantum-js-cli
base: ltnln-npm-package
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Quantum js cli #34
Changes from 2 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
40ae959
Create quantum-js-cli package
e66857c
Create tests for quantum-js-cli runner.js
9fa52c3
Update HTML files to reference bundled Qjs
788727e
Set up build script for bundling css files
e7de83d
Update dispatchCustomEventToGlobal method and references in other files
105614d
Change entry point for quantum-js-util package
9692c33
Create quantum-js-vis visualization package for the GUI
931f168
Adjust help() calls to use logger module
54dd31f
Create bundles
b3f92a8
Merge pull request #1 from Altanali/quantum-js-vis-branch
Altanali c36ecd9
Update gate to not create default amazonBraketSymbols for gates.
f6f5551
Merge pull request #2 from Altanali/quantum-js-vis-branch
Altanali 1eb9203
Update bundle.js, no longer creates default AmazonBraketSymbols for Qjs
e98a7af
Merge pull request #3 from Altanali/quantum-js-vis-branch
Altanali 19eff83
Reduce width of palette on creation in a JupyterNotebook via braket()…
50c6ea4
Merge pull request #4 from Altanali/quantum-js-vis-branch
Altanali c3c08fa
Reduce width of palette on creation in a JupyterNotebook via braket()…
059d221
Update eslint scripts in root and quantum-js-vis package
26ad302
Correct circuit.clone() variable names and add gate.name and gate.css…
4e46ec2
Add Gate.findByNameCss function to Gate class
0a4aec8
RClarify regex usage, user input prompts, and correct circuitBackup g…
fd302f3
Change prepareCircuit() prompt to clarify that a Number is wanted by …
49b52cd
Revert Q.css to original Q.css to remove unicode discrepancies. Updat…
1c99533
Merge pull request #5 from Altanali/quantum-js-vis-branch
Altanali 21ef0ab
Merge pull request #6 from Altanali/quantum-js-vis-branch
Altanali ade84d7
Merge pull request #7 from Altanali/quantum-js-cli
Altanali cc37d84
Update set10754() when referred to by gate names/symbols to include p…
afc8355
Make prompt an argument for testability and flexible i/o.
2547699
Add tests for prepareCircuit(), only from scratch
aff2a77
Change quantum-js-util entry point to index.js
374e395
remove extraneous console.log
5608587
packages/quantum-js-cli/runner.js
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
const runner = require('../runner'); | ||
const quantumjs = require('quantum-js-util'); | ||
|
||
|
||
describe("Checking evaluateInput calls the correct functions and/or logs the correct information given a certain input", () => { | ||
//create empty circuit. | ||
let circuit = quantumjs.Q(); | ||
console.log = jest.fn(); | ||
test("Testing evaluateInput() with input 'help' and an empty circuit.", () => { | ||
let expectedText = runner.printMenu(); | ||
runner.evaluateInput("help", circuit); | ||
expect(console.log).toHaveBeenCalled(); | ||
expect(console.log).toHaveBeenCalledWith(expectedText); | ||
}) | ||
test("Testing evaluateInput() with input '-h' and an empty circuit.", () => { | ||
let expectedText = runner.printMenu(); | ||
runner.evaluateInput("-h", circuit); | ||
expect(console.log).toHaveBeenCalled(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
expect(console.log).toHaveBeenCalledWith(expectedText); | ||
}) | ||
test("Testing evaluateInput() with input 'toAmazonBraket' and an empty circuit", () => { | ||
runner.evaluateInput("toAmazonBraket", circuit); | ||
expectedText = circuit.toAmazonBraket(); | ||
expect(console.log).toHaveBeenCalled(); | ||
expect(console.log).toHaveBeenCalledWith(expectedText); | ||
}) | ||
test("Testing evaluateInput() with input 'toDiagram' and an empty circuit", () => { | ||
runner.evaluateInput("toDiagram", circuit); | ||
expectedText = circuit.toDiagram(); | ||
expect(console.log).toHaveBeenCalled(); | ||
expect(console.log).toHaveBeenCalledWith(expectedText); | ||
}) | ||
test("Testing evaluateInput() with input 'toLaTeX' and an empty circuit", () => { | ||
runner.evaluateInput("toLaTeX", circuit); | ||
expectedText = circuit.toLatex(); | ||
expect(console.log).toHaveBeenCalled(); | ||
expect(console.log).toHaveBeenCalledWith(expectedText); | ||
}) | ||
test("Testing evaluateInput() with input 'report' and an empty circuit", () => { | ||
runner.evaluateInput("report", circuit); | ||
expectedText = circuit.report$(); | ||
expect(console.log).toHaveBeenCalled(); | ||
expect(console.log).toHaveBeenCalledWith(expectedText); | ||
}) | ||
test("Testing evaluateInput() with input 'toText' and an empty circuit", () => { | ||
runner.evaluateInput("toText", circuit); | ||
expectedText = circuit.toAmazonBraket(); | ||
expect(console.log).toHaveBeenCalled(); | ||
expect(console.log).toHaveBeenCalledWith(expectedText); | ||
}) | ||
test("Testing evaluateInput() with input 'clear' and an empty circuit", () => { | ||
console.clear = jest.fn(); | ||
runner.evaluateInput("clear", circuit); | ||
expectedText = circuit.toAmazonBraket(); | ||
expect(console.clear).toHaveBeenCalled(); | ||
}) | ||
}) | ||
|
||
//Gate operation syntax is of the regex form /(\w+\(\s*\d+\s*,\s*\[(\s*\d+\s*,{0,1}\s*)+\](,\s*\d+\.{0,1}\d+\s*)*\))/g | ||
//or more easily described: | ||
//'gate-symbol(moment-index, [registerIndex0, registerIndex1,...], parameter0, parameter1,...)' with white space allowed liberally | ||
describe("Testing various forms of gate-operation call expression and see that evaluateOperation is called", ()=> { | ||
//create empty circuit. | ||
test("Check that evaluateOperation is called once for input 'h(1, [1])'", () => { | ||
let circuit = quantumjs.Q(); | ||
runner.evaluateInput("h(1, [1])", circuit); | ||
expect(circuit.get(1, 1).gate.symbol).toBe('H'); | ||
}) | ||
//messing with the white space | ||
test("Check that evaluateOperation is called once for input 'h( 1 , [ 1 ])'", () => { | ||
let circuit = quantumjs.Q(); | ||
runner.evaluateInput("h( 1 , [ 1 ])", circuit) | ||
expect(circuit.get(1, 1).gate.symbol).toBe('H'); | ||
}) | ||
//while this doesn't update the circuit, it should still call evaluateOperation which detects the flaw later. | ||
test("Check that no operation is added for 'h( 1 , [ 1 ], 3)' as the Hadamard operation takes no parameters", () => { | ||
let circuit = quantumjs.Q(); | ||
runner.evaluateInput("h( 1 , [ 1 ], 3)", circuit) | ||
expect(circuit.get(1, 1)).toBeUndefined(); | ||
}) | ||
test("Check that evaluateOperation is called for input 'x(1, [1, 2])'", () => { | ||
let circuit = quantumjs.Q(); | ||
runner.evaluateInput("x(1, [1, 2])", circuit) | ||
expect(circuit.get(1, 1).gate.symbol).toBe("X"); | ||
expect(circuit.get(1, 2).gate.symbol).toBe("X"); | ||
}) | ||
//messing with the white space | ||
test("Check that evaluateOperation is called for input 'x( 1, [ 1, 2 ] )'", () => { | ||
let circuit = quantumjs.Q(); | ||
runner.evaluateInput("x(1, [1, 2])", circuit) | ||
expect(circuit.get(1, 1).gate.symbol).toBe("X"); | ||
expect(circuit.get(1, 2).gate.symbol).toBe("X"); | ||
}) | ||
describe("Check that gateSymbol(...,[...]) is valid for all gate constants in the Gate module using their nameCss value", ()=> { | ||
Object.entries(quantumjs.Gate.constants).forEach(function(entry) { | ||
let gate = entry[1]; | ||
let input = gate.nameCss + (gate.is_multi_qubit ? "(1, [1, 2])" : "(1, [1])"); | ||
console.log(input); | ||
test("Checking that evaluate operation is called once for the input '" + input + "'", () => { | ||
let circuit = quantumjs.Q(); | ||
runner.evaluateInput(input, circuit); | ||
expect(circuit.get(1, 1).gate.nameCss).toBe(gate.nameCss); | ||
if(gate.is_multi_qubit) { | ||
expect(circuit.get(1, 2).gate.nameCss).toBe(gate.nameCss); | ||
} | ||
}) | ||
}) | ||
}) | ||
|
||
|
||
}) | ||
|
||
|
||
describe("Testing removal operations (of the same regex form as above) and that removeOperation() is called", () => { | ||
test("Check that removeOperation is called once for input remove(1, [1])", () => { | ||
let circuit = quantumjs.Q(); | ||
//Set a hadamard operation on the circuit. | ||
circuit.set$('H', 1, [1]); | ||
runner.evaluateInput('remove(1, [1])', circuit); | ||
expect(circuit.get(1, 1)).toBeUndefined(); | ||
}) | ||
//messing with the whitepsace | ||
test("Check that removeOperation is called once for input remove( 1 , [ 1 ] )", () => { | ||
let circuit = quantumjs.Q(); | ||
//Set a hadamard operation on the circuit. | ||
circuit.set$('H', 1, [1]); | ||
runner.evaluateInput('remove( 1 , [ 1 ] )', circuit); | ||
expect(circuit.get(1, 1)).toBeUndefined(); | ||
}) | ||
test("Check that removeOperation removes all sibling indices of the operation x(1, [1, 2]) when remove(1, [1]) is called", ()=> { | ||
let circuit = quantumjs.Q(); | ||
circuit.set$('X', 1, [1, 2]); | ||
runner.evaluateInput('remove(1, [1])', circuit); | ||
expect(circuit.get(1, 1)).toBeUndefined(); | ||
expect(circuit.get(1, 2)).toBeUndefined(); | ||
}) | ||
test("Check that the removeOperation does NOT remove any operation given a set of indices that are not siblings", ()=> { | ||
let circuit = quantumjs.Q(); | ||
//Set a hadamard operation on the circuit. | ||
circuit.set$('H', 1, [1]); | ||
runner.evaluateInput('remove(1, [1, 2])', circuit); | ||
expect(circuit.get(1, 1)).toBeDefined(); | ||
expect(circuit.get(1, 1).gate.symbol).toBe('H'); | ||
}) | ||
}) | ||
|
||
describe("Check that parameters parameterized gates can be input and set properly", ()=> { | ||
test("Check that the input 'u(1, [1], 3, 2, 5)' results in the creation of a unitary gate with phi=3,theta=2,lambda=5", ()=>{ | ||
let circuit = quantumjs.Q(4, 4); | ||
runner.evaluateInput("u(1, [1], 3, 2, 5)", circuit); | ||
let result = circuit.get(1, 1).gate; | ||
expect(result.symbol).toBe('U'); | ||
expect(result.parameters['phi']).toBe(3); | ||
expect(result.parameters['theta']).toBe(2); | ||
expect(result.parameters['lambda']).toBe(5); | ||
}) | ||
//messing with whitespace | ||
test("Check that the input 'u(1, [1], 3 , 2 , 5 )' results in the creation of a unitary gate with phi=3,theta=2,lambda=5", ()=>{ | ||
let circuit = quantumjs.Q(4, 4); | ||
runner.evaluateInput("u(1, [1], 3 , 2 , 5 )", circuit); | ||
let result = circuit.get(1, 1).gate; | ||
expect(result.symbol).toBe('U'); | ||
expect(result.parameters['phi']).toBe(3); | ||
expect(result.parameters['theta']).toBe(2); | ||
expect(result.parameters['lambda']).toBe(5); | ||
}) | ||
//checking regex accepts decimal values | ||
test("Check that the input 'u(1, [1], 3.93, 2.24, 5.12)' results in the creation of a unitary gate with phi=3,theta=2,lambda=5", ()=>{ | ||
let circuit = quantumjs.Q(4, 4); | ||
runner.evaluateInput("u(1, [1], 3.93, 2.24, 5.12)", circuit); | ||
let result = circuit.get(1, 1).gate; | ||
expect(result.symbol).toBe('U'); | ||
expect(result.parameters['phi']).toBe(3.93); | ||
expect(result.parameters['theta']).toBe(2.24); | ||
expect(result.parameters['lambda']).toBe(5.12); | ||
}) | ||
//check that too many parameters results in a failed set operation | ||
test("Check that the input 'u(1, [1], 1, 2, 3, 4)' does NOT result creation of a unitary gate", ()=>{ | ||
let circuit = quantumjs.Q(4, 4); | ||
runner.evaluateInput("u(1, [1], 1, 2, 3, 4)", circuit); | ||
expect(circuit.get(1, [1])).toBeUndefined(); | ||
}) | ||
test("Check that the input 'u(1, [1], 1, 2)' does result creation of a unitary gate with phi=1,theta=2,lambda=[default]", ()=>{ | ||
let circuit = quantumjs.Q(4, 4); | ||
runner.evaluateInput("u(1, [1], 1, 2)", circuit); | ||
let result = circuit.get(1, 1).gate; | ||
let defaultUnitary = Gate.findBySymbol('U'); | ||
expect(result.symbol).toBe('U'); | ||
expect(result.parameters['phi']).toBe(1); | ||
expect(result.parameters['theta']).toBe(2); | ||
expect(result.parameters['lambda']).toBe(defaultUnitary.parameters['lambda']); | ||
}) | ||
}) | ||
|
||
|
||
describe("Test various combinations of set and remove operations chained together", ()=> { | ||
test("Check that the input 'h(1, [1]).x(2, [1])' results in valid gate set operations", ()=> { | ||
let circuit = quantumjs.Q(4, 4); | ||
runner.evaluateInput("h(1, [1]).x(2, [1])", circuit); | ||
expect(circuit.get(1, 1).gate.symbol).toBe('H'); | ||
expect(circuit.get(2, 1).gate.symbol).toBe('X'); | ||
}) | ||
//Messing with the whitespace | ||
test("Check that the input 'h(1, [ 1 ]).x( 2 , [ 1 ] )' results in valid gate set operations", ()=> { | ||
let circuit = quantumjs.Q(4, 4); | ||
runner.evaluateInput("h(1, [ 1 ]).x( 2 , [ 1 ] )", circuit); | ||
expect(circuit.get(1, 1).gate.symbol).toBe('H'); | ||
expect(circuit.get(2, 1).gate.symbol).toBe('X'); | ||
}) | ||
test("Check that the input 'h(1, [1]).x(2, [1, 2]).p(1, [3], 3.14159)' results in valid gate set operations", ()=> { | ||
let circuit = quantumjs.Q(4, 4); | ||
runner.evaluateInput("h(1, [1]).x(2, [1, 2]).p(1, [3], 3.14159)", circuit); | ||
expect(circuit.get(1, 1).gate.symbol).toBe('H'); | ||
expect(circuit.get(2, 1).gate.symbol).toBe('X'); | ||
expect(circuit.get(2, 1).registerIndices).toEqual([1, 2]); | ||
expect(circuit.get(1, 3).gate.symbol).toBe('P'); | ||
expect(circuit.get(1, 3).gate.parameters['phi']).toBe(3.14159); | ||
}) | ||
test("Check that the input 'h(1, [1]).x(2, [1, 2]).remove(1, [1]).p(1, [3], 3.14159)' results in valid gate set operations", ()=> { | ||
let circuit = quantumjs.Q(4, 4); | ||
runner.evaluateInput("h(1, [1]).x(2, [1, 2]).remove(1, [1]).p(1, [3], 3.14159)", circuit); | ||
expect(circuit.get(1, 1)).toBeUndefined(); | ||
expect(circuit.get(2, 1).gate.symbol).toBe('X'); | ||
expect(circuit.get(2, 1).registerIndices).toEqual([1, 2]); | ||
expect(circuit.get(1, 3).gate.symbol).toBe('P'); | ||
expect(circuit.get(1, 3).gate.parameters['phi']).toBe(3.14159); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const {run} = require('./runner'); | ||
|
||
run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "quantum-js-cli", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jest" | ||
}, | ||
"dependencies": { | ||
"prompt-sync": "^4.2.0", | ||
"readline-sync": "^1.4.10" | ||
}, | ||
"author": "", | ||
"license": "ISC" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant test