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 -qubit QFT is the unitary defined by its action on every basis state :
This is the discrete Fourier transform of the unit vector with a at position . It maps computational-basis states to uniform superpositions with relative phases that encode the Fourier frequencies of .
The product-state representation
The key to an efficient circuit is the product representation. Writing in binary as , the QFT output factors as
where is a binary fraction equal to reduced modulo 1. Each output qubit is in a single-qubit superposition whose phase depends on only a few bits of .
This factored form dictates the circuit structure directly: qubit can be prepared independently by a Hadamard (which creates the factor) followed by controlled-phase gates that contribute the remaining fractional bits.
The reusable circuit pattern
For qubits, label them (most significant) through (least significant). The QFT without bit-reversal SWAPs is built by the following loop:
For :
- Apply to qubit .
- For : apply with control and target .
For this expands to exactly six gates:
| Step | Gate | Control | Target | Angle | |------|------|---------|--------|-------| | | | — | | — | | | | | | | | | | | | | | | | — | | — | | | | | | | | | | — | | — |
Gate count and scalability
The loop above uses Hadamards and
controlled-phase gates, giving gates in total — in the circuit depth. A classical FFT on the same -point input requires arithmetic operations, which is exponentially more than the gate count here (though the quantum circuit must still be read out, which collapses the superposition).
Why packaging matters
A reusable QFT routine means:
- Correctness by construction. You verify the unitary once; every caller inherits the proof.
- Easy composition. Phase estimation needs immediately after a controlled-unitary block. If the QFT is a function, swapping it for its adjoint is one line.
- Benchmarking. Module 15 uses the QFT subroutine as a calibration target: its known gate count and unitary let you measure compiler overhead and simulator accuracy.
Try it
The starter code completes the blocks for and but leaves the controlled-phase gate for 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.
Sign in on the full site to ask questions and join the discussion.