|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

Uu=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

Hn0n=12nk=02n1k.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,,n1j = 0, 1, \ldots, n-1 from MSB to LSB. Qubit jj controls the operation U2n1jU^{2^{n-1-j}} on the eigenstate register.

Because Uu=e2πiφuU|u\rangle = e^{2\pi i\varphi}|u\rangle, repeated application gives U2ku=e2πiφ2kuU^{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

12nj=0n1(0+e2πiφ2n1j1).\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 QFTQFT^\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 S1=eiπ/21=e2πi1/41S|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 S1=i1S|1\rangle = i|1\rangle and Z1=1Z|1\rangle = -|1\rangle:

12(01)(0+i1)1.\tfrac{1}{2}(|0\rangle - |1\rangle)(|0\rangle + i|1\rangle)\,|1\rangle.

The counting register 12(01)(0+i1)\tfrac{1}{2}(|0\rangle-|1\rangle)(|0\rangle+i|1\rangle) equals QFT401QFT_4|01\rangle, because QFTj=14k=03e2πijk/4kQFT|j\rangle = \tfrac{1}{\sqrt{4}}\sum_{k=0}^{3}e^{2\pi ijk/4}|k\rangle, giving QFT1=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 (QFTQFT^\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/4401 = 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.