The quantum Fourier transform (QFT) is the discrete Fourier transform applied to the amplitudes of a quantum state, and it can be built from gates on qubits. Quantum phase estimation (QPE) uses it to read out the eigenphase of a unitary as a binary fraction. These two pieces are the engine of Shor’s algorithm, quantum counting, and the phase-estimation approach to chemistry, so it is worth being able to derive the circuit from scratch.

Phase estimation reads a hidden angle out as ordinary bits, one digit per counting qubit. Image: Erdabravest2001, CC BY-SA 4.0, via Wikimedia Commons.

Mental Model

  • The QFT does not speed up the classical FFT task, because you cannot read out all amplitudes. It is useful when the answer is a peak that a few measurements can find.
  • Phase kickback from controlled- gates writes the eigenphase into the relative phases of the counting register. The inverse QFT converts those phases into a bit string.
  • With counting qubits the grid of outcomes has spacing . A dyadic phase lands exactly on the grid, and anything else leaks into neighbouring outcomes.
  • More counting qubits mean more precision and more controlled- cost, and the cost of the largest power usually dominates.

1. Definition

On basis states the QFT is

As a matrix this is with , the unitary DFT matrix. It is the same transform as numpy.fft.ifft up to the factor (numpy uses the positive exponent for the inverse). A state with period 4 on 16 amplitudes maps to peaks at multiples of ; I checked this with numpy and the output is 0.25 at positions 0, 4, 8, 12 and zero elsewhere.

2. The Circuit

Write in binary, with the most significant bit. The output factorizes into a product state:

where is a binary fraction. Each factor is produced by a Hadamard followed by controlled phase rotations from the lower-order bits. The recipe for each qubit, going from the most significant down:

  1. Apply to the qubit. This gives phase from its own bit.
  2. For every less significant qubit , apply a controlled- with distance + 1, which adds the fractional bits one by one.
  3. After all qubits are done, reverse the qubit order with swaps.

The gate count for qubits is Hadamards, controlled phases and swaps, so in total. For comparison, the classical FFT on points takes operations. Rotations by tiny angles can be dropped with little error, which gives an approximate QFT with gates, and hardware implementations usually do this.

import numpy as np
from qiskit import QuantumCircuit
from qiskit.quantum_info import Operator
 
def qft(n):
    qc = QuantumCircuit(n)
    for j in reversed(range(n)):
        qc.h(j)
        for k in 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; N = 2 ** n; w = np.exp(2j * np.pi / N)
F = np.array([[w ** (j * k) for k in range(N)] for j in range(N)]) / np.sqrt(N)
print(np.allclose(Operator(qft(n)).data, F), dict(qft(n).count_ops()))

This prints True {'h': 3, 'cp': 3, 'swap': 1}: the circuit equals the DFT matrix exactly (in Qiskit’s little-endian convention) and has the predicted gate counts.

3. Phase Estimation

Given a unitary with , find . You need the ability to apply controlled- and a copy of the eigenstate (or something with overlap on it).

The circuit has counting qubits and a target register holding :

  1. Hadamard on every counting qubit.
  2. For , apply controlled- with counting qubit as control.
  3. Apply the inverse QFT to the counting register and measure it.

After step 2, phase kickback leaves the counting register in

If for an integer , this is exactly , so the inverse QFT returns and you read with no error. In general the probability of outcome is

The nearest grid point always has probability at least . To get accurate to bits with success probability at least , Nielsen and Chuang give counting qubits.

4. Worked Example: The T Gate

has eigenstate with eigenvalue , so . With counting qubits, is on the grid (). Controlled- is just a controlled phase gate with angle .

import numpy as np
from qiskit import QuantumCircuit
from qiskit.circuit.library import QFTGate
from qiskit.quantum_info import Statevector
 
def qpe(phi, t):
    qc = QuantumCircuit(t + 1)
    qc.x(t)                                       # eigenstate |1> of the phase gate
    qc.h(range(t))
    for j in range(t):
        qc.cp(2 * np.pi * phi * 2 ** j, j, t)     # controlled-U^(2^j)
    qc.append(QFTGate(t).inverse(), range(t))
    probs = Statevector(qc).probabilities_dict(qargs=list(range(t)))
    return sorted(probs.items(), key=lambda kv: -kv[1])[:3]
 
print(qpe(1 / 8, 3))
print(qpe(1 / 3, 3))

For the output is the string 001 with probability 1.0, i.e. and exactly.

5. A Non-Dyadic Phase

Take , which no finite binary fraction represents. With the grid is multiples of , and the true value sits between and . Running the code above gives

Outcome Estimate Probability
30.3750.6878
20.2500.1749
40.5000.0469
10.1250.0316

The remaining probability (about 0.06) is spread over the other four outcomes. The best outcome has probability 0.69, above the floor, because is fairly close to a grid point. Increasing shrinks the grid spacing but not the leakage: for the best outcome (11/32 = 0.344) has probability 0.6842, and for the best outcome (85/256 = 0.332) has probability 0.6839. So extra qubits improve the resolution of the answer while the probability of landing at the single closest outcome stays near 0.68 for this . To concentrate the probability on a window around the true value, you add a few extra counting qubits beyond the precision you want, and the formula in section 3 says how many.

6. Cost and Caveats

The controlled- gates are the expensive part. If is a Trotterized time evolution, costs times as much as , so the last counting qubit pays the price of the whole run. Total query cost scales as in the target precision , the Heisenberg limit, compared with samples when you estimate an expectation value by repeated measurement. That is the advantage of QPE over variational methods, and it comes with fault-tolerant depth. For modular exponentiation in Shor’s algorithm the powers can be computed classically, so the cost does not grow with there.