qalgora-Q Docs Hub量子文档 ✦ Ask AI✦ 问问文档

Quantum Fourier Transform

The QFT maps the computational basis to the Fourier basis and underlies phase estimation, Shor's algorithm, and more. It re-expresses the amplitude distribution of the computational basis in the Fourier basis, making periodic and phase structure easier to extract through interference.

The problem it solves

The classical discrete Fourier transform turns a length-N sequence into its frequency content; it is everywhere, but it is also expensive — O(N²) operations done directly, O(N log N) with the fast Fourier transform (FFT). The QFT is its quantum counterpart: it transforms the quantum state of N = 2ⁿ numbers encoded as amplitudes into the Fourier basis.

The point is not that the transform is somehow "fast", but how it folds global information into phases. The QFT does not hand you the transformed values one by one (no quantum measurement can do that), but it arranges periodicity and phase information into a form that later interference can exploit — which is exactly what phase estimation and Shor's algorithm rely on.

What it does — from amplitudes to phases

On a basis state |x⟩, the QFT sends it to a uniform superposition in which every basis state carries a phase proportional to x:

|x⟩ → (1/√N) Σ_y e^(2πi·xy/N) |y⟩

In other words, the input label x is "written into" the rate at which the relative phase rotates across the components of the output superposition. A slowly varying phase corresponds to a low frequency; a fast-winding phase to a high frequency. The QFT thus translates "which number is this basis state" into "how fast do these phases wind".

Why the circuit needs only O(n²) gates

The QFT circuit has an elegant recursive structure. For each qubit, apply one Hadamard — which alone performs the single-qubit Fourier transform on that qubit — followed by a ladder of controlled phase rotations that fold in contributions from the lower-order qubits at ever-finer angles. Qubit i receives a rotation of π / 2^(j−i) from qubit j.

The controlled-phase gates number n(n−1)/2; together with n Hadamards and up to ⌊n/2⌋ swaps, the exact QFT therefore uses O(n²) gates. By comparison, a classical FFT on N = 2ⁿ amplitudes needs O(N log N) = O(2ⁿ·n) operations — the QFT manipulates an exponentially large amplitude space with a polynomial number of gates in the qubit count. It is worth stressing that this does not speed up the Fourier transform of classical data (you cannot cheaply load data into amplitudes, nor read all the results out); its power appears only as a subroutine inside a larger algorithm. More precisely, the QFT acts on an already-prepared quantum state Σx ax|x⟩ and outputs a quantum state Σy ãy|y⟩; unless the surrounding algorithm only needs some global property, you cannot read out all the ãy at once by measurement — so the QFT's power is as a subroutine inside phase estimation and Shor's algorithm, not a drop-in replacement for the classical FFT.

Implementation

import qalgora, numpy as np

