Qiskit Aqua: Chemistry basic how to

The latest version of this notebook is available on https://github.com/Qiskit/qiskit-tutorial.


Contributors

Richard Chen[1], Antonio Mezzacapo[1], Marco Pistoia[1], Stephen Wood[1]

Affiliation

  • [1]IBMQ

Introduction

This notebook demonstrates how to use Qiskit Aqua Chemistry to compute the ground state energy of a Hydrogen (H2) molecule using VQE and UCCSD.

This notebook has been written to use the HDF5 chemistry driver. This driver uses molecular data that has been saved from a prior computation so that this notebook can be run with no additional driver installation requirements. See the HDF5 chemistry driver readme for more detail.

First we import AquaChemistry, which is the object that will carry out the computation for us


In [1]:
from qiskit_aqua_chemistry import AquaChemistry

Next, we create a Python dictionary to specify the problem we want to solve. There are defaults for many additional values that are not show here for simpicity. Indeed we take advantage of using sensisble defaults that the qischem stack provides to help us here. Please notice that the Qiskit Aqua Chemistry GUI allows for automatic extraction of the Python dictionary reflecting the current configuration. Once the Python dictionary has been extracted, it can be pasted into a Python program or a Jupyter Notebook and, if necessary, edited.

The first entry names a chemistry driver. This example uses HDF5 and the next line configures the driver for an hdf5 file that contains data from a prior computation for an H2 molecule with basis set sto-3g. The operator line would default but I have added it here to show it and to say that this is where the problem is converted into a quantum qubit form. We then have a VQE algorithm, using the COBYLA optimizer with a UCCSD variatonal form and initial state of HartreeFock. VQE is Variational Quantum Eigensolver and as its name suggests uses a variational method to find the mimimum eigenvalue of the problem, which in this case is the ground state energy of the molecule.

[Optional] Setup token to run the experiment on a real device

If you would like to run the experiement on a real device, you need to setup your account first.

Note: If you do not store your token yet, use IBMQ.save_accounts() to store it first.


In [2]:
from qiskit import IBMQ
IBMQ.load_accounts()

In [3]:
# Input dictionary to configure Qiskit AQUA Chemistry for the chemistry problem.
aqua_chemistry_dict = {
    'driver': {'name': 'HDF5'},
    'HDF5': {'hdf5_input': 'H2/0.7_sto-3g.hdf5'},
    'operator': {'name': 'hamiltonian'},
    'algorithm': {'name': 'VQE'},
    'optimizer': {'name': 'COBYLA'},
    'variational_form': {'name': 'UCCSD'},
    'initial_state': {'name': 'HartreeFock'},
    'backend': {'name': 'statevector_simulator'}
}

We can now create a AquaChemistry object and call run on it passing in the problem dictionary to get a result. This may take a short time and it will use a local quantum simulator to carry out the quantum computation that the VQE algorithm uses.


In [4]:
solver = AquaChemistry()
result = solver.run(aqua_chemistry_dict)

The run method returns a result dictionary. Some notable fields include 'energy' which is the computed ground state energy. We can print it.


In [5]:
print('Ground state energy: {}'.format(result['energy']))


Ground state energy: -1.1361894321408221

There is also a 'printable' field containing a complete ready to print readable result


In [6]:
for line in result['printable']:
    print(line)


=== GROUND STATE ENERGY ===
 
* Electronic ground state energy (Hartree): -1.892156876312
  - computed part:      -1.892156876312
  - frozen energy part: 0.0
  - particle hole part: 0.0
~ Nuclear repulsion energy (Hartree): 0.755967444171
> Total ground state energy (Hartree): -1.136189432141
  Measured:: Num particles: 2.000, S: 0.000, M: 0.00000
 
=== DIPOLE MOMENT ===
 
* Electronic dipole moment (a.u.): [0.0  0.0  0.00029555]
  - computed part:      [0.0  0.0  0.00029555]
  - frozen energy part: [0.0  0.0  0.0]
  - particle hole part: [0.0  0.0  0.0]
~ Nuclear dipole moment (a.u.): [0.0  0.0  0.0]
> Dipole moment (a.u.): [0.0  0.0  -0.00029555]  Total: 0.00029555
               (debye): [0.0  0.0  -0.00075122]  Total: 0.00075122

This was a very simple example showing how to get started. There are more elaborate notebooks here as well documentation describing the various components and their configurations to help you to experiment with quantum computing and its application to solving chemistry problems.