QuantumCore

Lesson 31 · 100 min · advanced

The HHL Algorithm for Ax = b

Loading progress…

The Harrow–Hassidim–Lloyd (HHL) algorithm prepares a quantum state ∣x⟩∝A−1∣b⟩\lvert x\rangle\propto A^{-1}\lvert b\rangle for a Hermitian, sparse, well-conditioned matrix AA. It writes the eigenvalues λj\lambda_j of AA into a clock register by quantum phase estimation of eiAte^{iAt}, rotates an ancilla by an angle that encodes C/λjC/\lambda_j, uncomputes the clock and post-selects the ancilla. This lesson derives each stage, the success probability and the O~(log⁡(N)s2κ2/ε)\tilde O(\log(N)s^2\kappa^2/\varepsilon) cost, lists the caveats that limit the claimed exponential speedup, and runs the full algorithm on a 2×22\times2 system whose output (3,1)/10(3,1)/\sqrt{10} is checked against the classical solution.

Prerequisites: Quantum Phase Estimation, Spectral Theorem & Functions of Operators

Objectives and key results
  • Express A−1∣b⟩A^{-1}\lvert b\rangle in the eigenbasis of a Hermitian AA and embed a non-Hermitian AA in the Hermitian matrix (0AA†0)\begin{pmatrix}0&A\\A^\dagger&0\end{pmatrix}.

  • Show that phase estimation of U=eiAtU=e^{iAt} maps ∣0⟩⊗tc∣b⟩\lvert0\rangle^{\otimes t_c}\lvert b\rangle to ∑jβj∣kj⟩∣uj⟩\sum_j\beta_j\lvert k_j\rangle\lvert u_j\rangle with kj=λjt 2tc/(2π)k_j=\lambda_jt\,2^{t_c}/(2\pi).

  • Implement the eigenvalue inversion ∣k⟩∣0⟩↦∣k⟩(1−C2/k2∣0⟩+Ck∣1⟩)\lvert k\rangle\lvert0\rangle\mapsto\lvert k\rangle\big(\sqrt{1-C^2/k^2}\lvert0\rangle+\tfrac Ck\lvert1\rangle\big) with RY(2arcsin⁡(C/k))R_Y(2\arcsin(C/k)) and derive the success probability ∑j∣βj∣2C2/kj2\sum_j\lvert\beta_j\rvert^2C^2/k_j^2.

  • State the complexity O~(log⁡(N)s2κ2/ε)\tilde O(\log(N)s^2\kappa^2/\varepsilon) and the input/output caveats, including dequantisation for low-rank matrices.

  • Compute eiAte^{iAt}, (eiAt)2=−X(e^{iAt})^2=-X, the clock values, the angles and the output state of a 2×22\times2 instance by hand.

formula · Solution in the eigenbasis
A=∑jλj∣uj⟩⟨uj∣,∣b⟩=∑jβj∣uj⟩ ⇒ A−1∣b⟩=∑jβjλj∣uj⟩A=\sum_j\lambda_j\lvert u_j\rangle\langle u_j\rvert,\quad\lvert b\rangle=\sum_j\beta_j\lvert u_j\rangle\ \Rightarrow\ A^{-1}\lvert b\rangle=\sum_j\frac{\beta_j}{\lambda_j}\lvert u_j\rangle
formula · Clock value
kj=λjt 2tc2πk_j=\frac{\lambda_j t\,2^{t_c}}{2\pi}
formula · Inversion angle
θj=2arcsin⁡Ckj,C≤min⁡jkj\theta_j=2\arcsin\frac{C}{k_j},\qquad C\le\min_jk_j
formula · Success probability
psucc=∑j∣βj∣2C2kj2 ≥ 1κ2  (C=kmin⁡)p_{\text{succ}}=\sum_j\lvert\beta_j\rvert^2\frac{C^2}{k_j^2}\ \ge\ \frac{1}{\kappa^2}\ \ (C=k_{\min})
formula · Complexity
O~ ⁣(log⁡(N) s2κ2ε)\tilde O\!\left(\frac{\log(N)\,s^2\kappa^2}{\varepsilon}\right)

The problem in the eigenbasis#

Definition

Quantum linear-systems problem

