Skip to main content
Quantum Phase Estimation (QPE) determines the phase φ in the eigenvalue equation U|ψ⟩ = e^(i·2π·φ)|ψ⟩ for a given unitary U and its eigenstate |ψ⟩. It is a core subroutine in Shor’s algorithm, quantum chemistry simulations, and many other quantum algorithms. The circuit uses a register of counting qubits to encode φ in binary, then extracts it via an inverse Quantum Fourier Transform (QFT). Precision scales with the number of counting qubits: n bits yield a resolution of 1/2ⁿ.

Code

Step-by-step explanation

Gate definitions
  • h (Hadamard): Creates superposition over the counting register.
  • x (Pauli-X): Flips the ancilla a to |1⟩, the eigenstate of both target unitaries.
  • cr(theta): Controlled phase rotation — controlled U(0, 0, theta) applies a phase of e^(i·theta) to |1⟩ on the target when the control is |1⟩.
Structure of QPE All QPE circuits follow the same three-stage structure:
  1. Hadamard stage — Place all counting qubits in uniform superposition with h c.
  2. Controlled-U powers — Apply controlled-U^(2^i) for each counting qubit c[i].
  3. Inverse QFT — Extract the phase from the counting register.
Eigenstate preparation x a sets the ancilla to |1⟩ — the +1 eigenstate of U(0, 0, theta). Because U(0, 0, theta)|1⟩ = e^(i·theta)|1⟩, each controlled application introduces a phase factor onto the corresponding counting qubit without disturbing the ancilla. Controlled-U powers For the T gate example with theta = pi/4:
The phase φ = 1/8 in binary is 0.001, so only c[2] (the highest bit of the 3-bit fraction) accumulates a non-trivial phase — the inverse QFT will concentrate probability on the binary string 001. Inverse QFT The inv_qft subroutine is the standard n-qubit inverse QFT built from Hadamard gates and controlled phase rotations with negative angles. It transforms the phase-encoded state in the counting register into a computational basis state whose binary value approximates φ.

Expected output

T gate (3 counting qubits, φ = 1/8) The phase 1/8 = 0.001 in binary is exactly representable in 3 bits. The measurement is deterministic: Rz(pi/3) (7 counting qubits, φ = 1/6) The phase 1/6 = 0.001010101… in binary is irrational and cannot be represented exactly in 7 bits. The QPE output spreads across nearby representable values:
Increasing the number of counting qubits improves phase precision. With n qubits you can resolve phases differing by as little as 1/2ⁿ. The T gate example uses 3 qubits because 1/8 is exactly a 3-bit fraction. Rz(pi/3) uses 7 qubits to get closer to the irrational value 1/6.

Quantum mechanics concepts

Eigenphase — A unitary operator U has complex eigenvalues of the form e^(i·2π·φ). QPE estimates this phase φ ∈ [0, 1) using quantum parallelism: the counting register holds a superposition of all possible phase values simultaneously, and interference causes the correct one to have the highest probability amplitude. Quantum Fourier Transform — The QFT is the quantum analogue of the Discrete Fourier Transform. Applied to a state encoding phase φ in the amplitudes, it transforms that state into a computational basis state close to the binary encoding of φ. The inverse QFT (used here) undoes the encoding introduced by the controlled-U applications. Phase kickback — When a controlled-U acts on a control qubit and the target is already in an eigenstate |ψ⟩ of U with eigenvalue e^(i·φ), the eigenvalue phase is “kicked back” onto the control qubit’s amplitude. This is what encodes φ into the counting register without disturbing the ancilla. Precision and resources — QPE with n counting qubits succeeds with probability at least (8/π²) ≈ 81% when measuring the closest n-bit approximation to φ. Increasing n by one qubit doubles the resolution and adds one layer of controlled-U and one controlled rotation to the inverse QFT.