2. Interference
Superposition is reversible. A second Hadamard brings the qubit back to , because the two paths cancel. Insert a phase between the Hadamards and the same cancellation produces instead.
1. The idea
Each amplitude is a complex number, so it has a size and a phase. The second Hadamard adds the paths together. Equal phases reinforce and cancel . A phase of on one path swaps which outcome survives.
2. The code
import math
import numpy as np
from fqkit import QuantumCircuit, Hadamard, RZ, run
np.set_printoptions(precision=4, suppress=True)
plain = QuantumCircuit(1)
plain.add_gate(Hadamard(), [0])
plain.add_gate(Hadamard(), [0])
phased = QuantumCircuit(1)
phased.add_gate(Hadamard(), [0])
phased.add_gate(RZ(math.pi), [0]) # a relative phase of pi
phased.add_gate(Hadamard(), [0])
print("H H :", np.round(run(plain), 4))
print("H RZ(pi) H :", np.round(run(phased), 4))3. The output
H H : [1.+0.j 0.+0.j]
H RZ(pi) H : [0.+0.j 0.-1.j]The first circuit is definitely 0. The second is definitely 1. The -1j
is a global phase: it does not change the measurement, which is 1 on every
shot.
4. The explanation
- The first Hadamard creates .
- Hadamard applied to and to produces opposite signs on . Adding them cancels and restores .
RZ(π)multiplies by and by . That relative minus sign reverses the cancellation, so cancels and remains.- No measurement noise is involved. Both results have probability 1.
5. The math
Adding those two expansions, the terms cancel:
RZ(π) is a phase gate,
and the full sequence leaves . Because , every
measurement returns 1.
Key insight: a phase is invisible on a single amplitude and decisive when two amplitudes are added. Interference is that addition.
Try it yourself
- Replace
math.piwithmath.pi / 2and print the probabilities. Are they still 0 and 1? - Apply
RZbefore the first Hadamard. Does the final state change? - Continue with The Bell state, which uses the same Hadamard and then shares it with a second qubit.