The Quantum Fourier Transform, Explained with Code You Can Check

The Quantum Fourier Transform (QFT) sits inside Shor's algorithm, phase estimation and most of the "big" quantum algorithms. It is also one of the most hand-waved parts of any introduction. This post builds it from gates, checks it against Qiskit's own implementation, shows what it actually does with a period-finding example, and then measures what it costs on hardware.

What the QFT does

The classical discrete Fourier transform turns a list of numbers into its frequency components. The QFT does the same to the amplitudes of a quantum state: a state that repeats with some period gets mapped to a state concentrated on values related to that period. You cannot read all the amplitudes out (measurement gives one sample), so the QFT is only useful when the answer you need is in where the probability concentrates — which is exactly the situation in period finding.

Building it from gates

For n qubits the circuit is a Hadamard on each qubit followed by controlled phase rotations of angle π/2, π/4, π/8… from the qubits below it, and finally a reversal of the qubit order with SWAPs.

import numpy as np
from qiskit import QuantumCircuit

def qft(n):
    qc = QuantumCircuit(n)
    for j in reversed(range(n)):
        qc.h(j)
        for k in reversed(range(j)):
            qc.cp(np.pi / 2 ** (j - k), k, j)   # controlled phase rotation
    for i in range(n // 2):
        qc.swap(i, n - 1 - i)                    # reverse qubit order
    return qc

Hand-written quantum circuits are easy to get subtly wrong, so the first thing I do is compare the full unitary against Qiskit's reference QFTGate:

from qiskit.quantum_info import Operator
from qiskit.circuit.library import QFTGate

for n in [3, 4]:
    print(n, Operator(qft(n)).equiv(Operator(QFTGate(n))))
# 3 True
# 4 True

Both match exactly, so the rest of the post can use this version with confidence.

Seeing it work: finding a period

Take 3 qubits (values 0–7) and prepare an equal superposition of 0, 2, 4 and 6 — a signal with period 2. Putting Hadamards on qubits 1 and 2 and leaving qubit 0 at |0⟩ does exactly that. Then apply the inverse QFT and measure:

from qiskit.quantum_info import Statevector

qc = QuantumCircuit(3, 3)
qc.h([1, 2])                              # superposition of 0, 2, 4, 6
qc.compose(qft(3).inverse(), inplace=True)
qc.measure(range(3), range(3))

probs = Statevector(qc.remove_final_measurements(inplace=False)).probabilities_dict()
print({k: round(v, 3) for k, v in probs.items() if v > 1e-9})
# {'000': 0.5, '100': 0.5}

All the probability lands on 000 and 100 — the values 0 and 4. Those are the multiples of 8/2: the size of the register divided by the period. Read either peak, and you can recover the period.

Periods that do not divide the register size are less clean. With the values 0, 3 and 6 (period 3), the ideal distribution is 37.5% on 000 and 24.3% each on 011 and 101 — the values 3 and 5, closest to the multiples of 8/3 ≈ 2.67. This is why Shor's algorithm uses a register much larger than the number being factored, and why its classical post-processing uses continued fractions to turn "close to a multiple" into the exact period.

On a noisy model of real hardware

I ran the period-2 circuit 20,000 times on FakeFez, the local noise model of IBM's ibm_fez. The two correct peaks came out at 48.7% and 47.5%, with the remaining 4% spread across the wrong values. At 3 qubits the QFT is very robust.

The problem is scaling. The QFT needs a controlled rotation between every pair of qubits, so the number of two-qubit gates grows roughly as n². On a chip where each qubit only talks to a few neighbours, the transpiler also has to add SWAPs:

QubitsTranspiled depth (ibm_fez)Two-qubit gates
34012
514041
8322155

At 8 qubits that is over 150 two-qubit gates for the QFT alone, before any useful work around it. This is the practical reason why Shor's algorithm on meaningful numbers needs error-corrected machines, not today's NISQ devices.

Two ideas that make it cheaper

  • Approximate QFT. The tiny rotations (π/64, π/128…) barely change the result. Dropping rotations below a threshold removes most of the gates with almost no loss in accuracy. Qiskit's QFT synthesis exposes this as an approximation degree.
  • Skip the SWAPs. The final qubit reversal is often unnecessary: you can just read the output bits in reverse order in software.

Where to go from here

The QFT is the engine of quantum phase estimation, which is the engine of Shor's algorithm. If you can follow the period-finding example above, you already understand the core of why Shor works — the rest is modular arithmetic and a lot of error correction.

References

  • D. Coppersmith, "An approximate Fourier transform useful in quantum factoring," IBM Research Report RC19642 (1994). arXiv:quant-ph/0201067.
  • M. A. Nielsen and I. L. Chuang, Quantum Computation and Quantum Information, chapter 5 (the quantum Fourier transform and its applications).
  • Qiskit documentation: QFTGate and circuit synthesis.

Comments

Popular posts from this blog

Quantum Key Distribution for Developers — BB84 with Qiskit

Qiskit Runtime & the Primitives — SamplerV2 and EstimatorV2 in 2026

Post-Quantum Cryptography in 2026 — What Developers Actually Need to Know