Note

This page was generated from tutorials/chemistry/04_excited_states_solvers.ipynb.

Run interactively in the IBM Quantum lab.

# Excited states solvers¶

## Introduction¶

In this tutorial we are going to discuss the excited states calculation interface of Qiskit Chemistry. The goal is to compute the excited states of a molecular Hamiltonian. This Hamiltonian can be electronic or vibronic. To know more about the preparation of the Hamiltonian, check out the Electronic structure and Vibronic structure tutorials.

The first step is to define the molecular system. In the following we ask for the electronic part of a hydrogen molecule.

```
[1]:
```

```
from qiskit.chemistry.drivers import PySCFDriver, UnitsType, Molecule
from qiskit.chemistry.transformations import FermionicTransformation, FermionicQubitMappingType
molecule = Molecule(geometry=[['H', [0., 0., 0.]],
['H', [0., 0., 0.735]]],
charge=0, multiplicity=1)
driver = PySCFDriver(molecule = molecule, unit=UnitsType.ANGSTROM, basis='sto3g')
transformation = FermionicTransformation(qubit_mapping=FermionicQubitMappingType.JORDAN_WIGNER)
```

## The Solver¶

Then we need to define a solver. The solver is the algorithm through which the ground state is computed.

Let’s first start with a purely classical example: the NumPy eigensolver. This algorithm exactly diagonalizes the Hamiltonian. Although it scales badely, it can be used on small systems to check the results of the quantum algorithms. Here, we are only interested to look at eigenstates with a given number of particle. To compute only those states a filter function can be passed to the NumPy eigensolver. A default filter function is already implemented in Qiskit and can be used in this way:

```
[2]:
```

```
from qiskit.chemistry.algorithms import NumPyEigensolverFactory
numpy_solver = NumPyEigensolverFactory(use_default_filter_criterion=True)
```

The excitation energies can also be accessed with the qEOM algorithm [arXiv preprint arXiv:1910.12890 (2019)]. The EOM method finds the excitation energies (differences in energy between the ground state and all \(n\)th excited states) by solving the following pseudo-eigenvalue problem.

with

Although the previous equation can be solved classically, each matrix element must be measured on the quantum computer with the corresponding ground state. To use the qEOM as a solver in Qiskit, we have to define a ground state calculation first, explaining the algorithms how to find the ground state. With this the qEOM solver can be initialized:

```
[3]:
```

```
from qiskit import BasicAer
from qiskit.aqua import QuantumInstance
from qiskit.chemistry.algorithms.ground_state_solvers import (GroundStateEigensolver,
VQEUCCSDFactory)
from qiskit.chemistry.algorithms.excited_states_solvers import QEOM
# This first part sets the ground state solver
# see more about this part in the ground state calculation tutorial
quantum_instance = QuantumInstance(BasicAer.get_backend('statevector_simulator'))
solver = VQEUCCSDFactory(quantum_instance)
gsc = GroundStateEigensolver(transformation, solver)
# The qEOM algorithm is simply instantiated with the chosen ground state solver
qeom_excited_states_calculation = QEOM(gsc, 'sd')
```

## The calculation and results¶

The results are computed and printed

```
[4]:
```

```
from qiskit.chemistry.algorithms.excited_states_solvers import ExcitedStatesEigensolver
numpy_excited_states_calculation = ExcitedStatesEigensolver(transformation, numpy_solver)
numpy_results = numpy_excited_states_calculation.solve(driver)
qeom_results = qeom_excited_states_calculation.solve(driver)
print(numpy_results)
print('\n\n')
print(qeom_results)
```

