braket.experimental.algorithms.hhl.hhl module
Harrow-Hassidim-Lloyd (HHL) Algorithm for Solving Linear Systems of Equations.
The HHL algorithm is a quantum algorithm for solving systems of linear equations of the form Ax = b. Given an N x N Hermitian matrix A and a unit vector b, the algorithm produces a quantum state |x> proportional to A^{-1}|b>.
For certain classes of problems (sparse, well-conditioned matrices), and when only summary statistics of the solution are needed (e.g., <x|M|x> for some operator M), HHL can offer a speedup over classical methods. However, the overall advantage depends on the efficiency of state preparation and readout.
This implementation provides a simplified version of HHL suitable for small systems (2x2 matrices), illustrating the core concepts: 1. State preparation: encode |b> into a quantum state 2. Quantum Phase Estimation (QPE): decompose |b> in the eigenbasis of A 3. Controlled rotation: apply the eigenvalue inversion C/lambda 4. Inverse QPE: uncompute the eigenvalue register 5. Measurement: post-select on the ancilla qubit
References
- [1] A. W. Harrow, A. Hassidim, S. Lloyd, “Quantum algorithm for linear systems
of equations”, Phys. Rev. Lett. 103, 150502 (2009). arXiv:0811.3171
[2] Wikipedia: https://en.wikipedia.org/wiki/HHL_algorithm
- braket.experimental.algorithms.hhl.hhl.hhl_circuit(matrix: ndarray, b_vector: ndarray, num_clock_qubits: int = 2, scaling_factor: float | None = None) Circuit[source]
Construct the full HHL circuit for solving Ax = b.
The circuit uses: - 1 input qubit for encoding |b> - num_clock_qubits clock qubits for QPE - 1 ancilla qubit for eigenvalue inversion (post-selection)
Qubit layout: - Clock qubits: 0 to num_clock_qubits - 1 - Input qubit: num_clock_qubits - Ancilla qubit: num_clock_qubits + 1
- Parameters:
matrix (np.ndarray) – A 2x2 Hermitian matrix A.
b_vector (np.ndarray) – A normalized 2-element vector b.
num_clock_qubits (int) – Number of clock qubits for QPE (default: 2).
scaling_factor (Optional[float]) – Scaling factor for Hamiltonian simulation. If None, automatically computed from the eigenvalues.
- Returns:
Circuit – The complete HHL circuit.
- Raises:
ValueError – If matrix is not 2x2 Hermitian or b_vector is invalid.
- braket.experimental.algorithms.hhl.hhl.run_hhl(circuit: Circuit, device: Device, shots: int = 1000) QuantumTask[source]
Run the HHL circuit on the specified device.
- Parameters:
circuit (Circuit) – The HHL circuit to run.
device (Device) – Braket device backend.
shots (int) – Number of measurement shots (default: 1000).
- Returns:
QuantumTask – Task from running HHL.
- braket.experimental.algorithms.hhl.hhl.get_hhl_results(task: QuantumTask, matrix: ndarray, b_vector: ndarray, num_clock_qubits: int = 2, verbose: bool = False) Dict[str, Any][source]
Post-process results from an HHL run.
Extracts the solution state by post-selecting on the ancilla qubit measuring |1>. The solution |x> is proportional to A^{-1}|b>.
- Parameters:
task (QuantumTask) – The task containing HHL results.
matrix (np.ndarray) – The original 2x2 matrix A.
b_vector (np.ndarray) – The original vector b.
num_clock_qubits (int) – Number of clock qubits used (default: 2).
verbose (bool) – If True, prints detailed results (default: False).
- Returns:
Dict[str, Any] –
- Dictionary containing:
measurement_counts: Raw measurement counts
post_selected_counts: Counts post-selected on ancilla=1
solution_state_probabilities: Probabilities of solution components
classical_solution: The exact classical solution for comparison
fidelity: Fidelity between quantum and classical solutions
success_probability: Probability of ancilla measuring |1>