A Qiskit implementation of the HHL algorithm for solving a 4x4 linear system A x = b. The circuit follows Cao et al., Quantum circuit design for solving linear systems of equations.
quantum_linear_solver/quantum_linear_solver.py runs the whole pipeline:
- Defines a Hermitian 4x4 matrix
Aand a vectorb, and normalises both by|b|. - Computes the exact solution with NumPy for reference.
- Builds the HHL circuit: state preparation of
|b>, phase estimation with controlledHamiltonianGates on a 4-qubit clock register, controlledRYrotations on an ancilla, and the inverse phase estimation. - Runs 30,000 shots on the Aer simulator and keeps the shots where the ancilla is measured as
1. - Rebuilds an approximate solution vector from those counts and compares it with the exact one.
The run uses r = 6 and t = 3π.
Results are written to quantum_linear_solver/output/:
results.txt: exact solution, quantum solution, norm of the difference, raw countscircuit.txt: text drawing of the full circuit- a histogram of the measured counts
In the committed run the quantum estimate is still far from the exact answer (norm of difference ≈ 0.83). The circuit structure is the main point of this repo. The rotation parameters r and t need tuning to get a closer match.
python -m venv .venv
source .venv/bin/activate
pip install qiskit qiskit-aer numpy matplotlib
python quantum_linear_solver/quantum_linear_solver.pyY. Cao, A. Daskin, S. Frankel, S. Kais, Quantum circuit design for solving linear systems of equations, arXiv:1110.2232.