primitive job for computing gradients in AdaptVQE fails when using Aer's Estimator with approximation=True
See original GitHub issueEnvironment
- Qiskit Terra version: 0.22.2 (and Aer version 0.11.1)
- Python version: 3.9.5
- Operating system: MacOS/ Linux Mint
What is happening?
When using Aer’s implementation of Estimator
and setting approximation=True
, the primitive job for computing the commutator expectation values for the gradients in AdaptVQE fails. This estimator seems to work fine for regular VQE and also for AdaptVQE when approximation=True
is omitted, which tells me that the issue is specifically related to the AdaptVQE implementation of the gradient calculation.
How can we reproduce the issue?
The following program for calculating the ground state of diatomic hydrogen:
from qiskit.algorithms.minimum_eigensolvers import AdaptVQE, VQE
from qiskit_aer.primitives import Estimator
from qiskit_nature.problems.second_quantization import ElectronicStructureProblem
from qiskit_nature.converters.second_quantization import QubitConverter
from qiskit_nature.mappers.second_quantization import JordanWignerMapper
from qiskit_nature.circuit.library import UCCSD, HartreeFock
from qiskit.algorithms.optimizers import COBYLA
import numpy as np
basis = 'sto-3g'
interatomic_distance = 0.735
from qiskit_nature.drivers.second_quantization import ElectronicStructureMoleculeDriver, ElectronicStructureDriverType
from qiskit_nature.drivers import Molecule
molecule = Molecule(
geometry=[["H", [0.0, 0.0, 0.0]], ["H", [0.0, 0.0, interatomic_distance]]], charge=0, multiplicity=1
)
driver = ElectronicStructureMoleculeDriver(
molecule, basis=basis, driver_type=ElectronicStructureDriverType.PYSCF
)
q_molecule = driver.run()
qubit_converter = QubitConverter(JordanWignerMapper())
es_problem = ElectronicStructureProblem(driver=driver)
second_q_op = es_problem.second_q_ops()
qubit_op = qubit_converter.convert(second_q_op[0])
num_qubits = es_problem.num_spin_orbitals
HF_state = HartreeFock(num_spin_orbitals=es_problem.num_spin_orbitals,
num_particles=es_problem.num_particles,
qubit_converter=qubit_converter)
ansatz = UCCSD(qubit_converter=qubit_converter,
num_particles=es_problem.num_particles,
num_spin_orbitals=es_problem.num_spin_orbitals,
initial_state=HF_state)
VQE_instance = VQE(ansatz=ansatz,
optimizer=COBYLA(),
estimator=Estimator(approximation=True),
initial_point=np.zeros(ansatz.num_parameters),
callback=None)
AdaptVQE_instance = AdaptVQE(VQE_instance)
result = AdaptVQE_instance.compute_minimum_eigenvalue(qubit_op)
print(result)
print(result.optimal_circuit.decompose())
AdaptVQE_instance.ansatz = result.optimal_circuit
print(AdaptVQE_instance.ansatz.decompose())
results in the following traceback:
Traceback (most recent call last):
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit-0392-esm-010-nature-045/lib/python3.9/site-packages/qiskit/algorithms/observables_evaluator.py", line 70, in estimate_observables
expectation_values = estimator_job.result().values
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit-0392-esm-010-nature-045/lib/python3.9/site-packages/qiskit/primitives/primitive_job.py", line 50, in result
return self._future.result()
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit-0392-esm-010-nature-045/lib/python3.9/concurrent/futures/_base.py", line 438, in result
return self.__get_result()
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit-0392-esm-010-nature-045/lib/python3.9/concurrent/futures/_base.py", line 390, in __get_result
raise self._exception
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit-0392-esm-010-nature-045/lib/python3.9/concurrent/futures/thread.py", line 52, in run
result = self.fn(*self.args, **self.kwargs)
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit-0392-esm-010-nature-045/lib/python3.9/site-packages/qiskit_aer/primitives/estimator.py", line 135, in _call
return self._compute_with_approximation(
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit-0392-esm-010-nature-045/lib/python3.9/site-packages/qiskit_aer/primitives/estimator.py", line 292, in _compute_with_approximation
circuit.save_expectation_value(observable, self._layouts[i])
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit-0392-esm-010-nature-045/lib/python3.9/site-packages/qiskit_aer/library/save_instructions/save_expectation_value.py", line 191, in save_expectation_value
instr = SaveExpectationValue(operator,
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit-0392-esm-010-nature-045/lib/python3.9/site-packages/qiskit_aer/library/save_instructions/save_expectation_value.py", line 64, in __init__
raise ExtensionError("Input operator is not Hermitian.")
qiskit.extensions.exceptions.ExtensionError: 'Input operator is not Hermitian.'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/joelbierman/Desktop/electronic-structure-methods/test/adaptVQE/H2_AdaptVQE_bug.py", line 46, in <module>
result = AdaptVQE_instance.compute_minimum_eigenvalue(qubit_op)
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit-0392-esm-010-nature-045/lib/python3.9/site-packages/qiskit/algorithms/minimum_eigensolvers/adapt_vqe.py", line 215, in compute_minimum_eigenvalue
cur_grads = self._compute_gradients(theta, operator)
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit-0392-esm-010-nature-045/lib/python3.9/site-packages/qiskit/algorithms/minimum_eigensolvers/adapt_vqe.py", line 146, in _compute_gradients
res = estimate_observables(self.solver.estimator, self.solver.ansatz, commutators, theta)
File "/Users/joelbierman/opt/anaconda3/envs/Qiskit-0392-esm-010-nature-045/lib/python3.9/site-packages/qiskit/algorithms/observables_evaluator.py", line 72, in estimate_observables
raise AlgorithmError("The primitive job failed!") from exc
qiskit.algorithms.exceptions.AlgorithmError: 'The primitive job failed!'
What should happen?
The algorithm completes without primitive job failure. Aer seems to be flagging these commutator operators as non-Hermitian, which is a bit odd considering that using Aer’s Estimator implementation without setting approximation=True
works just fine. The hermiticity should be independent of this argument.
Any suggestions?
No response
Issue Analytics
- State:
- Created 10 months ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
If it is an Aer issue it can be transferred over there. Maybe let @ikkoham make a determination since he is working with the Aer primitives too and we can go from there.
I guess for now, if you wanted to run noiseless
AdaptVQE
simulations using Aer, the thing to do would be to setapproximation=True
, but make the number of shots really large to make the variance effectively zero since it is just sampling from a gaussian and not actually running the circuit shots individually as whenApproximation=False
. The performance does not seem to get much worse if for instance I setshots=10**30