Bit-Reversal and SWAPs
The rotation stage of the QFT circuit — Hadamards plus controlled-phase gates — produces the correct Fourier amplitudes, but it places them in bit-reversed order. The final step of a standard QFT circuit corrects that order with a layer of SWAP gates.
Why the outputs are reversed
Recall the -qubit QFT definition for an input :
The rotation circuit builds the output qubit by qubit, starting from the most-significant qubit. The first qubit processed (, the most significant) ends up encoding the most-significant bit of the output frequency index . The last qubit processed (, least significant) encodes the least-significant bit of . In short, the rotation stage delivers the frequency index in reversed bit order.
For the mapping is:
where in standard binary. To get the natural order () we swap and .
The SWAP correction
For qubits the correction requires SWAPs: swap qubit with qubit , qubit with qubit , and so on, stopping at the middle.
| | SWAPs needed | |-----|-------------| | 2 | 1 (swap qubits 0 and 1) | | 3 | 1 (swap qubits 0 and 2; qubit 1 stays) | | 4 | 2 (swap 0,3 and swap 1,2) |
Each SWAP costs three CNOT gates in hardware, so practitioners sometimes fold the reversal into downstream circuits to avoid paying the cost explicitly. Textbook presentations always include the SWAPs so the QFT matrix matches the classical DFT matrix exactly.
Full 3-qubit QFT
Combining the rotation stage and the SWAP correction gives the complete circuit:
- , ,
- ,
This 7-gate sequence implements the exact QFT unitary.
Try it
The starter code contains the full rotation stage. Add the missing SWAP to put the output in natural bit order. The grader checks the full unitary matrix, so leaving the SWAP out will produce the bit-reversed variant and fail.
Sign in on the full site to ask questions and join the discussion.