Week 7 | the Fourier transform on 2n amplitudes with about n2/2 gates, and why that does not make it a fast FFT | reading: Watrous Ch. 7 (the QFT); de Wolf Ch. 4
Simon's algorithm found a hidden XOR-period with the Hadamard transform. Shor's algorithm finds an ordinary period, \(f(x+r)=f(x)\) with integer addition, and for that it needs the Fourier transform over the integers mod \(N\). This lecture builds it. By the end you should be able to define the quantum Fourier transform, prove that it is unitary, state and use its product formula, build its circuit and count its gates, explain why it is not a faster FFT, and show how it turns a periodic state into peaks at multiples of \(N/r\).
The discrete Fourier transform of a list \((a_0,\dots,a_{N-1})\) is the list \(b_y=\tfrac{1}{\sqrt N}\sum_x\omega^{xy}a_x\), where \(\omega=e^{2\pi i/N}\) is a primitive \(N\)-th root of unity. Engineers use it to find the frequencies hidden in a signal: a signal that repeats every \(r\) samples has a spectrum concentrated on multiples of \(N/r\). The quantum version applies the same matrix to the amplitudes of a state.
For \(N=2^n\) and \(\omega=e^{2\pi i/N}\), the quantum Fourier transform on \(n\) qubits is the linear map with
$$\mathrm{QFT}_N\,|x\rangle=\frac{1}{\sqrt N}\sum_{y=0}^{N-1}\omega^{xy}\,|y\rangle ,\qquad x=0,1,\dots,N-1 ,$$where \(|x\rangle\) is the basis state whose bits are the binary digits of \(x\) (qubit \(j\) holds the \(2^j\) digit, as in Lecture 5). Its matrix has entry \(\omega^{xy}/\sqrt N\) in row \(y\), column \(x\).
In \(\mathrm{QFT}_8|2\rangle\), the amplitude of \(|3\rangle\) is \(\tfrac{1}{\sqrt8}e^{i\varphi}\) with \(0\le\varphi\lt2\pi\). Find \(\varphi\).
For \(N=2\), \(\omega=-1\) and the matrix is \(\tfrac{1}{\sqrt2}\begin{pmatrix}1&1\\1&-1\end{pmatrix}=H\): the one-qubit QFT is the Hadamard gate. For larger \(N\) the QFT is not \(H^{\otimes n}\); the Hadamard transform of Lecture 9 is the Fourier transform for XOR, and the QFT is the one for addition mod \(N\).
The columns of \(\mathrm{QFT}_N\) are orthonormal, so \(\mathrm{QFT}_N\) is unitary, and its inverse is \(\mathrm{QFT}_N^{-1}|y\rangle=\tfrac{1}{\sqrt N}\sum_x\omega^{-xy}|x\rangle\).
The inner product of columns \(x\) and \(x'\) is \(\tfrac1N\sum_y\overline{\omega^{xy}}\,\omega^{x'y}=\tfrac1N\sum_y\omega^{ky}\) with \(k=x'-x\). If \(k\equiv0\pmod N\) every term is 1 and the sum is \(N\). Otherwise \(\omega^k\neq1\), and the geometric series gives
$$\sum_{y=0}^{N-1}\big(\omega^k\big)^y=\frac{\omega^{kN}-1}{\omega^k-1}=0 ,$$since \(\omega^{kN}=1\). (This is Lecture 2's fact that the \(N\)-th roots of unity, walked around the circle, return to the start.) So the columns are orthonormal. The inverse of a unitary is its conjugate transpose, whose entries are \(\overline{\omega^{xy}}/\sqrt N=\omega^{-xy}/\sqrt N\).
By Proposition 13.1, \(\mathrm{QFT}_8^{-1}|1\rangle\) is equal to \(\mathrm{QFT}_8|x\rangle\) for exactly one \(x\in\{0,\dots,7\}\). Which?
Describe \(\mathrm{QFT}_8|0\rangle\), \(\mathrm{QFT}_8|1\rangle\) and \(\mathrm{QFT}_8|3\rangle\).
Every output amplitude has size \(1/\sqrt8\); only the phases differ. For \(x=0\) all phases are \(\omega^0=1\): \(\mathrm{QFT}|0\rangle\) is the uniform superposition, exactly like \(H^{\otimes3}|000\rangle\). For \(x=1\) the phase of \(|y\rangle\) is \(\omega^y\), which advances by one eighth of a turn per step of \(y\) and winds once around the circle as \(y\) runs from 0 to 7. For \(x=3\) the phase \(\omega^{3y}\) advances by three eighths per step and winds three times. In general \(\mathrm{QFT}|x\rangle\) is a "wave" of frequency \(x\).
The QFT of a basis state is not entangled: it is a product of single-qubit states. That fact is what makes it cheap to implement. We write \(e^{2\pi i\,x/2^k}\) for the phase that a full turn, divided into \(2^k\) parts, reaches after \(x\) steps; it depends only on the last \(k\) bits of \(x\), since adding multiples of \(2^k\) to \(x\) adds whole turns.
For \(N=2^n\) and every \(x\),
$$\begin{aligned}\mathrm{QFT}_N|x\rangle=\;&\frac{|0\rangle+e^{2\pi i\,x/2}|1\rangle}{\sqrt2}\otimes\frac{|0\rangle+e^{2\pi i\,x/4}|1\rangle}{\sqrt2}\\[4pt] &\otimes\cdots\otimes\frac{|0\rangle+e^{2\pi i\,x/2^n}|1\rangle}{\sqrt2},\end{aligned}$$with the factors in ket order: the leftmost is qubit \(n-1\) and the rightmost is qubit 0. So output qubit \(j\) is in the state \(\tfrac{1}{\sqrt2}\big(|0\rangle+e^{2\pi i\,x/2^{n-j}}|1\rangle\big)\).
Proof. HW7 asks you to derive this for \(n=3\); Example 13.2 does \(n=2\). The idea: write \(y=\sum_jy_j2^j\), so \(\omega^{xy}=\prod_j\omega^{xy_j2^j}\), and the sum over \(y\) splits into a product of sums over each bit \(y_j\).
By Theorem 13.2, in \(\mathrm{QFT}_8|5\rangle\) qubit 2 is in the state and qubit 1 is in the state .
Show that \(\mathrm{QFT}_4|x\rangle=\tfrac{1}{\sqrt2}\big(|0\rangle+(-1)^{x_0}|1\rangle\big)\otimes\tfrac{1}{\sqrt2}\big(|0\rangle+i^{x}|1\rangle\big)\), where \(x=2x_1+x_0\).
Here \(\omega=i\). Write \(y=2y_1+y_0\), so \(\omega^{xy}=i^{2xy_1}\,i^{xy_0}=(-1)^{xy_1}\,i^{xy_0}\). Then
$$\begin{aligned}\mathrm{QFT}_4|x\rangle&=\frac12\sum_{y_1,y_0}(-1)^{xy_1}i^{xy_0}\,|y_1\rangle|y_0\rangle\\[4pt] &=\Big(\frac{1}{\sqrt2}\sum_{y_1}(-1)^{xy_1}|y_1\rangle\Big)\otimes\Big(\frac{1}{\sqrt2}\sum_{y_0}i^{xy_0}|y_0\rangle\Big).\end{aligned}$$Finally \((-1)^x=(-1)^{x_0}\), because \(2x_1\) is even. The left factor (qubit 1) depends only on the last bit of \(x\), and the right factor (qubit 0) on all of \(x\), matching Theorem 13.2: \(e^{2\pi ix/2}=(-1)^x\) and \(e^{2\pi ix/4}=i^x\).
Theorem 13.2 says that each output qubit needs the phase \(e^{2\pi ix/2^k}\) on its \(|1\rangle\) part for one value of \(k\). Expanding \(x=\sum_jx_j2^j\), that phase is a product of factors \(e^{2\pi i\,x_j2^j/2^k}\), one for each input bit \(j\lt k\). The circuit creates them with one Hadamard and a ladder of controlled phase gates \(CP(\varphi)=\mathrm{diag}(1,1,1,e^{i\varphi})\), which multiply \(|11\rangle\) by \(e^{i\varphi}\) and, like CZ, have no preferred control.
For \(j=n-1,n-2,\dots,0\): apply \(H\) to qubit \(j\), then for each \(k\lt j\) a controlled phase \(CP(\pi/2^{\,j-k})\) between qubits \(k\) and \(j\). Finally reverse the order of the qubits with \(\lfloor n/2\rfloor\) SWAPs. This circuit implements \(\mathrm{QFT}_{2^n}\) with \(n\) Hadamards and \(n(n-1)/2\) controlled phases: \(n(n+1)/2\) gates, plus the SWAPs, which a compiler can often absorb by relabelling qubits.
When \(H\) acts on qubit \(j\), that qubit still holds \(x_j\), and \(H|x_j\rangle=\tfrac{1}{\sqrt2}(|0\rangle+e^{2\pi i\,x_j2^j/2^{j+1}}|1\rangle)\) because \(e^{\pi ix_j}=(-1)^{x_j}\). Each \(CP(\pi/2^{\,j-k})\) controlled by qubit \(k\), which still holds \(x_k\), multiplies the \(|1\rangle\) part by \(e^{i\pi x_k/2^{j-k}}=e^{2\pi i\,x_k2^k/2^{j+1}}\). After the ladder, qubit \(j\) carries \(\prod_{k\le j}e^{2\pi i\,x_k2^k/2^{j+1}}=e^{2\pi i\,x/2^{j+1}}\), using that only the bits of \(x\) below \(2^{j+1}\) matter. Qubits below \(j\) have not been touched yet, so they still hold their bits when their turn comes. Qubit \(j\) now holds the factor that Theorem 13.2 assigns to qubit \(n-1-j\); the SWAPs move each factor to its place. (verify_L13.py also checks the circuit against the matrix for \(n\le5\).)
How many controlled phases and how many SWAPs does the circuit of Theorem 13.3 use for \(n=9\) qubits?
The classical fast Fourier transform computes the DFT of \(N=2^n\) numbers with about \(N\log_2N=n2^n\) arithmetic operations. The QFT uses about \(n^2/2\) gates. The gap is exponential:
| \(n\) | \(N=2^n\) | QFT gates \(n(n+1)/2\) | FFT operations \(\approx n2^n\) |
|---|---|---|---|
| 10 | 1,024 | 55 | about \(1.0\times10^4\) |
| 20 | 1,048,576 | 210 | about \(2.1\times10^7\) |
| 30 | about \(1.1\times10^9\) | 465 | about \(3.2\times10^{10}\) |
The comparison is unfair in both directions. The input: loading \(N\) arbitrary numbers into the amplitudes of a state generally takes about \(N\) gates, which already costs as much as the FFT. The output: the QFT leaves the \(N\) Fourier coefficients in the amplitudes, and a measurement returns one \(y\), with probability \(|b_y|^2\). Reading all coefficients would take about \(N\) repetitions per digit of precision. So the QFT is useful only inside algorithms that prepare the input state efficiently and need a single sample from the output distribution. Period finding is exactly such a problem.
Tick every true statement about the \(n\)-qubit QFT with \(N=2^n\).
The controlled phases \(CP(\pi/2^d)\) for large distance \(d\) are nearly the identity: \(\pi/2^{10}\) is under 0.2 degrees. Coppersmith (1994) observed that they can simply be dropped.
Keep only the controlled phases \(CP(\pi/2^d)\) with \(d\lt m\). The resulting circuit \(\widetilde{\mathrm{QFT}}\) satisfies \(\|\widetilde{\mathrm{QFT}}-\mathrm{QFT}\|\le n\pi/2^{m-1}\) in operator norm, and it uses about \(nm\) controlled phases instead of \(n^2/2\). Choosing \(m\approx\log_2(n/\varepsilon)+3\) gives error at most \(\varepsilon\).
A dropped gate \(CP(\varphi)\) differs from the identity by \(|e^{i\varphi}-1|\le\varphi\) in operator norm. Replacing gates one at a time and using that unitaries preserve norms, the errors of a sequence of gates add up. There are \(n-d\le n\) dropped gates at each distance \(d\ge m\), so the total is at most \(\sum_{d\ge m}n\pi/2^d=n\pi/2^{m-1}\). For the gate count, each qubit keeps at most \(m-1\) controlled phases.
For a 16-qubit QFT that keeps only the controlled phases with distance \(d\lt10\), what error bound does Proposition 13.4 give? (Three decimal places.)
For \(n=8\) the exact operator-norm errors, computed in verify_L13.py, are 0.414, 0.123 and 0.025 for \(m=5,6,7\). For a 20-qubit QFT with \(m=8\), 112 of the 190 controlled phases are kept. In Shor's algorithm \(n\) is in the thousands, so dropping all but about \(\log n\) rotations per qubit turns \(O(n^2)\) into \(O(n\log n)\) gates, and it removes the need for absurdly precise rotation angles, which a fault-tolerant machine would have to synthesise from many \(T\) gates (Lecture 7).
Here is the property Shor's algorithm uses.
Let \(r\) divide \(N\), let \(0\le x_0\lt r\), and let \(|\psi\rangle=\sqrt{r/N}\sum_{j=0}^{N/r-1}|x_0+jr\rangle\), the uniform superposition of an arithmetic progression with step \(r\). Then measuring \(\mathrm{QFT}|\psi\rangle\) gives each multiple of \(N/r\) with probability \(1/r\), and never anything else. The offset \(x_0\) only changes phases.
The amplitude of \(|y\rangle\) is
$$\frac{1}{\sqrt N}\sqrt{\frac rN}\sum_{j=0}^{N/r-1}\omega^{(x_0+jr)y}=\frac{\sqrt r}{N}\,\omega^{x_0y}\sum_{j=0}^{N/r-1}\big(\omega^{ry}\big)^j .$$Now \(\omega^{r}=e^{2\pi i/(N/r)}\) is a primitive \((N/r)\)-th root of unity, so by the geometric-series argument of Proposition 13.1 the sum is \(N/r\) when \(N/r\) divides \(y\) and 0 otherwise. In the first case the amplitude has size \(\tfrac{\sqrt r}{N}\cdot\tfrac Nr=\tfrac{1}{\sqrt r}\), so its probability is \(\tfrac1r\); there are exactly \(r\) multiples of \(N/r\) below \(N\). The factor \(\omega^{x_0y}\) is a phase.
Take \(N=32\) and the periodic state with period \(r=8\) and offset \(x_0=3\). After the QFT, what are the probabilities of measuring \(y=12\) and \(y=6\)?
A measured \(y\) is a multiple of \(N/r\), say \(y=c\,N/r\), so \(y/N=c/r\): the measurement reveals a random fraction with denominator \(r\). A few samples pin \(r\) down. Shor's algorithm (Lectures 15–16) prepares such a periodic state from \(f(x)=a^x\bmod M\), where the period \(r\) is unknown and in general does not divide \(N\), which is why the bottom row of the figure matters.
With \(N=32\) and a period \(r\) that divides 32, one run returns \(y=20\). The period \(r\) is .
import numpy as np
from qiskit import QuantumCircuit
from qiskit.circuit.library import QFTGate
from qiskit.quantum_info import Statevector
n = 4; N = 2 ** n
def periodic_state(r, x0):
"""uniform superposition of x0, x0 + r, x0 + 2r, ... below N"""
amps = np.zeros(N); idx = np.arange(x0, N, r); amps[idx] = 1 / np.sqrt(len(idx))
return Statevector(amps)
qft = QuantumCircuit(n); qft.append(QFTGate(n), range(n))
for r, x0 in [(4, 1), (3, 0)]:
p = periodic_state(r, x0).evolve(qft).probabilities()
top = np.argsort(-p)[:5]
print(f"r = {r}:", {int(y): round(float(p[y]), 3) for y in sorted(top) if p[y] > 1e-9})
r = 4: {0: 0.25, 4: 0.25, 8: 0.25, 12: 0.25}
r = 3: {0: 0.375, 5: 0.234, 6: 0.036, 10: 0.036, 11: 0.234}
QFTGate(n) implements Definition 13.1 with Qiskit's usual ordering: the integer \(x\) is read with qubit 0 as the \(2^0\) digit, and the output index \(y\) the same way. The two runs reproduce Proposition 13.5 and the bottom row of the figure.
from qiskit import QuantumCircuit, transpile
from qiskit.circuit.library import QFTGate
for n in range(3, 9):
qc = QuantumCircuit(n); qc.append(QFTGate(n), range(n))
textbook = transpile(qc, basis_gates=["h", "cp", "swap"], optimization_level=0)
ibm = transpile(qc, basis_gates=["cx", "rz", "sx", "x"], optimization_level=1, seed_transpiler=1)
print(n, dict(textbook.count_ops()), " cx after transpiling:", ibm.count_ops()["cx"])
3 {'h': 3, 'cp': 3, 'swap': 1} cx after transpiling: 9
4 {'cp': 6, 'h': 4, 'swap': 2} cx after transpiling: 18
5 {'cp': 10, 'h': 5, 'swap': 2} cx after transpiling: 26
6 {'cp': 15, 'h': 6, 'swap': 3} cx after transpiling: 39
7 {'cp': 21, 'h': 7, 'swap': 3} cx after transpiling: 51
8 {'cp': 28, 'h': 8, 'swap': 4} cx after transpiling: 68
The counts match Theorem 13.3: \(n\) Hadamards, \(n(n-1)/2\) controlled phases and \(\lfloor n/2\rfloor\) SWAPs. Each controlled phase costs 2 CNOTs and each SWAP 3 (Lecture 6), which gives the CNOT column exactly: for \(n=8\), \(2\cdot28+3\cdot4=68\).
Next time: phase estimation, which uses the inverse QFT to read eigenvalues of unitaries. HW7 covers Lectures 13 and 14; the midterm follows.
all numbers and figures in these notes are computed in python (verify_L13.py) and follow the page theme