Given an N×NN\times N Hermitian matrix AA with spectral decomposition A=∑j=1Nλj∣uj⟩⟨uj∣A=\sum_{j=1}^{N}\lambda_j\lvert u_j\rangle\langle u_j\rvert (orthonormal eigenvectors ∣uj⟩\lvert u_j\rangle, real non-zero eigenvalues λj\lambda_j) and a normalised state ∣b⟩=∑jβj∣uj⟩\lvert b\rangle=\sum_j\beta_j\lvert u_j\rangle with βj=⟨uj∣b⟩\beta_j=\langle u_j\vert b\rangle, prepare the normalised state ∣x⟩=A−1∣b⟩/∥A−1∣b⟩∥\lvert x\rangle=A^{-1}\lvert b\rangle/\lVert A^{-1}\lvert b\rangle\rVert. The condition number is κ=λmax⁡/λmin⁡\kappa=\lambda_{\max}/\lambda_{\min} (ratio of the largest to the smallest ∣λj∣\lvert\lambda_j\rvert); after rescaling one assumes 1/κ≤∣λj∣≤11/\kappa\le\lvert\lambda_j\rvert\le1.

Derivation

A−1∣b⟩A^{-1}\lvert b\rangle in the eigenbasis

  1. Step 1:
    A−1=∑jλj−1∣uj⟩⟨uj∣A^{-1}=\sum_j\lambda_j^{-1}\lvert u_j\rangle\langle u_j\rvert
    Spectral theorem: f(A)=∑jf(λj)∣uj⟩⟨uj∣f(A)=\sum_jf(\lambda_j)\lvert u_j\rangle\langle u_j\rvert for f(λ)=1/λf(\lambda)=1/\lambda, defined because no λj\lambda_j vanishes. Check: A⋅A−1=∑j,lλjλl−1∣uj⟩⟨uj∣ul⟩⟨ul∣=∑j∣uj⟩⟨uj∣=IA\cdot A^{-1}=\sum_{j,l}\lambda_j\lambda_l^{-1}\lvert u_j\rangle\langle u_j\vert u_l\rangle\langle u_l\rvert=\sum_j\lvert u_j\rangle\langle u_j\rvert=I.
  2. Step 2:
    A−1∣b⟩=∑jλj−1∣uj⟩⟨uj∣b⟩=∑jβjλj∣uj⟩A^{-1}\lvert b\rangle=\sum_j\lambda_j^{-1}\lvert u_j\rangle\langle u_j\vert b\rangle=\sum_j\frac{\beta_j}{\lambda_j}\lvert u_j\rangle
    Apply the operator to ∣b⟩\lvert b\rangle and use ⟨uj∣b⟩=βj\langle u_j\vert b\rangle=\beta_j.
  3. Step 3:
    ∣x⟩=∑jβjλj−1∣uj⟩∑j∣βj∣2λj−2\lvert x\rangle=\frac{\sum_j\beta_j\lambda_j^{-1}\lvert u_j\rangle}{\sqrt{\sum_j\lvert\beta_j\rvert^2\lambda_j^{-2}}}
    Normalise with ∥∑jcj∣uj⟩∥2=∑j∣cj∣2\lVert\sum_jc_j\lvert u_j\rangle\rVert^2=\sum_j\lvert c_j\rvert^2 (orthonormality). HHL therefore only has to multiply each eigencomponent by a number proportional to λj−1\lambda_j^{-1}.

Result

∣x⟩∝∑jβjλj−1∣uj⟩\boxed{\lvert x\rangle\propto\sum_j\beta_j\lambda_j^{-1}\lvert u_j\rangle}
Derivation

Embedding a non-Hermitian AA

  1. Step 1:
    A~=(0AA†0),A~†=(0†(A†)†A†0†)=(0AA†0)=A~\tilde A=\begin{pmatrix}0&A\\A^\dagger&0\end{pmatrix},\qquad\tilde A^\dagger=\begin{pmatrix}0^\dagger&(A^\dagger)^\dagger\\A^\dagger&0^\dagger\end{pmatrix}=\begin{pmatrix}0&A\\A^\dagger&0\end{pmatrix}=\tilde A
    The adjoint of a block matrix transposes the block positions and takes the adjoint of each block; (A†)†=A(A^\dagger)^\dagger=A. So A~\tilde A is Hermitian for any square AA, at the cost of one extra qubit.
  2. Step 2:
    A~(0x⃗)=(Ax⃗0)=(b⃗0)\tilde A\begin{pmatrix}0\\\vec x\end{pmatrix}=\begin{pmatrix}A\vec x\\0\end{pmatrix}=\begin{pmatrix}\vec b\\0\end{pmatrix}
    Block multiplication. Solving A~y⃗=(b⃗,0)T\tilde A\vec y=(\vec b,0)^T therefore gives y⃗=(0,x⃗)T\vec y=(0,\vec x)^T with Ax⃗=b⃗A\vec x=\vec b; the eigenvalues of A~\tilde A are ±σj\pm\sigma_j (singular values of AA), so κ(A~)=κ(A)\kappa(\tilde A)=\kappa(A).

