Building the QFT circuit, gate by gate
A Hadamard and a ladder of controlled phase rotations per qubit, then a row of swaps. This guide derives why that circuit equals the Fourier matrix, with a worked three-qubit example.
10 min read · reviewed December 2026
The target: a product of single-qubit states
The definition of the QFT sums over N terms, which looks too heavy for a short circuit. The key fact is that the output factorises into a product of n single-qubit states. Write the input integer j in binary as j₁j₂…jₙ, with j₁ the most significant bit. Then the output is a product state in which each qubit carries a different pattern of phases.
F |j₁ j₂ … jₙ⟩ = 2^(−n/2) (|0⟩ + e^(2πi·0.jₙ)|1⟩) (|0⟩ + e^(2πi·0.jₙ₋₁jₙ)|1⟩) … (|0⟩ + e^(2πi·0.j₁j₂…jₙ)|1⟩)
- The notation 0.j₁j₂…jₙ means the binary fraction j₁/2 + j₂/4 + … + jₙ/2^n. It is j divided by N.
- The first factor depends only on the least significant bit. The last factor depends on all the bits. The factors are in reverse order compared with the input, and this is the source of the final swaps.
- There is no entanglement in the output of a basis-state input. Every qubit is its own little state, and each is prepared by a short sequence of gates.
One qubit at a time
Take the first qubit, holding j₁. A Hadamard gate turns |0⟩ into an equal superposition with a plus sign and |1⟩ into an equal superposition with a minus sign. That minus sign is a phase of e^(2πi·0.j₁), because 0.j₁ is 1/2 when j₁ is 1, and e^(πi) is −1. So a single Hadamard produces the factor (|0⟩ + e^(2πi·0.j₁)|1⟩)/√2.
To bring in the next bit, j₂, we need an extra phase of e^(2πi·j₂/4), which is a quarter turn when j₂ is 1 and nothing when j₂ is 0. A controlled phase gate does that: it multiplies the state by the phase only when its control qubit (holding j₂) is 1. This gate is called R₂. The next bit, j₃, needs an eighth of a turn, which is R₃, and so on. The gate R_k adds a phase of 2π/2^k on the state where both qubits are 1.
| Gate | Angle | Degrees | Common name |
|---|---|---|---|
| R2 | 2π / 4 | 90.00° | S gate (controlled) |
| R3 | 2π / 8 | 45.00° | T gate (controlled) |
| R4 | 2π / 16 | 22.50° | none |
| R5 | 2π / 32 | 11.25° | none |
| R6 | 2π / 64 | 5.63° | none |
| R8 | 2π / 256 | 1.406° | none |
| R10 | 2π / 1024 | 0.352° | none |
Worked example on three qubits
Label the qubits 1, 2 and 3 from the most significant. The circuit is, in order:
- Hadamard on qubit 1. Controlled R₂ with control qubit 2 and target qubit 1. Controlled R₃ with control qubit 3 and target qubit 1. Qubit 1 now holds the factor with the phase 0.j₁j₂j₃.
- Hadamard on qubit 2. Controlled R₂ with control qubit 3 and target qubit 2. Qubit 2 now holds the factor with phase 0.j₂j₃.
- Hadamard on qubit 3. Qubit 3 now holds the factor with phase 0.j₃.
- Swap qubit 1 with qubit 3. Qubit 2 stays where it is. The factors are now in the order the definition requires.
Why the swaps are needed
After the Hadamards and rotations, the qubit that holds j₁ has become the qubit that carries the longest binary fraction, which is the last factor of the product above. The output is therefore a reversed copy of what the definition asks for. A row of ⌊n/2⌋ swaps puts it right. Some algorithms leave the swaps out and read the register in reverse order, which is fine when the next step is a measurement and the bit order can be fixed in classical processing. Phase estimation is one case where the order matters, so the lab keeps the swaps.
When you press Next in the lab, the last step is named Reverse qubit order. Look at the table before and after: the sizes do not change, only the order of the qubit labels does.
The inverse circuit
To undo the transform, run the same gates in the opposite order with every rotation angle negated. The swaps come first, then for each qubit from the last to the first, the controlled rotations with negative angles, and then the Hadamard. The lab’s phase-estimation tab uses this circuit, and its tests confirm that applying the forward circuit and then the inverse returns the original state to numerical precision.
Counting the gates
Qubit 1 needs one Hadamard and n − 1 rotations, qubit 2 needs one Hadamard and n − 2, and so on. In total there are n Hadamards and n(n − 1)/2 controlled phases, plus ⌊n/2⌋ swaps. That is a number of gates that grows like n², where n is the number of qubits, which is the logarithm of the vector length N. The cost guide compares this with the classical fast Fourier transform.
The circuit is short because the Fourier output factorises into n single-qubit states. That structure, not any trick of simulation, is what makes the QFT cheap.
Try it
Questions
Why does the circuit need controlled phase gates?
Each output qubit must carry a phase that depends on several input bits at once. A controlled phase gate lets one qubit’s value change the phase of another.
Can the swaps be skipped?
Yes, if you read the output register in reverse order afterwards. Algorithms that feed the result into another quantum step usually need them.
Is the order of the rotations important?
The controlled phase gates all commute with one another, so the order within one qubit’s ladder does not matter. The Hadamards must come in the order shown.
Read next
Sources and further reading
- P. Shor, “Polynomial-time algorithms for prime factorization and discrete logarithms on a quantum computer” (1994, journal version 1997)
- A. Kitaev, “Quantum measurements and the Abelian stabilizer problem” (1995)
- R. Cleve, A. Ekert, C. Macchiavello and M. Mosca, “Quantum algorithms revisited” (1998)
- D. Coppersmith, “An approximate Fourier transform useful in quantum factoring” (1994, IBM research report)
- J. Cooley and J. Tukey, “An algorithm for the machine calculation of complex Fourier series” (1965)
- M. Nielsen and I. Chuang, Quantum Computation and Quantum Information, chapter 5
Check the original sources for exact citations.