[PYTHON] Try the Variational-Quantum-Eigensolver (VQE) algorithm with Blueqat

\def\bra#1{\mathinner{\left\langle{#1}\right|}} \def\ket#1{\mathinner{\left|{#1}\right\rangle}} \def\braket#1#2{\mathinner{\left\langle{#1}\middle|#2\right\rangle}}

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/

Setting

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.

Actually try using Blueqat

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() 
スクリーンショット 2020-02-09 20.52.21.png

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 , you can use the RX ( \ theta ) gate instead of the RY ( \ theta ) gate. The VQE algorithm succeeds. However, it does not succeed at the RZ ( \ theta ) gate. That is RZ ( \ 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 $.

Finally

Quantum computer fun

Recommended Posts

Try the Variational-Quantum-Eigensolver (VQE) algorithm with Blueqat
How to try the friends-of-friends algorithm with pyfof
I implemented VQE with Blueqat
Try blurring the image with opencv2
Compute the partition function with the sum-product algorithm
Try using the camera with Python's OpenCV
Search the maze with the python A * algorithm
Try the cloud-based algorithm trading service "Quantopian"
Try rewriting the file with the less command
Try to solve the traveling salesman problem with a genetic algorithm (Theory)
Try to solve the fizzbuzz problem with Keras
Try to solve the traveling salesman problem with a genetic algorithm (Python code)
Try to solve the traveling salesman problem with a genetic algorithm (execution result)
Try to solve the man-machine chart with Python
Visualize the behavior of the sorting algorithm with matplotlib
Find the shortest path with the Python Dijkstra's algorithm
Solving the Python knapsack problem with the greedy algorithm
Try to solve the programming challenge book with python3
Try translating with Python while maintaining the PDF layout
Try to visualize the room with Raspberry Pi, part 1
Try touching the micro: bit with VS Code + Python
The first algorithm to learn with Python: FizzBuzz problem
Try moving the servo motor with RasPi (180 degree version)
Try to get the contents of Word with Golang
[Neo4J] ④ Try to handle the graph structure with Cypher
Try to specify the axis with PyTorch's Softmax function
Try scraping with Python.
Try SNN with BindsNET
Try regression with TensorFlow
Try scraping the data of COVID-19 in Tokyo with Python
Try hitting the Twitter API quickly and easily with Python
CNN with keras Try it with the image you picked up
Try to play with the uprobe that supports Systemtap directly
Finding a solution to the N-Queen problem with a genetic algorithm (2)
[Python] Try optimizing FX systole parameters with a genetic algorithm
I implemented the FloodFill algorithm with TRON BATTLE of CodinGame.
Solve the spiral book (algorithm and data structure) with python!
Try to automate the operation of network devices with Python
Try to model a multimodal distribution using the EM algorithm
Try to decipher the garbled attachment file name with Python
Finding a solution to the N-Queen problem with a genetic algorithm (1)
Try to extract the features of the sensor data with CNN