Definition
The Quantum Fourier Transform on qubits transforms:
In matrix form, is a unitary matrix with entries:
Two small cases make the map concrete. On one qubit, â the transform is just the Hadamard. On two qubits with , feeding in gives , because the phase factor cycles through as runs. The output is a uniform magnitude over every basis state with all the information packed into those phases â which is exactly why reading it out is a problem, and why the QFT is useful as an inner circuit rather than as a drop-in replacement for a library FFT.
Three properties carry most of the downstream results. The transform is unitary, so it is reversible and no amplitude is lost. It maps a shift in the time or position register to a phase in the frequency register, which is how period in Shor's algorithm becomes the sharply peaked pattern at multiples of . And it diagonalises cyclic structure: a state with period over points lands entirely on the frequencies commensurate with .
QFT Circuit
The QFT circuit for qubits uses gates:
- Apply Hadamard to qubit
- Apply controlled- gates from qubits
- Swap qubits to reverse the order
The controlled- gate applies phase :
Count the gates and the quadratic advantage stops being abstract. For qubits the circuit uses Hadamards, controlled rotations, and trailing swaps â 7 gates at , 12 at , 840 at . A classical discrete Fourier transform over the same amplitudes takes butterfly operations, roughly arithmetic steps, and writes numbers to memory. The catch sits on the input side: loading arbitrary amplitudes into a register is the exponential step the QFT does not solve, so the speedup applies to states a machine can prepare structurally, not to arbitrary classical vectors.
The ordering of the operations matters for anyone implementing it. Apply the Hadamard to the most-significant qubit first, then controlled rotations whose controls are the lower qubits, then repeat on the next qubit down, and finish with swaps that reverse the bit order. Build it in the wrong sequence and the output is a permuted transform â still unitary, still fast, and wrong for every downstream routine that expects in the usual convention.
QFT vs FFT
| Aspect | FFT | QFT |
|---|---|---|
| Input | classical numbers | qubits holding amplitudes |
| Output | classical numbers | the same qubits, rephased |
| Complexity | operations | gates |
| Parallelism | sequential butterflies | all qubits evolve together |
| Data loading | already in memory | preparation, the real cost |
The honest statement of the comparison is that neither dominates. An FFT transforms a vector that already exists in classical memory and returns a vector you can read directly; the QFT transforms a state that had to be prepared by some other quantum procedure and returns a state that must still be measured or used as a control. Counted in gates the QFT is exponentially ahead, counted end-to-end on classical data it is exponentially behind, and every algorithm that uses it â Shor's, phase estimation, hidden subgroup searches â wins precisely because its input state has structure the machine built itself.
Applications
QFT is a subroutine for:
- Shor's algorithm: period finding, where the QFT turns the periodic pattern of into peaks at multiples of
- Quantum phase estimation: the inverse QFT reads the estimated phase out of the ancilla register
- Quantum signal processing: eigenvalue and spectral analysis of encoded operators
- Hidden subgroup problems: identifying periodicity, abelian phases, and related structure
Approximate QFT
For most of those uses the exact transform is more accuracy than the algorithm needs, so an approximate QFT drops controlled- gates whose angles fall below a chosen threshold:
- Remove rotations with angle smaller than
- Per dropped rotation the error is on the order of that angle, so total error stays near
- Gate count drops from to
The threshold is a precision dial, not a guess: an algorithm that only needs a phase to a fixed number of bits can set from that requirement and discard every rotation finer than the answer will ever resolve.
Python: QFT Implementation
import numpy as np
def qft_matrix(n):
# Compute the QFT matrix for n qubits.
N = 2**n
Q = np.zeros((N, N), dtype=complex)
for j in range(N):
for k in range(N):
Q[k, j] = np.exp(2j * np.pi * j * k / N) / np.sqrt(N)
return Q
def qft_circuit_demo(n=3):
Q = qft_matrix(n)
print(f"QFT matrix ({2**n}x{2**n}):")
print(np.round(Q, 3))
print(f"Is unitary: {np.allclose(Q @ Q.conj().T, np.eye(2**n))}")
qft_circuit_demo()
Python: Approximate QFT and Its Cost
Building the circuit out of its actual gates â Hadamards, controlled rotations, swaps â lets you delete rotations and watch the fidelity respond:
import numpy as np
def op_on_qubit(op, q, n):
out = np.array([[1.0 + 0j]])
for i in range(n):
out = np.kron(out, op if i == q else np.eye(2, dtype=complex))
return out
def controlled_phase(angle, c, t, n):
N = 2 ** n
d = np.ones(N, dtype=complex)
for i in range(N):
if ((i >> (n - 1 - c)) & 1) and ((i >> (n - 1 - t)) & 1):
d[i] = np.exp(1j * angle)
return np.diag(d)
def swap_gate(a, b, n):
N = 2 ** n
M = np.zeros((N, N), dtype=complex)
for i in range(N):
bits = list(format(i, f"0{n}b"))
bits[a], bits[b] = bits[b], bits[a]
M[int("".join(bits), 2), i] = 1.0
return M
def qft_circuit(n, drop_below=0.0):
"""QFT as a gate list; rotations below drop_below are never applied."""
N = 2 ** n
H = np.array([[1, 1], [1, -1]], dtype=complex) / np.sqrt(2)
U = np.eye(N, dtype=complex)
gates = 0
for j in range(n):
U = op_on_qubit(H, j, n) @ U
gates += 1
for k in range(1, n - j):
angle = 2 * np.pi / 2 ** (k + 1)
if angle >= drop_below:
U = controlled_phase(angle, j + k, j, n) @ U
gates += 1
for i in range(n // 2):
U = swap_gate(i, n - 1 - i, n) @ U
gates += 1
return U, gates
def fidelity(n, drop_below):
N = 2 ** n
F = np.exp(2j * np.pi * np.outer(np.arange(N), np.arange(N)) / N) / np.sqrt(N)
rng = np.random.default_rng(1)
psi = rng.normal(size=N) + 1j * rng.normal(size=N)
psi /= np.linalg.norm(psi)
U, g = qft_circuit(n, drop_below)
return g, abs(np.vdot(F @ psi, U @ psi)) ** 2
for n in (6, 8):
for drop in (0.0, 0.1, 0.2, 0.5):
gates, fid = fidelity(n, drop)
print(f"n={n} drop<{drop:4.1f} gates={gates:3d} fidelity={fid:.4f}")
For the exact circuit takes 24 gates; dropping rotations below 0.1 radians costs one gate and leaves fidelity at 0.998, dropping below 0.2 removes three gates for fidelity 0.978, and 0.5 leaves 18 gates at 0.884. The same sweep at runs 40, 34, 30 and 25 gates with fidelities 1.000, 0.991, 0.946 and 0.723. The pattern is what the truncation rule predicts: small-angle rotations are the deep-tail corrections, so removing them costs little accuracy at first and then a great deal once the threshold crosses angles the answer actually depends on.
Depth, Parallelism, and Bit Ordering
Counting gates and counting layers are different exercises here. Every controlled- is diagonal in the computational basis, so any two of them commute and can be scheduled in the same time window. With Hadamard layers, roughly rotation layers and one swap layer, the circuit depth is even though the gate count is â at that is 840 gates but only about 80 sequential layers, and on hardware where two-qubit gates dominate the error, the parallel schedule is what keeps the transform inside the coherence budget.
The swaps at the end are not decoration. The recurrence naturally writes the least-significant bit of the frequency index first, so without the swaps the output register reads the result backwards â a bit-reversed copy of the correct answer. Some compilers fold that reversal into how the register is later interpreted instead of emitting physical swaps, saving gates whenever the downstream consumer accepts reversed indexing.
Summary
The quantum Fourier transform is the same linear map as the discrete Fourier transform, written as a unitary on qubits and factored into Hadamards, controlled phase rotations and swaps. Gate count is against the FFT's , and depth is only because all the rotations commute. That gap is real, and it is not free: the QFT consumes and produces quantum states, so any advantage is confined to algorithms whose input state was built by structure rather than loaded from memory.
Precision is a dial. Rotations below a chosen angle can be dropped, trading fidelity for gates â the sweep in this lesson shows a six-qubit transform holding fidelity 0.998 after losing one gate, and a full point of fidelity once the threshold passes the rotations the answer depends on. Algorithms that only need a phase to a fixed number of bits should set the truncation from that requirement rather than compiling the exact transform.
Everything large rests on it: Shor's period finding, phase estimation, and the hidden subgroup searches all inherit the QFT's quadratic behaviour and its read-out caveats.
Common Mistakes
Comparing QFT gate count against FFT operations as though both read the same input
Fix: Loading arbitrary amplitudes costs O(N); the QFT wins only where the input state was prepared structurally by another quantum routine
Compiling the exact transform for an algorithm that only needs a coarse phase
Fix: Set a truncation angle from the precision the algorithm requires and drop every smaller rotation, converting O(n squared) gates into O(n times k_max)
Ignoring the trailing swaps and then reading a reversed frequency index
Fix: Either emit the n over two swaps or document that downstream logic consumes the register in bit-reversed order
Key Takeaways
- The QFT on n qubits needs n Hadamards, n(n-1)/2 controlled rotations and n over two swaps, giving O(n squared) gates at O(n) depth
- All controlled rotations are diagonal and commute, so the quadratic gate count schedules into roughly 2n sequential layers
- QFT and FFT are not interchangeable: the QFT needs a quantum state input and returns a state, while loading classical amplitudes costs O(N)
- Truncating rotations below a threshold converts the exact transform into an O(n times k_max) approximation whose fidelity the sweep in this lesson measures directly