0
$\begingroup$

In the code below, I am using VQE without noise to calculate the minimum expectation value of the two-qubit operator X^X. I get the correct result when I use statevector_simulator but do not get the correct result most of the time with qasm_simulator. Please help me understand why that is the case.

import numpy as np
import pylab

from qiskit import Aer
from qiskit.algorithms import VQE, NumPyMinimumEigensolver
from qiskit.opflow import I, X, Y, Z
from qiskit import QuantumCircuit
from qiskit.circuit import Parameter

ansatz = QuantumCircuit(2)
ansatz.x(0)
α = Parameter(f'α0')
ansatz.ry(-2.0*α, 1)
ansatz.cx(1, 0)

op = 1.0 * (X^X)
npme = NumPyMinimumEigensolver()
result = npme.compute_minimum_eigenvalue(operator=op)
ref_value = result.eigenvalue.real
print(f'Reference value: {ref_value:.5f}')


counts = []
values = []


def store_intermediate_result(eval_count, parameters, mean, std):
    counts.append(eval_count)
    values.append(mean)
    
backend = Aer.get_backend("qasm_simulator")

statevector_simulator = Aer.get_backend("statevector_simulator")
qasm_simulator = Aer.get_backend("qasm_simulator")

vqe = VQE(ansatz=ansatz, callback=store_intermediate_result, quantum_instance=qasm_simulator)
result = vqe.compute_minimum_eigenvalue(operator=op)

print(f"VQE on Aer qasm simulator (no noise): {result.eigenvalue.real:.5f}")
print(
    f"Delta from reference energy value is {(result.eigenvalue.real - ref_value):.5f}"
)

pylab.rcParams["figure.figsize"] = (12, 4)
pylab.plot(counts, values)
pylab.xlabel("Eval count")
pylab.ylabel("Energy")
pylab.title("Convergence with no noise")</code>

enter image description here

$\endgroup$

1 Answer 1

1
$\begingroup$

VQE, by default, since you are not creating and passing an optimizer, internally creates/uses an instance of SLSQP. Given the ideal outcome from the statevector simulator that optimizer works fine. It finds a minimum using finite diff gradient based on a very small epsilon value change to the point to compute the gradient. This will not work well for qasm_simulator where the output is based on shots and can differs each time - has sampling noise. Try using an instance of SPSA optimizer, which was designed to work in noise, or perhaps COBYLA which uses a different technique.

I will note that using the quantum instance based VQE is deprecated. There algorithms were all changed to accept primitives i.e. Estimator and Sampler, where Estimator is needed for VQE. This tutorial shows VQE with an Aer Estimator using SPSA https://qiskit.org/documentation/tutorials/algorithms/03_vqe_simulation_with_noise.html It is possible to configure an Aer Estimator to give an outcome like you would get from using statevector - in this case you would use approximation=True and shots=None when configuring the Estimator.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.