নোট
এই পৃষ্ঠাটি tutorials/algorithms/01_algorithms_introduction.ipynb-থেকে বানানো হয়েছে।
Qiskit-এ ধারাক্রমগুলোর (অ্যালগরিদম) ভূমিকা।¶
এই লেখাটি কিস্কিট এর অ্যালগরিদমগুলোর সাথে পরিচয় করিয়ে দেবে, যা বিভিন্ন ক্ষেত্রে এই অ্যালগরিদমের ব্যবহার সম্পর্কে একটা উচ্চপর্যায়ের ধারণা দিয়ে শেখা শুরু করেতে সাহায্য করবে। অন্যান্য টিউটোরিয়ালগুলোতে প্রদত্ত অ্যালগোরিদমসমূহের আরও গভীর বিষয়গুলো এবং তাদের ব্যবহার নিয়ে আরও বিষদ ধারণা দেওয়া হবে।
অ্যালগরিদম লাইব্রেরিটি কীভাবে সাজানো হয়েছে?¶
Qiskit provides a number of Algorithms and they are grouped by category according to the task they can perform. For instance Minimum Eigensolvers
to find the smallest eigen value of an operator, for example ground state energy of a chemistry Hamiltonian or a solution to an optimization problem when expressed as an Ising Hamiltonian. There are Time Evolvers
for the time evolution of quantum systems and Amplitude Estimators
for value estimation that can be used say in financial applications. The full set of categories can be seen in the Algorithms documentation link above.
ধারাক্রমদের (অ্যালোগরিদম) সাজানো সম্ভব (কনফিগারেবল), আর সাধারণত সাজানোর (কনফিগারেশন) কিছুটা ভাগ ছোট ছোট মৌলিক অংশ (বিল্ডিং ব্লক) দিয়ে তৈরী, যা কিনা বিভিন্ন রকম হতে পারে। যেমন VQE
, কিংবা ভ্যারিয়শনাল কোয়ান্টাম আইগেনসলভার এর ক্ষেত্রে পরিক্ষীত (ট্রায়াল) ওয়েভফাংশন, QuantumCircuit
এবং প্রথাগত (ক্লাসিকাল) অপটিমাইজার ইত্যাদির মাধ্যমে নেওয়া হয়।
Let’s take a look at an example to construct a VQE instance. Here TwoLocal
is the variational form (trial wavefunction), a parameterized circuit which can be varied, and SLSQP
a classical optimizer. These are created as separate instances and passed to VQE when it is constructed. Trying, for example, a different classical optimizer, or variational form is simply a case of creating an instance of the one you want and passing it into VQE.
[1]:
from qiskit.algorithms.optimizers import SLSQP
from qiskit.circuit.library import TwoLocal
num_qubits = 2
ansatz = TwoLocal(num_qubits, "ry", "cz")
optimizer = SLSQP(maxiter=1000)
Let’s draw the ansatz so we can see it’s a QuantumCircuit
where θ[0] through θ[7] will be the parameters that are varied as VQE optimizer finds the minimum eigenvalue. We’ll come back to the parameters later in a working example below.
[2]:
ansatz.decompose().draw("mpl", style="iqx")
[2]:

