Qiskit / qiskit-metapackage

Qiskit is an open-source SDK for working with quantum computers at the level of circuits, algorithms, and application modules.
https://qiskit.org
Apache License 2.0
3.03k stars 751 forks source link

VQE on two qubit Heisenberg model silently fails and outputs an energy lower than the grounstate energy #1587

Closed tomohiro-soejima closed 1 year ago

tomohiro-soejima commented 2 years ago

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?

ottoeretnaesehorn commented 1 year ago

I would like to investigate this problem?

jakelishman commented 1 year ago

This appears to have been resolved via Qiskit/qiskit-aer#1595. Please re-open an issue on either Aer or qiskit_algorithms if there are still problems.

tomohiro-soejima commented 1 year ago

Thank you!