Note

# Excited states solvers#

## Introduction#

In this tutorial we are going to discuss the excited states calculation interface of Qiskit Nature. 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.units import DistanceUnit
from qiskit_nature.second_q.drivers import PySCFDriver

driver = PySCFDriver(
atom="H 0 0 0; H 0 0 0.735",
basis="sto3g",
charge=0,
spin=0,
unit=DistanceUnit.ANGSTROM,
)

es_problem = driver.run()

/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/pyscf/dft/libxc.py:771: UserWarning: Since PySCF-2.3, B3LYP (and B3P86) are changed to the VWN-RPA variant, corresponding to the original definition by Stephens et al. (issue 1480) and the same as the B3LYP functional in Gaussian. To restore the VWN5 definition, you can put the setting "B3LYP_WITH_VWN5 = True" in pyscf_conf.py
warnings.warn('Since PySCF-2.3, B3LYP (and B3P86) are changed to the VWN-RPA variant, '


We will also be sticking to the Jordan-Wigner mapping. To learn more about the various mappers available in Qiskit Nature, check out the Qubit Mappers tutorial.

[2]:

from qiskit_nature.second_q.mappers import JordanWignerMapper

mapper = JordanWignerMapper()


## The Solver#

After these steps we need to define a solver. The solver is the algorithm through which the excited states are computed.

Letâ€™s first start with a purely classical example: the NumPyEigensolver. 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 particles. To compute only those states a filter function can be passed to the NumPyEigensolver. A default filter function is already implemented in Qiskit Nature which you can use for this purpose.

We also need to specify the number of eigenvalues to be computed by the NumPyEigensolver. For this particular system, we are interested in the ground and first three excited states, so we will set k=4 (which defaults to 1 so be sure to set this, otherwise you will only obtain the ground state!).

[3]:

from qiskit_algorithms import NumPyEigensolver

numpy_solver = NumPyEigensolver(k=4, filter_criterion=es_problem.get_default_filter_criterion())


The excitation energies can also be accessed with the qEOM algorithm. 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.

$\begin{split}\begin{pmatrix} \text{M} & \text{Q}\\ \text{Q*} & \text{M*} \end{pmatrix} \begin{pmatrix} \text{X}_n\\ \text{Y}_n \end{pmatrix} = E_{0n} \begin{pmatrix} \text{V} & \text{W}\\ -\text{W*} & -\text{V*} \end{pmatrix} \begin{pmatrix} \text{X}_n\\ \text{Y}_n \end{pmatrix}\end{split}$

with

$M_{\mu_{\alpha}\nu_{\beta}} = \langle0| [(\hat{\text{E}}_{\mu_{\alpha}}^{(\alpha)})^{\dagger},\hat{\text{H}}, \hat{\text{E}}_{\nu_{\beta}}^{(\beta)}]|0\rangle$
$Q_{\mu_{\alpha}\nu_{\beta}} = -\langle0| [(\hat{\text{E}}_{\mu_{\alpha}}^{(\alpha)})^{\dagger}, \hat{\text{H}}, (\hat{\text{E}}_{\nu_{\beta}}^{(\beta)})^{\dagger}]|0\rangle$
$V_{\mu_{\alpha}\nu_{\beta}} = \langle0| [(\hat{\text{E}}_{\mu_{\alpha}}^{(\alpha)})^{\dagger}, \hat{\text{E}}_{\nu_{\beta}}^{(\beta)}]|0\rangle$
$W_{\mu_{\alpha}\nu_{\beta}} = -\langle0| [(\hat{\text{E}}_{\mu_\alpha}^{(\alpha)})^{\dagger}, (\hat{\text{E}}_{\nu_{\beta}}^{(\beta)})^{\dagger}]|0\rangle$

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, which will provide the required ground state information to the algorithm. With this the qEOM solver can be initialized like so:

[4]:

from qiskit_algorithms import VQE
from qiskit_algorithms.optimizers import SLSQP
from qiskit.primitives import Estimator
from qiskit_nature.second_q.algorithms import GroundStateEigensolver, QEOM, EvaluationRule
from qiskit_nature.second_q.circuit.library import HartreeFock, UCCSD

ansatz = UCCSD(
es_problem.num_spatial_orbitals,
es_problem.num_particles,
mapper,
initial_state=HartreeFock(
es_problem.num_spatial_orbitals,
es_problem.num_particles,
mapper,
),
)

estimator = Estimator()
# This first part sets the ground state solver
solver = VQE(estimator, ansatz, SLSQP())
solver.initial_point = [0.0] * ansatz.num_parameters
gse = GroundStateEigensolver(mapper, solver)

# The qEOM algorithm is simply instantiated with the chosen ground state solver and Estimator primitive
qeom_excited_states_solver = QEOM(gse, estimator, "sd", EvaluationRule.ALL)


## The calculation and results#

We are now ready to compute the results. Below, we are comparing the results obtained by the exact NumPyEigensolver with the default filter criterion enabled with the results obtained by the qEOM algorithm.

[5]:

from qiskit_nature.second_q.algorithms import ExcitedStatesEigensolver

numpy_excited_states_solver = ExcitedStatesEigensolver(mapper, numpy_solver)
numpy_results = numpy_excited_states_solver.solve(es_problem)

qeom_results = qeom_excited_states_solver.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.388948701555]
- computed part:      [0.0  0.0  1.388948701555]
> Dipole moment (a.u.): [0.0  0.0  -0.000000001555]  Total: 0.000000001555
(debye): [0.0  0.0  -0.000000003953]  Total: 0.000000003953