Writing the eigenvalues into a clock#

Derivation

Phase estimation of U=eiAtU=e^{iAt} on ∣b⟩\lvert b\rangle

  1. Step 1:
    U∣uj⟩=eiAt∣uj⟩=eiλjt∣uj⟩U\lvert u_j\rangle=e^{iAt}\lvert u_j\rangle=e^{i\lambda_jt}\lvert u_j\rangle
    Spectral theorem with f(λ)=eiλtf(\lambda)=e^{i\lambda t}: eiAt=∑jeiλjt∣uj⟩⟨uj∣e^{iAt}=\sum_je^{i\lambda_jt}\lvert u_j\rangle\langle u_j\rvert, a unitary because each ∣eiλjt∣=1\lvert e^{i\lambda_jt}\rvert=1.
  2. Step 2:
    eiλjt=e2πiφj,φj=λjt2πe^{i\lambda_jt}=e^{2\pi i\varphi_j},\qquad\varphi_j=\frac{\lambda_jt}{2\pi}
    Write the eigenphase as a fraction of a full turn, as in quantum phase estimation (QPE). Choosing tt with 0<λjt<2π0<\lambda_jt<2\pi makes φj∈(0,1)\varphi_j\in(0,1) unambiguous.
  3. Step 3:
    QPE(∣0⟩⊗tc∣uj⟩)=∣kj⟩∣uj⟩,kj=2tcφj=λjt 2tc2π\mathrm{QPE}\big(\lvert0\rangle^{\otimes t_c}\lvert u_j\rangle\big)=\lvert k_j\rangle\lvert u_j\rangle,\qquad k_j=2^{t_c}\varphi_j=\frac{\lambda_jt\,2^{t_c}}{2\pi}
    QPE with tct_c clock qubits applies H⊗tcH^{\otimes t_c}, the controlled powers U2mU^{2^m} (phase kickback gives 12tc/2∑ke2πiφjk∣k⟩=QFT∣2tcφj⟩\tfrac1{2^{t_c/2}}\sum_ke^{2\pi i\varphi_jk}\lvert k\rangle=\mathrm{QFT}\lvert2^{t_c}\varphi_j\rangle) and QFT†\mathrm{QFT}^\dagger. The output is exact when 2tcφj2^{t_c}\varphi_j is an integer.
  4. Step 4:
    QPE(∣0⟩⊗tc∣b⟩)=∑jβj∣kj⟩∣uj⟩\mathrm{QPE}\big(\lvert0\rangle^{\otimes t_c}\lvert b\rangle\big)=\sum_j\beta_j\lvert k_j\rangle\lvert u_j\rangle
    QPE is linear and ∣b⟩=∑jβj∣uj⟩\lvert b\rangle=\sum_j\beta_j\lvert u_j\rangle. The eigenvectors are never computed: the circuit acts on all components at once.

Result

∣0⟩⊗tc∣b⟩ → QPE(eiAt)  ∑jβj∣kj⟩∣uj⟩\boxed{\lvert0\rangle^{\otimes t_c}\lvert b\rangle\ \xrightarrow{\ \mathrm{QPE}(e^{iAt})\ }\ \sum_j\beta_j\lvert k_j\rangle\lvert u_j\rangle}

Eigenvalue inversion by a controlled rotation#

Derivation

