Note
This page was generated from docs/tutorials/04_excited_states_solvers.ipynb.
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()
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.
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, 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
# see more about this part in the ground state calculation tutorial
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.857275030146
- computed part: -1.857275030146
~ Nuclear repulsion energy (Hartree): 0.719968994449
> Total ground state energy (Hartree): -1.137306035697
=== EXCITED STATE ENERGIES ===
1:
* Electronic excited state energy (Hartree): -1.244586737479
> Total excited state energy (Hartree): -0.52461774303
2:
* Electronic excited state energy (Hartree): -0.882724337881
> Total excited state energy (Hartree): -0.162755343432
3:
* Electronic excited state energy (Hartree): -0.224913440475
> Total excited state energy (Hartree): 0.495055553974
=== 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.38894868734]
- computed part: [0.0 0.0 1.38894868734]
> Dipole moment (a.u.): [0.0 0.0 0.00000001266] Total: 0.00000001266
(debye): [0.0 0.0 0.000000032179] Total: 0.000000032179
1:
* Electronic dipole moment (a.u.): [0.0 0.0 1.388948701645]
- computed part: [0.0 0.0 1.388948701645]
> Dipole moment (a.u.): [0.0 0.0 -0.000000001645] Total: 0.000000001645
(debye): [0.0 0.0 -0.000000004182] Total: 0.000000004182
2:
* Electronic dipole moment (a.u.): [0.0 0.0 1.388948683327]
- computed part: [0.0 0.0 1.388948683327]
> Dipole moment (a.u.): [0.0 0.0 0.000000016673] Total: 0.000000016673
(debye): [0.0 0.0 0.000000042379] Total: 0.000000042379
3:
* Electronic dipole moment (a.u.): [0.0 0.0 1.388948699532]
- computed part: [0.0 0.0 1.388948699532]
> Dipole moment (a.u.): [0.0 0.0 0.000000000468] Total: 0.000000000468
(debye): [0.0 0.0 0.00000000119] Total: 0.00000000119
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 tutorial_magics
%qiskit_version_table
%qiskit_copyright
Version Information
Software | Version |
---|---|
qiskit | 1.0.1 |
qiskit_nature | 0.7.2 |
qiskit_algorithms | 0.3.0 |
System information | |
Python version | 3.8.18 |
OS | Linux |
Fri Feb 23 10:25:18 2024 UTC |
This code is a part of a Qiskit project
© Copyright IBM 2017, 2024.
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.