Lesson 7 of 15

Quantum Fourier Transform

The Quantum Fourier Transform

The Quantum Fourier Transform (QFT) is the quantum analogue of the Discrete Fourier Transform. It is a unitary operation that maps basis states as:

QFTj=1Nk=0N1e2πijk/Nk\text{QFT}|j\rangle = \frac{1}{\sqrt{N}}\sum_{k=0}^{N-1} e^{2\pi i jk/N}|k\rangle

where N=2nN = 2^n for an nn-qubit system.

For a general superposition ψ=jcjj|\psi\rangle = \sum_j c_j|j\rangle, linearity gives the kk-th output amplitude:

QFTψk=1Nj=0N1cje2πijk/N\text{QFT}|\psi\rangle_k = \frac{1}{\sqrt{N}}\sum_{j=0}^{N-1} c_j\, e^{2\pi ijk/N}

Connection to the Hadamard Gate

For a single qubit (N=2N=2), the QFT is exactly the Hadamard gate HH:

H=12(1111)H = \frac{1}{\sqrt{2}}\begin{pmatrix}1&1\\1&-1\end{pmatrix}

Applying HH to 0=[1,0]|0\rangle = [1, 0] gives [1/2,1/2][1/\sqrt{2},\, 1/\sqrt{2}] — equal superposition.

Unitarity

The QFT is unitary: it preserves the norm of the state vector. If jcj2=1\sum_j |c_j|^2 = 1, then kQFT(ψ)k2=1\sum_k |\text{QFT}(\psi)_k|^2 = 1.

import cmath, math

def qft(state):
    N = len(state)
    result = []
    for k in range(N):
        amp = sum(state[j] * cmath.exp(2j * math.pi * j * k / N)
                  for j in range(N))
        result.append(amp / math.sqrt(N))
    return result

# QFT on |0⟩ is the Hadamard: each amplitude = 1/√2
result = qft([1.0, 0.0])
print(round(abs(result[0]), 4))  # 0.7071
print(round(abs(result[1]), 4))  # 0.7071

Applications

The QFT is a key subroutine in:

  • Shor's algorithm — integer factorization in polynomial time
  • Quantum phase estimation — extracting eigenphases of unitary operators
  • Hidden subgroup problems — a broad class of quantum speedups

Your Task

Implement qft(state) that takes an NN-element amplitude list and returns the QFT output as a list of complex amplitudes. Use the formula:

result[k]=1Nj=0N1state[j]e2πijk/N\text{result}[k] = \frac{1}{\sqrt{N}}\sum_{j=0}^{N-1} \text{state}[j]\cdot e^{2\pi ijk/N}

Python runtime loading...
Loading...
Click "Run" to execute your code.