This sitory contains a functional quantum computing implementation of the Harrow-Hassidim-Lloyd (HHL) algorithm using Qiskit to solve linear systems of equations of the form:
Our implementation extends the standard HHL algorithm by automatically supporting non-power-of-two matrix dimensions (via intelligent zero-padding) and non-Hermitian matrices (using the normal equations transformation
The HHL algorithm solves linear systems by encoding the solution vector in the amplitudes of a quantum register. The pipeline implemented follows these key stages:
-
State Preparation (
$b$ -register encoding): Encodes the normalized vector$\vec{b}$ into the amplitudes of the$b$ -register using Qiskit'sStatePreparationcomponent. -
Quantum Phase Estimation (QPE):
Estimates the eigenvalues of the matrix
$A$ (or the transformed Hermitian matrix) into a dedicated precision register ($c$ -register).- Hadamard gates create a uniform superposition in the
$c$ -register. - Controlled Hamiltonian evolution steps (
$e^{-i A t}$ ) are synthesized using a Trotter-Suzuki product formula (specificallySuzukiTrotterorder 1). - An Inverse Quantum Fourier Transform (
$QFT^\dagger$ ) translates the phase information into binary eigenvalue representations.
- Hadamard gates create a uniform superposition in the
-
Controlled Reciprocal Rotation:
Applies a controlled rotation onto an auxiliary (ancilla) register qubit using an
ExactReciprocalGate. The rotation angle is proportional to$\arcsin(s/\lambda)$ , where$\lambda$ represents the estimated eigenvalues and$s$ is a scaling factor to guarantee valid rotation bounds. -
Inverse Quantum Phase Estimation (Uncomputation):
Reverses the QPE steps (QFT and controlled evolution) to uncompute the eigenvalue registers, leaving the
$c$ -register clean at$|0\rangle$ and disentangled from the$b$ -register. -
Measurement & Post-selection:
Measures the system. Conditioned on measuring the auxiliary qubit in the
$|1\rangle$ state, the$b$ -register collapses into the solution ket$|\x\rangle$ .
- hhl.py: The core implementation containing the
HHLCircuitclass. It manages the construction of the quantum registers, circuit operations, Trotter evolution scaling, and contains a default 2x2 example run. - tests.py: A comprehensive test suite containing 11 validation scenarios. It tests matrices of various dimensions (up to 16x16), dense/sparse patterns, non-Hermitian systems, and checks the Total Variation Distance (TVD) between the quantum simulator outputs and classical numpy results.
- requirements.txt: Python package dependencies.
- Python 3.8 or higher is recommended.
Ensure pip is updated, then install the required packages:
pip install --upgrade pip
pip install -r requirements.txtThen you can run the main script to build, draw, and simulate the default HHL system:
python hhl.pyThis runs the solver for a small example. And outputs a textual circuit diagram along with a side-by-side comparison of the quantum simulation counts vs. the classical analytical solution.
The automated test suite runs 11 different linear systems and validates the correctness by computing the Total Variation Distance (TVD) against classical results.
python tests.pyThe test cases verify:
- 2x2 Diagonal Matrix (eigenvalues 1.0, 2.0)
- 2x2 Off-Diagonal Matrix (eigenvalues 1.0, 2.0)
- 4x4 Diagonal Matrix (eigenvalues 1.0, 2.0, 3.0, 4.0)
- 4x4 Off-Diagonal Matrix (symmetric matrix using Householder reflection)
- 8x8 Diagonal Matrix (eigenvalues 1.0 to 8.0)
- 16x16 Sparse Block-Diagonal Matrix
- 16x16 Dense Matrix
- 3x3 Diagonal Matrix (requires padding to 4x4)
- 5x5 Off-Diagonal Matrix (requires padding to 8x8)
- 2x2 Non-Hermitian Matrix (transformed via normal equations)
- 5x5 Non-Hermitian Matrix (requires normal equations transformation and padding to 8x8)
A test is flagged as SUCCESS if the TVD is within the 5% threshold (0.05) to account for QPE approximation errors and finite shots.
This project is available under the MIT license
The authors of this project are Salvatore Biamonte and Davide Pirrò