Qubits & Quantum Gates
A qubit is the fundamental unit of quantum information: a two-level quantum system whose state lives on the Bloch sphere. Single-qubit gates rotate this sphere; multi-qubit gates create entanglement. We build a complete quantum circuit simulator in JavaScript — no external dependencies — and implement Grover search and quantum teleportation step by step.
1. Qubit State Vectors
A classical bit is either 0 or 1. A qubit is a vector in a 2D complex Hilbert space:
The complex amplitudes α and β do not have direct physical meaning — only the probabilities |α|² (measuring 0) and |β|² (measuring 1) are observable. The relative phase between α and β controls interference effects.
2. The Bloch Sphere
Any normalised qubit state (up to global phase) maps to a point on the unit sphere — the Bloch sphere:
Every single-qubit gate is a rotation of the Bloch sphere. This geometric picture is extremely powerful: X is a 180° rotation around x-axis, Z around z-axis, and a general SU(2) gate Rₙ(θ) = e^(−iθn̂·σ/2) rotates by θ around axis n̂.
3. Single-Qubit Gates
Quantum gates are unitary matrices (U†U = I) applied to qubits:
Pauli X (NOT)
X = [ 0 1 ]
[ 1 0 ]
Flips |0⟩↔|1⟩. 180° rotation around x-axis. Quantum NOT gate.
Pauli Y
Y = [ 0 −i ]
[ i 0 ]
180° rotation around y-axis. Flips with phase: Y|0⟩ = i|1⟩.
Pauli Z
Z = [ 1 0 ]
[ 0 −1 ]
Phase flip: Z|1⟩ = −|1⟩. 180° rotation around z-axis.
Hadamard H
H = 1/√2 [ 1 1 ]
[ 1 −1 ]
Creates superposition: H|0⟩ = |+⟩ = (|0⟩+|1⟩)/√2. The "quantum coin flip".
Phase S
S = [ 1 0 ]
[ 0 i ]
Adds phase π/2 to |1⟩. S² = Z. Also called √Z.
T gate
T = [ 1 0 ]
[ 0 e^(iπ/4) ]
Phase π/4 to |1⟩. T⁴ = Z. Fundamental for fault-tolerant circuits.
4. Measurement & Born Rule
Measuring a qubit in the computational basis collapses it irreversibly:
This irreversibility is the fundamental asymmetry between quantum evolution (unitary, reversible) and quantum measurement (stochastic, irreversible). The measurement basis need not be computational — measuring in the X basis {|+⟩, |−⟩} uses the Hadamard gate before measurement.
No-cloning theorem: It is impossible to create an identical copy of an unknown quantum state — a crucial difference from classical bits. This underpins quantum cryptography (BB84 protocol).
5. Two-Qubit States & CNOT
The state space of two qubits is the tensor product of individual spaces — a 4D complex vector:
The CNOT (Controlled-NOT) gate flips the target qubit if and only if the control qubit is |1⟩:
CNOT is the paradigmatic two-qubit entangling gate. Together with single-qubit gates, it forms a universal gate set — any quantum computation can be decomposed into single-qubit gates and CNOT.
6. Entanglement & Bell States
A state is entangled if it cannot be written as a tensor product of individual qubit states. The four maximally entangled Bell states are:
Entangled qubits exhibit non-local correlations: measuring one qubit instantly determines the outcome probability for the other, regardless of separation. This is not information transfer and does not violate relativity — but it does violate Bell inequalities, ruling out local hidden-variable theories (Aspect experiment 1982, Nobel Prize 2022).
7. JavaScript — Quantum Circuit Simulator
A statevector simulator supporting arbitrary single-qubit gates and CNOT on up to ~20 qubits (2²⁰ = 1M amplitudes).
// Statevector quantum circuit simulator
// State: Complex128 array of length 2^n
class QuantumCircuit {
constructor(n) {
this.n = n;
const dim = 1 << n;
this.re = new Float64Array(dim);
this.im = new Float64Array(dim);
this.re[0] = 1; // start in |000…0⟩
}
// Apply 2×2 unitary [[a,b],[c,d]] to qubit q
gate1(q, a_re, a_im, b_re, b_im, c_re, c_im, d_re, d_im) {
const dim = 1 << this.n;
const bit = 1 << q;
for (let i = 0; i < dim; i++) {
if (i & bit) continue; // only process pairs once
const j = i | bit;
const r0 = this.re[i], i0 = this.im[i];
const r1 = this.re[j], i1 = this.im[j];
this.re[i] = a_re*r0 - a_im*i0 + b_re*r1 - b_im*i1;
this.im[i] = a_re*i0 + a_im*r0 + b_re*i1 + b_im*r1;
this.re[j] = c_re*r0 - c_im*i0 + d_re*r1 - d_im*i1;
this.im[j] = c_re*i0 + c_im*r0 + d_re*i1 + d_im*r1;
}
}
X(q) { this.gate1(q, 0,0, 1,0, 1,0, 0,0); }
Z(q) { this.gate1(q, 1,0, 0,0, 0,0, -1,0); }
H(q) {
const s = 1 / Math.sqrt(2);
this.gate1(q, s,0, s,0, s,0, -s,0);
}
S(q) { this.gate1(q, 1,0, 0,0, 0,0, 0,1); } // phase i
T(q) {
const s = Math.SQRT2 / 2;
this.gate1(q, 1,0, 0,0, 0,0, s,s); // e^(iπ/4)
}
// CNOT: control=ctrl, target=tgt
CNOT(ctrl, tgt) {
const dim = 1 << this.n;
const cb = 1 << ctrl, tb = 1 << tgt;
for (let i = 0; i < dim; i++) {
if (!(i & cb)) continue; // control must be |1⟩
if (i & tb) continue; // only process pairs once
const j = i | tb;
[this.re[i], this.re[j]] = [this.re[j], this.re[i]];
[this.im[i], this.im[j]] = [this.im[j], this.im[i]];
}
}
// Sample one measurement outcome, return bit string
measure() {
let r = Math.random(), cumul = 0;
for (let i = 0; i < this.re.length; i++) {
cumul += this.re[i] ** 2 + this.im[i] ** 2;
if (r < cumul) return i.toString(2).padStart(this.n, '0');
}
}
// Return probability table { bitstring: probability }
probabilities() {
const out = {};
for (let i = 0; i < this.re.length; i++) {
const p = this.re[i] ** 2 + this.im[i] ** 2;
if (p > 1e-12) out[i.toString(2).padStart(this.n, '0')] = p;
}
return out;
}
}
// Bell state |Φ⁺⟩ = (|00⟩ + |11⟩)/√2
const qc = new QuantumCircuit(2);
qc.H(0);
qc.CNOT(0, 1);
console.log('Bell state probabilities:', qc.probabilities());
// → { '00': 0.5, '11': 0.5 }
// GHZ state (|000⟩ + |111⟩)/√2 — 3-qubit entanglement
const ghz = new QuantumCircuit(3);
ghz.H(0);
ghz.CNOT(0, 1);
ghz.CNOT(0, 2);
console.log('GHZ probabilities:', ghz.probabilities());
// → { '000': 0.5, '111': 0.5 }
8. Quantum Algorithms & Hardware
- Grover's search: Searches an unsorted database of N items in O(√N) queries — quadratic speedup over classical O(N).
- Shor's algorithm: Factors integers in polynomial time using quantum Fourier transform — threatens RSA encryption.
- Quantum error correction: Surface codes require ~1000 physical qubits per logical qubit, the main engineering challenge.
- Quantum simulation: Simulating quantum chemistry (drug discovery, materials) is the near-term "killer app" — exponentially hard classically.
- Current hardware: IBM, Google (127→1000 qubit superconducting), IonQ (trapped ion), Photonic (photonic), Neutral atom (Pasqal) — all currently limited by decoherence.