|q⟩ Bad Qubits

intermediate · Programming · The Quantum Fourier Transform

Checkpoint: Implement the QFT

This checkpoint asks you to assemble the complete 3-qubit QFT from scratch — Hadamards, controlled-phase rotations, and the final bit-reversal SWAPs. It consolidates everything covered in this module.

Recap: the product representation

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

QFTnj=12nk=0n1(0+e2πij/2k+11).\mathrm{QFT}_n|j\rangle = \frac{1}{\sqrt{2^n}} \bigotimes_{k=0}^{n-1} \Bigl(|0\rangle + e^{2\pi i\, j / 2^{k+1}}\,|1\rangle\Bigr).

Each tensor factor depends only on a single binary fraction of jj, which is what makes an efficient circuit possible: a Hadamard creates the 0+eiϕ1|0\rangle + e^{i\phi}|1\rangle superposition, and controlled-phase gates CP(λ)CP(\lambda) accumulate the remaining phase contributions from the more-significant (lower-indexed) qubits.

The 3-qubit circuit structure

Label the qubits q0q_0 (most significant), q1q_1, q2q_2 (least significant). The algorithm proceeds in three stages:

Stage for q0q_0: Apply H(q0)H(q_0) to start the Fourier superposition, then use q1q_1 and q2q_2 as controls to add the finer phase contributions. The angles are π/2\pi/2 (from q1q_1) and π/4\pi/4 (from q2q_2):

H(q0),CP ⁣(π2)q1q0,CP ⁣(π4)q2q0.H(q_0),\quad CP\!\left(\tfrac{\pi}{2}\right)_{q_1 \to q_0},\quad CP\!\left(\tfrac{\pi}{4}\right)_{q_2 \to q_0}.

Stage for q1q_1: Apply H(q1)H(q_1), then one controlled-phase from q2q_2 with angle π/2\pi/2:

H(q1),CP ⁣(π2)q2q1.H(q_1),\quad CP\!\left(\tfrac{\pi}{2}\right)_{q_2 \to q_1}.

Stage for q2q_2: Just a Hadamard:

H(q2).H(q_2).

Bit reversal: The stages above leave the output in reversed bit order — q0q_0 holds the least significant Fourier frequency. A single SWAP between q0q_0 and q2q_2 restores the standard ordering where q0q_0 carries the most significant frequency.

Gate count

For n=3n = 3 the circuit uses n=3n = 3 Hadamards and n(n1)/2=3n(n-1)/2 = 3 controlled-phase gates, for 6 gates total before the n/2=1\lfloor n/2 \rfloor = 1 SWAP. In general the QFT uses O(n2)O(n^2) gates, compared with the O(n2n)O(n \cdot 2^n) operations of a classical FFT on the same 2n2^n-point input.

Try it

Complete the circuit by adding the missing controlled-phase rotation. The grader checks the full unitary matrix of your 3-qubit circuit against the exact QFT unitary, so every gate must be correct — an incomplete or wrong circuit will fail.

Run your code to see the quantum state.

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