|q⟩ Bad Qubits

intermediate · Programming · Quantum Phase Estimation

Reading the Phase Register

After the two earlier stages of Quantum Phase Estimation — superposing the counting register and kicking back the phase through controlled-UU powers — the counting register holds a superposition of the form

12n/2k=02n1e2πiφkk.\frac{1}{2^{n/2}} \sum_{k=0}^{2^n - 1} e^{2\pi i \varphi k} |k\rangle.

This is exactly the Quantum Fourier Transform of the computational-basis state 2nφ|2^n \varphi\rangle (when φ\varphi is exactly representable in nn bits). To decode the phase we must undo the QFT — i.e. apply the inverse QFT — and then measure.

Decoding with the inverse QFT

Applying the inverse QFT (IQFT) to the counting register maps the QFT-encoded superposition back to a single computational-basis state:

IQFT(12n/2k=02n1e2πiφkk)=2nφ.\text{IQFT} \left( \frac{1}{2^{n/2}} \sum_{k=0}^{2^n - 1} e^{2\pi i \varphi k} |k\rangle \right) = |2^n \varphi\rangle.

Measuring then yields the nn-bit binary string for 2nφ2^n \varphi with probability 1. The decimal value of that string divided by 2n2^n is φ\varphi.

For n=2n = 2 counting qubits, the IQFT is a small four-gate circuit on qubits q0q_0 (MSB) and q1q_1 (LSB):

  1. SWAP q0q_0 and q1q_1 — undoes the bit-reversal implicit in the QFT.
  2. HH on q1q_1 — Hadamard on the (now-leading) qubit.
  3. CP(π/2)CP(-\pi/2) controlled on q0q_0, targeting q1q_1 — conjugate of the half-turn phase.
  4. HH on q0q_0 — final Hadamard.

A worked example: U=ZU = Z, φ=1/2\varphi = 1/2

The Pauli ZZ gate acts on its 1|1\rangle eigenstate as

Z1=1=eiπ1=e2πi1/21,Z|1\rangle = -|1\rangle = e^{i\pi}|1\rangle = e^{2\pi i \cdot 1/2}|1\rangle,

so the eigenphase is φ=1/2\varphi = 1/2. In binary this is 0.1020.10_2, meaning 22φ=22^2 \cdot \varphi = 2 and the expected measurement result on two counting qubits is 10|10\rangle (q0 = 1, q1 = 0).

With two counting qubits the controlled-unitary stage applies:

After these controlled operations the counting register is in the state +q0q1|{+}\rangle_{q_0} \otimes |{-}\rangle_{q_1}, which is exactly QFT10\text{QFT}|10\rangle. The IQFT maps it back to 10|10\rangle, and measurement reads out 10 — confirming φ=2/4=1/2\varphi = 2/4 = 1/2.

Try it

The starter code builds the QPE circuit up through the controlled-ZZ kickback step. Add the 2-qubit IQFT on q0q_0 and q1q_1 so the distribution peaks at 101|101\rangle (counting register 10|10\rangle plus eigenstate 1|1\rangle), confirming φ=1/2\varphi = 1/2.

Run your code to see the quantum state.

After running, the Probabilities panel should show 100% on the single state 101. The first two bits 10 give φ=121+022=1/2\varphi = 1 \cdot 2^{-1} + 0 \cdot 2^{-2} = 1/2, matching ZZ's eigenphase.

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