Skip to content

The quantum Fourier transform

The last lesson ended with a recipe: write a function into the phases, then apply a transform that turns a pattern in the phases into a measurable basis state. A row of Hadamards can detect only patterns of plus and minus signs. The quantum Fourier transform, or QFT, detects periods: patterns that repeat. It is the engine inside phase estimation and Shor’s algorithm.

From here on, a register of nn qubits will often stand for a number. The basis state ∣x⟩\ket{x} means the bits of xx written on the wires, with the most significant bit on the top wire. With three qubits, ∣6⟩=∣110⟩\ket{6} = \ket{110}. There are N=2nN = 2^n such states, ∣0⟩\ket{0} to ∣N−1⟩\ket{N-1}.

The QFT acts on a basis state like this:

QFT∣x⟩=1N∑y=0N−1e2πi xy/N∣y⟩.\mathrm{QFT}\ket{x} = \frac{1}{\sqrt N}\sum_{y=0}^{N-1} e^{2\pi i\,xy/N}\ket{y}.

Every output ∣y⟩\ket{y} gets the same magnitude, 1/N1/\sqrt N. All the information about xx is in the phases. As yy steps from 0 to N−1N - 1, the phase winds around the unit circle xx times. So ∣0⟩\ket{0} becomes a flat superposition, ∣1⟩\ket{1} becomes one slow turn of phase, and larger xx gives faster winding.

For one qubit, N=2N = 2 and the phases are eπixy=(−1)xye^{\pi i xy} = (-1)^{xy}. That is the Hadamard gate. The QFT is the generalisation of HH from signs to all the NN-th roots of unity.

For two qubits, N=4N = 4 and the phases are powers of e2πi/4=ie^{2\pi i/4} = i:

QFT4=12(11111i−1−i1−11−11−i−1i).\mathrm{QFT}_4 = \frac12\begin{pmatrix} 1 & 1 & 1 & 1\\ 1 & i & -1 & -i\\ 1 & -1 & 1 & -1\\ 1 & -i & -1 & i \end{pmatrix}.

This is the matrix of the classical discrete Fourier transform, and it is unitary. On a superposition ∑xax∣x⟩\sum_x a_x\ket{x} the QFT acts linearly, so the new amplitudes are the discrete Fourier transform of the old ones.

Check

Compare the QFT with a row of Hadamards when both act on ∣00…0⟩\ket{00\dots0}.

The simulator uses six qubits, so N=64N = 64. The input is a state with equal amplitudes on every rr-th value, starting at some offset: a comb with period rr. The bars show probabilities before and after the QFT.

SimulatorFourier transform of a periodic state
Before: probability of each value x
After the QFT: probability of each value y

Things to try:

  1. Set the period to 4, then 8, then 16. Where are the peaks after the transform? How many are there?
  2. With the period at 8, move the offset. What changes in the lower plot?
  3. Set the period to 5, which does not divide 64. What happens to the peaks?

Three facts are worth taking away.

Periods become peaks. A state with period rr is sent to peaks at the multiples of N/rN/r. The amplitudes at every other yy cancel by interference. A long period gives closely spaced peaks, and a short period gives widely spaced ones.

The offset disappears. Shifting the comb sideways changes only the phases of the output, and a measurement does not see phases. This matters enormously in Shor’s algorithm, where the offset is random and unknown, and would otherwise hide the period.

When rr does not divide NN, the peaks blur a little. They sit at the whole numbers nearest to the multiples of N/rN/r, with some probability leaking to the neighbours. Enough stays near the right places for the period to be recovered.

So measuring after a QFT returns a random multiple of N/rN/r, or something close to one. From that, the period can be worked out.

Check

On 64 points, a state has period 8. Where are the peaks after the QFT?

A general unitary on nn qubits needs a number of gates that grows exponentially with nn. The QFT needs about n2/2n^2/2. The reason is that its output is a product state.

Write yy in binary, with bit yjy_j on wire jj. Then y/N=y0/2+y1/4+⋯+yn−1/2ny/N = y_0/2 + y_1/4 + \dots + y_{n-1}/2^n, and the phase e2πi xy/Ne^{2\pi i\,xy/N} factorises into one piece per wire:

