নোট
এই পৃষ্ঠাটি docs/tutorials/04_excited_states_solvers.ipynb থেকে বানানো হয়েছে।
উত্তেজিত স্তর বা অবস্থা সমাধানকারী (সলভার)#
ভূমিকা#
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.
প্রথম ধাপ হল আণবিক সিস্টেমকে সংজ্ঞায়িত করা। এরপর আমরা হাইড্রোজেন অণুর বৈদ্যুতিন অংশটি পেতে চাই।
[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()
সমাধানকারী (সলভার)#
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.
সাথে বা সঙ্গে
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)
হিসাব এবং ফলাফল।#
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.244586756145
> Total excited state energy (Hartree): -0.524617761696
2:
* Electronic excited state energy (Hartree): -0.882724356546
> Total excited state energy (Hartree): -0.162755362097
3:
* Electronic excited state energy (Hartree): -0.224913459141
> Total excited state energy (Hartree): 0.495055535308
=== 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.388948961657]
- computed part: [0.0 0.0 1.388948961657]
> Dipole moment (a.u.): [0.0 0.0 -0.000000261657] Total: 0.000000261657
(debye): [0.0 0.0 -0.000000665065] Total: 0.000000665065
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.388949035771]
- computed part: [0.0 0.0 1.388949035771]
> Dipole moment (a.u.): [0.0 0.0 -0.000000335771] Total: 0.000000335771
(debye): [0.0 0.0 -0.000000853445] Total: 0.000000853445
3:
* Electronic dipole moment (a.u.): [0.0 0.0 1.388948738594]
- computed part: [0.0 0.0 1.388948738594]
> Dipole moment (a.u.): [0.0 0.0 -0.000000038594] Total: 0.000000038594
(debye): [0.0 0.0 -0.000000098097] Total: 0.000000098097
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
%qiskit_copyright
Version Information
Qiskit Software | Version |
---|---|
qiskit-terra | 0.24.1 |
qiskit-nature | 0.7.0 |
System information | |
Python version | 3.10.11 |
Python compiler | GCC 12.2.1 20221121 (Red Hat 12.2.1-4) |
Python build | main, Apr 5 2023 00:00:00 |
OS | Linux |
CPUs | 8 |
Memory (Gb) | 62.48404312133789 |
Wed Jun 07 10:45:42 2023 CEST |
This code is a part of Qiskit
© Copyright IBM 2017, 2023.
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.