Mini-lessons
Step-by-step qubit math, from state vectors to your first algorithms. Practise each idea in the playground.
1. State vectors
A qubit's state is a unit vector in a 2-dimensional complex space. We write the two basis states as kets:
|0⟩ = [1, 0]ᵀ |1⟩ = [0, 1]ᵀ
|ψ⟩ = α|0⟩ + β|1⟩ = [α, β]ᵀ, with |α|² + |β|² = 1
Step by step: take α = 3/5, β = 4i/5. Then |α|² = 9/25 and |β|² = |4i/5|² = 16/25, which sum to 1, so this is a valid state.
Any single-qubit state can be written (up to an unobservable global phase) as
|ψ⟩ = cos(θ/2)|0⟩ + e^{iφ} sin(θ/2)|1⟩, 0 ≤ θ ≤ π, 0 ≤ φ < 2π
The angles θ and φ place the state on the Bloch sphere: |0⟩ is the north pole (θ = 0), |1⟩ the south pole (θ = π). These are exactly the sliders in the playground.
2. Measurement and the Born rule
Measuring in the computational basis gives outcome 0 with probability |α|² and outcome 1 with probability |β|² — the Born rule. After the measurement the state becomes the basis state that was observed.
Example: |ψ⟩ = (3/5)|0⟩ + (4i/5)|1⟩
P(0) = |3/5|² = 0.36 P(1) = |4i/5|² = 0.64
A global phase doesn't change any probability: e^{iγ}|ψ⟩ gives the same results as |ψ⟩. A relative phase (the φ angle) does matter once further gates make amplitudes interfere.
For two qubits the basis is |00⟩, |01⟩, |10⟩, |11⟩ and P(xy) = |amplitude of |xy⟩|². In this site the left digit is qubit A.
3. Superposition
A state is in superposition (with respect to a basis) when more than one basis amplitude is non-zero. Two important examples:
|+⟩ = (|0⟩ + |1⟩)/√2 |−⟩ = (|0⟩ − |1⟩)/√2
Both give 50/50 measurement results in the 0/1 basis, yet they are different states: they differ by a relative phase of π. Applying a Hadamard sends |+⟩ → |0⟩ and |−⟩ → |1⟩, so the difference becomes visible. That is interference: amplitudes add with signs, unlike classical probabilities.
4. Pauli X, Y, Z gates
Gates are unitary matrices (U†U = I) that act on the state vector by matrix multiplication.
X = [[0, 1],[1, 0]] X|0⟩ = |1⟩, X|1⟩ = |0⟩ (bit flip)
Y = [[0, −i],[i, 0]] Y|0⟩ = i|1⟩, Y|1⟩ = −i|0⟩
Z = [[1, 0],[0, −1]] Z|0⟩ = |0⟩, Z|1⟩ = −|1⟩ (phase flip)
Worked example: Z|+⟩ = (|0⟩ − |1⟩)/√2 = |−⟩. On the Bloch sphere X, Y and Z are 180° rotations about the x, y and z axes. Each is its own inverse: X² = Y² = Z² = I.
5. The Hadamard gate
H = (1/√2)[[1, 1],[1, −1]]
H|0⟩ = (|0⟩ + |1⟩)/√2 = |+⟩
H|1⟩ = (|0⟩ − |1⟩)/√2 = |−⟩
H·H = I
Step by step: H|0⟩ = (1/√2)[1·1 + 1·0, 1·1 + (−1)·0]ᵀ = (1/√2)[1, 1]ᵀ. Applying H again: (1/2)[1+1, 1−1]ᵀ = [1, 0]ᵀ = |0⟩. The |1⟩ amplitudes cancel — destructive interference.
Try it: drop H twice on qubit A in the playground and the probabilities return to 100% |00⟩.
6. Two qubits and CNOT
Two-qubit states live in a 4-dimensional space. A product state is the tensor product of two single-qubit states:
(a₀|0⟩ + a₁|1⟩) ⊗ (b₀|0⟩ + b₁|1⟩) = a₀b₀|00⟩ + a₀b₁|01⟩ + a₁b₀|10⟩ + a₁b₁|11⟩
The CNOT (controlled-NOT) gate flips the target qubit if and only if the control is 1. With A as control and B as target:
|00⟩→|00⟩ |01⟩→|01⟩ |10⟩→|11⟩ |11⟩→|10⟩
CNOT = [[1,0,0,0],[0,1,0,0],[0,0,0,1],[0,0,1,0]] (basis order 00, 01, 10, 11)
7. Bell states and entanglement
Start in |00⟩, apply H to A, then CNOT (A→B):
|00⟩ —H on A→ (|00⟩ + |10⟩)/√2 —CNOT→ (|00⟩ + |11⟩)/√2 = |Φ⁺⟩
|Φ⁺⟩ is entangled: no choice of single-qubit states gives it as a product. Quick test: a state a|00⟩+b|01⟩+c|10⟩+d|11⟩ is a product state exactly when ad − bc = 0. For |Φ⁺⟩, ad − bc = 1/2 ≠ 0. (The playground uses this same test for its Entangled badge.)
The four Bell states:
|Φ±⟩ = (|00⟩ ± |11⟩)/√2 |Ψ±⟩ = (|01⟩ ± |10⟩)/√2
Measuring |Φ⁺⟩ gives 00 or 11, each with probability 1/2: the results are individually random but always agree. Entanglement doesn't allow faster-than-light signalling (the no-communication theorem).
8. Deutsch's algorithm
Problem: a function f:{0,1}→{0,1} is either constant (f(0)=f(1)) or balanced (f(0)≠f(1)). Classically you need two evaluations to tell. Deutsch's algorithm uses one call to the oracle U_f|x⟩|y⟩ = |x⟩|y ⊕ f(x)⟩.
1. Start |0⟩|1⟩ (in the playground: X on B)
2. H on both: (|0⟩+|1⟩)(|0⟩−|1⟩)/2
3. Oracle: phase kickback gives (−1)^{f(x)} on each |x⟩:
((−1)^{f(0)}|0⟩ + (−1)^{f(1)}|1⟩)(|0⟩−|1⟩)/2
4. H on A: qubit A becomes ±|0⟩ if f is constant, ±|1⟩ if balanced
5. Measure A: 0 → constant, 1 → balanced
For f(x) = x the oracle is exactly CNOT A→B. For f(x)=0 it is "do nothing" (constant), and the measurement on A gives 0. Challenge 6 in the playground walks through the balanced case.
9. Grover search on 2 qubits
Grover's algorithm finds a marked item among N with about (π/4)√N oracle calls. For N = 4 a single iteration finds it with certainty.
Marked item |11⟩:
1. H⊗H: |s⟩ = (|00⟩+|01⟩+|10⟩+|11⟩)/2
2. Oracle (CZ): (|00⟩+|01⟩+|10⟩−|11⟩)/2
3. Diffusion 2|s⟩⟨s|−I (built as H⊗H, X⊗X, CZ, X⊗X, H⊗H):
amplitude of |11⟩ becomes 1, all others 0
4. Measure: 11 with probability 1
Check of step 3: the mean amplitude after the oracle is (1+1+1−1)/8 = 1/4; diffusion maps each amplitude a → 2·(1/4) − a, so 1/2 → 0 and −1/2 → 1. In the playground, CZ can be built as H on B, CNOT A→B, H on B.
Further reading (free): Nielsen & Chuang, Quantum Computation and Quantum Information (textbook); the IBM Quantum Learning courses.