A very silly simple quantum computing circuit simulator.
(the implementation could be wrong though)
By Yiyi Wang
const {QuantumComputing} = require('qubit.js')const qc = new QuantumComputing()qc.boot(3) // simulate 3 qubits.x(0) // apply Pauli X Gate (Not Gate) to qubit[0].measure() // measure allconsole.log(qc.toQASM()) // convert to QASM codeconsole.log(qc.getResult()) // print probabilities
8 possible quantum states
q[2]q[1]q[0] | 000 | 001 | 010 | 011 | 100 | 101 | 110 | 111 |
---|---|---|---|---|---|---|---|---|
amplitude | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
8 possible quantum states
q[2]q[1]q[0] | 000 | 001 | 010 | 011 | 100 | 101 | 110 | 111 |
---|---|---|---|---|---|---|---|---|
amplitude | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
qc.boot(3)// =>amplitudes = [1, 0, 0, 0, 0, 0, 0, 0]
It is a unary quantum gateunary quantum gate
matrix:
X=[0110]X=[0110]
q[2]q[1]q[0] | 000 | 001 | 010 | 011 | 100 | 101 | 110 | 111 |
---|---|---|---|---|---|---|---|---|
amplitude | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
qc.boot(2).x(0)amplitudes = [1, 0, 0, 0, 0, 0, 0, 0]// =>// what should the amplitudes be ?
We are applying the matrix to a single qubit q[0]...
q[2]q[1]q[0] | 000 | 001 | 010 | 011 | 100 | 101 | 110 | 111 |
---|---|---|---|---|---|---|---|---|
amplitude | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
So we found all possible pairs.
α|0⟩+β|1⟩α|0⟩+β|1⟩
|000⟩|000⟩ and |001⟩|001⟩
|010⟩|010⟩ and |011⟩|011⟩
|100⟩|100⟩ and |101⟩|101⟩
|110⟩|110⟩ and |111⟩|111⟩
q[2]q[1]q[0] | 000 | 001 | 010 | 011 | 100 | 101 | 110 | 111 |
---|---|---|---|---|---|---|---|---|
amplitude | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
For the pair:
|000⟩|000⟩ and |001⟩|001⟩
We have amplitudes α=1α=1 and β=0β=0
Therefore
[α′β′]=X[αβ]=[0110][10]=[01]
q[2]q[1]q[0] | 000 | 001 | 010 | 011 | 100 | 101 | 110 | 111 |
---|---|---|---|---|---|---|---|---|
amplitude | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
qc.boot(2).x(0)amplitudes = [1, 0, 0, 0, 0, 0, 0, 0]// =>amplitudes = [0, 1, 0, 0, 0, 0, 0, 0]
q[2]q[1]q[0] | 000 | 001 | 010 | 011 | 100 | 101 | 110 | 111 |
---|---|---|---|---|---|---|---|---|
amplitude | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
qc.boot(2).x(0).measure() // one qubit register to one classic registeramplitudes = [1, 0, 0, 0, 0, 0, 0, 0]// =>amplitudes = [0, 1, 0, 0, 0, 0, 0, 0]
∵probability = amplitude2
∴p(|001⟩)=12=1
qc.boot(3) // simulate 3 qubits.x(0) // apply Pauli X Gate (Not Gate) to qubit[0].cnot(0, 1) // apply Controlled Not (CNot Gate)// q[0] is control, q[1] is target..measure()
q[2]q[1]q[0] | 000 | 001 | 010 | 011 | 100 | 101 | 110 | 111 |
---|---|---|---|---|---|---|---|---|
amplitude | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
qc.boot(2).x(0).cnot(0, 1) // q[0] is control, q[1] is targetamplitudes = [1, 0, 0, 0, 0, 0, 0, 0]// =>amplitudes = [0, 1, 0, 0, 0, 0, 0, 0]// =>// what should the amplitudes be ?
it is a Binary Quantum Gate that operates on two qubits
CNOT=[1000010000010010]
transforms the quantum state: (|control,target⟩)
a|00⟩+b|01⟩+c|10⟩+d|11⟩
into:
a|00⟩+b|01⟩+c|11⟩+d|10⟩
We are applying the matrix to a two qubits (control) q[0] and (target) q[1]...
q[2]q[1]q[0] | 000 | 001 | 010 | 011 | 100 | 101 | 110 | 111 |
---|---|---|---|---|---|---|---|---|
amplitude | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
|control,target⟩
a|00⟩+b|01⟩+c|10⟩+d|11⟩
So we found all possible pairs
|000⟩ and |001⟩ and |010⟩ and |011⟩
|100⟩ and |101⟩ and |110⟩ and |111⟩
For the pair:
|000⟩ and |001⟩ and |010⟩ and |011⟩
∵|control,target⟩ and q[0] is control, q[1] is target.
∴a|000⟩+b|010⟩+c|001⟩+d|011⟩
We have a=0, b=0, c=1, d=0 ∴[a′b′c′d′]=CNOT[abcd]=[1000010000010010][0010]=[0001]
q[2]q[1]q[0] | 000 | 001 | 010 | 011 | 100 | 101 | 110 | 111 |
---|---|---|---|---|---|---|---|---|
amplitude | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
qc.boot(2).x(0).cnot(0, 1) // q[0] is control, q[1] is targetamplitudes = [1, 0, 0, 0, 0, 0, 0, 0]// =>amplitudes = [0, 1, 0, 0, 0, 0, 0, 0]// =>amplitudes = [0, 0, 0, 1, 0, 0, 0, 0]
q[2]q[1]q[0] | 000 | 001 | 010 | 011 | 100 | 101 | 110 | 111 |
---|---|---|---|---|---|---|---|---|
amplitude | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
qc.boot(2).x(0).measure() // one qubit register to one classic registeramplitudes = [1, 0, 0, 0, 0, 0, 0, 0]// =>amplitudes = [0, 1, 0, 0, 0, 0, 0, 0]// =>amplitudes = [0, 0, 0, 1, 0, 0, 0, 0]
∵probability = amplitude2
∴p(|011⟩)=12=1
I implemented two helper functions for applying matrix.
applySingleBitMatrix(offset, matrix)
apply unary quantum gate
applyTwoBitsMatrix(control, target, matrix)
apply binary quantum gate
For example, Hadamard Gate:
H=[1/√21/√21/√2−1/√2]
qc.h(0)// is the same asconst t = 1 / Math.sqrt(2)qc.applySingleBitMatrix(0, [[t, t], [t, -t]])
For more information about this project, check the qubit.js github repo:
https://github.com/shd101wyy/qubit.js