|q⟩ Bad Qubits

intermediate · Programming · Algorithm Projects & Benchmarking

Project: Estimate a Phase Precisely

Quantum phase estimation (QPE) extracts the phase ϕ\phi of a unitary's eigenvalue by encoding it into a register of counting qubits and then reading it out with an inverse QFT. The central trade-off is simple: adding one more counting qubit halves the smallest phase difference that the circuit can distinguish.

The precision formula

With nn counting qubits the register can represent 2n2^n evenly spaced values in [0,1)[0, 1). The precision (the spacing between adjacent representable phases) is

δϕ=12n.\delta\phi = \frac{1}{2^n}.

Each extra qubit adds one bit to the binary fraction that encodes ϕ\phi:

| nn | Precision δϕ\delta\phi | Representable values | |-----|------------------------|----------------------| | 1 | 1/21/2 | 2 | | 2 | 1/41/4 | 4 | | 3 | 1/81/8 | 8 | | 4 | 1/161/16 | 16 |

When ϕ\phi is an exact nn-bit binary fraction the algorithm returns the correct integer k=ϕ2nk = \phi \cdot 2^n with probability 1. When ϕ\phi falls between two representable values the probability spreads, but the closest integer still receives at least 4/π240%4/\pi^2 \approx 40\% of the probability mass (Nielsen and Chuang, §5.2).

Circuit structure

The QPE circuit has three stages:

  1. Hadamard layer. Apply HH to every counting qubit to create a uniform superposition.

  2. Phase kicks. Each counting qubit jj is given a phase gate P(λj)P(\lambda_j) with λj=2π2jϕ\lambda_j = 2\pi \cdot 2^j \cdot \phi. This encodes the binary digits of ϕ\phi directly into the phases of the counting register:

    +j    0+e2πi2jϕ12.|+\rangle_j \;\longrightarrow\; \frac{|0\rangle + e^{2\pi i \cdot 2^j \phi}\,|1\rangle}{\sqrt{2}}.
  3. Inverse QFT. Decodes the phase-encoded register back to the computational basis state k|k\rangle where k=ϕ2nk = \phi \cdot 2^n.

A worked example: ϕ=5/8\phi = 5/8

Binary expansion: 5/8=0.10125/8 = 0.101_2. With n=3n = 3 qubits the precision is 1/81/8, and 5/85/8 is an exact 3-bit fraction, so QPE should return the integer k=5k = 5 (binary 101|101\rangle) with probability 1.

Phase kicks for ϕ=5/8\phi = 5/8:

After the inverse QFT the three counting qubits collapse to 101|101\rangle. A 2-qubit register could not represent 5/85/8 — the nearest values are 1/21/2 and 3/43/4 — so those extra qubits are genuinely necessary.

Try it

The starter code has the Hadamard layer, the phase kicks for qubits 0 and 2, and the full inverse QFT. Add the missing phase kick for qubit 1. When your circuit is correct, the distribution grader will show all probability on outcome 101|101\rangle (decimal 5).

Run your code to see the quantum state.

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