Run the QFT and phase estimation in code

A short NumPy script that reproduces the lab’s numbers, a Qiskit outline for a real circuit, and the bit-order mistakes that make results look wrong.

8 min read · reviewed December 2026

Start with NumPy: the matrix and the formula

The cleanest check of the lab is a few lines of NumPy. The first function builds the QFT matrix straight from the definition. The second computes the phase-estimation outcome probabilities from the sum of phases that the controlled powers produce, and applies the inverse transform’s decoding by direct summation.

import numpy as np

def qft_matrix(n):
    N = 2 ** n
    j, k = np.meshgrid(np.arange(N), np.arange(N))
    return np.exp(2j * np.pi * j * k / N) / np.sqrt(N)

def qpe_probs(t, phi):
    N = 2 ** t
    m = np.arange(N)
    k = np.arange(N)
    amps = np.exp(2j * np.pi * np.outer(m / N - phi, k)).sum(axis=1) / N
    return np.abs(amps) ** 2

F = qft_matrix(3)
print("unitary:", np.allclose(F.conj().T @ F, np.eye(8)))
print("F|0> is uniform:", np.allclose(F[:, 0], 1 / np.sqrt(8)))

p = qpe_probs(5, 0.34)
best = int(p.argmax())
print("best outcome:", format(best, "05b"), best / 32, "chance", round(float(p[best]), 4))
print("sum of chances:", round(float(p.sum()), 6))
print("chance within one step:", round(float(p[10] + p[11]), 4))

This script was run before publishing. Output: unitary True; F|0> uniform True; best outcome 01011 = 0.34375 with chance 0.9536; chances sum to 1.0; chance within one step 0.9713. The lab shows the same 95.4 percent and 97.1 percent for t = 5 and φ = 0.34.

What the script shows

Matching the lab to a script written separately is a useful habit. The lab’s own tests compare its gate-level circuit with the closed form for every register size from 1 to 8 qubits, and the numbers above agree with the lab’s output to four decimal places.

  • The matrix is unitary, so it is a legal quantum operation, and its first column is the equal superposition, which is the QFT of |0⟩.
  • For a phase of 0.34 turns with five counting qubits, the grid has spacing 1/32 = 0.03125. The nearest grid point is 11/32 = 0.34375, and the best reading lands there about 95 percent of the time. Neighbours 10/32 and 11/32 together carry about 97 percent.
  • The probabilities add to 1, a cheap sanity check that catches many indexing mistakes.

A Qiskit outline

The outline below builds a phase estimation circuit for a phase gate with eigenphase 0.34, using five counting qubits. It has not been run for this page, so treat it as a starting point. Class names and helper functions in Qiskit change between versions: check the current documentation for how the QFT and the controlled phase gates are exposed in your installed release.

# Not run. Check class names against your installed Qiskit version.
from math import pi
from qiskit import QuantumCircuit
from qiskit.circuit.library import QFT

t, phi = 5, 0.34
qc = QuantumCircuit(t + 1, t)
qc.x(t)                               # target in the eigenstate |1> of the phase gate
for q in range(t):
    qc.h(q)
for q in range(t):
    # Qiskit qubit 0 is the least significant, so qubit q controls U^(2^q)
    qc.cp(2 * pi * phi * (2 ** q), q, t)
qc.append(QFT(t, inverse=True, do_swaps=True).to_gate(), range(t))
qc.measure(range(t), range(t))

Bit order: the usual source of wrong answers

This lab and most textbooks put the most significant bit first. Qiskit does the opposite: qubit 0 is the least significant bit, and printed bit strings are read with qubit 0 on the right. That is why the outline above lets counting qubit q control U^(2^q), where the lab’s first qubit controls the largest power. If you build a circuit with the wrong convention, the controlled powers are attached to the wrong qubits, and the result is a distribution that looks scrambled rather than peaked.

  • If the peak is in the wrong place, suspect the bit order first, then the swaps.
  • If the register reads as the bit-reversed number, you left the swaps out or used a QFT with swaps turned off.
  • If the result is flat when you expected a peak, the eigenstate is wrong: check that the target qubit really is in an eigenstate of your gate.
  • Compare the simulated counts with the closed-form probabilities from the NumPy script before trusting a new circuit.

Reading the counts

The output of a run is a set of bit strings with counts. Convert the most common string to an integer m (checking the bit order) and divide by 2^t to get the estimate of φ. With few shots the counts of rarer outcomes are noisy, so compare the shape with the closed form rather than every number. On a noisy device the peak is lower and the neighbouring outcomes are higher, which the Noise Lab shows for simpler circuits.

A phase estimation circuit is easy to get almost right. Check it against a closed form before reading anything into the result.

Try it

Questions

Why compare with NumPy?

Because NumPy gives the exact distribution in a few lines, so any difference with your circuit’s counts points to a mistake, not to randomness.

Does the Qiskit code run as shown?

It was not run for this page. Library details change between versions, so check the documentation of your installed release.

Why does Qiskit print bit strings backwards?

Qiskit labels qubit 0 as the least significant bit and writes it on the right of the string.

Read next

Sources and further reading

Check the original sources for exact citations.