Lesson 31 · 100 min · advanced
The HHL Algorithm for Ax = b
The Harrow–Hassidim–Lloyd (HHL) algorithm prepares a quantum state for a Hermitian, sparse, well-conditioned matrix . It writes the eigenvalues of into a clock register by quantum phase estimation of , rotates an ancilla by an angle that encodes , uncomputes the clock and post-selects the ancilla. This lesson derives each stage, the success probability and the cost, lists the caveats that limit the claimed exponential speedup, and runs the full algorithm on a system whose output is checked against the classical solution.
Prerequisites: Quantum Phase Estimation, Spectral Theorem & Functions of Operators
Objectives and key results
Express in the eigenbasis of a Hermitian and embed a non-Hermitian in the Hermitian matrix .
Show that phase estimation of maps to with .
Implement the eigenvalue inversion with and derive the success probability .
State the complexity and the input/output caveats, including dequantisation for low-rank matrices.
Compute , , the clock values, the angles and the output state of a instance by hand.
- formula · Solution in the eigenbasis
- formula · Clock value
- formula · Inversion angle
- formula · Success probability
- formula · Complexity
The problem in the eigenbasis#
Definition
Quantum linear-systems problem
Given an Hermitian matrix with spectral decomposition (orthonormal eigenvectors , real non-zero eigenvalues ) and a normalised state with , prepare the normalised state . The condition number is (ratio of the largest to the smallest ); after rescaling one assumes .
in the eigenbasis
- Step 1:Spectral theorem: for , defined because no vanishes. Check: .
- Step 2:Apply the operator to and use .
- Step 3:Normalise with (orthonormality). HHL therefore only has to multiply each eigencomponent by a number proportional to .
Result
Embedding a non-Hermitian
- Step 1:The adjoint of a block matrix transposes the block positions and takes the adjoint of each block; . So is Hermitian for any square , at the cost of one extra qubit.
- Step 2:Block multiplication. Solving therefore gives with ; the eigenvalues of are (singular values of ), so .
Writing the eigenvalues into a clock#
Phase estimation of on
- Step 1:Spectral theorem with : , a unitary because each .
- Step 2:Write the eigenphase as a fraction of a full turn, as in quantum phase estimation (QPE). Choosing with makes unambiguous.
- Step 3:QPE with clock qubits applies , the controlled powers (phase kickback gives ) and . The output is exact when is an integer.
- Step 4:QPE is linear and . The eigenvectors are never computed: the circuit acts on all components at once.
Result
Eigenvalue inversion by a controlled rotation#
- Step 1:First column of .
- Step 2:Requires , i.e. ; then because .
- Step 3:The rotation is controlled by the clock: for each basis value of the clock apply to the ancilla. With clock qubits this is a uniformly controlled rotation; in the worked example the clock values and each have a single set bit, so one CRY per clock qubit suffices.
Uncomputation and post-selection#
Output state and success probability
- Step 1:(reverse order, adjoint gates: QFT, controlled , ) maps and does not act on the ancilla. Without uncomputation the clock stays entangled with and the system would be in a mixed state.
- Step 2:Project the ancilla onto : only the amplitudes survive, and .
- Step 3:Born rule; orthonormality of the .
- Step 4:The post-measurement state; the common factor in cancels in the normalisation.
- Step 5:Each term is bounded below by and . Repeating until success needs on average runs; amplitude amplification boosts an amplitude to with rounds.
Result
The fine print#
HHL runs in time for an -sparse matrix and output error (Harrow, Hassidim and Lloyd 2009), compared with for the classical conjugate-gradient method. The exponential separation in holds only under conditions that must all be met (Aaronson 2015):
- Input state. must be prepared in time . Preparing an arbitrary -dimensional vector generally costs gates, which would erase the speedup.
- Sparsity. must be simulable efficiently; Hamiltonian simulation of an -sparse with efficiently computable entries costs , which is where the factor comes from.
- Conditioning. must be ; the phase-estimation precision needed to resolve and the amplification rounds both scale with .
- Output. The result is the state , not the vector . Reading all amplitudes needs measurements. HHL is useful when only an expectation value (an overlap, a norm, a weighted sum) is required.
- 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 of high rank.
A 2×2 instance worked in full#
Spectrum of and
- Step 1:has on the diagonal and off it. The eigenvectors of are .
- Step 2:. Condition number .
- Step 3:.
for and
- Step 1:and commute, so .
- Step 2:Power series with : even powers give , odd powers give .
- Step 3:, , .
- Step 4:Diagonal entries ; off-diagonal entries .
- Step 5:and ; .
- Step 6:Direct matrix check: , , . So , and .
Result
Clock values, angles and the output state
- Step 1:Two clock qubits, . Both values are integers, so QPE is exact: the clock holds for and for (qubit is the MSB). Consistency: and .
- Step 2:. The rotation for is controlled by the LSB clock qubit, the one for by the MSB.
- Step 3:, , ; .
- Step 4:. Normalising gives . This exceeds the bound .
- Step 5:Classical check with the inverse , . The direction agrees with the quantum output.
Result
Quick check
In the worked example keep but prepare instead of . What is the post-selection probability?
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
- 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
- S. Aaronson, "Read the fine print", Nature Physics 11, 291–293 (2015).doi:10.1038/nphys3272
- 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-gradedOpen labSolve for and with the HHL algorithm on four qubits: read the clock values produced by phase estimation, then choose the controlled- angles that divide each eigencomponent by its eigenvalue, so that post-selection yields .