Quantum Fourier transform on three qubits

The subroutine inside Shor’s algorithm and phase estimation, built from H and controlled-phase gates.

Advanced · 3 qubits · 7 gates · 4 two-qubit · depth 7 · reviewed November 2026

Open in the Circuit Builder

What is happening

The QFT maps a basis state into an equal superposition with phases that wind around at a rate set by the input. The probabilities are all 1/8, so the Probabilities tab looks flat. The information is in the phases, which you can read in the State vector tab.

The input here is |001⟩, a value of 1. The output amplitude for k carries the phase e^(2πi·k/8). Doubling the input value doubles the winding rate, which is how the QFT turns a hidden period into something measurable.

Step by step

  1. 1. H and controlled phases on q0

    Each later qubit rotates q0’s phase by a smaller angle (π/2, then π/4).

  2. 2. Repeat on q1

    H on q1 then a controlled phase of π/2 from q2.

  3. 3. H on q2 and a SWAP

    The final SWAP reverses the qubit order so that the output reads in the usual order.

Expected result

All eight outcomes have probability 12.5%. In the State vector tab the phase of |k⟩ increases by 1/4 of a half-turn (π/4) with each step of k.

Qubit 0 is the leftmost bit. In Qiskit, bitstrings are printed in the reverse order.

Qiskit code

from qiskit import QuantumCircuit
from math import pi

qc = QuantumCircuit(3, 3)
qc.x(2)
qc.h(0)
qc.barrier()
qc.cp(pi/2, 1, 0)
qc.barrier()
qc.cp(pi/4, 2, 0)
qc.barrier()
qc.h(1)
qc.barrier()
qc.cp(pi/2, 2, 1)
qc.barrier()
qc.h(2)
qc.barrier()
qc.swap(0, 2)
qc.barrier()
qc.measure(0, 0)
qc.measure(1, 1)
qc.measure(2, 2)

# Qiskit prints bit strings with qubit 0 on the RIGHT; this site writes qubit 0 on the LEFT.

Try this

  • Change the input to |010⟩ and see the phases wind twice as fast.
  • Apply the circuit twice with the gates inverted in between to recover the input.
  • Read the Qiskit code and compare it with Qiskit’s built-in QFT.

Gates used

Related reading

Questions

Why does the QFT need so few gates?

For n qubits it needs about n²/2 gates, much fewer than the cost of a classical fast Fourier transform on 2^n values, though reading the result out is limited by measurement.