```
=== GROUND STATE ENERGY ===
* Electronic ground state energy (Hartree): -1.857275030202
- computed part: -1.857275030202
- frozen energy part: 0.0
- particle hole part: 0.0
~ Nuclear repulsion energy (Hartree): 0.719968994449
> Total ground state energy (Hartree): -1.137306035753
=== EXCITED STATE ENERGIES ===
1:
* Electronic excited state energy (Hartree): -0.882722150245
> Total excited state energy (Hartree): -0.162753155796
2:
* Electronic excited state energy (Hartree): -0.224911252831
> Total excited state energy (Hartree): 0.495057741618
=== MEASURED OBSERVABLES ===
0: # Particles: 2.000 S: 0.000 S^2: 0.000 M: 0.000
1: # Particles: 2.000 S: 0.000 S^2: 0.000 M: 0.000
2: # Particles: 2.000 S: 0.000 S^2: 0.000 M: 0.000
=== DIPOLE MOMENTS ===
~ Nuclear dipole moment (a.u.): [0.0 0.0 1.3889487]
0:
* Electronic dipole moment (a.u.): [0.0 0.0 1.3889487]
- computed part: [0.0 0.0 1.3889487]
- frozen energy part: [0.0 0.0 0.0]
- particle hole part: [0.0 0.0 0.0]
> Dipole moment (a.u.): [0.0 0.0 0.0] Total: 0.
(debye): [0.0 0.0 0.0] Total: 0.
1:
* Electronic dipole moment (a.u.): [0.0 0.0 1.3889487]
- computed part: [0.0 0.0 1.3889487]
- frozen energy part: [0.0 0.0 0.0]
- particle hole part: [0.0 0.0 0.0]
> Dipole moment (a.u.): [0.0 0.0 0.0] Total: 0.
(debye): [0.0 0.0 0.0] Total: 0.
2:
* Electronic dipole moment (a.u.): [0.0 0.0 1.3889487]
- computed part: [0.0 0.0 1.3889487]
- frozen energy part: [0.0 0.0 0.0]
- particle hole part: [0.0 0.0 0.0]
> Dipole moment (a.u.): [0.0 0.0 0.0] Total: 0.
(debye): [0.0 0.0 0.0] Total: 0.
=== GROUND STATE ENERGY ===
* Electronic ground state energy (Hartree): -1.857275030146
- computed part: -1.857275030146
- frozen energy part: 0.0
- particle hole part: 0.0
~ Nuclear repulsion energy (Hartree): 0.719968994449
> Total ground state energy (Hartree): -1.137306035697
=== EXCITED STATE ENERGIES ===
1:
* Electronic excited state energy (Hartree): -1.244586739621
> Total excited state energy (Hartree): -0.524617745172
2:
* Electronic excited state energy (Hartree): -0.882724340023
> Total excited state energy (Hartree): -0.162755345574
3:
* Electronic excited state energy (Hartree): -0.224913442617
> Total excited state energy (Hartree): 0.495055551832
=== MEASURED OBSERVABLES ===
0: # Particles: 2.000 S: 0.000 S^2: 0.000 M: -0.000
=== DIPOLE MOMENTS ===
~ Nuclear dipole moment (a.u.): [0.0 0.0 1.3889487]
0:
* Electronic dipole moment (a.u.): [0.0 0.0 1.38894867]
- computed part: [0.0 0.0 1.38894867]
- frozen energy part: [0.0 0.0 0.0]
- particle hole part: [0.0 0.0 0.0]
> Dipole moment (a.u.): [0.0 0.0 0.00000003] Total: 0.00000003
(debye): [0.0 0.0 0.00000008] Total: 0.00000008
```

One can see from these results that one state is missing from the NumPy results. The reason for this is because the spin is also used as a filter and only singlet states are shown. In the following we use a custom filter function to check consistently our results and only filter out states with incorrect number of particle (in this case the number of particle is 2).

```
[5]:
```

```
import numpy as np
def filter_criterion(eigenstate, eigenvalue, aux_values):
return np.isclose(aux_values[0][0], 2.)
new_numpy_solver = NumPyEigensolverFactory(filter_criterion=filter_criterion)
new_numpy_excited_states_calculation = ExcitedStatesEigensolver(transformation, new_numpy_solver)
new_numpy_results = new_numpy_excited_states_calculation.solve(driver)
print(new_numpy_results)
```