# Convention: r1(theta) = diag(1, exp(i*theta)), and q[0] is the most significant qubit.
# Under this convention positive angles give the forward (+2 pi i x y / N) QFT; a backend
# with the opposite phase sign would realise the inverse instead.
@qalgora.kernel
def qft(q: qalgora.qview):
    n = q.size()
    for i in range(n):
        h(q[i])
        for j in range(i + 1, n):
            angle = np.pi / (2 ** (j - i))
            r1.ctrl(angle, q[j], q[i])
    # reverse qubit order
    for i in range(n // 2):
        swap(q[i], q[n - 1 - i])

Seeing it in code

Read the kernel against the theory above. The outer loop walks over each qubit: h(q[i]) is the single-qubit Fourier transform on that qubit; inside the inner loop, r1.ctrl(angle, q[j], q[i]) applies a controlled phase rotation whose angle angle = π / 2^(j−i) is exactly the fine contribution coming from qubit j. The final swap loop reverses the qubit order — this bit-by-bit construction produces its output in reversed bit order, so the swaps restore the standard convention.

The output bit order depends on the implementation convention; this page restores the standard order with the final swaps. On a backend that omits the swaps or uses a different measurement endianness, the output bit string may need to be read in reverse.

Inverse QFT

The inverse simply runs the adjoint — qalgora-Q generates it for you:

@qalgora.kernel
def iqft(q: qalgora.qview):
    qalgora.adjoint(qft, q)
Why the inverse QFT is everywhere
In phase estimation and Shor's algorithm, it is usually the inverse QFT that does the work: it collapses frequency information already spread across phases back into a binary number you can read directly. The forward transform writes phases; the inverse reads them.

量子傅里叶变换

QFT 将计算基映射到傅里叶基,是相位估计、Shor 算法等众多算法的核心子程序。它把计算基中的振幅分布重新表达在傅里叶基中,使周期性和相位结构更容易通过干涉被提取。

它解决的问题

经典的离散傅里叶变换把一个长度为 N 的序列转换到频域;它无处不在,却也很昂贵——直接计算需 O(N²) 次运算,快速傅里叶变换(FFT)将其降至 O(N log N)。QFT 是它的量子对应物:它把振幅编码的 N = 2ⁿ 个数所构成的量子态,变换到傅里叶基。

关键之处不在于变换本身有多"快",而在于它如何把全局信息折叠进相位之中。QFT 不会把变换结果逐项交给你(量子测量做不到这点),但它能把周期性相位信息排布成可被后续干涉利用的形式——这正是相位估计与 Shor 算法所倚赖的。

它在做什么 从振幅到相位

对一个基态 |x⟩,QFT 把它送往一个均匀叠加态,其中每个基态都带上一个与 x 成正比的相位:

|x⟩ → (1/√N) Σ_y e^(2πi·xy/N) |y⟩

换言之,输入编号 x 被"写进"了输出叠加态各分量之间的相对相位旋转速率里。一个变化缓慢的相位对应低频,快速缠绕的相位对应高频。QFT 因此把"这个基态是哪个数"翻译成了"这些相位以多快的速度绕圈"。

线路为何只需 O(n²) 门

QFT 的线路有一个优雅的递归结构。对每个量子比特,先施加一个 Hadamard 门——它单独完成该比特上的单比特傅里叶变换;随后施加一串受控相位旋转,把来自更低位比特的贡献以越来越精细的角度叠加进来。第 i 个比特从第 j 个比特收到的旋转角为 π / 2^(j−i)

受控相位门共有 n(n−1)/2 个;再加上 n 个 Hadamard 门和最多 ⌊n/2⌋ 个 swap,精确 QFT 的门数为 O(n²)。相比之下,经典 FFT 在 N = 2ⁿ 个振幅上需要 O(N log N) = O(2ⁿ·n) 次运算——QFT 用关于比特数的多项式门数,操控了一个指数大的振幅空间。需要强调的是,这并不能加速对经典数据的傅里叶变换(你无法廉价地把数据装进振幅,也无法廉价地读出全部结果),其威力只有作为大算法内部的子程序时才显现。更确切地说,QFT 作用的是一个已制备好的量子态 Σx ax|x⟩,输出也是量子态 Σy ãy|y⟩。除非后续算法只需要某种全局性质,否则无法通过测量一次性读出全部 ãy。因此 QFT 的威力主要体现在作为相位估计、Shor 算法等大算法内部的子程序,而非直接替代经典 FFT。

实现

import qalgora, numpy as np

# 约定:r1(theta) = diag(1, exp(i*theta)),且 q[0] 为最高位比特。
# 在此约定下,正角度实现正向 (+2πi·xy/N) QFT;若后端相位符号相反,则为逆变换。
@qalgora.kernel
def qft(q: qalgora.qview):
    n = q.size()
    for i in range(n):
        h(q[i])
        for j in range(i + 1, n):
            angle = np.pi / (2 ** (j - i))
            r1.ctrl(angle, q[j], q[i])
    # reverse qubit order
    for i in range(n // 2):
        swap(q[i], q[n - 1 - i])

对照代码理解

请把这段内核对照上文原理来读。外层循环逐一处理每个比特:h(q[i]) 是该比特上的单比特傅里叶变换;内层循环里的 r1.ctrl(angle, q[j], q[i]) 施加受控相位旋转,其角度 angle = π / 2^(j−i) 恰是来自第 j 位比特的精细贡献。末尾的 swap 循环翻转比特顺序——因为这种逐比特构造产生的输出位序是颠倒的,需要反转才能恢复标准约定。

QFT 线路的输出位序与实现约定有关;本文通过末尾 swap 恢复标准位序。若在其他后端中省略 swap 或采用不同测量端序,输出比特串可能需要反序解释。

逆 QFT

逆变换只需运行伴随操作 —— qalgora-Q 会自动为您生成:

@qalgora.kernel
def iqft(q: qalgora.qview):
    qalgora.adjoint(qft, q)
为何处处需要逆 QFT
相位估计Shor 算法中,真正用到的往往是 QFT:把已被铺陈在相位里的频率信息,重新坍缩回一个可直接读出的二进制数。正变换写入相位,逆变换读出相位。