|q⟩ Bad Qubits

intermediate · Programming · The Quantum Fourier Transform

Controlled Rotations in the QFT

The nn-qubit QFT maps a computational basis state j|j\rangle to

QFTnj=12nk=02n1e2πijk/2nk.\mathrm{QFT}_n|j\rangle = \frac{1}{\sqrt{2^n}} \sum_{k=0}^{2^n - 1} e^{2\pi i \, jk / 2^n} |k\rangle.

The single-qubit case (previous lesson) reduces to a Hadamard. For more qubits the circuit needs controlled phase gates to encode the finer frequency components.

The controlled phase gate

The controlled-P(λ)P(\lambda) gate (written CP(λ)CP(\lambda)) acts on a control qubit cc and a target qubit tt:

CP(λ)=(100001000010000eiλ)CP(\lambda) = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & e^{i\lambda} \end{pmatrix}

in the {00,01,10,11}\{|00\rangle, |01\rangle, |10\rangle, |11\rangle\} basis. It does nothing unless both qubits are 1|1\rangle, in which case it multiplies the amplitude by eiλe^{i\lambda}. No control, no rotation — this conditional action is what makes the QFT constructive.

The QFT uses a family of these gates with angles λk=π/2k1\lambda_k = \pi / 2^{k-1}. In traditional notation these are called RkR_k gates:

Rk=CP ⁣(π2k1),k=1,2,3,R_k = CP\!\left(\frac{\pi}{2^{k-1}}\right), \qquad k = 1, 2, 3, \ldots

So R1=CP(π)R_1 = CP(\pi) (a controlled-ZZ), R2=CP(π/2)R_2 = CP(\pi/2), R3=CP(π/4)R_3 = CP(\pi/4), and so on.

The 3-qubit QFT circuit

For n=3n = 3 qubits (labeled q0,q1,q2q_0, q_1, q_2 from most to least significant) the QFT circuit reads:

  1. H(q0)H(q_0), then CP(π/2)CP(\pi/2) with control q0q_0, target q1q_1, then CP(π/4)CP(\pi/4) with control q0q_0, target q2q_2.
  2. H(q1)H(q_1), then CP(π/2)CP(\pi/2) with control q1q_1, target q2q_2.
  3. H(q2)H(q_2).

The pattern is systematic: each qubit first gets a Hadamard, then acts as the control for successively finer phase kicks to the less-significant qubits. The angle halves with each step because the target qubit sits one position further down the binary fraction.

Gate count

The nn-qubit QFT uses exactly nn Hadamards and n(n1)/2n(n-1)/2 controlled-phase gates — a total of O(n2)O(n^2) gates. This is exponentially fewer than the O(n2n)O(n \cdot 2^n) operations needed by the classical FFT on the same 2n2^n-point input.

For n=3n = 3: 3 Hadamards and 32/2=33 \cdot 2 / 2 = 3 controlled-phase gates, for 6 gates total.

Try it

The starter code gives you the first qubit's block and the Hadamard for qubit 1. Add the missing controlled-phase rotation for qubit 1. The grader checks the full unitary matrix, so leaving the circuit incomplete will fail.

Run your code to see the quantum state.

Sign in on the full site to ask questions and join the discussion.