∣k⟩∣0⟩↦∣k⟩(1−C2/k2∣0⟩+Ck∣1⟩)\lvert k\rangle\lvert0\rangle\mapsto\lvert k\rangle\big(\sqrt{1-C^2/k^2}\lvert0\rangle+\tfrac Ck\lvert1\rangle\big)

  1. Step 1:
    RY(θ)∣0⟩=cos⁡θ2∣0⟩+sin⁡θ2∣1⟩R_Y(\theta)\lvert0\rangle=\cos\tfrac\theta2\lvert0\rangle+\sin\tfrac\theta2\lvert1\rangle
    First column of RY(θ)=(cos⁡θ2−sin⁡θ2sin⁡θ2cos⁡θ2)R_Y(\theta)=\begin{pmatrix}\cos\frac\theta2&-\sin\frac\theta2\\\sin\frac\theta2&\cos\frac\theta2\end{pmatrix}.
  2. Step 2:
    sin⁡θk2=Ck  ⟺  θk=2arcsin⁡Ck\sin\tfrac{\theta_k}2=\frac Ck\iff\theta_k=2\arcsin\frac Ck
    Requires 0<C/k≤10<C/k\le1, i.e. C≤min⁡jkjC\le\min_jk_j; then cos⁡θk2=1−C2/k2≥0\cos\tfrac{\theta_k}{2}=\sqrt{1-C^2/k^2}\ge0 because θk/2∈(0,π/2]\theta_k/2\in(0,\pi/2].
  3. Step 3:
    ∑jβj∣kj⟩∣uj⟩∣0⟩a↦∑jβj∣kj⟩∣uj⟩(1−C2kj2∣0⟩a+Ckj∣1⟩a)\sum_j\beta_j\lvert k_j\rangle\lvert u_j\rangle\lvert0\rangle_a\mapsto\sum_j\beta_j\lvert k_j\rangle\lvert u_j\rangle\Big(\sqrt{1-\tfrac{C^2}{k_j^2}}\lvert0\rangle_a+\tfrac{C}{k_j}\lvert1\rangle_a\Big)
    The rotation is controlled by the clock: for each basis value kk of the clock apply RY(θk)R_Y(\theta_k) to the ancilla. With tct_c clock qubits this is a uniformly controlled rotation; in the worked example the clock values 1=∣01⟩1=\lvert01\rangle and 2=∣10⟩2=\lvert10\rangle each have a single set bit, so one CRY per clock qubit suffices.

Uncomputation and post-selection#

Derivation

Output state and success probability

  1. Step 1:
    QPE†: ∑jβj∣kj⟩∣uj⟩∣aj⟩↦∑jβj∣0⟩⊗tc∣uj⟩∣aj⟩\mathrm{QPE}^\dagger:\ \sum_j\beta_j\lvert k_j\rangle\lvert u_j\rangle\lvert a_j\rangle\mapsto\sum_j\beta_j\lvert0\rangle^{\otimes t_c}\lvert u_j\rangle\lvert a_j\rangle
    QPE†\mathrm{QPE}^\dagger (reverse order, adjoint gates: QFT, controlled U−2mU^{-2^m}, H⊗tcH^{\otimes t_c}) maps ∣kj⟩∣uj⟩↦∣0⟩∣uj⟩\lvert k_j\rangle\lvert u_j\rangle\mapsto\lvert0\rangle\lvert u_j\rangle and does not act on the ancilla. Without uncomputation the clock stays entangled with ∣uj⟩\lvert u_j\rangle and the system would be in a mixed state.
  2. Step 2:
    (⟨1∣a⊗I) ∣Ψ⟩=∣0⟩⊗tc⊗C∑jβjkj∣uj⟩(\langle1\rvert_a\otimes I)\,\lvert\Psi\rangle=\lvert0\rangle^{\otimes t_c}\otimes C\sum_j\frac{\beta_j}{k_j}\lvert u_j\rangle
    Project the ancilla onto ∣1⟩\lvert1\rangle: only the amplitudes C/kjC/k_j survive, and kj∝λjk_j\propto\lambda_j.
  3. Step 3:
    psucc=∥C∑jβjkj∣uj⟩∥2=∑j∣βj∣2C2kj2p_{\text{succ}}=\Big\lVert C\sum_j\frac{\beta_j}{k_j}\lvert u_j\rangle\Big\rVert^2=\sum_j\lvert\beta_j\rvert^2\frac{C^2}{k_j^2}
    Born rule; orthonormality of the ∣uj⟩\lvert u_j\rangle.
  4. Step 4:
    ∑jβjkj−1∣uj⟩∑j∣βj∣2kj−2=∑jβjλj−1∣uj⟩∑j∣βj∣2λj−2=∣x⟩\frac{\sum_j\beta_jk_j^{-1}\lvert u_j\rangle}{\sqrt{\sum_j\lvert\beta_j\rvert^2k_j^{-2}}}=\frac{\sum_j\beta_j\lambda_j^{-1}\lvert u_j\rangle}{\sqrt{\sum_j\lvert\beta_j\rvert^2\lambda_j^{-2}}}=\lvert x\rangle
    The post-measurement state; the common factor 2π/(t2tc)2\pi/(t2^{t_c}) in kj−1=2πt2tcλj−1k_j^{-1}=\tfrac{2\pi}{t2^{t_c}}\lambda_j^{-1} cancels in the normalisation.
  5. Step 5:
    C=kmin⁡ ⇒ C2kj2=λmin⁡2λj2≥1κ2 ⇒ psucc≥1κ2∑j∣βj∣2=1κ2C=k_{\min}\ \Rightarrow\ \frac{C^2}{k_j^2}=\frac{\lambda_{\min}^2}{\lambda_j^2}\ge\frac1{\kappa^2}\ \Rightarrow\ p_{\text{succ}}\ge\frac1{\kappa^2}\sum_j\lvert\beta_j\rvert^2=\frac1{\kappa^2}
    Each term is bounded below by 1/κ21/\kappa^2 and ∑j∣βj∣2=1\sum_j\lvert\beta_j\rvert^2=1. Repeating until success needs on average 1/psucc=O(κ2)1/p_{\text{succ}}=O(\kappa^2) runs; amplitude amplification boosts an amplitude p\sqrt p to Θ(1)\Theta(1) with O(1/p)=O(κ)O(1/\sqrt p)=O(\kappa) rounds.

