After practicing the quantum gate simulator Blueqat, let's find a simple Hamiltonian base energy using the VQE algorithm. What is Blueqat: https://blueqat.readthedocs.io/ja/latest/ What is VQE: http://dojo.qulacs.org/ja/latest/notebooks/5.1_variational_quantum_eigensolver.html Other referenced pages: https://qiita.com/YuichiroMinato/items/62444351b712743d83b7 http://dkopczyk.quantee.co.uk/vqe/
This time, like this article, RY to state $ \ ket {0} = (1, 0) ^ {\ mathrm {T}} $ By making the ($ \ theta $) gate work, a state $ \ ket {\ psi (\ theta)} $ that depends on the parameter $ \ theta $ is created. Calculate the inner product $ \ braket {\ psi (\ theta)} {H \ psi (\ theta)} $ with various $ \ theta $ values, Due to the Variational Method (https://en.wikipedia.org/wiki/Variational_method_(quantum_mechanics)), this dot product $ \ braket {\ psi (\ theta)} {H \ psi (\ theta)} $ is the smallest By finding the $ \ theta (= \ theta_ {\ mathrm {min}}) $ that becomes, the ground state $ \ ket {\ psi (\ theta_ {\ mathrm {min}})} $ is found. The RY ($ \ theta $) gate is an operation that rotates the state around the y-axis of the bloch sphere.
RY(\theta) =
\left(
\begin{array}{cc}
\cos{\Big( \frac{\theta}{2}\Big)} & -\sin{\Big( \frac{\theta}{2}\Big)} \\
\sin{\Big( \frac{\theta}{2}\Big)} & \cos{\Big( \frac{\theta}{2}\Big)}\\
\end{array}
\right)
Also this time, as Hamiltonian $ H $, Pauli matrices $ Z $ as in this article
Z = \left(
\begin{array}{cc}
1 & 0 \\
0 & -1\\
\end{array}
\right)
Is used. Originally, $ Z $ is diagonalized, and its ground state is $ \ ket {1} = (0, 1) ^ {\ mathrm {T}} $ (complex multiple), so this parameter $ The \ theta $ search is successful if it is found that $ \ theta = \ pi $ is a parameter that gives the ground state. Because in this setting
\ket{\psi(\theta = \pi)} = \left(
\begin{array}{cc}
\cos{\Big( \frac{\pi}{2}\Big)} & -\sin{\Big( \frac{\pi}{2}\Big)} \\
\sin{\Big( \frac{\pi}{2}\Big)} & \cos{\Big( \frac{\pi}{2}\Big)}\\
\end{array}
\right)
\left(
\begin{array}{c}
1 \\
0 \\
\end{array}
\right)
=
\left(
\begin{array}{cc}
0 & -1 \\
1 & 0\\
\end{array}
\right)
\left(
\begin{array}{c}
1 \\
0 \\
\end{array}
\right)
=
\left(
\begin{array}{c}
0 \\
1 \\
\end{array}
\right)
That is why.
Do it with jupyter notebook. If you haven't installed Blueqat, install pip below.
!pip3 install blueqat
Import the library used this time
import numpy as np
import matplotlib.pyplot as plt
from blueqat import Circuit
First, as a parameter candidate, prepare a parameter from $ 0 $ to $ 2 \ pi $ divided into 20 equal parts.
angles = np.linspace(0.0,2*np.pi,20)
angles
# array([0. , 0.33069396, 0.66138793, 0.99208189, 1.32277585,
# 1.65346982, 1.98416378, 2.31485774, 2.64555171, 2.97624567,
# 3.30693964, 3.6376336 , 3.96832756, 4.29902153, 4.62971549,
# 4.96040945, 5.29110342, 5.62179738, 5.95249134, 6.28318531])
First, use the fourth angle (angle [3] = 0.99208189) to calculate the inner product $ \ braket {\ psi (\ theta)} {H \ psi (\ theta)} $. In Blueqat, you can build a gate circuit by first preparing a circuit (Circuit ()) and adding a gate (RY gate this time) to it. The RY gate may add ry ({angle}) [{qubit number to act}] as follows.
#circuit
Circuit().ry(angles[3])[0]
Also, when the run method is executed on the gate circuit, the state vector at that stage is obtained by numpy.
#Because it is in the initial state| 0>
Circuit().run()
# array([1.+0.j])
# | 0>State after applying RY gate to
Circuit().ry(angles[3])[0].run()
# array([0.87947375+0.j, 0.47594739+0.j])
Now, let's actually calculate the inner product $ \ braket {\ psi (\ theta)} {H \ psi (\ theta)} $. First, $ \ ket {\ psi (\ theta)} $ looks like this:
psi = Circuit().ry(angles[3])[0].run()
psi
# array([0.87947375+0.j, 0.47594739+0.j])
Then, $ \ ket {H \ psi (\ theta)} $ becomes as follows.
h_psi = Circuit().ry(angles[3])[0].z[0].run()
h_psi
# array([ 0.87947375+0.j, -0.47594739+0.j])
From these, the inner product $ \ braket {\ psi (\ theta)} {H \ psi (\ theta)} $ is as follows.
np.dot(psi, h_psi)
# (0.5469481581224267+0j)
Then, calculate this inner product for the first 20 angles created, plot the horizontal axis as the angle and the vertical axis as the inner product, and draw a line as follows.
energies = []
for angle in angles:
psi = Circuit().ry(angle)[0].run()
z_psi = Circuit().ry(angle)[0].z[0].run()
energies.append(np.dot(psi, z_psi))
plt.xlabel('angle')
plt.ylabel('Expectation value')
plt.plot(angles, energies)
plt.show()
It seems that the minimum value is taken near $ \ theta = 3 $, so the VQE algorithm is successful.
By the way, when creating a state $ \ ket {\ psi (\ theta)} $ that depends on the parameter $ \ theta
RZ(\theta) =
\left(
\begin{array}{cc}
e^{-i\frac{\theta}{2}} & 0 \\
0 & e^{ i\frac{\theta}{2}}\\
\end{array}
\right)
The state $ \ ket {\ psi (\ theta)} $ created by this is in the form of
\ket{\psi(\theta)} = \left(
\begin{array}{cc}
e^{-i\frac{\theta}{2}} & 0 \\
0 & e^{ i\frac{\theta}{2}}\\
\end{array}
\right)
\left(
\begin{array}{c}
1 \\
0 \\
\end{array}
\right)
=
\left(
\begin{array}{c}
e^{-i\frac{\theta}{2}} \\
0 \\
\end{array}
\right)
=
e^{-i\frac{\theta}{2}}
\left(
\begin{array}{c}
1 \\
0 \\
\end{array}
\right)
This is because the ground state $ \ ket {1} = (0, 1) ^ {\ mathrm {T}} $ (complex multiple) cannot be created with the value of any parameter $ \ theta $.
Quantum computer fun
Recommended Posts