Code exampleAdvancedQuantum algorithms

Quantum Phase Estimation

Estimate the phase of P(2π·5/16) with four counting qubits and an inverse QFT; the result 0101 equals 5/16 exactly.

The vector $|1\rangle$ is an eigenvector of $P(\lambda)$ with eigenvalue $e^{i\lambda}$. We set $\lambda=2\pi\varphi$ with $\varphi=5/16$. After H on the counting qubits, each qubit $k$ applies the gate $U^{2^k}$ as a controlled operation, i.e. $CP(2\pi\varphi2^k)$; the phase kicks back to the counting qubits (Phase Kickback), forming the Fourier state of the number $16\varphi=5$, and the inverse QFT then returns it to $|0101\rangle$.

Because $\varphi$ is exactly representable in four bits, the result is deterministic; otherwise the probabilities would spread around the nearest value.

Requirements: pip install qiskit qiskit-aer. The code runs on your machine or in Colab, not inside WordPress.

Code

Qiskit
import numpy as np
from qiskit import QuantumCircuit, transpile
from qiskit.circuit.library import QFTGate
from qiskit_aer import AerSimulator

phi = 5 / 16
t = 4
qc = QuantumCircuit(t + 1, t)
qc.x(t)
qc.h(range(t))
for k in range(t):
    qc.cp(2 * np.pi * phi * 2 ** k, k, t)
qc.append(QFTGate(t).inverse(), range(t))
qc.measure(range(t), range(t))

sim = AerSimulator()
counts = sim.run(transpile(qc, sim), shots=1000, seed_simulator=1).result().get_counts()
print(counts)
best = max(counts, key=counts.get)
print("estimate =", int(best, 2) / 2 ** t)

Line-by-line code explanation

LineExplanation
1-4Imports.
6The phase to be estimated.
7Number of counting qubits (precision 1/16).
8t counting qubits + one target qubit.
9Prepare the eigenvector |1⟩ on the target qubit.
10Superposition on the counting qubits.
11-12Controlled U^(2^k): phase 2πφ·2^k.
13Inverse QFT on the counting register.
14Measure the counting register.
16-18Run and print the counts.
19The most frequent outcome.
20Convert it into a phase estimate m/2^t.

Expected output (from an actual run)

{'0101': 1000}
estimate = 0.3125

The result is deterministic (0101 in every shot); seed seed_simulator=1.

Try it yourself

Related topics

References

  1. A. Yu. Kitaev (1995). Quantum measurements and the Abelian Stabilizer Problem. arXiv:quant-ph/9511026 doi:10.48550/arXiv.quant-ph/9511026
  2. Michael A. Nielsen; Isaac L. Chuang (2010). Quantum Computation and Quantum Information (10th Anniversary Edition). Cambridge University Press doi:10.1017/CBO9780511976667The standard reference for quantum computing; the Crossref record gives 2012 for the Cambridge Core online edition.
  3. IBM Quantum (2025). Qiskit SDK API documentation. IBM Quantum Documentation quantum.cloud.ibm.com