Free Qiskit tutorial · Intermediate
Quantum phase estimation in Qiskit: read a phase as bits
A gate leaves a phase on its eigenstate. Phase estimation reads that phase out as a binary fraction, and it's the step that makes Shor's algorithm work.
- To work through
- 20 min
- Level
- Intermediate
- Tested on
- Qiskit 2.6
- Runs on your laptop
- Free
The idea
Phase estimation, in plain words
If a gate U turns a state |ψ⟩ into e^(2πiθ)|ψ⟩, the number θ is the phase, a fraction between 0 and 1. Phase estimation measures it to t binary places, using t extra counting qubits.
Each counting qubit controls U applied 2ᵏ times, so it collects θ·2ᵏ turns of phase by kickback. Together they hold θ written in the Fourier basis, and the inverse QFT turns that into the bits of θ.
The code
The whole script, ready to run
Install Qiskit and its simulator once, save the script as quantum_phase_estimation.py and run it with python quantum_phase_estimation.py. It runs on Qiskit's free simulator, so you don't need an account or a quantum computer.
Install onceTerminal
pip install qiskit qiskit-aerquantum_phase_estimation.pyPython · Qiskit
from math import pi
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator
def qft(n):
qc = QuantumCircuit(n, name="QFT")
for target in reversed(range(n)):
qc.h(target)
for control in reversed(range(target)):
qc.cp(pi / 2 ** (target - control), control, target)
for q in range(n // 2):
qc.swap(q, n - q - 1)
return qc
def estimate(theta, t=3):
"""Estimate the phase of P(2*pi*theta) on |1>, with t counting qubits."""
qc = QuantumCircuit(t + 1, t)
qc.x(t) # |1> is the phase gate's eigenstate
qc.h(range(t))
for k in range(t):
qc.cp(2 * pi * theta * 2 ** k, k, t) # controlled-U applied 2^k times
qc.compose(qft(t).inverse(), range(t), inplace=True)
qc.measure(range(t), range(t))
return qc
sim = AerSimulator(seed_simulator=7)
for theta in (3 / 8, 1 / 3):
counts = sim.run(transpile(estimate(theta), sim), shots=1000).result().get_counts()
best = max(counts, key=counts.get)
print(f"theta = {theta:.4f}: most often {best} = {int(best, 2) / 8:.3f}")
print(" ", dict(sorted(counts.items())))What it printedSeed 7 · 1,000 shots
theta = 0.3750: most often 011 = 0.375
{'011': 1000}
theta = 0.3333: most often 011 = 0.375
{'000': 15, '001': 25, '010': 190, '011': 669, '100': 57, '101': 14, '110': 11, '111': 19}What happened
What the output shows
A phase of 3/8 is 0.011 in binary, exactly three places, so every shot reads 011 and the estimate is exactly 0.375.
A phase of 1/3 is 0.010101… and doesn't fit in three bits. The circuit then gives a spread of answers around it: most often the closest one, 011 = 0.375 (669 of the 1,000 shots), then 010 = 0.25 (190). Add counting qubits and the answers close in on 1/3.
This is the heart of Shor's algorithm: there, U multiplies by a number modulo N, its phases are fractions s/r, and r, the period, is what factors N.
Try next
Three things to try next
- Use t=5 counting qubits (and divide by 32 instead of 8): the most common answer for 1/3 becomes 01011 = 0.344.
- The T gate is a phase gate with θ = 1/8. Run estimate(1 / 8) and check every shot reads 001.
- Open the Shor's algorithm project, where the same circuit runs with modular multiplication as U.
Take it further · Project 08
Build Shor's Algorithm for real
In the Shor's algorithm project, phase estimation runs with modular multiplication as U, and the phases it reads give the period that factors 15 and 21.
Keep going
More free Qiskit tutorials
Questions
Phase estimation in Qiskit
What is quantum phase estimation?
An algorithm that measures the phase θ a unitary gate puts on one of its eigenstates, to t binary places, using t counting qubits, controlled powers of the gate and an inverse QFT.
What happens when the phase doesn't fit in the counting qubits?
You get a spread of answers around the true phase, with the nearest t-bit fraction the most likely. More counting qubits narrow the spread.
Where is phase estimation used?
In Shor's factoring algorithm, in quantum chemistry to estimate energies, and in algorithms like HHL for linear systems.
Your first circuit is 10 minutes away
Stop watching quantum. Start shipping it.
$0 today. 7 days free, then $7.99 a week or $190 a year. Cancel any time.