Skip to main contentIBM Quantum Documentation
You are viewing the API reference for an old version of Qiskit SDK. Switch to latest version

HHL

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)

GitHub(opens in a new tab)

The HHL algorithm.

The HHL algorithm (after the author’s surnames Harrow-Hassidim-Lloyd) is a quantum algorithm to solve systems of linear equations Ax=bA\overrightarrow{x}=\overrightarrow{b}. Using Quantum Phase Estimation, the linear system is transformed into diagonal form in which the matrix AA is easily invertible. The inversion is achieved by rotating an ancillary qubit by an angle arcsinCλi\arcsin{ \frac{C}{\lambda_\mathrm{i}}} around the y-axis where λi\lambda_\mathrm{i} are the eigenvalues of AA. 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|x\rangle. In many cases one is not interested in the single vector elements of x|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 2n2^{n} the must be converted to an hermitian matrix and next higher dimension 2n2^{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

(0AHA0)\begin{split}\begin{pmatrix} 0 & A^\mathsf{H}\\ A & 0 \end{pmatrix}\end{split}

where the conjugate transpose of matrix AA is denoted by AHA^\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 2n2^{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(opens in a new tab)

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, BaseBackend, None]) – Quantum Instance or Backend

Raises

ValueError – Invalid input


Attributes

backend

qiskit.providers.basebackend.BaseBackend

Returns backend.

Return type

BaseBackend

quantum_instance

Union[None, qiskit.aqua.quantum_instance.QuantumInstance]

Returns quantum instance.

Return type

Optional[QuantumInstance]

random

Return a numpy random.


Methods

construct_circuit

HHL.construct_circuit(measurement=False)

Construct the HHL circuit.

Parameters

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

Returns

the QuantumCircuit object for the constructed circuit

Return type

QuantumCircuit

expand_to_hermitian

static 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].

Parameters

  • matrix (np.array) – the input matrix
  • vector (np.array) – the input vector

Returns

the expanded matrix, the expanded vector

Return type

tuple(np.array, np.array)

expand_to_powerdim

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

Parameters

  • matrix (np.array) – the input matrix
  • vector (np.array) – the input vector

Returns

the expanded matrix, the expanded vector

Return type

tuple(np.array, np.array)

matrix_resize

static HHL.matrix_resize(matrix, vector)

Resizes matrix if necessary

Parameters

  • matrix (np.array) – the input matrix of linear system of equations
  • vector (np.array) – the input vector of linear system of equations

Returns

new matrix, vector, truncate_powerdim, truncate_hermitian

Return type

tuple

Raises

ValueError – invalid input

run

HHL.run(quantum_instance=None, **kwargs)

Execute the algorithm with selected backend.

Parameters

Returns

results of an algorithm.

Return type

dict

Raises

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

set_backend

HHL.set_backend(backend, **kwargs)

Sets backend with configuration.

Return type

None

Was this page helpful?
Report a bug or request content on GitHub.