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.
Numbers on wires
Section titled “Numbers on wires”From here on, a register of qubits will often stand for a number. The basis state means the bits of written on the wires, with the most significant bit on the top wire. With three qubits, . There are such states, to .
The definition
Section titled “The definition”The QFT acts on a basis state like this:
Every output gets the same magnitude, . All the information about is in the phases. As steps from 0 to , the phase winds around the unit circle times. So becomes a flat superposition, becomes one slow turn of phase, and larger gives faster winding.
For one qubit, and the phases are . That is the Hadamard gate. The QFT is the generalisation of from signs to all the -th roots of unity.
For two qubits, and the phases are powers of :
This is the matrix of the classical discrete Fourier transform, and it is unitary. On a superposition the QFT acts linearly, so the new amplitudes are the discrete Fourier transform of the old ones.
Compare the QFT with a row of Hadamards when both act on .
With every phase equals 1. The two transforms differ on other inputs.
What it does to a periodic state
Section titled “What it does to a periodic state”The simulator uses six qubits, so . The input is a state with equal amplitudes on every -th value, starting at some offset: a comb with period . The bars show probabilities before and after the QFT.
Things to try:
- Set the period to 4, then 8, then 16. Where are the peaks after the transform? How many are there?
- With the period at 8, move the offset. What changes in the lower plot?
- 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 is sent to peaks at the multiples of . The amplitudes at every other 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 does not divide , the peaks blur a little. They sit at the whole numbers nearest to the multiples of , 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 , or something close to one. From that, the period can be worked out.
On 64 points, a state has period 8. Where are the peaks after the QFT?
The peaks are at multiples of . There are of them, equally likely.
The circuit
Section titled “The circuit”A general unitary on qubits needs a number of gates that grows exponentially with . The QFT needs about . The reason is that its output is a product state.
Write in binary, with bit on wire . Then , and the phase factorises into one piece per wire:
Each output qubit is an equal superposition of and with a phase that depends on . The top qubit’s phase is , which depends only on whether is odd. Each wire further down sees at twice the resolution of the one above.
To build such a qubit we need two ingredients: , and the controlled phase gates. The gate controlled- multiplies the state by when both of its qubits are 1, and does nothing otherwise. Controlled- does the same with . 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 , so that :
- on the top wire gives , leaving out the .
- Controlled- between the wires adds a factor to the part. The top wire now holds .
- on the bottom wire gives , which is .
- The two wires now hold the right states in the wrong order, so swap them.
Build a circuit that acts as 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 on the bottom wire must come after the controlled-.
The pattern continues. For three qubits: on the top wire, then controlled- and controlled- onto it from the wires below, then the same again for the middle wire with one fewer gate, then on the bottom wire, and finally a swap of top and bottom. For qubits that is gates plus about swaps.
The controlled phases get finer with distance: controlled- between neighbouring wires, controlled- one wire further apart, then phases of , 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 in place of , and turns a state with a steadily winding phase back into a single basis state. You will use it in the next lesson.
What the QFT is not
Section titled “What the QFT is not”The classical fast Fourier transform on numbers takes about steps. The QFT takes about 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 amplitudes. A measurement returns one value of , sampled according to the probabilities.
- The input must already exist as a quantum state. Loading arbitrary numbers into amplitudes takes about 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 , and a few samples pin down .
The next lesson uses the inverse QFT for phase estimation, the subroutine that reads a phase out of a gate.
Remember it
Section titled “Remember it”How does the quantum Fourier transform act on a basis state of a register with values?
.
What is the QFT on a single qubit?
The Hadamard gate.
A state on points has period . Where are the peaks after the QFT?
At the multiples of (or the whole numbers nearest to them).
What happens to the QFT output when the input state is shifted sideways?
Only the phases change. The measurement probabilities stay the same.
Which gates build the QFT on qubits, and roughly how many?
Hadamards and controlled phase gates, about of them, followed by swaps to reverse the order of the wires.
Why is the QFT not a faster way to compute the Fourier transform of a list of data?
The output amplitudes cannot be read, only sampled once per run, and loading arbitrary data into amplitudes takes about steps.