I used the following code when solving linear equations with VQLS:
import numpy as np
from qiskit_aer import AerSimulator
from vqls_prototype import VQLS, VQLSLog
from qiskit.primitives import Estimator,Sampler, BackendEstimator
from qiskit_algorithms import optimizers as opt
from matplotlib import pyplot as plt
from qiskit.quantum_info import Statevector
from qiskit.circuit.library.n_local.real_amplitudes import RealAmplitudes
from qiskit_ibm_runtime import QiskitRuntimeService, Session, Options
# size of the system
size = 4
# matrix of the linear system
A = np.random.rand(size, size)
A = A + A.T
# right hand side of the linear system
b = np.random.rand(size)
aer_sim = AerSimulator()
backend = aer_sim
nqbit = int(np.log2(size))
ansatz = RealAmplitudes(nqbit, entanglement="full", reps=3, insert_barriers=False)
estimator=BackendEstimator(backend=backend)
# log
log = VQLSLog([], [])
# declare the solver
vqls = VQLS(estimator, ansatz, optimizer=opt.CG(maxiter=200))
vqls.options = vqls._validate_solve_options({"use_local_cost_function": True})
_, qc_test = vqls.construct_circuit(A, b)
qc_test[0].circuits[0].decompose().draw("mpl")
plt.show()
# solve the linear system
res = vqls.solve(A, b)
plt.semilogy(vqls.logger.values)
plt.ylabel("Cost Function")
plt.xlabel("Iterations")
plt.show()
vqls_solution = np.real(Statevector(res.state).data)
ref_solution = np.linalg.solve(A, b / np.linalg.norm(b))
ref_solution = ref_solution / np.linalg.norm(ref_solution)
print( ref_solution)
print( vqls_solution)
What is the current behavior?
In actual use, I obtained the following results:
From the first figure, it can be seen that the cost function does not converge. The second figure also shows that the final result of the VQLS algorithm is inconsistent with the classical solution.
What is the expected behavior?
Given that the AerEstimator is still noiseless, its results should be identical to those of the Estimator, and the quantum solution should also be consistent with the classical one.
In fact, the reason I replaced the Estimator in the routine with the BackendEstimator was to test whether the VQLS can still run effectively under a noisy simulator, in preparation for running on a real quantum computer.
Steps to reproduce the problem
I used the following code when solving linear equations with VQLS:
What is the current behavior?
In actual use, I obtained the following results:
From the first figure, it can be seen that the cost function does not converge. The second figure also shows that the final result of the VQLS algorithm is inconsistent with the classical solution.
What is the expected behavior?
Given that the AerEstimator is still noiseless, its results should be identical to those of the Estimator, and the quantum solution should also be consistent with the classical one. In fact, the reason I replaced the Estimator in the routine with the BackendEstimator was to test whether the VQLS can still run effectively under a noisy simulator, in preparation for running on a real quantum computer.