```
=== GROUND STATE ENERGY ===
* Electronic ground state energy (Hartree): -1.857275030202
- computed part: -1.857275030202
- frozen energy part: 0.0
- particle hole part: 0.0
~ Nuclear repulsion energy (Hartree): 0.719968994449
> Total ground state energy (Hartree): -1.137306035753
=== EXCITED STATE ENERGIES ===
1:
* Electronic excited state energy (Hartree): -1.244584549813
> Total excited state energy (Hartree): -0.524615555364
2:
* Electronic excited state energy (Hartree): -1.244584549813
> Total excited state energy (Hartree): -0.524615555364
3:
* Electronic excited state energy (Hartree): -1.244584549813
> Total excited state energy (Hartree): -0.524615555364
4:
* Electronic excited state energy (Hartree): -0.882722150245
> Total excited state energy (Hartree): -0.162753155796
5:
* Electronic excited state energy (Hartree): -0.224911252831
> Total excited state energy (Hartree): 0.495057741618
=== MEASURED OBSERVABLES ===
0: # Particles: 2.000 S: 0.000 S^2: 0.000 M: 0.000
1: # Particles: 2.000 S: 1.000 S^2: 2.000 M: 0.000
2: # Particles: 2.000 S: 1.000 S^2: 2.000 M: 1.000
3: # Particles: 2.000 S: 1.000 S^2: 2.000 M: -1.000
4: # Particles: 2.000 S: 0.000 S^2: 0.000 M: 0.000
5: # Particles: 2.000 S: 0.000 S^2: 0.000 M: 0.000
=== DIPOLE MOMENTS ===
~ Nuclear dipole moment (a.u.): [0.0 0.0 1.3889487]
0:
* Electronic dipole moment (a.u.): [0.0 0.0 1.3889487]
- computed part: [0.0 0.0 1.3889487]
- frozen energy part: [0.0 0.0 0.0]
- particle hole part: [0.0 0.0 0.0]
> Dipole moment (a.u.): [0.0 0.0 0.0] Total: 0.
(debye): [0.0 0.0 0.0] Total: 0.
1:
* Electronic dipole moment (a.u.): [0.0 0.0 1.3889487]
- computed part: [0.0 0.0 1.3889487]
- frozen energy part: [0.0 0.0 0.0]
- particle hole part: [0.0 0.0 0.0]
> Dipole moment (a.u.): [0.0 0.0 0.0] Total: 0.
(debye): [0.0 0.0 0.0] Total: 0.
2:
* Electronic dipole moment (a.u.): [0.0 0.0 1.3889487]
- computed part: [0.0 0.0 1.3889487]
- frozen energy part: [0.0 0.0 0.0]
- particle hole part: [0.0 0.0 0.0]
> Dipole moment (a.u.): [0.0 0.0 0.0] Total: 0.
(debye): [0.0 0.0 0.0] Total: 0.
3:
* Electronic dipole moment (a.u.): [0.0 0.0 1.3889487]
- computed part: [0.0 0.0 1.3889487]
- frozen energy part: [0.0 0.0 0.0]
- particle hole part: [0.0 0.0 0.0]
> Dipole moment (a.u.): [0.0 0.0 0.0] Total: 0.
(debye): [0.0 0.0 0.0] Total: 0.
4:
* Electronic dipole moment (a.u.): [0.0 0.0 1.3889487]
- computed part: [0.0 0.0 1.3889487]
- frozen energy part: [0.0 0.0 0.0]
- particle hole part: [0.0 0.0 0.0]
> Dipole moment (a.u.): [0.0 0.0 0.0] Total: 0.
(debye): [0.0 0.0 0.0] Total: 0.
5:
* Electronic dipole moment (a.u.): [0.0 0.0 1.3889487]
- computed part: [0.0 0.0 1.3889487]
- frozen energy part: [0.0 0.0 0.0]
- particle hole part: [0.0 0.0 0.0]
> Dipole moment (a.u.): [0.0 0.0 0.0] Total: 0.
(debye): [0.0 0.0 0.0] Total: 0.
```

```
[6]:
```

```
import qiskit.tools.jupyter
%qiskit_version_table
%qiskit_copyright
```

### Version Information

Qiskit Software | Version |
---|---|

Qiskit | 0.23.1 |

Terra | 0.16.1 |

Aer | 0.7.1 |

Ignis | 0.5.1 |

Aqua | 0.8.1 |

IBM Q Provider | 0.11.1 |

System information | |

Python | 3.8.6 (default, Oct 28 2020, 13:08:18) [GCC 7.5.0] |

OS | Linux |

CPUs | 2 |

Memory (Gb) | 6.791393280029297 |

Mon Nov 30 18:48:00 2020 UTC |

### This code is a part of Qiskit

© Copyright IBM 2017, 2020.

This code is licensed under the Apache License, Version 2.0. You may

obtain a copy of this license in the LICENSE.txt file in the root directory

of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.

Any modifications or derivative works of this code must retain this

copyright notice, and modified files need to carry a notice indicating

that they have been altered from the originals.

```
[ ]:
```

```
```