|q⟩ Bad Qubits

intermediate · Programming · Algorithm Projects & Benchmarking

Project: Build a QFT Library

Real quantum programs reuse sub-circuits the way classical programs reuse functions. The Quantum Fourier Transform (QFT) is the canonical example: it appears inside phase estimation, Shor's algorithm, and quantum arithmetic as a black-box subroutine. In this project you package a correct, self-contained QFT routine that can be dropped into any larger circuit.

Recap: what the QFT does

The nn-qubit QFT is the unitary defined by its action on every basis state j|j\rangle:

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.

This is the discrete Fourier transform of the unit vector with a 11 at position jj. It maps computational-basis states to uniform superpositions with relative phases that encode the Fourier frequencies of jj.

The product-state representation

The key to an efficient circuit is the product representation. Writing jj in binary as j=jn12n1++j0j = j_{n-1} 2^{n-1} + \cdots + j_0, the QFT output factors as

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

where 0.jn1kj00.j_{n-1-k}\cdots j_0 is a binary fraction equal to j/2nkj / 2^{n-k} reduced modulo 1. Each output qubit is in a single-qubit superposition whose phase depends on only a few bits of jj.

This factored form dictates the circuit structure directly: qubit kk can be prepared independently by a Hadamard (which creates the 0+eiπjn1k1|0\rangle + e^{i\pi j_{n-1-k}}|1\rangle factor) followed by controlled-phase gates that contribute the remaining fractional bits.

The reusable circuit pattern

For nn qubits, label them q0q_0 (most significant) through qn1q_{n-1} (least significant). The QFT without bit-reversal SWAPs is built by the following loop:

For k=0,1,,n1k = 0, 1, \ldots, n-1:

  1. Apply HH to qubit qkq_k.
  2. For j=1,2,,n1kj = 1, 2, \ldots, n-1-k: apply CP ⁣(π2j)CP\!\left(\dfrac{\pi}{2^j}\right) with control qkq_k and target qk+jq_{k+j}.

For n=3n = 3 this expands to exactly six gates:

| Step | Gate | Control | Target | Angle | |------|------|---------|--------|-------| | k=0k=0 | HH | — | q0q_0 | — | | k=0,j=1k=0, j=1 | CPCP | q0q_0 | q1q_1 | π/2\pi/2 | | k=0,j=2k=0, j=2 | CPCP | q0q_0 | q2q_2 | π/4\pi/4 | | k=1k=1 | HH | — | q1q_1 | — | | k=1,j=1k=1, j=1 | CPCP | q1q_1 | q2q_2 | π/2\pi/2 | | k=2k=2 | HH | — | q2q_2 | — |

Gate count and scalability

The loop above uses nn Hadamards and

k=0n1(n1k)=n(n1)2\sum_{k=0}^{n-1}(n - 1 - k) = \frac{n(n-1)}{2}

controlled-phase gates, giving n(n+1)2\dfrac{n(n+1)}{2} gates in total — O(n2)O(n^2) in the circuit depth. A classical FFT on the same N=2nN = 2^n-point input requires O(NlogN)=O(n2n)O(N \log N) = O(n \cdot 2^n) arithmetic operations, which is exponentially more than the O(n2)O(n^2) gate count here (though the quantum circuit must still be read out, which collapses the superposition).

Why packaging matters

A reusable QFT routine means:

Try it

The starter code completes the blocks for q0q_0 and q2q_2 but leaves the controlled-phase gate for q1q_1 as a TODO. Add the missing gate to produce the correct 3-qubit QFT unitary. The grader checks the full unitary matrix, so a missing or wrong gate will fail.

Run your code to see the quantum state.

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