1:
* Electronic dipole moment (a.u.): [0.0  0.0  1.388948701555]
- computed part:      [0.0  0.0  1.388948701555]
> Dipole moment (a.u.): [0.0  0.0  -0.000000001555]  Total: 0.000000001555
(debye): [0.0  0.0  -0.000000003953]  Total: 0.000000003953

2:
* Electronic dipole moment (a.u.): [0.0  0.0  1.388948701555]
- computed part:      [0.0  0.0  1.388948701555]
> Dipole moment (a.u.): [0.0  0.0  -0.000000001555]  Total: 0.000000001555
(debye): [0.0  0.0  -0.000000003953]  Total: 0.000000003953

=== 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.244586754179
> Total excited state energy (Hartree): -0.52461775973
2:
* Electronic excited state energy (Hartree): -0.88272435458
> Total excited state energy (Hartree): -0.162755360131
3:
* Electronic excited state energy (Hartree): -0.224913457175
> Total excited state energy (Hartree): 0.495055537274

=== 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: 0.000 S^2: -0.000 M: -0.000
3:  # 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.388948930893]
- computed part:      [0.0  0.0  1.388948930893]
> Dipole moment (a.u.): [0.0  0.0  -0.000000230893]  Total: 0.000000230893
(debye): [0.0  0.0  -0.000000586872]  Total: 0.000000586872

1:
* Electronic dipole moment (a.u.): [0.0  0.0  1.388948701647]
- computed part:      [0.0  0.0  1.388948701647]
> Dipole moment (a.u.): [0.0  0.0  -0.000000001647]  Total: 0.000000001647
(debye): [0.0  0.0  -0.000000004186]  Total: 0.000000004186

2:
* Electronic dipole moment (a.u.): [0.0  0.0  1.388948996246]
- computed part:      [0.0  0.0  1.388948996246]
> Dipole moment (a.u.): [0.0  0.0  -0.000000296246]  Total: 0.000000296246
(debye): [0.0  0.0  -0.000000752982]  Total: 0.000000752982

3:
* Electronic dipole moment (a.u.): [0.0  0.0  1.388948734214]
- computed part:      [0.0  0.0  1.388948734214]
> Dipole moment (a.u.): [0.0  0.0  -0.000000034214]  Total: 0.000000034214
(debye): [0.0  0.0  -0.000000086962]  Total: 0.000000086962



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 our results consistently and only filter out states with the incorrect number of particles (in this case the number of particle is 2) as well as the wrong magnetization (which we enforce to be 0).

[6]:

import numpy as np

def filter_criterion(eigenstate, eigenvalue, aux_values):
return np.isclose(aux_values["ParticleNumber"][0], 2.0) and np.isclose(
aux_values["Magnetization"][0], 0.0
)

new_numpy_solver = NumPyEigensolver(k=4, filter_criterion=filter_criterion)
new_numpy_excited_states_solver = ExcitedStatesEigensolver(mapper, new_numpy_solver)
new_numpy_results = new_numpy_excited_states_solver.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): -0.882722150245
> Total excited state energy (Hartree): -0.162753155796
3:
* 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: 0.000 S^2: 0.000 M: 0.000
3:  # 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.388948701555]
- computed part:      [0.0  0.0  1.388948701555]
> Dipole moment (a.u.): [0.0  0.0  -0.000000001555]  Total: 0.000000001555
(debye): [0.0  0.0  -0.000000003953]  Total: 0.000000003953

1:
* Electronic dipole moment (a.u.): [0.0  0.0  1.388948701555]
- computed part:      [0.0  0.0  1.388948701555]
> Dipole moment (a.u.): [0.0  0.0  -0.000000001555]  Total: 0.000000001555
(debye): [0.0  0.0  -0.000000003953]  Total: 0.000000003953

2:
* Electronic dipole moment (a.u.): [0.0  0.0  1.388948701555]
- computed part:      [0.0  0.0  1.388948701555]
> Dipole moment (a.u.): [0.0  0.0  -0.000000001555]  Total: 0.000000001555
(debye): [0.0  0.0  -0.000000003953]  Total: 0.000000003953

3:
* Electronic dipole moment (a.u.): [0.0  0.0  1.388948701555]
- computed part:      [0.0  0.0  1.388948701555]
> Dipole moment (a.u.): [0.0  0.0  -0.000000001555]  Total: 0.000000001555
(debye): [0.0  0.0  -0.000000003953]  Total: 0.000000003953


[7]:

import qiskit.tools.jupyter

%qiskit_version_table


### Version Information

SoftwareVersion
qiskit0.45.0
qiskit_algorithms0.2.1
qiskit_nature0.7.1
System information
Python version3.8.18
Python compilerGCC 11.4.0
Python builddefault, Aug 28 2023 08:27:22
OSLinux
CPUs2
Memory (Gb)15.60689926147461
Wed Nov 29 13:17:27 2023 UTC

### This code is a part of Qiskit

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

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.