I am running the code, which gets the ground state energy with different atomic distances of LiH. However, I encountered this error. The code is as follow
import numpy as np import pylab import copy
from qiskit import BasicAer
from qiskit.aqua import QuantumInstance
from qiskit.aqua.algorithms import NumPyMinimumEigensolver, VQE
from qiskit.aqua.components.optimizers import SLSQP
from qiskit.chemistry.components.initial_states import HartreeFock
from qiskit.chemistry.components.variational_forms import UCCSD
from qiskit.chemistry.drivers import PySCFDriver
from qiskit.chemistry.algorithms.ground_state_solvers import GroundStateEigensolver
from qiskit.chemistry.algorithms.ground_state_solvers.minimum_eigensolver_factories import VQEUCCSDFactory
from qiskit.chemistry.transformations import (FermionicTransformation,
FermionicTransformationType,
FermionicQubitMappingType)
from qiskit.chemistry.algorithms.ground_state_solvers.minimum_eigensolver_factories import VQEUCCSDFactory
molecule = 'H .0 .0 -{0}; Li .0 .0 {0}'
distances = np.arange(0.5,4.25,0.25)
vqe_energies = []
hf_energies = []
exact_energies = []
for i,d in enumerate(distances):
print('step', i)
#set up the experiment
driver = PySCFDriver(molecule.format(d/2), basis='sto3g')
fermionic_transformation = FermionicTransformation(
transformation=FermionicTransformationType.FULL,
qubit_mapping=FermionicQubitMappingType.JORDAN_WIGNER,
two_qubit_reduction=False,
freeze_core=False)
qubit_op, aux_ops = fermionic_transformation.transform(driver)
#VQE
optimizer = SLSQP(maxiter=1000)
initial_state = HartreeFock(fermionic_transformation.molecule_info['num_orbitals'],
fermionic_transformation.molecule_info['num_particles'],
qubit_mapping=fermionic_transformation.qubit_mapping,
two_qubit_reduction=fermionic_transformation._two_qubit_reduction)
#VQE
vqe_solver = VQEUCCSDFactory(QuantumInstance(BasicAer.get_backend('statevector_simulator')),
optimizer,
initial_state)
calc = GroundStateEigensolver(fermionic_transformation, vqe_solver)
res = calc.solve(driver)
print(res)
The traceback is here
File "/home/ironmanaudi/research/VQE/VQE_tutorial/LiH_VQE.py", line 76, in <module>
res = calc.solve(driver)
File "/home/ironmanaudi/anaconda3/lib/python3.8/site-packages/qiskit/chemistry/algorithms/ground_state_solvers/ground_state_eigensolver.py", line 102, in solve
raw_mes_result = solver.compute_minimum_eigenvalue(operator, aux_operators)
File "/home/ironmanaudi/anaconda3/lib/python3.8/site-packages/qiskit/aqua/algorithms/minimum_eigen_solvers/vqe.py", line 494, in compute_minimum_eigenvalue
return self._run()
File "/home/ironmanaudi/anaconda3/lib/python3.8/site-packages/qiskit/aqua/algorithms/minimum_eigen_solvers/vqe.py", line 429, in _run
vqresult = self.find_minimum(initial_point=self.initial_point,
File "/home/ironmanaudi/anaconda3/lib/python3.8/site-packages/qiskit/aqua/algorithms/vq_algorithm.py", line 172, in find_minimum
if initial_point is not None and len(initial_point) != nparms:
TypeError: object of type 'HartreeFock' has no len()
When change the syntax of getting the result to
res = calc.solve(driver, aux_ops)
the error becomes
File "/home/ironmanaudi/research/VQE/VQE_tutorial/LiH_VQE.py", line 70, in <module>
res = calc.solve(driver, aux_ops)
File "/home/ironmanaudi/anaconda3/lib/python3.8/site-packages/qiskit/chemistry/algorithms/ground_state_solvers/ground_state_eigensolver.py", line 90, in solve
operator, aux_operators = self.transformation.transform(driver, aux_operators)
File "/home/ironmanaudi/anaconda3/lib/python3.8/site-packages/qiskit/chemistry/transformations/fermionic_transformation.py", line 152, in transform
ops, aux_ops = self._do_transform(q_molecule, aux_operators)
File "/home/ironmanaudi/anaconda3/lib/python3.8/site-packages/qiskit/chemistry/transformations/fermionic_transformation.py", line 358, in _do_transform
_add_aux_op(aux_op, name)
File "/home/ironmanaudi/anaconda3/lib/python3.8/site-packages/qiskit/chemistry/transformations/fermionic_transformation.py", line 289, in _add_aux_op
aux_qop = FermionicTransformation._map_fermionic_operator_to_qubit(
File "/home/ironmanaudi/anaconda3/lib/python3.8/site-packages/qiskit/chemistry/transformations/fermionic_transformation.py", line 645, in _map_fermionic_operator_to_qubit
qubit_op = fer_op.mapping(map_type=qubit_mapping, threshold=0.00000001)
AttributeError: 'SummedOp' object has no attribute 'mapping'
Can anybody tell me how to work this out?