This notebook demonstrates using the Qiskit Aqua Chemistry to plot graphs of the ground state energy of the Sodium Hydride (NaH) molecule over a range of inter-atomic distances using VQE and UCCSD. It is compared to the same energies as computed by the ExactEigensolver
This notebook populates a dictionary, that is a progammatic representation of an input file, in order to drive the Qiskit Aqua Chemistry stack. Such a dictionary can be manipulated programmatically and this is indeed the case here where we alter the molecule supplied to the driver in each loop.
This notebook has been written to use the PYSCF chemistry driver. See the PYSCF chemistry driver readme if you need to install the external PySCF library that this driver requires.
In [1]:
import numpy as np
import pylab
from qiskit_aqua_chemistry import AquaChemistry
# Input dictionary to configure Qiskit Aqua Chemistry for the chemistry problem.
aqua_chemistry_dict = {
'driver': {'name': 'PYSCF'},
'PYSCF': {'atom': '', 'basis': 'sto3g'},
'operator': {'name': 'hamiltonian', 'qubit_mapping': 'parity',
'two_qubit_reduction': True, 'freeze_core': True, 'orbital_reduction': []},
'algorithm': {'name': ''},
'optimizer': {'name': 'COBYLA', 'maxiter': 10000 },
'variational_form': {'name': 'UCCSD'},
'initial_state': {'name': 'HartreeFock'}
}
molecule = 'H .0 .0 -{0}; Na .0 .0 {0}'
algorithms = ['VQE', 'ExactEigensolver']
pts = [x * 0.1 for x in range(10, 25)]
pts += [x * 0.25 for x in range(10, 18)]
pts += [4.5]
energies = np.empty([len(algorithms), len(pts)])
hf_energies = np.empty(len(pts))
distances = np.empty(len(pts))
dipoles = np.empty([len(algorithms), len(pts)])
eval_counts = np.empty(len(pts))
print('Processing step __', end='')
for i, d in enumerate(pts):
print('\b\b{:2d}'.format(i), end='', flush=True)
aqua_chemistry_dict['PYSCF']['atom'] = molecule.format(d/2)
for j in range(len(algorithms)):
aqua_chemistry_dict['algorithm']['name'] = algorithms[j]
solver = AquaChemistry()
result = solver.run(aqua_chemistry_dict)
energies[j][i] = result['energy']
hf_energies[i] = result['hf_energy']
dipoles[j][i] = result['total_dipole_moment'] / 0.393430307
if algorithms[j] == 'VQE':
eval_counts[i] = result['algorithm_retvals']['eval_count']
distances[i] = d
print(' --- complete')
print('Distances: ', distances)
print('Energies:', energies)
print('Hartree-Fock energies:', hf_energies)
print('Dipoles:', dipoles)
print('VQE num evaluations:', eval_counts)
In [2]:
pylab.plot(distances, hf_energies, label='Hartree-Fock')
for j in range(len(algorithms)):
pylab.plot(distances, energies[j], label=algorithms[j])
pylab.xlabel('Interatomic distance')
pylab.ylabel('Energy')
pylab.title('NaH Ground State Energy')
pylab.legend(loc='upper right')
Out[2]:
In [3]:
pylab.plot(distances, np.subtract(hf_energies, energies[1]), label='Hartree-Fock')
pylab.plot(distances, np.subtract(energies[0], energies[1]), label='VQE')
pylab.xlabel('Interatomic distance')
pylab.ylabel('Energy')
pylab.title('Energy difference from ExactEigensolver')
pylab.legend(loc='upper left')
Out[3]:
In [4]:
for j in reversed(range(len(algorithms))):
pylab.plot(distances, dipoles[j], label=algorithms[j])
pylab.xlabel('Interatomic distance')
pylab.ylabel('Moment in debye')
pylab.title('NaH Dipole Moment')
pylab.legend(loc='upper right')
Out[4]:
In [5]:
pylab.plot(distances, eval_counts, '-o', color=[0.8500, 0.3250, 0.0980], label='VQE')
pylab.xlabel('Interatomic distance')
pylab.ylabel('Evaluations')
pylab.title('VQE number of evaluations')
pylab.legend(loc='upper left')
Out[5]:
In [ ]: