|q⟩ Bad Qubits

intermediate · Programming · Quantum Phase Estimation

The QPE Circuit

Quantum Phase Estimation (QPE) turns an unobservable global phase into a measurable integer stored in a binary register. This lesson shows how the three building blocks — a Hadamard layer, controlled-UU powers, and an inverse Quantum Fourier Transform — fit together into a single circuit.

The goal

Given a unitary UU and one of its eigenvectors ∣u⟩|u\rangle with

U∣u⟩=e2πiφ ∣u⟩,φ∈[0,1),U|u\rangle = e^{2\pi i\varphi}\,|u\rangle, \quad \varphi \in [0,1),

QPE writes an nn-bit binary approximation of φ\varphi into a dedicated counting register of nn qubits. If φ\varphi is exactly representable as an nn-bit fraction, the result is exact.

Circuit structure

The circuit uses two registers:

Stage 1 — Hadamard layer

Apply HH to every qubit in the counting register to create the uniform superposition

H⊗n∣0⟩⊗n=12n∑k=02n−1∣k⟩.H^{\otimes n}|0\rangle^{\otimes n} = \frac{1}{\sqrt{2^n}}\sum_{k=0}^{2^n-1}|k\rangle.

Stage 2 — Controlled-UU powers

Index the counting qubits j=0,1,…,n−1j = 0, 1, \ldots, n-1 from MSB to LSB. Qubit jj controls the operation U2n−1−jU^{2^{n-1-j}} on the eigenstate register.

Because U∣u⟩=e2πiφ∣u⟩U|u\rangle = e^{2\pi i\varphi}|u\rangle, repeated application gives U2k∣u⟩=e2πiφ⋅2k∣u⟩U^{2^k}|u\rangle = e^{2\pi i\varphi\cdot 2^k}|u\rangle.

A controlled-U2kU^{2^k} imprints the phase e2πiφ⋅2ke^{2\pi i\varphi\cdot 2^k} on the control qubit only when that qubit is ∣1⟩|1\rangle. After all nn controlled operations the counting register is

12n⨂j=0n−1(∣0⟩+e2πiφ⋅2n−1−j∣1⟩).\frac{1}{\sqrt{2^n}}\bigotimes_{j=0}^{n-1}\bigl(|0\rangle + e^{2\pi i\varphi\cdot 2^{n-1-j}}|1\rangle\bigr).

This tensor product is the Quantum Fourier Transform of ∣φ~⟩|\tilde\varphi\rangle, where φ~\tilde\varphi is the integer 2nφ2^n\varphi (exact when φ\varphi is an nn-bit fraction).

Stage 3 — Inverse QFT

Applying QFT†QFT^\dagger to the counting register maps the Fourier-encoded state back to the computational-basis state ∣φ~⟩|\tilde\varphi\rangle. A measurement then reads out the nn-bit integer whose binary value gives the phase.

Worked example: U=SU = S, n=2n = 2 counting qubits

The SS gate has matrix diag⁡(1,eiπ/2)\operatorname{diag}(1, e^{i\pi/2}), so S∣1⟩=eiπ/2∣1⟩=e2πi⋅1/4∣1⟩S|1\rangle = e^{i\pi/2}|1\rangle = e^{2\pi i\cdot 1/4}|1\rangle. The phase is φ=1/4\varphi = 1/4, which in 2-bit binary is 0.0120.01_2, encoding the integer φ~=1\tilde\varphi = 1.

Registers: q0q_0 (MSB counting), q1q_1 (LSB counting), q2q_2 (eigenstate, prepared as ∣1⟩|1\rangle).

After Stage 1, Hadamard on both counting qubits:

12(∣0⟩+∣1⟩)(∣0⟩+∣1⟩) ∣1⟩.\tfrac{1}{2}(|0\rangle+|1\rangle)(|0\rangle+|1\rangle)\,|1\rangle.

Stage 2 applies CS20=CSCS^{2^0} = CS controlled by q1q_1, then CS21=CZCS^{2^1} = CZ controlled by q0q_0. Since S∣1⟩=i∣1⟩S|1\rangle = i|1\rangle and Z∣1⟩=−∣1⟩Z|1\rangle = -|1\rangle:

12(∣0⟩−∣1⟩)(∣0⟩+i∣1⟩) ∣1⟩.\tfrac{1}{2}(|0\rangle - |1\rangle)(|0\rangle + i|1\rangle)\,|1\rangle.

The counting register 12(∣0⟩−∣1⟩)(∣0⟩+i∣1⟩)\tfrac{1}{2}(|0\rangle-|1\rangle)(|0\rangle+i|1\rangle) equals QFT4∣01⟩QFT_4|01\rangle, because QFT∣j⟩=14∑k=03e2πijk/4∣k⟩QFT|j\rangle = \tfrac{1}{\sqrt{4}}\sum_{k=0}^{3}e^{2\pi ijk/4}|k\rangle, giving QFT∣1⟩=12(1, i, −1, −i)TQFT|1\rangle = \tfrac{1}{2}(1,\,i,\,-1,\,-i)^T in the basis ∣00⟩,∣01⟩,∣10⟩,∣11⟩|00\rangle, |01\rangle, |10\rangle, |11\rangle, which matches the tensor product above.

Stage 3 (QFT†QFT^\dagger on q0,q1q_0, q_1) returns the counting register to ∣01⟩|01\rangle. Measuring all three qubits yields the bitstring 011011 with certainty, and the top two bits 01=1/4⋅401 = 1/4 \cdot 4 correctly encode φ=1/4\varphi = 1/4.

Try it

Build the complete QPE circuit for U=SU = S with 2 counting qubits. The starter code prepares the eigenstate ∣1⟩|1\rangle on q2q_2. Add Stage 1, Stage 2, Stage 3, and a final measurement. The grader checks the output distribution, which should place all probability on the bitstring 011011.

Run your code to see the quantum state.

Sign in on the full site to ask questions and join the discussion.