Variational Quantum Linear Solver
See https://arxiv.org/abs/1909.05820
VQLSLog(values: List, parameters: List)
update(count, cost, parameters)
Systems of linear equations arise naturally in many real-life applications in a wide range of areas, such as in the solution of Partial Differential Equations, the calibration of financial models, fluid simulation or numerical field calculation. The problem can be defined as, given a matrix :math:A\in\mathbb{C}^{N\times N}
and a vector :math:\vec{b}\in\mathbb{C}^{N}
, find :math:\vec{x}\in\mathbb{C}^{N}
satisfying :math:A\vec{x}=\vec{b}
.
from qalcore.qiskit.vqls.vqls import VQLS, VQLSLog
from qiskit.circuit.library.n_local.real_amplitudes import RealAmplitudes
from qiskit_algorithms import optimizers as opt
from qiskit_aer import Aer, BasicAer
import numpy as np
from qiskit.quantum_info import Statevector
import matplotlib.pyplot as plt f
rom qiskit.primitives import Estimator, Sampler, BackendEstimator
# create random symmetric matrix
A = np.random.rand(4, 4) A = A + A.T
# create rhight hand side
b = np.random.rand(4)
# solve using numpy
classical_solution = np.linalg.solve(A, b / np.linalg.norm(b))
ref_solution = classical_solution / np.linalg.norm(classical_solution)
# define the wave function ansatz
ansatz = RealAmplitudes(2, entanglement="full", reps=3, insert_barriers=False)
# define an estimator primitive
estimator = Estimator()
# define the logger
log = VQLSLog([],[])
# create the solver
vqls = VQLS( estimator, ansatz, opt.CG(maxiter=200), callback=log.update )
# solve
res = vqls.solve(A, b, opt) vqls_solution = np.real(Statevector(res.state).data)
# plot solution
plt.scatter(ref_solution, vqls_solution) plt.plot([-1, 1], [-1, 1], "--") plt.show()
# plot cost function
plt.plot(log.values) plt.ylabel('Cost Function') plt.xlabel('Iterations') plt.show()
References:
[1] Carlos Bravo-Prieto, Ryan LaRose, M. Cerezo, Yigit Subasi, Lukasz Cincio, Patrick J. Coles Variational Quantum Linear Solver arXiv:1909.05820 <https://arxiv.org/abs/1909.05820>
__init__(
estimator: BaseEstimator,
ansatz: QuantumCircuit,
optimizer: Union[Optimizer, Minimizer],
sampler: Optional[BaseSampler] = None,
initial_point: Optional[ndarray] = None,
gradient: Optional[GradientBase, Callable] = None,
max_evals_grouped: Optional[int] = 1,
callback: Optional[Callable[[int, ndarray, float, float]], NoneType] = None
) → None
Args:
estimator
: an Estimator primitive to compute the expected values of the quantum circuits needed for the cost functionansatz
: A parameterized circuit used as Ansatz for the wave function.optimizer
: A classical optimizer. Can either be a Qiskit optimizer or a callable that takes an array as input and returns a Qiskit or SciPy optimization result.sampler
: a Sampler primitive to sample the output of some quantum circuits needed to compute the cost function. This is only needed if overal Hadammard tests are used.initial_point
: An optional initial point (i.e. initial parameter values) for the optimizer. IfNone
then VQLS will look to the ansatz for a preferred point and if not will simply compute a random one.gradient
: An optional gradient function or operator for optimizer.max_evals_grouped
: Max number of evaluations performed simultaneously. Signals the given optimizer that more than one set of parameters can be supplied so that potentially the expectation values can be computed in parallel. Typically this is possible when a finite difference gradient is used by the optimizer such that multiple points to compute the gradient can be passed and if computed in parallel improve overall execution time. Deprecated if a gradient operator or function is given.callback
: a callback that can access the intermediate data during the optimization. Three parameter values are passed to the callback as follows during each evaluation by the optimizer for its current set of parameters as it works towards the minimum.These are
: the evaluation count, the cost and the optimizer parameters for the ansatz
Returns the ansatz.
Returns callback
Returns initial point
Returns max_evals_grouped
return the numner of classical bits
return the numner of qubits
Returns optimizer
construct_circuit(
matrix: Union[ndarray, QuantumCircuit, List],
vector: Union[ndarray, QuantumCircuit],
options: Dict
) → List[QuantumCircuit]
Returns the a list of circuits required to compute the expectation value
Args:
matrix
(Union[np.ndarray, QuantumCircuit, List]): matrix of the linear systemvector
(Union[np.ndarray, QuantumCircuit]): rhs of thge linear systemoptions
(Dict): Options to compute define the quantum circuits that compute the cost function
Raises:
ValueError
: if vector and matrix have different sizeValueError
: if vector and matrix have different numner of qubitsValueError
: the input matrix is not a numoy array nor a quantum circuit
Returns:
List[QuantumCircuit]
: Quantum Circuits required to compute the cost function
get_coefficient_matrix(coeffs) → ndarray
Compute all the vi* vj terms
Args:
coeffs
(np.ndarray): list of complex coefficients
get_cost_evaluation_function(
hdmr_tests_norm: List,
hdmr_tests_overlap: List,
coefficient_matrix: ndarray,
options: Dict
) → Callable[[ndarray], Union[float, List[float]]]
Generate the cost function of the minimazation process
Args:
hdmr_tests_norm
(List): list of quantum circuits needed to compute the normhdmr_tests_overlap
(List): list of quantum circuits needed to compute the normcoefficient_matrix
(np.ndarray): the matrix values of the c_n^* c_m coefficientsoptions
(Dict): Option to compute the cost function
Raises:
RuntimeError
: If the ansatz is not parametrizable
Returns:
Callable[[np.ndarray], Union[float, List[float]]]
: the cost function
solve(
matrix: Union[ndarray, QuantumCircuit, List[QuantumCircuit]],
vector: Union[ndarray, QuantumCircuit],
options: Optional[Dict] = None
) → VariationalLinearSolverResult
Solve the linear system
Args:
matrix
(Union[List, np.ndarray, QuantumCircuit]): matrix of the linear systemvector
(Union[np.ndarray, QuantumCircuit]): rhs of the linear systemoptions
(Union[Dict, None]): options for the calculation of the cost function
Returns:
VariationalLinearSolverResult
: Result of the optimization and solution vector of the linear system
This file was automatically generated via lazydocs.