Building the QFT by Hand
Build the QFT from H, controlled phases and SWAPs; check it equals QFTGate and the DFT matrix; apply it to |5⟩.
Definition of the QFT: $\mathrm{QFT}|x\rangle=\frac{1}{\sqrt N}\sum_y e^{2\pi ixy/N}|y\rangle$. The standard construction: for each qubit from highest to lowest, apply H and then $CP(\pi/2^{j-k})$ from each lower qubit, and finally reverse the qubit order with SWAPs.
We verify in two ways: Operator.equiv with QFTGate (the library's modern class), and comparing the matrix with the DFT matrix built with numpy. Then we apply the QFT to $|5\rangle$: all probabilities are $1/8$, and the phase of the amplitude of $|1\rangle$ is $2\pi\cdot5/8=5\pi/4\equiv-3\pi/4$, so $-0.75\pi$ is printed.
Requirements: pip install qiskit numpy. The code runs on your machine or in Colab, not inside WordPress.
Code
import numpy as np
from qiskit import QuantumCircuit
from qiskit.circuit.library import QFTGate
from qiskit.quantum_info import Operator, Statevector
def qft(n):
qc = QuantumCircuit(n, name="QFT")
for j in reversed(range(n)):
qc.h(j)
for k in reversed(range(j)):
qc.cp(np.pi / 2 ** (j - k), k, j)
for i in range(n // 2):
qc.swap(i, n - 1 - i)
return qc
n = 3
print(Operator(qft(n)).equiv(Operator(QFTGate(n))))
N = 2 ** n
dft = np.array([[np.exp(2j * np.pi * x * y / N) for x in range(N)] for y in range(N)]) / np.sqrt(N)
print(np.allclose(Operator(qft(n)).data, dft))
sv = Statevector.from_int(5, N).evolve(qft(n))
print(np.round(np.abs(sv.data) ** 2, 3))
print(np.round(np.angle(sv.data[1]) / np.pi, 3), "* pi")
Line-by-line code explanation
| Line | Explanation |
|---|---|
| 1-4 | Imports: numpy, the circuit, QFTGate, Operator and Statevector. |
| 7 | A function that builds the QFT on n qubits. |
| 8 | An empty circuit named QFT. |
| 9-10 | For each qubit j from the top: a Hadamard. |
| 11-12 | Then a controlled phase by π/2^(j−k) from each lower qubit k. |
| 13-14 | Reverse the qubit order with swaps. |
| 15 | Return the circuit. |
| 18-19 | Compare the operator with QFTGate (up to global phase). |
| 21-22 | Build the DFT matrix explicitly. |
| 23 | Compare the elements directly. |
| 25 | Apply the QFT to the basis state |5⟩. |
| 26 | Probabilities: all 1/8. |
| 27 | Phase of the amplitude of |001⟩ in units of π. |
Expected output (from an actual run)
True True [0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125] -0.75 * pi
The output is deterministic.
Try it yourself
Related topics
References
- Don Coppersmith (1994). An approximate Fourier transform useful in quantum factoring. IBM Research Report RC19642 (arXiv:quant-ph/0201067, 2002) doi:10.48550/arXiv.quant-ph/0201067
- Michael A. Nielsen; Isaac L. Chuang (2010). Quantum Computation and Quantum Information (10th Anniversary Edition). Cambridge University Press doi:10.1017/CBO9780511976667The standard reference for quantum computing; the Crossref record gives 2012 for the Cambridge Core online edition.
- IBM Quantum (2025). Qiskit SDK API documentation. IBM Quantum Documentation quantum.cloud.ibm.com
