⚛️ Quantum Information Science Kit simulator in pure JavaScript. As a first feature it includes an unitary one, with specific support for OpenQASM circuits representation.
Please visit the main repository to know more about the rest of the project tools.
☕ Install lastest Node.js stable version (or LTS) and then:
npm i @qiskit/sim
📝 You can visit more complete examples in the tests.
const util = require('util');
const { Circuit, Gate } = require('@qiskit/sim');
function randomizeInput(nQubits) {
const input = [];
for (let i = 0; i < nQubits; i += 1) {
const x = !!Math.round(Math.random());
input.push(x);
console.log(`${i}:${x ? '|1>' : '|0>'}`);
}
return input;
}
const circuit = Circuit.createCircuit(2);
circuit.add(Gate.h, 0, 0);
circuit.add(Gate.cx, 1, [0, 1]);
circuit.print();
console.log('\nInput randomized (as string):');
const input = randomizeInput(circuit.nQubits);
console.log('\nInput randomized:');
console.log(input);
console.log('\nRunning the circuit now ...');
circuit.run(input);
console.log('\nDone, internal state:');
console.log(circuit.state);
console.log('\nInternal state (as string):');
console.log(circuit.stateToString());
const circuitIr = circuit.save();
console.log('\nSaved IR:');
console.log(util.inspect(circuitIr, { showHidden: false, depth: null }));
👀 Full specification.
The actual version of the library.
version
(string) - Version number.
Gates definition.
gates
(object) - Supported gates definition.
opts
(object) -The constructor accepts next options:nQubits
(number) - Number of qubits needed to run the circuit. It will be automatically updated by theaddGate
method if needed.
circuit
(object) - New instance.
state
(object, Math.js matrix) - Internal state of the simulation.
stateStr
(string) - Human friendly representation of the internal state.
Add a gate to the circuit.
gate
(Gate|string) - Gate instance or name of the gate, fromgates
field.column
(number) - Qubit to connect the gate.wires
(number / [number]) - Gate connections. An array is used for multi-gates.
Add a gate to the circuit. This function is identical to addGate
but only
accepts Gate
instances.
gate
(Gate) - Gate instance to add to the circuit.column
(number) - Qubit to connect the gate.wires
(number / [number]) - Gate connections. An array is used for multi-gates.
Prints a visual representation of the circuit to standard out (by default).
writable
(object) - Optional Writable object which will be written to. Defaults to process.stdout.
Make the simulation.
input
([boolean]) - Initial state of each qubit.
Export the circuit setup for a future reuse.
circuitIr
(object) - Simulator internal representation of the circuit (JSON).
Import a circuit setup.
circuitIr