Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VQLS behaves badly under BackendEstimator #16

Open
MrEightL opened this issue May 25, 2024 · 0 comments
Open

VQLS behaves badly under BackendEstimator #16

MrEightL opened this issue May 25, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@MrEightL
Copy link

MrEightL commented May 25, 2024

Steps to reproduce the problem

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:
image
image
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.

@MrEightL MrEightL added the bug Something isn't working label May 25, 2024
@MrEightL MrEightL changed the title VQLS behaves badly under AerEstimator & BackendEstimator VQLS behaves badly under BackendEstimator May 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant