234 lines
12 KiB
Plaintext
234 lines
12 KiB
Plaintext
---
|
||
title: HHL
|
||
description: API reference for qiskit.aqua.algorithms.HHL
|
||
in_page_toc_min_heading_level: 1
|
||
python_api_type: class
|
||
python_api_name: qiskit.aqua.algorithms.HHL
|
||
---
|
||
|
||
# qiskit.aqua.algorithms.HHL
|
||
|
||
<Class id="qiskit.aqua.algorithms.HHL" isDedicatedPage={true} github="https://github.com/qiskit-community/qiskit-aqua/tree/stable/0.9/qiskit/aqua/algorithms/linear_solvers/hhl.py" signature="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)" modifiers="class">
|
||
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()`](#qiskit.aqua.algorithms.HHL.matrix_resize "qiskit.aqua.algorithms.HHL.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](https://arxiv.org/abs/0811.3171)
|
||
|
||
**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** (`Union`\[`QuantumCircuit`, `InitialState`, `None`]) – 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\_\_
|
||
|
||
<Function id="qiskit.aqua.algorithms.HHL.__init__" signature="__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)">
|
||
**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** (`Union`\[`QuantumCircuit`, `InitialState`, `None`]) – 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
|
||
</Function>
|
||
|
||
## Methods
|
||
|
||
| | |
|
||
| ----------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||
| [`__init__`](#qiskit.aqua.algorithms.HHL.__init__ "qiskit.aqua.algorithms.HHL.__init__")(matrix, vector\[, …]) | **type matrix**`ndarray` |
|
||
| [`construct_circuit`](#qiskit.aqua.algorithms.HHL.construct_circuit "qiskit.aqua.algorithms.HHL.construct_circuit")(\[measurement]) | Construct the HHL circuit. |
|
||
| [`expand_to_hermitian`](#qiskit.aqua.algorithms.HHL.expand_to_hermitian "qiskit.aqua.algorithms.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]. |
|
||
| [`expand_to_powerdim`](#qiskit.aqua.algorithms.HHL.expand_to_powerdim "qiskit.aqua.algorithms.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. |
|
||
| [`matrix_resize`](#qiskit.aqua.algorithms.HHL.matrix_resize "qiskit.aqua.algorithms.HHL.matrix_resize")(matrix, vector) | Resizes matrix if necessary |
|
||
| [`run`](#qiskit.aqua.algorithms.HHL.run "qiskit.aqua.algorithms.HHL.run")(\[quantum\_instance]) | Execute the algorithm with selected backend. |
|
||
| [`set_backend`](#qiskit.aqua.algorithms.HHL.set_backend "qiskit.aqua.algorithms.HHL.set_backend")(backend, \*\*kwargs) | Sets backend with configuration. |
|
||
|
||
## Attributes
|
||
|
||
| | |
|
||
| ---------------------------------------------------------------------------------------------------------------- | ------------------------- |
|
||
| [`backend`](#qiskit.aqua.algorithms.HHL.backend "qiskit.aqua.algorithms.HHL.backend") | Returns backend. |
|
||
| [`quantum_instance`](#qiskit.aqua.algorithms.HHL.quantum_instance "qiskit.aqua.algorithms.HHL.quantum_instance") | Returns quantum instance. |
|
||
| [`random`](#qiskit.aqua.algorithms.HHL.random "qiskit.aqua.algorithms.HHL.random") | Return a numpy random. |
|
||
|
||
### backend
|
||
|
||
<Attribute id="qiskit.aqua.algorithms.HHL.backend">
|
||
Returns backend.
|
||
|
||
**Return type**
|
||
|
||
`Union`\[`Backend`, `BaseBackend`]
|
||
</Attribute>
|
||
|
||
### construct\_circuit
|
||
|
||
<Function id="qiskit.aqua.algorithms.HHL.construct_circuit" signature="construct_circuit(measurement=False)">
|
||
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
|
||
</Function>
|
||
|
||
### expand\_to\_hermitian
|
||
|
||
<Function id="qiskit.aqua.algorithms.HHL.expand_to_hermitian" signature="expand_to_hermitian(matrix, vector)" modifiers="static">
|
||
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
|
||
</Function>
|
||
|
||
### expand\_to\_powerdim
|
||
|
||
<Function id="qiskit.aqua.algorithms.HHL.expand_to_powerdim" signature="expand_to_powerdim(matrix, vector)" modifiers="static">
|
||
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
|
||
</Function>
|
||
|
||
### matrix\_resize
|
||
|
||
<Function id="qiskit.aqua.algorithms.HHL.matrix_resize" signature="matrix_resize(matrix, vector)" modifiers="static">
|
||
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
|
||
</Function>
|
||
|
||
### quantum\_instance
|
||
|
||
<Attribute id="qiskit.aqua.algorithms.HHL.quantum_instance">
|
||
Returns quantum instance.
|
||
|
||
**Return type**
|
||
|
||
`Optional`\[`QuantumInstance`]
|
||
</Attribute>
|
||
|
||
### random
|
||
|
||
<Attribute id="qiskit.aqua.algorithms.HHL.random">
|
||
Return a numpy random.
|
||
</Attribute>
|
||
|
||
### run
|
||
|
||
<Function id="qiskit.aqua.algorithms.HHL.run" signature="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**](qiskit.aqua.AquaError "qiskit.aqua.AquaError") – If a quantum instance or backend has not been provided
|
||
</Function>
|
||
|
||
### set\_backend
|
||
|
||
<Function id="qiskit.aqua.algorithms.HHL.set_backend" signature="set_backend(backend, **kwargs)">
|
||
Sets backend with configuration.
|
||
|
||
**Return type**
|
||
|
||
`None`
|
||
</Function>
|
||
</Class>
|
||
|