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)
The HHL algorithm.
The HHL algorithm (after the author’s surnames Harrow-Hassidim-Lloyd) is a quantum algorithm to solve systems of linear equations . Using Quantum Phase Estimation, the linear system is transformed into diagonal form in which the matrix is easily invertible. The inversion is achieved by rotating an ancillary qubit by an angle around the y-axis where are the eigenvalues of . 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 . In many cases one is not interested in the single vector elements of 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 the must be converted to an hermitian matrix and next higher dimension , 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
where the conjugate transpose of matrix is denoted by . 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 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
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__
__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
Methods
__init__ (matrix, vector[, …]) | type matrixndarray |
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. |
backend
Returns backend.
Return type
Union
[Backend
, BaseBackend
]
construct_circuit
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
expand_to_hermitian
static 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 (
ndarray
) – the input matrix - vector (
ndarray
) – the input vector
Return type
Tuple
[ndarray
, ndarray
]
Returns
the expanded matrix, the expanded vector
expand_to_powerdim
static 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 (
ndarray
) – the input matrix - vector (
ndarray
) – the input vector
Return type
Tuple
[ndarray
, ndarray
]
Returns
the expanded matrix, the expanded vector
matrix_resize
static matrix_resize(matrix, vector)
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
quantum_instance
Returns quantum instance.
Return type
Optional
[QuantumInstance
]
random
Return a numpy random.
run
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
set_backend(backend, **kwargs)
Sets backend with configuration.
Return type
None