TransWikia.com

error when running VQE in Qiskit

Quantum Computing Asked on June 13, 2021

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?

3 Answers

Again here you are mixing some things. The initial_state in the VQE (that again you assign in the initial_points) is the starting values of the variational parameters. You have already specified the initial_state in the var_form. Then you should provide a solver in the calc and not run the solver manually. The code that works is here:

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, UnitsType, Molecule
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


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
    molecule = Molecule(geometry=[['H', [0., 0., -1*(d/2)]],
                              ['H', [0., 0., (d/2)]]],
                     charge=0, multiplicity=1)
    driver = PySCFDriver(molecule = molecule, unit=UnitsType.ANGSTROM, 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=1)
    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)
    var_form = UCCSD(num_orbitals=fermionic_transformation.molecule_info['num_orbitals'],
                              num_particles=fermionic_transformation.molecule_info['num_particles'],
                              initial_state=initial_state,
                              qubit_mapping=fermionic_transformation.qubit_mapping,
                              two_qubit_reduction=fermionic_transformation._two_qubit_reduction)
    
    vqe_solver = VQE(qubit_op,var_form,optimizer, quantum_instance=QuantumInstance(BasicAer.get_backend('statevector_simulator')))
    
    calc = GroundStateEigensolver(fermionic_transformation, vqe_solver)
    
    res = calc.solve(driver)
    
    vqe_energies.append(res.computed_energies[0])

I also removed the double definition of the driver. Maybe consider the tutorials here: Ground_state_solver

Correct answer by Panagiotis Barkoutsos on June 13, 2021

Seems like you are mixing a couple of things here. The VQEUCCSDFactory uses the HartreeFock state as an initial_state by default. See also here VQEUCCSDFactory. The initial_point that you are specifying is the initial values of the variational parameters to start the optimization from.

One more point, seeing you snippet seems like you want to calculate a PES and you may want to take a look also to our BOPES Sampler tutorials here BOPES tutorial .

Hope this helps.

Answered by Panagiotis Barkoutsos on June 13, 2021

The first method is as the following

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, UnitsType, Molecule
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


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
    molecule = Molecule(geometry=[['H', [0., 0., -1*(d/2)]],
                              ['H', [0., 0., (d/2)]]],
                     charge=0, multiplicity=1)
    driver = PySCFDriver(molecule = molecule, unit=UnitsType.ANGSTROM, 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)
    driver = PySCFDriver(molecule = molecule, unit=UnitsType.ANGSTROM, basis='sto3g')

    #VQE
    optimizer = SLSQP(maxiter=1000)
    vqe_solver = VQEUCCSDFactory(QuantumInstance(BasicAer.get_backend('statevector_simulator')), optimizer)
    
    calc = GroundStateEigensolver(fermionic_transformation, vqe_solver)
    res = calc.solve(driver)
    vqe_energies.append(res.computed_energies[0])

The second method :

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, UnitsType, Molecule
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


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
    molecule = Molecule(geometry=[['H', [0., 0., -1*(d/2)]],
                              ['H', [0., 0., (d/2)]]],
                     charge=0, multiplicity=1)
    driver = PySCFDriver(molecule = molecule, unit=UnitsType.ANGSTROM, 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)
    driver = PySCFDriver(molecule = molecule, unit=UnitsType.ANGSTROM, basis='sto3g')


    vqe_solver = VQEUCCSDFactory(QuantumInstance(BasicAer.get_backend('statevector_simulator')))
    
    #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)
    var_form = UCCSD(num_orbitals=fermionic_transformation.molecule_info['num_orbitals'],
                              num_particles=fermionic_transformation.molecule_info['num_particles'],
                              initial_state=initial_state,
                              qubit_mapping=fermionic_transformation.qubit_mapping,
                              two_qubit_reduction=fermionic_transformation._two_qubit_reduction)
    vqe_solver = VQE(qubit_op,var_form,optimizer,initial_state)
    vqe_solver = vqe_solver.run(QuantumInstance(BasicAer.get_backend('statevector_simulator')))
    
    
    
    calc = GroundStateEigensolver(fermionic_transformation, vqe_solver)
    res = calc.solve(driver)
    vqe_energies.append(res.computed_energies[0])
```

Answered by ironmanaudi on June 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP