Quantum Builders ClubQiskit tutorials

Free Qiskit tutorial · Intermediate

The quantum Fourier transform in Qiskit, built gate by gate

The QFT turns a number into a pattern of phases, and its inverse turns the phases back into the number. It's the engine inside Shor's algorithm and phase estimation.

Start my 7-day free trialJump to the code

To work through
20 min
Level
Intermediate
Tested on
Qiskit 2.6
Runs on your laptop
Free

The idea

Quantum Fourier transform, in plain words

The quantum Fourier transform does to amplitudes what the ordinary discrete Fourier transform does to a list of numbers. On a basis state |x⟩ of n qubits it gives an equal superposition of every |k⟩, each with a phase of x·k/2ⁿ turns.

It's built from just two gates: a Hadamard on each qubit, followed by controlled-phase rotations from the qubits below it, each half the size of the last, and swaps at the end to put the qubits back in order. That's about n²/2 gates, where the classical fast Fourier transform needs about n·2ⁿ steps.

The code

The whole script, ready to run

Install Qiskit and its simulator once, save the script as quantum_fourier_transform.py and run it with python quantum_fourier_transform.py. It runs on Qiskit's free simulator, so you don't need an account or a quantum computer.

Install onceTerminal

pip install qiskit qiskit-aer

quantum_fourier_transform.pyPython · Qiskit

from cmath import phase
from math import pi
from qiskit import QuantumCircuit, transpile
from qiskit.circuit.library import QFTGate
from qiskit.quantum_info import Operator, Statevector
from qiskit_aer import AerSimulator

def qft(n):
    qc = QuantumCircuit(n, name="QFT")
    for target in reversed(range(n)):
        qc.h(target)
        for control in reversed(range(target)):
            qc.cp(pi / 2 ** (target - control), control, target)
    for q in range(n // 2):
        qc.swap(q, n - q - 1)
    return qc

n = 3
print("same as Qiskit's QFT:", Operator(qft(n)).equiv(Operator(QFTGate(n))))

qc = QuantumCircuit(n)
qc.x([0, 2])                      # the number 5, binary 101
qc.compose(qft(n), inplace=True)
for k, amp in enumerate(Statevector(qc)):
    turns = round(phase(amp) / (2 * pi), 3) % 1   # the amplitude's phase, in turns
    print(f"|{k}>  probability {abs(amp) ** 2:.3f}  phase {turns:.3f} turns")

qc.compose(qft(n).inverse(), inplace=True)   # and back again
qc.measure_all()
sim = AerSimulator(seed_simulator=7)
print(sim.run(transpile(qc, sim), shots=1000).result().get_counts())

What it printedSeed 7 · 1,000 shots

same as Qiskit's QFT: True
|0>  probability 0.125  phase 0.000 turns
|1>  probability 0.125  phase 0.625 turns
|2>  probability 0.125  phase 0.250 turns
|3>  probability 0.125  phase 0.875 turns
|4>  probability 0.125  phase 0.500 turns
|5>  probability 0.125  phase 0.125 turns
|6>  probability 0.125  phase 0.750 turns
|7>  probability 0.125  phase 0.375 turns
{'101': 1000}
Run on Qiskit 2.6 and Qiskit Aer 0.18. The simulator's seed is fixed, so on the same versions you'll see the same numbers.

What happened

What the output shows

First, the hand-built circuit matches Qiskit's own QFTGate exactly. Then the number 5 goes in, and every one of the 8 states comes out equally likely, 0.125 each: on its own a measurement would tell you nothing. The information is all in the phases, which step by 5/8 of a turn from one state to the next.

Run the inverse QFT and the phases fold back into the number: all 1,000 shots read 101, which is 5.

That's how the QFT is used in practice. An algorithm writes an answer into phases, as Shor's does with the period of a function, and the inverse QFT turns those phases into bits you can measure.

Try next

Three things to try next

  • Put in a different number, say qc.x(1) for 2, and predict the phases before you run it.
  • Print qft(4).draw() and count the gates: 4 Hadamards, 6 controlled phases and 2 swaps.
  • Head to quantum phase estimation, which uses the inverse QFT to read an unknown phase.

Take it further · Project 08

Build Shor's Algorithm for real

Shor's algorithm uses the inverse QFT to find the period that breaks RSA. The project builds it end to end and factors 15 and 21 on the simulator, with tests.

Start my 7-day free trialSee the Shor's Algorithm project

Questions

Quantum Fourier transform in Qiskit

What is the quantum Fourier transform?

The quantum version of the discrete Fourier transform. It maps a basis state |x⟩ to an equal superposition of all states |k⟩ with phases of x·k/2ⁿ turns, using about n²/2 gates.

Why do the QFT's outputs all have the same probability?

For a single basis state going in, the QFT spreads it evenly over every state. What changes is the phase of each, which a measurement can't see until the inverse QFT turns it back into bits.

Does Qiskit have a built-in QFT?

Yes: QFTGate in qiskit.circuit.library. Building it yourself, as here, is the best way to see why it works, and the code checks yours against it.

Your first circuit is 10 minutes away

Stop watching quantum. Start shipping it.

Start my 7-day free trial

$0 today. 7 days free, then $7.99 a week or $190 a year. Cancel any time.

Detail C · A qubit's state