QFT∣x⟩=⨂j=0n−112(∣0⟩+e2πi x/2 j+1∣1⟩).\mathrm{QFT}\ket{x} = \bigotimes_{j=0}^{n-1}\frac{1}{\sqrt2}\Bigl(\ket{0} + e^{2\pi i\,x/2^{\,j+1}}\ket{1}\Bigr).

Each output qubit is an equal superposition of ∣0⟩\ket{0} and ∣1⟩\ket{1} with a phase that depends on xx. The top qubit’s phase is e2πix/2=(−1)xe^{2\pi i x/2} = (-1)^x, which depends only on whether xx is odd. Each wire further down sees xx at twice the resolution of the one above.

To build such a qubit we need two ingredients: HH, and the controlled phase gates. The gate controlled-SS multiplies the state by ii when both of its qubits are 1, and does nothing otherwise. Controlled-TT does the same with eiπ/4e^{i\pi/4}. Because they act only when both qubits are 1, it makes no difference which one is called the control.

Here is the two-qubit QFT, on an input ∣x0x1⟩\ket{x_0x_1}, so that x=2x0+x1x = 2x_0 + x_1:

  1. HH on the top wire gives ∣0⟩+(−1)x0∣1⟩\ket{0} + (-1)^{x_0}\ket{1}, leaving out the 12\tfrac{1}{\sqrt2}.
  2. Controlled-SS between the wires adds a factor ix1i^{x_1} to the ∣1⟩\ket{1} part. The top wire now holds ∣0⟩+e2πi x/4∣1⟩\ket{0} + e^{2\pi i\,x/4}\ket{1}.
  3. HH on the bottom wire gives ∣0⟩+(−1)x1∣1⟩\ket{0} + (-1)^{x_1}\ket{1}, which is ∣0⟩+e2πi x/2∣1⟩\ket{0} + e^{2\pi i\,x/2}\ket{1}.
  4. The two wires now hold the right states in the wrong order, so swap them.
PuzzleBuild the two-qubit QFT

Build a circuit that acts as QFT4\mathrm{QFT}_4 on every input. The table lists what your circuit does to each basis state. Compare its rows with the columns of the matrix above.

This puzzle needs JavaScript.

Hint

Follow the four steps above in order. The HH on the bottom wire must come after the controlled-SS.

The pattern continues. For three qubits: HH on the top wire, then controlled-SS and controlled-TT onto it from the wires below, then the same again for the middle wire with one fewer gate, then HH on the bottom wire, and finally a swap of top and bottom. For nn qubits that is n(n+1)/2n(n+1)/2 gates plus about n/2n/2 swaps.

The controlled phases get finer with distance: controlled-SS between neighbouring wires, controlled-TT one wire further apart, then phases of π/8\pi/8, π/16\pi/16 and so on. The smallest ones barely change the state. In practice they are left out, which gives the approximate QFT with fewer gates and almost the same output.

The inverse QFT is the same circuit run backwards with each phase gate replaced by its inverse. It uses e−2πi xy/Ne^{-2\pi i\,xy/N} in place of e2πi xy/Ne^{2\pi i\,xy/N}, and turns a state with a steadily winding phase back into a single basis state. You will use it in the next lesson.

The classical fast Fourier transform on N=2nN = 2^n numbers takes about n2nn2^n steps. The QFT takes about n2n^2 gates. It is tempting to conclude that a quantum computer does Fourier transforms exponentially faster. It does not, at least not in the sense of handing you the answer.

  • The output is a quantum state. You cannot read its NN amplitudes. A measurement returns one value of yy, sampled according to the probabilities.
  • The input must already exist as a quantum state. Loading NN arbitrary numbers into amplitudes takes about NN steps in general, which would wipe out the saving.

The QFT is useful when the state comes from a quantum computation, and when one sample from the transformed state is enough. Finding a period is exactly that situation: every sample is a multiple of N/rN/r, and a few samples pin down rr.

The next lesson uses the inverse QFT for phase estimation, the subroutine that reads a phase out of a gate.

RecallAnswer from memory, then check
LessonFinish this lesson