Shortcuts

# qiskit.aqua.algorithms.HHL¶

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).

Parameters
• 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

Raises

ValueError – Invalid input

__init__(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]
Parameters
• 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

Raises

ValueError – Invalid input

Methods

 __init__(matrix, vector[, …]) type matrix ndarray construct_circuit([measurement]) Construct the HHL circuit. 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]. 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. matrix_resize(matrix, vector) Resizes matrix if necessary run([quantum_instance]) Execute the algorithm with selected backend. set_backend(backend, **kwargs) Sets backend with configuration.

Attributes

 backend Returns backend. quantum_instance Returns quantum instance. random Return a numpy random.
property backend

Returns backend.

Return type

Union[Backend, BaseBackend]

construct_circuit(measurement=False)[source]

Construct the HHL circuit.

Parameters

measurement (bool) – indicate whether measurement on ancillary qubit should be performed

Return type

QuantumCircuit

Returns

the QuantumCircuit object for the constructed circuit

static expand_to_hermitian(matrix, vector)[source]

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

Parameters
• matrix (ndarray) – the input matrix

• vector (ndarray) – the input vector

Return type

Tuple[ndarray, ndarray]

Returns

the expanded matrix, the expanded vector

static expand_to_powerdim(matrix, vector)[source]

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.

Parameters
• matrix (ndarray) – the input matrix

• vector (ndarray) – the input vector

Return type

Tuple[ndarray, ndarray]

Returns

the expanded matrix, the expanded vector

static matrix_resize(matrix, vector)[source]

Resizes matrix if necessary

Parameters
• matrix (ndarray) – the input matrix of linear system of equations

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

Return type

Tuple[ndarray, ndarray, bool, bool]

Returns

new matrix, vector, truncate_powerdim, truncate_hermitian

Raises

ValueError – invalid input

property quantum_instance

Returns quantum instance.

Return type

Optional[QuantumInstance]

property random

Return a numpy random.

run(quantum_instance=None, **kwargs)

Execute the algorithm with selected backend.

Parameters
• quantum_instance (Union[QuantumInstance, Backend, BaseBackend, None]) – the experimental setting.

• kwargs (dict) – kwargs

Returns

results of an algorithm.

Return type

dict

Raises

AquaError – If a quantum instance or backend has not been provided

set_backend(backend, **kwargs)

Sets backend with configuration.

Return type

None