Result

psucc=∑j∣βj∣2C2kj2,O(κ2) repetitions, or O(κ) with amplitude amplification\boxed{p_{\text{succ}}=\sum_j\lvert\beta_j\rvert^2\frac{C^2}{k_j^2},\qquad O(\kappa^2)\ \text{repetitions, or }O(\kappa)\text{ with amplitude amplification}}

The fine print#

HHL runs in time O~ ⁣(log⁡(N) s2κ2/ε)\tilde O\!\left(\log(N)\,s^2\kappa^2/\varepsilon\right) for an ss-sparse N×NN\times N matrix and output error ε\varepsilon (Harrow, Hassidim and Lloyd 2009), compared with O(Nsκlog⁡(1/ε))O(Ns\kappa\log(1/\varepsilon)) for the classical conjugate-gradient method. The exponential separation in NN holds only under conditions that must all be met (Aaronson 2015):

  1. Input state. ∣b⟩\lvert b\rangle must be prepared in time polylog(N)\mathrm{polylog}(N). Preparing an arbitrary NN-dimensional vector generally costs Ω(N)\Omega(N) gates, which would erase the speedup.
  2. Sparsity. eiAte^{iAt} must be simulable efficiently; Hamiltonian simulation of an ss-sparse AA with efficiently computable entries costs O~(log⁡(N)s2t)\tilde O(\log(N)s^2t), which is where the factor s2s^2 comes from.
  3. Conditioning. κ\kappa must be polylog(N)\mathrm{polylog}(N); the phase-estimation precision needed to resolve λmin⁡\lambda_{\min} and the O(κ)O(\kappa) amplification rounds both scale with κ\kappa.
  4. Output. The result is the state ∣x⟩\lvert x\rangle, not the vector x⃗\vec x. Reading all NN amplitudes needs Ω(N)\Omega(N) measurements. HHL is useful when only an expectation value ⟨x∣M∣x⟩\langle x\rvert M\lvert x\rangle (an overlap, a norm, a weighted sum) is required.
  5. Dequantisation. For low-rank matrices with classical sample-and-query access to the inputs, Tang (2019) gave a classical algorithm for recommendation systems whose run time is polylogarithmic in the dimension; the technique extends to low-rank linear systems, so an exponential quantum advantage requires AA of high rank.

A 2×2 instance worked in full#

Derivation

Spectrum of A=(1−13−131)A=\begin{pmatrix}1&-\tfrac13\\-\tfrac13&1\end{pmatrix} and ∣b⟩=∣0⟩\lvert b\rangle=\lvert0\rangle

  1. Step 1:
    A=I−13X,X∣±⟩=±∣±⟩A=I-\tfrac13X,\qquad X\lvert\pm\rangle=\pm\lvert\pm\rangle
    AA has 11 on the diagonal and −13-\tfrac13 off it. The eigenvectors of XX are ∣±⟩=(1,±1)/2\lvert\pm\rangle=(1,\pm1)/\sqrt2.
  2. Step 2:
    λ1=1−13=23,  u1=12(1,1);λ2=1+13=43,  u2=12(1,−1)\lambda_1=1-\tfrac13=\tfrac23,\ \ u_1=\tfrac{1}{\sqrt2}(1,1);\qquad\lambda_2=1+\tfrac13=\tfrac43,\ \ u_2=\tfrac1{\sqrt2}(1,-1)
    A∣±⟩=(1∓13)∣±⟩A\lvert\pm\rangle=(1\mp\tfrac13)\lvert\pm\rangle. Condition number κ=4/32/3=2\kappa=\tfrac{4/3}{2/3}=2.
  3. Step 3:
    ∣0⟩=12(u1+u2),β1=β2=12\lvert0\rangle=\tfrac1{\sqrt2}\big(u_1+u_2\big),\qquad\beta_1=\beta_2=\tfrac1{\sqrt2}
    u1+u2=12(2,0)=2 (1,0)u_1+u_2=\tfrac1{\sqrt2}(2,0)=\sqrt2\,(1,0).
