class HHL(matrix, vector, truncate_powerdim=False, truncate_hermitian=False, eigs=None, init_state=None, reciprocal=None, num_q=0, num_a=0, orig_size=None, quantum_instance=None)[source]

The HHL algorithm.

The HHL algorithm (after the author’s surnames Harrow-Hassidim-Lloyd) is a quantum algorithm to solve systems of linear equations \(A\overrightarrow{x}=\overrightarrow{b}\). Using Quantum Phase Estimation, the linear system is transformed into diagonal form in which the matrix \(A\) is easily invertible. The inversion is achieved by rotating an ancillary qubit by an angle \(\arcsin{ \frac{C}{\lambda_\mathrm{i}}}\) around the y-axis where \(\lambda_\mathrm{i}\) are the eigenvalues of \(A\). After uncomputing the register storing the eigenvalues using the inverse QPE, one measures the ancillary qubit. A measurement of 1 indicates that the matrix inversion succeeded. This leaves the system in a state proportional to the solution vector \(|x\rangle\). In many cases one is not interested in the single vector elements of \(|x\rangle\) but only on certain properties. These are accessible by using problem-specific operators. Another use-case is the implementation in a larger quantum program.

When using non-hermitian matrices and matrices with dimensions other than \(2^{n}\) the must be converted to an hermitian matrix and next higher dimension \(2^{n}\), respectively. The truncate_hermitian, truncate_powerdim flags and orig_size are used to indicate conversion and the returned result of the HHL algorithm for expanded matrices will be truncated. The matrix_resize() method is provided for convenience to do this but any method of your choice can be used.

To further explain truncate_hermitian indicates whether or not to truncate matrix and result vector to half the dimension by simply cutting off entries with other indices after the input matrix was expanded to be hermitian following

\[\begin{split}\begin{pmatrix} 0 & A^\mathsf{H}\\ A & 0 \end{pmatrix}\end{split}\]

where the conjugate transpose of matrix \(A\) is denoted by \(A^\mathsf{H}\). The truncation of the result vector is done by simply cutting off entries of the upper half.

truncate_powerdim indicates whether to truncate matrix and result vector from dimension \(2^{n}\) to dimension given by orig_size by simply cutting off entries with larger indices.

Running the algorithm will execute the circuit and return the result vector, measured (real hardware backend) or derived (qasm_simulator) via state tomography or calculated from the statevector (statevector_simulator).

See also https://arxiv.org/abs/0811.3171

  • matrix (ndarray) – The input matrix of linear system of equations

  • vector (ndarray) – The input vector of linear system of equations

  • truncate_powerdim (bool) – Flag indicating expansion to 2**n matrix to be truncated

  • truncate_hermitian (bool) – Flag indicating expansion to hermitian matrix to be truncated

  • eigs (Optional[Eigenvalues]) – The eigenvalue estimation instance

  • init_state (Optional[InitialState]) – The initial quantum state preparation

  • reciprocal (Optional[Reciprocal]) – The eigenvalue reciprocal and controlled rotation instance

  • num_q (int) – Number of qubits required for the matrix Operator instance

  • num_a (int) – Number of ancillary qubits for Eigenvalues instance

  • orig_size (Optional[int]) – The original dimension of the problem (if truncate_powerdim)

  • quantum_instance (Union[QuantumInstance, Backend, BaseBackend, None]) – Quantum Instance or Backend


ValueError – Invalid input



Returns backend.


Returns quantum instance.


Return a numpy random.



Construct the HHL circuit.

HHL.expand_to_hermitian(matrix, vector)

Expand a non-hermitian matrix A to a hermitian matrix by [[0, A.H], [A, 0]] and expand vector b to [b.conj, b].

HHL.expand_to_powerdim(matrix, vector)

Expand a matrix to the next-larger 2**n dimensional matrix with ones on the diagonal and zeros on the off-diagonal and expand the vector with zeros accordingly.

HHL.matrix_resize(matrix, vector)

Resizes matrix if necessary


Execute the algorithm with selected backend.

HHL.set_backend(backend, **kwargs)

Sets backend with configuration.