Quantum Phase Estimation
Estimate the eigenphase φ of a unitary U on one of its eigenstates — the core subroutine behind Shor's algorithm and energy estimation. It extracts a continuous quantity hidden in a phase into a binary number you can read directly.
The problem it solves
Any unitary U acting on one of its eigenstates |ψ⟩ only multiplies that
state by a phase factor of unit modulus:
U|ψ⟩ = e^(2πiφ) |ψ⟩, 0 ≤ φ < 1
Looking at the target state alone, this eigenphase φ appears as an overall phase and
cannot be read out by direct measurement; QPE uses controlled-U to transcribe it into a
relative phase on the counting register. Many important problems reduce to this: in Shor's algorithm the phase of a
modular-multiplication operator encodes a period, and in quantum chemistry the phase of the
time-evolution operator e^(−iHt) encodes an energy eigenvalue.
The core idea — kicking the phase into bits
The trick is called phase kickback. When you use a target register that sits in an
eigenstate to trigger a controlled U, the eigenphase does not stay on the target — it
"kicks back" onto the counting qubit doing the controlling: that control goes from
|0⟩ + |1⟩ to |0⟩ + e^(2πiφ)|1⟩.
If counting qubit k controls U^(2^k), it collects the phase
2^k·φ. Arranging all n counting qubits this way, the phases accumulated across
the superposition lay out exactly the first n binary digits of φ — and that is
a state in the Fourier basis. So a single inverse QFT collapses it back to an ordinary
binary number j with φ ≈ j / 2ⁿ.
The precision / success-probability tradeoff
The number of counting qubits n sets both the precision and the reliability:
- Precision.
ncounting qubits quantiseφto a resolution of1/2ⁿ. Each extra qubit doubles the precision by one binary digit. - The exact case. If
φcan be written exactly innbinary digits, the measurement returns the correct result with probability 1. - The general case. If
φcannot be represented exactly innbits, the measurement distribution forms a main peak at the integer closest to2ⁿφ, with smaller side lobes; one can show the probability of landing on the bestn-bit approximation is at least4/π²(≈ 0.405). - Boosting reliability. To reach success probability
1−εfor a target number of correct digits, add aboutlog₂(1/ε)extra counting qubits. Taking more shots and using the mode works equally well.
What it is — and isn't — good for
- It is a universal building block. Shor's algorithm, the HHL linear-systems solver, and energy estimation in quantum chemistry are all phase estimation underneath.
- It needs an eigenstate. The algorithm assumes the input is already an eigenstate
|ψ⟩ofU. Given a superposition of eigenstates, phase estimation randomly projects onto one of their eigenphases with the corresponding probability — sometimes exactly what you want (as in Shor), sometimes requiring you to prepare the eigenstate first. - It needs powers of controlled U. You must implement
U^(2^k)efficiently. For some operators this is cheap, for others very expensive — often dominating the whole circuit. - It is demanding on coherence. The circuit is deep and long, so textbook phase estimation generally needs fault-tolerant hardware; near-term devices use iterative or variational alternatives instead.
Implementation
import qalgora
@qalgora.kernel
def qpe(counting: int):
cq = qalgora.qvector(counting)
eig = qalgora.qubit()
x(eig) # eigenstate of a phase gate
h(cq)
# controlled-U^(2^j): here U = T (phase pi/4). MSB-first powers, since the
# QFT above treats cq[0] as the most-significant qubit.
for k in range(counting):
for _ in range(2 ** (counting - 1 - k)):
t.ctrl(cq[k], eig)
qalgora.adjoint(qft, cq) # inverse QFT
mz(cq)
counts = qalgora.sample(qpe, 4)
# most-probable bitstring / 2^counting -> phi; counting=4 should peak at "0010" (=2/16=1/8)
Seeing it in code
Read the kernel against the theory above. Here U = T, a gate with phase
π/4, so φ = 1/8; |1⟩ is its eigenstate, which is why
x(eig) prepares the target in that eigenstate. |0⟩ is also an eigenstate of
T, but with phase φ = 0; here x(eig) selects the non-trivial
eigenphase of |1⟩. h(cq) spreads the counting
register into superposition to receive the kicked-back phase. The double loop applies the controlled
powers of U: the inner loop repeats t.ctrl(cq[k], eig) exactly
2^(counting−1−k) times, which is controlled U^(2^…), in most-significant-bit-
first order to match the QFT's bit convention. Finally qalgora.adjoint(qft, cq) applies the
inverse QFT to collapse the phase-spread information into a binary number; dividing the measured result
by 2^counting gives the estimate of φ (here it should peak on the bitstring
for 1/8). If a backend displays bit strings with a different endianness, the peak string may
appear reversed; interpret the measurement in this page's MSB-first order when estimating φ.
With counting = 4, φ·2⁴ = 2 is exactly representable, so this example should
peak deterministically at "0010", which serves as a regression assertion for the QFT
bit-order convention (counting qubit cq[0] is the MSB) — if someone later changes the QFT's
swaps or loop direction, this assertion breaks immediately.
qft, the quantum-Fourier-transform kernel defined on the
Quantum Fourier Transform page. Include that qft definition in
the same file (or import it) before running this example.
量子相位估计
估计幺正算符 U 在某一本征态上的本征相位 φ —— Shor 算法与能量估计的核心子程序。它把一个隐藏在相位里的连续量,提取成你能直接读出的二进制数。
它解决的问题
任何幺正算符 U 作用在它的某个本征态 |ψ⟩ 上,只会给这个态乘上一个模为 1 的相位因子:
U|ψ⟩ = e^(2πiφ) |ψ⟩, 0 ≤ φ < 1
如果只看目标态本身,这个本征相位 φ 表现为整体相位,不能由直接测量读出;QPE 通过受控 U 把它转写为计数寄存器上的相对相位。许多重大问题都能归结于此:Shor 算法中模乘算子的相位编码了周期,量子化学里时间演化算符 e^(−iHt) 的相位编码了能量本征值。
核心思想 把相位踢进比特
诀窍称为相位回踢。当你用一个处于本征态的目标寄存器去触发受控的 U 时,那个本征相位并不会留在目标上,而是"回踢"到充当控制位的计数比特上:控制比特从 |0⟩ + |1⟩ 变成 |0⟩ + e^(2πiφ)|1⟩。
若让第 k 个计数比特去控制 U^(2^k),它收获的相位就是 2^k·φ。把全部 n 个计数比特如此安排,叠加态各分量上累积的相位恰好把 φ 的前 n 个二进制位排布了出来——这正是一个傅里叶基里的态。于是只需施加逆 QFT,就能把它坍缩回一个普通二进制数 j,使得 φ ≈ j / 2ⁿ。
精度与成功概率的权衡
计数比特的数目 n 同时决定精度与可靠度:
- 精度。
n个计数比特把φ量化到1/2ⁿ的分辨率。每多一个比特,精度翻倍一位二进制。 - 恰好可表示的情形。若
φ本身能用n位二进制精确写出,测量将以概率 1 给出正确结果。 - 一般情形。若
φ不能用n位二进制精确表示,测量分布会在最接近2ⁿφ的整数附近形成主峰,并带有较小旁瓣;可以证明,落在最佳n位逼近上的概率至少为4/π²(约 0.405)。 - 提升可靠度。要让"误差不超过指定位数"的成功概率达到
1−ε,需额外增加约log₂(1/ε)个计数比特。多采样并取众数同样有效。
它擅长什么 又不擅长什么
- 它是一块通用基石。Shor 算法、HHL 线性方程组求解、量子化学的能量估计,骨架都是相位估计。
- 它需要一个本征态。算法假定输入已是
U的某个本征态|ψ⟩。若输入是若干本征态的叠加,相位估计会按各分量的概率随机投影到其中之一的本征相位——这有时正合所需(如 Shor),有时则需事先制备好本征态。 - 它需要受控 U 的幂次。必须高效实现
U^(2^k)。对部分算符这很廉价,对另一些则代价高昂,往往成为整条线路的主导开销。 - 它对相干性要求苛刻。线路又深又长,因此教科书式的相位估计通常需要容错硬件;近期设备多改用迭代式或变分式的替代方案。
实现
import qalgora
@qalgora.kernel
def qpe(counting: int):
cq = qalgora.qvector(counting)
eig = qalgora.qubit()
x(eig) # eigenstate of a phase gate
h(cq)
# controlled-U^(2^j): here U = T (phase pi/4). MSB-first powers, since the
# QFT above treats cq[0] as the most-significant qubit.
for k in range(counting):
for _ in range(2 ** (counting - 1 - k)):
t.ctrl(cq[k], eig)
qalgora.adjoint(qft, cq) # inverse QFT
mz(cq)
counts = qalgora.sample(qpe, 4)
# most-probable bitstring / 2^counting -> phi; counting=4 应峰值于 "0010" (=2/16=1/8)
对照代码理解
请对照上文原理来读这段内核。这里取 U = T 门,其相位为 π/4,故 φ = 1/8;|1⟩ 是它的本征态,所以 x(eig) 把目标比特制备成本征态。|0⟩ 也是 T 门的本征态,但对应相位 φ=0;这里用 x(eig) 是为了选择 |1⟩ 这个非平凡本征相位。h(cq) 在计数寄存器上铺开叠加态以接收回踢相位。双重循环施加受控 U 的幂次:内层把 t.ctrl(cq[k], eig) 重复 2^(counting−1−k) 次,等效于受控 U^(2^…),并采用最高位在前(MSB-first)的排布以配合 QFT 的比特约定。最后 qalgora.adjoint(qft, cq) 施加逆 QFT,把铺在相位里的信息坍缩成一个二进制数;测量结果除以 2^counting 即得 φ 的估计(此处应峰值于对应 1/8 的比特串)。若后端采用不同的比特串显示端序,峰值字符串可能按反序显示;估计 φ 时应按照本文约定的 MSB-first 顺序解释测量结果。
当 counting = 4 时,φ·2⁴ = 2 可精确表示,此例应确定性峰于 "0010",可作为 QFT 比特序约定(计数比特 cq[0] 为 MSB)的回归断言——日后若有人改了 QFT 的 swap 或循环方向,此断言会立即暴露。