Qiskit / qiskit-aer

Aer is a high performance simulator for quantum circuits that includes noise models
https://qiskit.github.io/qiskit-aer/
Apache License 2.0
472 stars 354 forks source link

VQE on two qubit Heisenberg model silently fails and outputs an energy lower than the ground state energy #1595

Closed tomohiro-soejima closed 1 year ago

tomohiro-soejima commented 1 year ago

This is the same as https://github.com/Qiskit/qiskit/issues/1587, but I realized qiskit-aer might be the right place to post it instead of qiskit. Please feel free to close either of these.

Informations

What is the current behavior?

When running VQE for the two spin Heisenberg model with the Hamiltonian H = -(X^X) - (Y^Y) - (Z^Z), whose ground state energy is -1. However, when I use a custom ansatz and run VQE with density_matrix backend, VQE reports an energy of -3, which is lower than the ground state energy.

I attach the circuit diagram of the ansatz below: image

Note that this is a rather pathological ansatz, in that the final state is independent of the parameter theta. Perhaps that is a cause of the problem?

Steps to reproduce the problem

Here is the MWE to reproduce the problem:

from inspect import Parameter
from qiskit import Aer
from qiskit.utils import QuantumInstance, algorithm_globals
from qiskit.algorithms import VQE, NumPyMinimumEigensolver
from qiskit.algorithms.optimizers import SPSA
from qiskit.opflow import I, X, Y, Z
from qiskit import QuantumCircuit
from qiskit.circuit import Parameter

def run_VQE(backend_str, operator, ansatz, ref_value):
    backend = Aer.get_backend(backend_str)
    seed = 175
    algorithm_globals.random_seed = seed
    qi = QuantumInstance(backend=backend, seed_simulator=seed)

    iterations = 10

    spsa = SPSA(maxiter=iterations)
    vqe = VQE(ansatz, optimizer=spsa, quantum_instance=qi)
    result1 = vqe.compute_minimum_eigenvalue(operator=operator)
    print(f'VQE on {backend_str}: {result1.eigenvalue.real:.5f}')
    print(f'Delta from reference energy value is {(result1.eigenvalue.real - ref_value):.5f}')

operator = -(X^X)  -(Y^Y) -(Z^Z)
npme = NumPyMinimumEigensolver()
# computes the reference eigenvalue from exact diagonalization
result = npme.compute_minimum_eigenvalue(operator=operator)
ref_value = result.eigenvalue.real
print(f'Reference value: {ref_value:.5f}')
qc = QuantumCircuit(2)
theta = Parameter(r"$\theta$")
qc.rzz(theta, 0, 1)
run_VQE("aer_simulator_density_matrix", operator, qc, ref_value)

# For comparison, we can run state vector simulation
# run_VQE("statevector_simulator", operator, qc, ref_value)

This prints out the following:

VQE on aer_simulator_density_matrix: -3.00000
Delta from reference energy value is -2.00000

What is the expected behavior?

VQE finds the energy of -1, which is the energy of the state 00.

Suggested solutions

If we uncomment the final line of the MWE (statevector_simulator computation) and run it, it prints the following result and runtime warning messages:

VQE on statevector_simulator: nan
Delta from reference energy value is nan
/home/tomohiro/anaconda3/envs/large-vqe/lib/python3.8/site-packages/qiskit/algorithms/optimizers/spsa.py:345: RuntimeWarning: divide by zero encountered in double_scalars
  a = target_magnitude / avg_magnitudes
/home/tomohiro/anaconda3/envs/large-vqe/lib/python3.8/site-packages/qiskit/algorithms/optimizers/spsa.py:565: RuntimeWarning: invalid value encountered in multiply
  update = update * next(eta)

This seems to signal there is a divide by zero error lurking around. Perhaps what is happening with the density_matrix calculation is some kind of silent divide by zero error?

hhorii commented 1 year ago

I'm sorry that my response is very lazy.

With statevector_simulator or BasicAer.get_backend("qasm_simulator"), the above program does not work correctly with the current qiskit (though behavior of aer_simulator_density_matrix looks unexpected). In addition, operator flow is now deprecated. Please create a new issue if we encounter the same issue with the latest API.

tomohiro-soejima commented 1 year ago

Thank you for your response!