Note

Run interactively in jupyter notebook.

# 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 vibrational. To know more about the preparation of the Hamiltonian, check out the Electronic structure and Vibrational 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_nature.drivers import PySCFDriver, UnitsType, Molecule
from qiskit_nature.problems.second_quantization import ElectronicStructureProblem
from qiskit_nature.converters.second_quantization import QubitConverter
from qiskit_nature.mappers.second_quantization import JordanWignerMapper
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')
es_problem = ElectronicStructureProblem(driver)
qubit_converter = QubitConverter(JordanWignerMapper())
```

```
/opt/hostedtoolcache/Python/3.8.10/x64/lib/python3.8/site-packages/pyscf/lib/misc.py:46: H5pyDeprecationWarning: Using default_file_mode other than 'r' is deprecated. Pass the mode to h5py.File() instead.
h5py.get_config().default_file_mode = 'a'
```

## 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 badly, 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_nature.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, providing to the algorithm information on how to find the ground state. With this the qEOM solver can be initialized:

```
[3]:
```

```
from qiskit import Aer
from qiskit.utils import QuantumInstance
from qiskit_nature.algorithms import GroundStateEigensolver, QEOM, VQEUCCFactory
# This first part sets the ground state solver
# see more about this part in the ground state calculation tutorial
quantum_instance = QuantumInstance(Aer.get_backend('statevector_simulator'))
solver = VQEUCCFactory(quantum_instance)
gsc = GroundStateEigensolver(qubit_converter, 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_nature.algorithms import ExcitedStatesEigensolver
numpy_excited_states_calculation = ExcitedStatesEigensolver(qubit_converter, numpy_solver)
numpy_results = numpy_excited_states_calculation.solve(es_problem)
qeom_results = qeom_excited_states_calculation.solve(es_problem)
print(numpy_results)
print('\n\n')
print(qeom_results)
```

```
=== GROUND STATE ENERGY ===
* Electronic ground state energy (Hartree): -1.857275030202
- computed part: -1.857275030202
~ 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]
> 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]
> 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]
> 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.857275030145
- computed part: -1.857275030145
~ Nuclear repulsion energy (Hartree): 0.719968994449
> Total ground state energy (Hartree): -1.137306035696
=== EXCITED STATE ENERGIES ===
1:
* Electronic excited state energy (Hartree): -1.244586746139
> Total excited state energy (Hartree): -0.52461775169
2:
* Electronic excited state energy (Hartree): -0.882724346541
> Total excited state energy (Hartree): -0.162755352092
3:
* Electronic excited state energy (Hartree): -0.224913449135
> Total excited state energy (Hartree): 0.495055545314
=== 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.38894854]
- computed part: [0.0 0.0 1.38894854]
> Dipole moment (a.u.): [0.0 0.0 0.00000016] Total: 0.00000016
(debye): [0.0 0.0 0.00000039] Total: 0.00000039
```

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(qubit_converter, new_numpy_solver)
new_numpy_results = new_numpy_excited_states_calculation.solve(es_problem)
print(new_numpy_results)
```

```
=== GROUND STATE ENERGY ===
* Electronic ground state energy (Hartree): -1.857275030202
- computed part: -1.857275030202
~ 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]
> 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]
> 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]
> 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]
> 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]
> 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]
> 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 | None |

Terra | 0.17.3 |

Aer | 0.8.2 |

Ignis | None |

Aqua | None |

IBM Q Provider | None |

System information | |

Python | 3.8.10 (default, May 4 2021, 07:16:51) [GCC 9.3.0] |

OS | Linux |

CPUs | 2 |

Memory (Gb) | 6.791343688964844 |

Tue May 11 14:50:56 2021 UTC |

### This code is a part of Qiskit

© Copyright IBM 2017, 2021.

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.