But more is needed before we can run the algorithm so let’s get to that next.
একটি অ্যালগরিদম কিভাবে চালানো যেতে পারে?¶
Algorithms rely on the primitives to evaluate expectation values or sample circuits. The primitives can be based on a simulator or real device and can be used interchangeably in the algorithms, as they all implement the same interface.
In the VQE, we have to evaluate expectation values, so for example we can use the qiskit.primitives.Estimator
which is shipped with the default Qiskit Terra installation.
[3]:
from qiskit.primitives import Estimator
estimator = Estimator()
This estimator uses an exact, statevector simulation to evaluate the expectation values. We can also use a shot-based and noisy simulators or real backends instead. For more information of the simulators you can check out Qiskit Aer and for the actual hardware Qiskit IBM Runtime.
With all the ingredients ready, we can now instantiate the VQE:
[4]:
from qiskit.algorithms.minimum_eigensolvers import VQE
vqe = VQE(estimator, ansatz, optimizer)
Now we can call the compute_mininum_eigenvalue() method. The latter is the interface of choice for the application modules, such as Nature and Optimization, in order that they can work interchangeably with any algorithm within the specific category.
একটি সম্পূর্ণ কার্যকরী উদাহরণ¶
Let’s put what we have learned from above together and create a complete working example. VQE will find the minimum eigenvalue, i.e. minimum energy value of a Hamiltonian operator and hence we need such an operator for VQE to work with. Such an operator is given below. This was originally created by the Nature application module as the Hamiltonian for an H2 molecule at 0.735A interatomic distance. It’s a sum of Pauli terms as below, but for now I am not going to say anything further about it since the goal is to run the algorithm, but further information on operators can be found in other tutorials.
[5]:
from qiskit.quantum_info import SparsePauliOp
H2_op = SparsePauliOp.from_list([
("II", -1.052373245772859),
("IZ", 0.39793742484318045),
("ZI", -0.39793742484318045),
("ZZ", -0.01128010425623538),
("XX", 0.18093119978423156)
])
So let’s run VQE and print the result object it returns.
[6]:
result = vqe.compute_minimum_eigenvalue(H2_op)
print(result)
{ 'aux_operators_evaluated': None,
'cost_function_evals': 102,
'eigenvalue': -1.857275020719397,
'optimal_circuit': <qiskit.circuit.library.n_local.two_local.TwoLocal object at 0x7f96da26a470>,
'optimal_parameters': { ParameterVectorElement(θ[0]): -2.403507257619715,
ParameterVectorElement(θ[5]): 1.7060524493254914,
ParameterVectorElement(θ[1]): 3.085467047665086,
ParameterVectorElement(θ[2]): -2.1949965223522487,
ParameterVectorElement(θ[3]): 4.276089268519914,
ParameterVectorElement(θ[4]): -3.098644972035885,
ParameterVectorElement(θ[6]): 0.032773583818940334,
ParameterVectorElement(θ[7]): 2.8861019033185396},
'optimal_point': array([-2.40350726, 3.08546705, -2.19499652, 4.27608927, -3.09864497,
1.70605245, 0.03277358, 2.8861019 ]),
'optimal_value': -1.857275020719397,
'optimizer_evals': None,
'optimizer_result': <qiskit.algorithms.optimizers.optimizer.OptimizerResult object at 0x7f96da2a4d60>,
'optimizer_time': 0.29071593284606934}
From the above result we can see the number of cost function (=energy) evaluations the optimizer took until it found the minimum eigenvalue of \(\approx -1.85727\) which is the electronic ground state energy of the given H2 molecule. The optimal parameters of the ansatz can also be seen which are the values that were in the ansatz at the minimum value.
Updating the primitive inside VQE¶
To close off let’s also change the estimator primitive inside the a VQE. Maybe you’re satisfied with the simulation results and now want to use a shot-based simulator, or run on hardware!
In this example we’re changing to a shot-based estimator, still using Qiskit Terra’s reference primitive. However you could replace the primitive by e.g. Qiskit Aer’s estimator (qiskit_aer.primitives.Estimator
) or even a real backend (qiskit_ibm_runtime.Estimator
).
For noisy loss functions, the SPSA optimizer typically performs well, so we also update the optimizer. See also the noisy VQE tutorial for more details on shot-based and noisy simulations.
[7]:
from qiskit.algorithms.optimizers import SPSA
estimator = Estimator(options={"shots": 1000})
vqe.estimator = estimator
vqe.optimizer = SPSA(maxiter=100)
result = vqe.compute_minimum_eigenvalue(operator=H2_op)
print(result)
{ 'aux_operators_evaluated': None,
'cost_function_evals': 200,
'eigenvalue': -1.8574503552440247,
'optimal_circuit': <qiskit.circuit.library.n_local.two_local.TwoLocal object at 0x7f96da2f4250>,
'optimal_parameters': { ParameterVectorElement(θ[0]): -7.7940259581467375,
ParameterVectorElement(θ[5]): 0.28827257835035214,
ParameterVectorElement(θ[1]): -1.8091021117029589,
ParameterVectorElement(θ[2]): -2.460381278734678,
ParameterVectorElement(θ[3]): -7.725013961075425,
ParameterVectorElement(θ[4]): -1.3793338621798832,
ParameterVectorElement(θ[6]): -2.4148423942537587,
ParameterVectorElement(θ[7]): -1.8555574263247812},
'optimal_point': array([-7.79402596, -1.80910211, -2.46038128, -7.72501396, -1.37933386,
0.28827258, -2.41484239, -1.85555743]),
'optimal_value': -1.8574503552440247,
'optimizer_evals': None,
'optimizer_result': <qiskit.algorithms.optimizers.optimizer.OptimizerResult object at 0x7f96da26a5f0>,
'optimizer_time': 0.8142139911651611}
Note: We do not fix the random seed in the simulators here, so re-running gives slightly varying results.
Qiskit এ অ্যালগরিদম এর ভূমিকা এখানেই উপসংহার। গভীর ভাবে বা আরো অন্যান্য অ্যালগরিদম পরখ করতে বাকি টিউটোরিয়াল গুলি দেখতে পারেন।
[8]:
import qiskit.tools.jupyter
%qiskit_version_table
%qiskit_copyright
Version Information
Qiskit Software | Version |
---|---|
qiskit-terra | 0.23.0.dev0+f52bb33 |
qiskit-aer | 0.11.1 |
qiskit-ignis | 0.7.1 |
qiskit-ibmq-provider | 0.19.2 |
qiskit-nature | 0.5.0 |
qiskit-optimization | 0.5.0 |
qiskit-machine-learning | 0.6.0 |
System information | |
Python version | 3.10.4 |
Python compiler | Clang 12.0.0 |
Python build | main, Mar 31 2022 03:38:35 |
OS | Darwin |
CPUs | 4 |
Memory (Gb) | 32.0 |
Wed Dec 07 11:02:26 2022 CET |
This code is a part of Qiskit
© Copyright IBM 2017, 2022.
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.
[ ]: