Why a Fourier transform on a quantum computer
The discrete Fourier transform (DFT) turns a sequence into its frequency content. Periodicity in the input becomes sharp peaks in the output, and that is exactly the structure that the famous exponential quantum algorithms exploit: Shor's factoring algorithm finds the period of a modular exponential, and phase estimation reads an eigenvalue out of a periodic phase pattern. The quantum Fourier transform (QFT) performs the DFT on the amplitudes of an -qubit state using only gates. That sounds like an exponential improvement over the classical fast Fourier transform, which needs operations on a vector of length . This lesson builds the QFT, counts its cost honestly, and explains why that comparison is misleading on its own.
Definitions and conventions
Let . The QFT is the unitary defined on basis states by
and extended linearly, so an arbitrary state is mapped to with
Two conventions are hidden in this formula and both cause real bugs.
- Sign. The QFT uses . NumPy's forward transform
np.fft.fftuses , and its inversenp.fft.ifftuses the plus sign. So the QFT matrix is the inverse FFT, not the forward one. - Normalisation. The QFT carries so that it is unitary. NumPy's default puts no
factor on
fftand onifft; passingnorm="ortho"gives on both.
Putting the two together: the QFT of a vector x is np.fft.ifft(x, norm="ortho"). In this
course, has qubit 0 as the most significant bit, so
with the leading bit. Amplitude vectors indexed by
the integer are the same in every convention; only the mapping from integers to wires changes.
Worked example: two qubits
For we have and . The basis state maps to
Every outcome has probability . That is general: the QFT of any basis state has a flat magnitude distribution, and all the information sits in the phases.
The product representation
The circuit follows from one observation: the output of is a product state. Write the output index in binary, . Then
and the sum over all factorises into a sum over each bit :
The phase on output qubit depends on modulo 1, which only involves the last bits of . With the binary-fraction notation :
- output qubit 0 carries the phase , which depends only on the last input bit;
- output qubit 1 carries ;
- and output qubit carries , which depends on every bit.
Check this against the worked example with : qubit 0 gets , giving , and qubit 1 gets , giving . Their product is , as before.
The circuit
A Hadamard on a qubit holding bit produces , since . Controlled phase gates then add the finer binary digits: the gate , controlled by a qubit holding bit , multiplies the component by , which appends the digit at position of the binary fraction.
So the circuit processes wire 0 first: a Hadamard gives it ; controlled from wire 1 adds ; controlled from wire 2 adds ; and so on up to , leaving on wire 0. Wire 1 is treated the same way with one fewer rotation, and so on down to wire , which gets only a Hadamard. Wire 0 now holds the factor that belongs to output qubit , so a final layer of swaps reverses the wire order.
The inverse QFT is the same circuit run backwards with negated angles. Many algorithms skip the swaps and relabel the wires instead, which is fine as long as the code that reads the output knows.
Cost: exact and approximate
The circuit uses Hadamards, controlled phase gates and swaps, so its size is . For that is 20 Hadamards, 190 controlled phases and 10 swaps.
Many of those rotations are tiny: rotates by , which for is about rad. Coppersmith observed that dropping the smallest rotations leaves a transform that is still very close to the QFT (Coppersmith 1994). Keep only the rotations with and call the result . A simple bound follows from two facts: a controlled differs from the identity by in operator norm, and for unitaries the errors of a product add at most linearly (). Each value of occurs at most times, so
Choosing makes the error at most with only gates. The table below shows the exact operator-norm error for , computed with the exercise's solution, next to the sum of the norms of the dropped rotations.
| kept orders | controlled phases | sum of dropped norms | |
|---|---|---|---|
| 4 | 18 | 1.131 | 1.201 |
| 5 | 22 | 0.414 | 0.417 |
| 6 | 25 | 0.123 | 0.123 |
| 7 | 27 | 0.0245 | 0.0245 |
| 8 | 28 | 0 | 0 |
The operator norm measures the worst case over all input states. For a specific input, such as the periodic states that occur in Shor's algorithm, the error in the output distribution can be much smaller. Error correction gives a second reason to drop tiny rotations: on fault-tolerant hardware every small-angle rotation must be synthesised from many gates, so fewer and coarser rotations save real resources.
What the QFT does not do
It is tempting to say the QFT computes Fourier transforms exponentially faster than the FFT. It does not, for three reasons.
- The input must already be a quantum state. To transform a classical vector of numbers, you first have to load it into amplitudes, and a general state preparation needs order gates.
- The output cannot be read. The result is a vector of amplitudes, but one measurement returns one index sampled with probability . Estimating all magnitudes, let alone the phases, needs order repetitions or more.
- The comparison counts different things. The is a gate count on a state that already exists; the FFT's is the cost of producing every output number.
The QFT is useful when the input is produced by a quantum computation and the question asked needs only a few samples of the output. The standard example is periodicity. Take the uniform superposition over with dividing . Its QFT has non-zero amplitude only on multiples of , each with probability , whatever the offset ; the offset only changes phases. For and , measuring after the QFT always returns one of . A handful of samples reveals , and in Shor's algorithm the periodic state is produced by modular exponentiation, which a quantum computer can do in polynomial time (Shor 1997). The next lesson develops the same idea for phase estimation (Cleve, Ekert, Macchiavello and Mosca 1998).
Checking a QFT in code
The quickest correctness check is against NumPy, with the convention fixed explicitly:
import numpy as np
n = 3
N = 2**n
idx = np.arange(N)
F = np.exp(2j * np.pi * np.outer(idx, idx) / N) / np.sqrt(N) # rows k, columns j
print(np.allclose(F, np.fft.ifft(np.eye(N), axis=0, norm="ortho"))) # True
print(np.allclose(F, np.fft.fft(np.eye(N), axis=0, norm="ortho"))) # False: opposite sign
The KetQat SDK applies the actual gate sequence and compares it with the DFT column itself (KetQat SDK). Its documentation states its convention explicitly: the same sign as this lesson, but with qubit 0 as the least significant bit. Because it lists output amplitudes by integer index, you can compare its output with yours directly:
from ketqat_runner import catalog
record = catalog.run("[email protected]", seed=7, n_qubits=2,
input_state="basis", basis_index=1, shots=100)
print(record.result["output_amplitudes"])
This prints [[0.5, 0.0], [0.0, 0.5], [-0.5, 0.0], [0.0, -0.5]]:
the worked example above, as [real, imaginary] pairs.
Exercise: build and test the QFT
In qft-circuit-and-dft you write the QFT matrix from the definition, apply the circuit gate by
gate to state vectors, count gates for the exact and approximate transform, and compute the
approximate-QFT error. The checks compare your matrix with np.fft.ifft(..., norm="ortho"),
check that it is not the forward FFT, compare your circuit with the matrix on random states,
test the error bound above, test the periodic-state prediction, and compare your output with the
SDK product [email protected] for three basis states.
Common mistakes
- Using
np.fft.fftas the reference. It has the opposite sign. Your QFT will look wrong, or a wrong QFT will look right if you flip a sign elsewhere to compensate. - Forgetting the swaps (or forgetting that you skipped them). Without them the circuit computes the QFT followed by a bit reversal; the amplitude magnitudes of a basis input still look flat, so magnitude-only tests do not catch it.
- Mixing bit orders. Qiskit, the KetQat SDK and this course do not all agree on which wire is the most significant bit. Compare amplitude vectors by integer index, and state the convention.
- Reading the as a speed-up for Fourier analysis of data. Loading and reading out cost order .
- Dropping rotations without a budget. The approximate QFT is accurate only if grows like plus the number of bits of accuracy you need.
Summary
The QFT is the unitary , which in NumPy terms is
ifft with orthonormal scaling. Its output on a basis state is a product of single-qubit states
whose phases are binary fractions of , and that product form gives a circuit of Hadamards,
controlled phases and swaps. Dropping rotations below order
reduces the gate count to with error at most . The QFT
does not make Fourier analysis of classical data fast; it is a component that turns a periodic
quantum state into a few informative samples, which is what phase estimation, the next lesson,
relies on.