Lesson 9 of 15

Quantum Phase Estimation

Quantum Phase Estimation (QPE)

Quantum Phase Estimation solves a fundamental problem: given a unitary operator UU and one of its eigenstates ψ|\psi\rangle, estimate the eigenphase ϕ\phi such that:

Uψ=e2πiϕψU|\psi\rangle = e^{2\pi i\phi}|\psi\rangle

The algorithm uses nn ancilla qubits, giving precision 1/2n1/2^n.

The QPE State

After the QFT-based interference step, the amplitude for measuring outcome mm is:

amplitude(m)=1Nk=0N1e2πi(ϕNm)k/N\text{amplitude}(m) = \frac{1}{N}\sum_{k=0}^{N-1} e^{2\pi i(\phi N - m)k/N}

where N=2nN = 2^n. This is a geometric series that peaks sharply when m=ϕNm = \phi N.

When ϕN\phi N is an integer (exact case), the sum equals 1 for m=ϕNm = \phi N and 0 for all other mm — perfect estimation!

For example, with ϕ=0.25\phi = 0.25 and n=2n = 2 bits (N=4N=4):

  • ϕN=1\phi N = 1, so amplitude is 1 for m=1m=1 and 0 for m{0,2,3}m \in \{0, 2, 3\}
  • Measurement returns 1 with certainty; estimate =1/4=0.25= 1/4 = 0.25

Algorithm Summary

  1. Initialize nn ancilla qubits in 0n|0\rangle^{\otimes n}
  2. Apply Hadamard to each ancilla → uniform superposition
  3. Apply controlled-U2kU^{2^k} for k=0,1,,n1k = 0, 1, \ldots, n-1
  4. Apply inverse QFT to ancilla register
  5. Measure ancilla → outcome mm; estimate ϕ^=m/2n\hat{\phi} = m / 2^n
import cmath, math

def qpe_state(phase, n_bits):
    N = 2 ** n_bits
    amplitudes = []
    for m in range(N):
        amp = 0
        for k in range(N):
            amp += cmath.exp(2j * math.pi * (phase * N - m) * k / N)
        amp /= N
        amplitudes.append(amp)
    return amplitudes

def phase_estimation(phase, n_bits):
    amps = qpe_state(phase, n_bits)
    probs = [abs(a)**2 for a in amps]
    best = max(range(len(probs)), key=lambda i: probs[i])
    return best / (2**n_bits)

print(phase_estimation(0.25, 2))  # 0.25

Precision Scaling

With nn ancilla bits, QPE can distinguish phases that differ by at least 1/2n1/2^n. Doubling precision requires one extra qubit — an exponential improvement over classical repeated sampling.

Your Task

Implement both functions:

  • qpe_state(phase, n_bits) — returns the N=2nN = 2^{n} complex amplitudes
  • phase_estimation(phase, n_bits) — returns the estimated phase as a float
Python runtime loading...
Loading...
Click "Run" to execute your code.