Recall from Unitary Matrices: every eigenvalue of a unitary U has the form e2πiθ for some real θ, since unitary matrices only ever rotate an eigenvector's phase, never scale its length. Quantum Phase Estimation (QPE) is the algorithm that reads θ back out, given U and one of its eigenvectors, using the Quantum Fourier Transform from last lesson as its final step.
Why This Is Hard Classically
Nothing about θ is written down anywhere; it's an intrinsic property of how U acts on a specific eigenvector, extractable only by actually applying U (or its powers) and reading out interference patterns, exactly the kind of task quantum circuits are suited for and classical linear algebra on an exponentially large matrix is not. QPE is also the engine inside Shor's algorithm (two lessons from now): the "hidden number" Shor's algorithm extracts turns out to be exactly the θ that QPE estimates, for a specific, cleverly chosen U.
The Circuit
Two registers: ncounting qubits (start at ∣0⟩, will hold the answer), and an eigenstate register holding ∣ψ⟩, an eigenvector of U with U∣ψ⟩=e2πiθ∣ψ⟩.
H on every counting qubit: uniform superposition, same starting move as every algorithm so far.
From counting qubit k (weight 2k), apply controlled-U2k onto the eigenstate register. Since ∣ψ⟩ is an eigenvector, this doesn't change ∣ψ⟩ itself at all, it kicks back a phase of e2πiθ⋅2k onto counting qubit k's ∣1⟩ amplitude, the exact same phase-kickback mechanism Deutsch-Jozsa was built on, just with a real (not just ±1) phase this time.
Apply the inverse QFT to the counting register. The forward QFT (last lesson) turns a phase pattern into basis-state amplitudes; running it backwards does the opposite, turning the phase pattern step 2 just wrote onto the counting register back into a readable basis state.
Measure the counting register: the result is θ, written out as an n-bit binary fraction (exact, if θ happens to be expressible in n bits; an approximation otherwise).
Worked Example: θ=1/4, Exactly
Let U=P(π/2) (the S gate) and ∣ψ⟩=∣1⟩: P(π/2)∣1⟩=eiπ/2∣1⟩=e2πi(1/4)∣1⟩, so θ=1/4, exactly representable in 2 bits (0.012=1/4). Use 2 counting qubits (q0, q1) and 1 eigenstate qubit (q2, prepared in ∣1⟩ with X):
Controlled-U20 from q0: a single CP(π/2).
Controlled-U21 from q1: applying U twice is the same as applying U2=P(π) once (both times, only on the branch where the control is ∣1⟩), so a single CP(π).
Inverse QFT on (q0, q1): the last lesson's QFT circuit, run in reverse with every angle negated: SWAP, H, CP(−π/2), H.
QPE Estimating θ=1/4
Step through gate by gate. Check the final Measurement Probabilities: the counting register (qubits 0, 1) lands on q1=0, q0=1 with certainty, reading out 0.012=1/4, exactly θ. Qubit 2 (the eigenstate) never leaves ∣1⟩, exactly as an eigenvector should.
Circuit
x
h
cp
swap
Step through the circuit: after gate 9 of 9
StartXHHCPCPSWAPHCPH
10 / 10
Measurement Probabilities
∣000⟩
0.0%
∣001⟩
0.0%
∣010⟩
0.0%
∣011⟩
0.0%
∣100⟩
0.0%
∣101⟩
100.0%
∣110⟩
0.0%
∣111⟩
0.0%
Exact State
Basis state
Re
Im
Probability
∣000⟩
0
0
0.0%
∣001⟩
0
0
0.0%
∣010⟩
0
0
0.0%
∣011⟩
0
0
0.0%
∣100⟩
0
0
0.0%
∣101⟩
1.000
0
100.0%
∣110⟩
0
0
0.0%
∣111⟩
0
0
0.0%
Exercise
Build the 3-qubit QPE circuit above, estimating θ=1/4 for U=P(π/2) with eigenstate ∣1⟩ on qubit 2: prepare the eigenstate (X on qubit 2), put the counting register (qubits 0, 1) in superposition (H on each), apply the controlled-U powers (CP(π/2) from qubit 0, CP(π) from qubit 1, both onto qubit 2), then the inverse QFT on the counting register (SWAP, H on qubit 0, CP(−π/2) from qubit 0 to qubit 1, H on qubit 1).
Live preview of the circuit detected in your code
Setup: qc.x(2) for the eigenstate, then qc.h(0) and qc.h(1) for the counting register.
Controlled-U powers, both targeting qubit 2: qc.cp(math.pi / 2, 0, 2), then qc.cp(math.pi, 1, 2).
Inverse QFT on qubits 0-1: qc.swap(0, 1), qc.h(0), qc.cp(-math.pi / 2, 0, 1), qc.h(1): same shape as the forward QFT, reversed, with the one angle negated.
Check your understanding
Throughout the QPE circuit above, qubit 2 (the eigenstate register) never changes from ∣1⟩. Why?
See the precision/qubit-count tradeoff
Pick a θ that doesn't land on a clean binary fraction, then drag t up and watch the measurement distribution sharpen around the best n-bit estimate instead of spreading across several outcomes.
True phase θ (as a fraction of 2π)
0.370
Counting qubits t
3 (8 outcomes)
Probability of measuring each m/8
0/8
0.0%
1/8
0.1%
2/8
0.2%
3/8
99.5%
4/8
0.2%
5/8
0.0%
6/8
0.0%
7/8
0.0%
The best estimate is m=3 (that is, θ≈3/8=0.375), hit with probability 99.5%. Push t up: more counting qubits means more possible outcomes m, so the distribution concentrates tighter around the true θ instead of spreading probability across neighboring values.
Check your understanding
If θ isn't exactly representable in n bits (e.g. θ=1/3 with only 2 counting qubits), what does QPE return?