Derivation

eiAte^{iAt} for t=3π4t=\tfrac{3\pi}4 and (eiAt)2=−X(e^{iAt})^2=-X

  1. Step 1:
    eiAt=eite−it3Xe^{iAt}=e^{it}e^{-i\frac t3X}
    II and XX commute, so ei(I−X/3)t=eitIe−itX/3e^{i(I-X/3)t}=e^{itI}e^{-itX/3}.
  2. Step 2:
    e−iαX=cos⁡α I−isin⁡α X,α=t3=π4e^{-i\alpha X}=\cos\alpha\,I-i\sin\alpha\,X,\qquad\alpha=\tfrac t3=\tfrac\pi4
    Power series with X2=IX^2=I: even powers give cos⁡α I\cos\alpha\,I, odd powers give −isin⁡α X-i\sin\alpha\,X.
  3. Step 3:
    ei3π4=−1+i2,e−iπ4X=12(I−iX)e^{i\frac{3\pi}4}=\frac{-1+i}{\sqrt2},\qquad e^{-i\frac\pi4X}=\frac1{\sqrt2}\big(I-iX\big)
    cos⁡3π4=−12\cos\tfrac{3\pi}4=-\tfrac1{\sqrt2}, sin⁡3π4=12\sin\tfrac{3\pi}4=\tfrac1{\sqrt2}, cos⁡π4=sin⁡π4=12\cos\tfrac\pi4=\sin\tfrac\pi4=\tfrac1{\sqrt2}.
  4. Step 4:
    eiAt=−1+i2(I−iX)=12(−1+i1+i1+i−1+i)e^{iAt}=\frac{-1+i}{2}\big(I-iX\big)=\frac12\begin{pmatrix}-1+i&1+i\\1+i&-1+i\end{pmatrix}
    Diagonal entries −1+i2\tfrac{-1+i}2; off-diagonal entries (−1+i)(−i)2=i−i22=1+i2\tfrac{(-1+i)(-i)}2=\tfrac{i-i^2}{2}=\tfrac{1+i}2.
  5. Step 5:
    (eiAt)2=e2i3π4(e−iπ4X)2=ei3π2e−iπ2X=(−i)(−iX)=−X\big(e^{iAt}\big)^2=e^{2i\frac{3\pi}4}\big(e^{-i\frac\pi4X}\big)^2=e^{i\frac{3\pi}2}e^{-i\frac\pi2X}=(-i)(-iX)=-X
    ei3π/2=−ie^{i3\pi/2}=-i and e−iπ2X=cos⁡π2I−isin⁡π2X=−iXe^{-i\frac\pi2X}=\cos\tfrac\pi2I-i\sin\tfrac\pi2X=-iX; (−i)(−i)=i2=−1(-i)(-i)=i^2=-1.
  6. Step 6:
    14((−1+i)2+(1+i)22(−1+i)(1+i)2(1+i)(−1+i)(1+i)2+(−1+i)2)=14(−2i+2i−4−42i−2i)=(0−1−10)\frac14\begin{pmatrix}(-1+i)^2+(1+i)^2&2(-1+i)(1+i)\\2(1+i)(-1+i)&(1+i)^2+(-1+i)^2\end{pmatrix}=\frac14\begin{pmatrix}-2i+2i&-4\\-4&2i-2i\end{pmatrix}=\begin{pmatrix}0&-1\\-1&0\end{pmatrix}
    Direct matrix check: (−1+i)2=−2i(-1+i)^2=-2i, (1+i)2=2i(1+i)^2=2i, (−1+i)(1+i)=−1−i+i+i2=−2(-1+i)(1+i)=-1-i+i+i^2=-2. So U2=−XU^2=-X, U−2=(−X)−1=−XU^{-2}=(-X)^{-1}=-X and U−1=U†=12(−1−i1−i1−i−1−i)U^{-1}=U^\dagger=\tfrac12\begin{pmatrix}-1-i&1-i\\1-i&-1-i\end{pmatrix}.

