The Hadamard Test
Estimate the real (or imaginary) part of ⟨ψ|U|ψ⟩ using one ancilla qubit — a building block for Krylov methods and overlap estimation. It measures a complex number you cannot observe directly using simple interference on a single qubit.
The problem it solves
Many quantum algorithms need one number: the overlap ⟨ψ|U|ψ⟩ of a state
|ψ⟩ with itself after a unitary U has acted on it. This is a complex number
that captures how much of |ψ⟩ the operator U maps back onto |ψ⟩.
It is generally a complex number that cannot be read out directly by one ordinary computational-basis
measurement; although its real and imaginary parts can each be written formally as a Hermitian-operator
expectation, the Hadamard test is the standard way to estimate them using controlled-U.
Re⟨U⟩ = ⟨(U+U†)/2⟩, Im⟨U⟩ = ⟨(U−U†)/(2i)⟩
The Hadamard test sidesteps this with a single ancilla qubit. It converts the hidden complex overlap into a statistical bias in a series of simple measurements of that ancilla, letting you estimate its real and imaginary parts separately.
The core idea — interference on one qubit
The ancilla is an interferometer. The first Hadamard puts it in an equal superposition of
|0⟩ and |1⟩. A controlled U then applies U to
|ψ⟩ only on the branch where the ancilla is |1⟩. This creates a relative phase
between the two ancilla branches that carries the information in ⟨ψ|U|ψ⟩. The second
Hadamard makes the two branches interfere, translating that phase difference into a measurable imbalance
between the probabilities of |0⟩ and |1⟩.
Why P(0) minus P(1) gives the real part
After the interference sequence, the probabilities of measuring the ancilla as 0 or 1 are
P(0) = ½(1 + Re⟨ψ|U|ψ⟩), P(1) = ½(1 − Re⟨ψ|U|ψ⟩)
Subtracting the two, the constant and the ½ factor cancel, leaving cleanly
P(0) − P(1) = Re⟨ψ|U|ψ⟩
That is why assigning +1 to outcome 0 and −1 to outcome 1 and averaging yields the real part. To get
the imaginary part, insert an S† gate on the ancilla before the controlled
U: it rotates the relative phase between the branches by 90°, moving what was the imaginary
part into the real-part slot, so the same readout now returns Im⟨ψ|U|ψ⟩.
Under this page's convention — controlled-U acting on the |1⟩ branch of the
ancilla, read out as P(0) − P(1) — inserting S† gives
+Im⟨ψ|U|ψ⟩; inserting S gives the opposite sign.
What it is — and isn't — good for
- It needs just one ancilla. However many qubits
|ψ⟩occupies, the overhead is a single ancilla plus one controlledU— a very simple, implementable structure. - It needs a controlled U. You must be able to apply
Uin controlled form, which is not cheap for every operator; this is often the main source of circuit depth. - It is statistical. You get an estimate: driving the error down to
εtakes aboutO(1/ε²)samples, the inherent scaling of sampled measurement. - It is a component of larger methods. Overlaps of the form
⟨ψ_i|H|ψ_j⟩are exactly what Krylov-subspace methods and quantum eigensolvers need; the Hadamard test is how you measure them on hardware.
Circuit
import qalgora
@qalgora.kernel
def hadamard_test(imag: bool):
anc = qalgora.qubit()
q = qalgora.qvector(2)
# prepare |psi> on q ...
h(anc)
if imag:
s.adj(anc) # imaginary part variant
# controlled-U on q, conditioned on anc ...
h(anc)
mz(anc)
counts = qalgora.sample(hadamard_test, False)
# Re<psi|U|psi> = P(0) - P(1)
Seeing it in code
Read the kernel against the theory above. anc is the ancilla acting as the
interferometer, and q carries the state |ψ⟩ (its preparation is left as a
commented placeholder). The first h(anc) puts the ancilla in an equal superposition. When
imag is true, s.adj(anc) applies the S† gate that switches the
readout from the real to the imaginary part. The commented controlled U applies
U to |ψ⟩ only on the |1⟩ branch of the ancilla, writing in the
relative phase. The second h(anc) completes the interference and mz(anc)
measures the ancilla. As the comment shows, processing the samples as P(0) − P(1) yields
Re⟨ψ|U|ψ⟩.
This code is a circuit skeleton; only after you fill in the preparation of |ψ⟩ and the
controlled-U does the sampled result correspond to a concrete
⟨ψ|U|ψ⟩.
Hadamard 测试
利用一个辅助量子比特估计 ⟨ψ|U|ψ⟩ 的实部(或虚部)——Krylov 方法与交叠估计的基础构件。它用一个比特上的简单干涉,量出一个无法直接观测的复数。
它解决的问题
许多量子算法都需要一个数:态 |ψ⟩ 经幺正算符 U 作用后,与自身的交叠 ⟨ψ|U|ψ⟩。这是一个复数,刻画了 U 把 |ψ⟩ 又映回多少 |ψ⟩。它通常是一个复数,不能通过一次普通计算基测量直接读出;虽然其实部和虚部可形式上写成厄米算符期望,Hadamard 测试给出了用受控 U 估计它们的标准方法。
Re⟨U⟩ = ⟨(U+U†)/2⟩, Im⟨U⟩ = ⟨(U−U†)/(2i)⟩
Hadamard 测试用一个辅助比特巧妙绕开了这一点。它把这个隐藏的复数交叠,转化成对那个辅助比特一系列简单测量结果的统计偏置,让你能分别估计其实部与虚部。
核心思想 用一个比特做干涉
辅助比特就是一台干涉仪。第一个 Hadamard 把它置于 |0⟩ 与 |1⟩ 的均匀叠加。随后施加受控 U:仅当辅助比特为 |1⟩ 时才把 U 作用到 |ψ⟩ 上。这就在辅助比特的两条分支间制造了一个携带 ⟨ψ|U|ψ⟩ 信息的相对相位。第二个 Hadamard 让两条分支重新干涉,把那个相位差翻译成 |0⟩ 与 |1⟩ 之间可测的概率失衡。
为何 P0 减 P1 恰好给出实部
算完这套干涉序列,测得辅助比特为 0 与为 1 的概率分别是
P(0) = ½(1 + Re⟨ψ|U|ψ⟩), P(1) = ½(1 − Re⟨ψ|U|ψ⟩)
两者相减,常数项与 ½ 因子约去,干净地剩下
P(0) − P(1) = Re⟨ψ|U|ψ⟩
这就是为何把测量结果按 +1(对应 0)与 −1(对应 1)赋值再取平均,得到的正是实部。要取虚部,只需在受控 U 之前给辅助比特插入一个 S† 门:它把分支间的相对相位旋转 90°,使原本属于虚部的信息转到实部位置,于是同一套读出此时给出 Im⟨ψ|U|ψ⟩。
在本文约定下,即受控 U 作用于辅助比特的 |1⟩ 分支,并按 P(0) − P(1) 读出,插入 S† 得到 +Im⟨ψ|U|ψ⟩;若插入 S,则得到相反符号。
它擅长什么 又不擅长什么
- 它只需一个辅助比特。无论
|ψ⟩占多少比特,开销都只是一个辅助比特加上一次受控U,结构极简且利于实现。 - 它需要受控 U。必须能以受控形式施加
U,对某些算符这并不便宜;这往往是线路深度的主要来源。 - 它是统计性的。得到的只是一个估计:要把误差降到
ε,所需采样数约为O(1/ε²),这是采样测量的固有标度。 - 它是更大方法的构件。形如
⟨ψ_i|H|ψ_j⟩的交叠正是 Krylov 子空间法与量子本征求解器所需的核心量;Hadamard 测试正是在硬件上测出它们的手段。
线路
import qalgora
@qalgora.kernel
def hadamard_test(imag: bool):
anc = qalgora.qubit()
q = qalgora.qvector(2)
# prepare |psi> on q ...
h(anc)
if imag:
s.adj(anc) # imaginary part variant
# controlled-U on q, conditioned on anc ...
h(anc)
mz(anc)
counts = qalgora.sample(hadamard_test, False)
# Re<psi|U|psi> = P(0) - P(1)
对照代码理解
请对照上文原理来读这段内核。anc 是充当干涉仪的辅助比特,q 承载态 |ψ⟩(其制备被注释占位)。第一个 h(anc) 把辅助比特置于均匀叠加。当 imag 为真时,s.adj(anc) 施加 S† 门,将读出从实部切换到虚部。被注释占位的受控 U 仅在辅助比特为 |1⟩ 的分支上把 U 作用于 |ψ⟩,从而写入相对相位。第二个 h(anc) 完成干涉,mz(anc) 测量辅助比特。如注释所示,按 P(0) − P(1) 处理采样结果,即得 Re⟨ψ|U|ψ⟩。
该代码是线路骨架;需要补全 |ψ⟩ 的制备和受控 U 后,采样结果才对应具体的 ⟨ψ|U|ψ⟩。