4
$\begingroup$

I'm trying to implement the same Jupyter notebook Jin-Sung Kim gave in his YouTube video The Variational Quantum Eigensolver — Programming on Quantum Computers — Coding with Qiskit S2E4; however, I'm facing the error as below:

Error:

step 0
/Users/bambrozi/.local/share/virtualenvs/ibm-quantum-challenge-2020-ZWRg31rS/lib/python3.8/site-packages/qiskit/chemistry/core/hamiltonian.py:91: DeprecationWarning: The ChemistryOperator is deprecated as of Qiskit Aqua 0.8.0 and will be removed no earlier than 3 months after the release date. Instead, the FermionicTransformation can be used to transform QMolecules and construct ground state result objects.
  super().__init__()
/Users/bambrozi/.local/share/virtualenvs/ibm-quantum-challenge-2020-ZWRg31rS/lib/python3.8/site-packages/qiskit/chemistry/core/hamiltonian.py:415: DeprecationWarning: Processing a dictionary result is deprecated, pass a (minimum) eigensolver result now.
  warnings.warn('Processing a dictionary result is deprecated,'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-66becf9098c9> in <module>
     13     # exact classical result
     14     exact_result = NumPyMinimumEigensolver(qubit_op, aux_operators=aux_ops)
---> 15     exact_result = operator.process_algorithm_result(exact_result)
     16 
     17     # VQE

~/.local/share/virtualenvs/ibm-quantum-challenge-2020-ZWRg31rS/lib/python3.8/site-packages/qiskit/chemistry/core/chemistry_operator.py in process_algorithm_result(self, algo_result)
     88             return self._process_algorithm_result(algo_result)
     89         else:
---> 90             lines, result = self._process_algorithm_result(algo_result)
     91             result['algorithm_retvals'] = algo_result
     92             return lines, result

~/.local/share/virtualenvs/ibm-quantum-challenge-2020-ZWRg31rS/lib/python3.8/site-packages/qiskit/chemistry/core/hamiltonian.py in _process_algorithm_result(self, algo_result)
    374             # TODO return self._process_algorithm_result_excited_states(algo_result)
    375         else:
--> 376             return self._process_algorithm_result_deprecated(algo_result)
    377 
    378     def _process_algorithm_result_ground_state(self, algo_result: MinimumEigensolverResult) \

~/.local/share/virtualenvs/ibm-quantum-challenge-2020-ZWRg31rS/lib/python3.8/site-packages/qiskit/chemistry/core/hamiltonian.py in _process_algorithm_result_deprecated(self, algo_result)
    419 
    420         # Ground state energy
--> 421         egse = algo_result['energy'] + self._energy_shift + self._ph_energy_shift
    422         result['energy'] = egse
    423         lines = ['=== GROUND STATE ENERGY ===']

TypeError: 'NumPyMinimumEigensolver' object is not subscriptable

Source-code:

# %%
import numpy as np 
import pylab 
import copy
from qiskit import BasicAer 
from qiskit.aqua import aqua_globals, 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.core import Hamiltonian, QubitMappingType
# %%
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 experiment:
    driver = PySCFDriver(molecule.format(d/2), basis='sto3g')
    qmolecule = driver.run()
    operator = Hamiltonian(qubit_mapping=QubitMappingType.PARITY,
                           two_qubit_reduction=True, 
                           freeze_core=True,
                           orbital_reduction=[-3, -2])
    qubit_op, aux_ops = operator.run(qmolecule)

    # exact classical result
    exact_result = NumPyMinimumEigensolver(qubit_op, aux_operators=aux_ops)
    exact_result = operator.process_algorithm_result(exact_result)

    # VQE
    optimzer = SLSQP(maxiter=1000)
    initial_state = HartreeFock(operator.molecule_info['num_orbitals'],
                                 operator.molecule_info['num_particles'],
                                 qubit_mapping=operator._qubit_mapping,
                                 two_qubit_reduction=operator._two_qubit_reduction)
    var_form = UCCSD(num_orbitals=operator.molecule_info['num_orbitals'],
                     num_particles=operator.molecule_info['num_particles'],
                     initial_state=initial_state,
                     qubit_mapping=operator._qubit_mapping,
                     two_qubit_reduction=operator._two_qubit_reduction)
    algo = VQE(qubit_op, var_form, optimzer, aux_operators=aux_ops)

    vqe_result = algo.run(QuantumInstance(BasicAer.get_backend('statevector_simulator')))
    vqe_result = operator.process_algorithm_result(vqe_result)

    exact_energies.append(exact_result.energy)
    vqe_energies.append(vqe_result.energy)
    hf_energies.append(vqe_result.hartree_fock_energy)

Thanks for the help!

$\endgroup$
3
$\begingroup$

The line:

exact_result = NumPyMinimumEigensolver(qubit_op, aux_operators=aux_ops)

should be

exact_result = NumPyMinimumEigensolver(qubit_op, aux_operators=aux_ops).run()


Here is the full code that I ran and its output:

# %%
import numpy as np 
import pylab 
import copy
from qiskit import BasicAer 
from qiskit.aqua import aqua_globals, 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.core import Hamiltonian, QubitMappingType
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'svg' # Makes the images look nice


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 experiment:
    driver = PySCFDriver(molecule.format(d/2), basis='sto3g')
    qmolecule = driver.run()
    operator = Hamiltonian(qubit_mapping=QubitMappingType.PARITY,
                           two_qubit_reduction=True, 
                           freeze_core=True,
                           orbital_reduction=[-3, -2])
    qubit_op, aux_ops = operator.run(qmolecule)

    # exact classical result
    exact_result = NumPyMinimumEigensolver(qubit_op, aux_operators=aux_ops).run()
    exact_result = operator.process_algorithm_result(exact_result)

    # VQE
    optimzer = SLSQP(maxiter=1000)
    initial_state = HartreeFock(operator.molecule_info['num_orbitals'],
                                 operator.molecule_info['num_particles'],
                                 qubit_mapping=operator._qubit_mapping,
                                 two_qubit_reduction=operator._two_qubit_reduction)
    var_form = UCCSD(num_orbitals=operator.molecule_info['num_orbitals'],
                     num_particles=operator.molecule_info['num_particles'],
                     initial_state=initial_state,
                     qubit_mapping=operator._qubit_mapping,
                     two_qubit_reduction=operator._two_qubit_reduction)
    algo = VQE(qubit_op, var_form, optimzer, aux_operators=aux_ops)

    vqe_result = algo.run(QuantumInstance(BasicAer.get_backend('statevector_simulator')))
    vqe_result = operator.process_algorithm_result(vqe_result)

    exact_energies.append(exact_result.energy)
    vqe_energies.append(vqe_result.energy)
    hf_energies.append(vqe_result.hartree_fock_energy)
    
fig = plt.figure()
plt.plot(distances,vqe_energies, label ='Mean Values')
plt.show()

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Hi @kaj226 - thank you very much. In the end, I fixed it changing the same line you pointed out; however, calling the .compute_minimum_eigenvalue() instead of .run(). Here's the whole line: exact_result = NumPyMinimumEigensolver(qubit_op, aux_operators=aux_ops).compute_minimum_eigenvalue(). Jack Woehr was who helped me on this one via Qiskit Slack. Thank you both! $\endgroup$ Nov 9 '20 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.