Result

eiAt=12(−1+i1+i1+i−1+i),(eiAt)2=−X\boxed{e^{iAt}=\frac12\begin{pmatrix}-1+i&1+i\\1+i&-1+i\end{pmatrix},\qquad\big(e^{iAt}\big)^2=-X}
Derivation

Clock values, angles and the output state

  1. Step 1:
    kj=λjt 2tc2π=λj⋅3π4⋅42π=3λj2 ⇒ k1=1, k2=2k_j=\frac{\lambda_j t\,2^{t_c}}{2\pi}=\frac{\lambda_j\cdot\frac{3\pi}{4}\cdot4}{2\pi}=\frac{3\lambda_j}{2}\ \Rightarrow\ k_1=1,\ k_2=2
    Two clock qubits, 2tc=42^{t_c}=4. Both values are integers, so QPE is exact: the clock holds ∣01⟩\lvert01\rangle for u1u_1 and ∣10⟩\lvert10\rangle for u2u_2 (qubit 11 is the MSB). Consistency: Uu1=eiπ/2u1=iu1U u_1=e^{i\pi/2}u_1=iu_1 and Uu2=eiπu2=−u2Uu_2=e^{i\pi}u_2=-u_2.
  2. Step 2:
    C=1=min⁡jkj:θ1=2arcsin⁡1=π,θ2=2arcsin⁡12=π3C=1=\min_jk_j:\qquad\theta_1=2\arcsin1=\pi,\qquad\theta_2=2\arcsin\tfrac12=\tfrac\pi3
    arcsin⁡12=π6\arcsin\tfrac12=\tfrac\pi6. The rotation for k1=1k_1=1 is controlled by the LSB clock qubit, the one for k2=2k_2=2 by the MSB.
  3. Step 3:
    (⟨1∣a⊗⟨00∣c)∣Ψ⟩=12(11u1+12u2)=12(32,12)=(34,14)\big(\langle1\rvert_a\otimes\langle00\rvert_c\big)\lvert\Psi\rangle=\tfrac1{\sqrt2}\Big(\tfrac11u_1+\tfrac12u_2\Big)=\tfrac12\Big(\tfrac32,\tfrac12\Big)=\Big(\tfrac34,\tfrac14\Big)
    βj=12\beta_j=\tfrac1{\sqrt2}, C/k1=1C/k_1=1, C/k2=12C/k_2=\tfrac12; u1+12u2=12(32,12)u_1+\tfrac12u_2=\tfrac1{\sqrt2}(\tfrac32,\tfrac12).
  4. Step 4:
    psucc=12(1+14)=58=(34)2+(14)2,∣x⟩=(3,1)10=(0.948683, 0.316228)p_{\text{succ}}=\tfrac12\Big(1+\tfrac14\Big)=\tfrac58=\Big(\tfrac34\Big)^2+\Big(\tfrac14\Big)^2,\qquad\lvert x\rangle=\frac{(3,1)}{\sqrt{10}}=(0.948683,\ 0.316228)
    916+116=1016=58\tfrac9{16}+\tfrac1{16}=\tfrac{10}{16}=\tfrac58. Normalising (34,14)(\tfrac34,\tfrac14) gives (3,1)/10(3,1)/\sqrt{10}. This exceeds the bound 1/κ2=141/\kappa^2=\tfrac14.
  5. Step 5:
    A−1=11−19(113131)=98(113131),A−1b⃗=98(1,13)∝(3,1)A^{-1}=\frac{1}{1-\frac19}\begin{pmatrix}1&\frac13\\\frac13&1\end{pmatrix}=\frac98\begin{pmatrix}1&\frac13\\\frac13&1\end{pmatrix},\qquad A^{-1}\vec b=\frac98\Big(1,\frac13\Big)\propto(3,1)
    Classical check with the 2×22\times2 inverse 1det⁡A(d−b−ca)\tfrac1{\det A}\begin{pmatrix}d&-b\\-c&a\end{pmatrix}, det⁡A=1−19=89\det A=1-\tfrac19=\tfrac89. The direction agrees with the quantum output.

Result

∣x⟩=3∣0⟩+∣1⟩10,psucc=58\boxed{\lvert x\rangle=\frac{3\lvert0\rangle+\lvert1\rangle}{\sqrt{10}},\qquad p_{\text{succ}}=\frac58}

Quick check

In the worked example keep C=1C=1 but prepare ∣b⟩=u2=(1,−1)/2\lvert b\rangle=u_2=(1,-1)/\sqrt2 instead of ∣0⟩\lvert0\rangle. What is the post-selection probability?

tolerance ±0.000001
TypeScriptrunnable
import { runStatevector } from '@/lib/quantum-simulator';

// Register: ancilla 0, clock 1 (MSB) and 2 (LSB), system 3; qubit 0 is the most significant bit.
// A = [[1, -1/3], [-1/3, 1]], t = 3 pi / 4, U = e^{iAt} = (1/2)[[-1+i, 1+i], [1+i, -1+i]], U^2 = -X.
const U = [[[-0.5, 0.5], [0.5, 0.5]], [[0.5, 0.5], [-0.5, 0.5]]];
const Uinv = [[[-0.5, -0.5], [0.5, -0.5]], [[0.5, -0.5], [-0.5, -0.5]]];
const minusX = [[[0, 0], [-1, 0]], [[-1, 0], [0, 0]]];
const gate = (g, targets, params) => (params ? { kind: 'gate', gate: g, targets, params } : { kind: 'gate', gate: g, targets });
const cU = (label, m, control) => ({ kind: 'unitary', label, targets: [3], controls: [control], matrix: m });
const theta1 = 2 * Math.asin(1 / 1); // C = 1, k = 1
const theta2 = 2 * Math.asin(1 / 2); // C = 1, k = 2
const ops = [
  gate('H', [1]), gate('H', [2]), cU('U', U, 2), cU('U_sq', minusX, 1),
  gate('SWAP', [1, 2]), gate('H', [2]), gate('CP', [2, 1], [-Math.PI / 2]), gate('H', [1]),
  gate('CRY', [2, 0], [theta1]), gate('CRY', [1, 0], [theta2]),
  gate('H', [1]), gate('CP', [2, 1], [Math.PI / 2]), gate('H', [2]), gate('SWAP', [1, 2]),
  cU('U_sq_inv', minusX, 1), cU('U_inv', Uinv, 2), gate('H', [1]), gate('H', [2]),
];
const amps = runStatevector({ version: 1, numQubits: 4, numClbits: 0, ops }).amplitudes();
// ancilla = 1, clock = 00: basis indices 0b1000 + s with s the system bit.
const a0 = amps[8], a1 = amps[9];
const p = a0.re ** 2 + a0.im ** 2 + a1.re ** 2 + a1.im ** 2;
console.log('P(ancilla=1, clock=00) = ' + p.toFixed(6));
console.log('x = (' + (a0.re / Math.sqrt(p)).toFixed(6) + ', ' + (a1.re / Math.sqrt(p)).toFixed(6) + ')');
// Classical check: A^{-1} = (9/8)[[1, 1/3], [1/3, 1]], so A^{-1} b = (9/8, 3/8).
const det = 1 - 1 / 9;
console.log('classical A^-1 b = (' + (1 / det).toFixed(6) + ', ' + (1 / 3 / det).toFixed(6) + ')');

Expected output

P(ancilla=1, clock=00) = 0.625000
x = (0.948683, 0.316228)
classical A^-1 b = (1.125000, 0.375000)

References

  1. A. W. Harrow, A. Hassidim and S. Lloyd, "Quantum algorithm for linear systems of equations", Physical Review Letters 103, 150502 (2009).doi:10.1103/PhysRevLett.103.150502
  2. S. Aaronson, "Read the fine print", Nature Physics 11, 291–293 (2015).doi:10.1038/nphys3272
  3. E. Tang, "A quantum-inspired classical algorithm for recommendation systems", Proceedings of the 51st Annual ACM SIGACT Symposium on Theory of Computing (STOC 2019), 217–228.doi:10.1145/3313276.3316310

Hands-on labs

  • HHL on a 2×2 system

    2 steps · auto-graded

    Solve Ax⃗=b⃗A\vec x=\vec b for A=(1−13−131)A=\begin{pmatrix}1&-\tfrac13\\-\tfrac13&1\end{pmatrix} and b⃗=(1,0)T\vec b=(1,0)^T with the HHL algorithm on four qubits: read the clock values produced by phase estimation, then choose the controlled-RYR_Y angles that divide each eigencomponent by its eigenvalue, so that post-selection yields ∣x⟩=(3,1)/10\lvert x\rangle=(3,1)/\sqrt{10}.

    Open lab