You are on page 1of 7

12/14/2020 Quantum HW

In [1]: from sympy import *


x, y , z = symbols('x y z',real = True )
# In my notation, I use 'x' for angle beta and 'y' for angle gamma

In [2]: #mixing terms in Hamiltonian


m2 = Matrix([[cos(x), -I*sin(x), 0, 0], [-I*sin(x), cos(x), 0, 0], [0, 0, cos(x), -I*sin(x)], [0, 0, -I*sin(x
), cos(x)]])
m3 = Matrix([[cos(x), 0,-I*sin(x), 0], [0, cos(x), 0, -I*sin(x)], [-I*sin(x), 0, cos(x), 0], [0, -I*sin(x), 0
, cos(x)]])
m4 = m3*m2
m4

Out[2]: ⎡ cos
2
(x) −i sin (x) cos (x) −i sin (x) cos (x) − sin
2
(x) ⎤
2 2
⎢ −i sin (x) cos (x) cos (x) − sin (x) −i sin (x) cos (x) ⎥
⎢ ⎥
⎢ 2 2

⎢ −i sin (x) cos (x) − sin (x) cos (x) −i sin (x) cos (x) ⎥

⎣ 2 2 ⎦
− sin (x) −i sin (x) cos (x) −i sin (x) cos (x) cos (x)

In [3]: # cost Hamiltonian


m5 = diag(exp(I*y/2),exp(I*y/2),exp(-I*y/2),exp(-I*y/2))
m6 = diag(exp(-I*y/2),exp(I*y/2),exp(I*y/2),exp(-I*y/2))
m7 = m5*m6
m7

Out[3]: 1 0 0 0
⎡ ⎤
iy
⎢0 e 0 0 ⎥
⎢ ⎥
⎢ ⎥
⎢0 0 1 0 ⎥

⎣ −iy ⎦
0 0 0 e

localhost:8888/nbconvert/html/Quantum HW.ipynb?download=false 1/7


12/14/2020 Quantum HW

In [4]: #total matrix acting on the superposed states V(beta)*U(gamma)


m8 = m4*m7
m8

Out[4]: ⎡ cos
2
(x) −ie
iy
sin (x) cos (x) −i sin (x) cos (x) −e
−iy
sin
2
(x) ⎤
iy 2 2 −iy
⎢ −i sin (x) cos (x) e cos (x) − sin (x) −ie sin (x) cos (x) ⎥
⎢ ⎥
⎢ iy 2 2 −iy

⎢ −i sin (x) cos (x) −e sin (x) cos (x) −ie sin (x) cos (x) ⎥

⎣ 2 iy −iy 2 ⎦
− sin (x) −ie sin (x) cos (x) −i sin (x) cos (x) e cos (x)

In [5]: #superposed states /s> = (0.5, 0.5, 0.5,0.5)


# vectors /y,beta> = m8* /s>
m9 = Matrix([[0.5],[0.5],[0.5],[0.5]])
m10 = m8*m9
m10 # vector /gamma,beta> for p = 1

Out[5]: ⎡ −0.5ie
iy
sin (x) cos (x) − 0.5i sin (x) cos (x) + 0.5 cos
2
(x) − 0.5e
−iy
sin
2
(x) ⎤

2
⎢ 0.5eiy cos2 (x) − 0.5 sin (x) − 0.5i sin (x) cos (x) − 0.5ie−iy sin (x) cos (x) ⎥
⎢ ⎥
⎢ iy 2 2 −iy

⎢ −0.5e sin (x) − 0.5i sin (x) cos (x) + 0.5 cos (x) − 0.5ie sin (x) cos (x) ⎥

⎣ iy 2 −iy 2 ⎦
−0.5ie sin (x) cos (x) − 0.5 sin (x) − 0.5i sin (x) cos (x) + 0.5e cos (x)

In [6]: # The expecation value is calculated from the graph with grid search method and the angle obtained is gamma =
0.9 and beta = 2.7.
# The minimum expectation value obtained is -0.5.

In [7]: # Inserting gamma = 0.9 and beta = 2.7 into our /gamma,beta>
m11 = m10.subs([(x,2.7),(y,0.9)])
m11

Out[7]: ⎡
0.408673218985659 − 0.0913267810143413e
−0.9i
+ 0.193191121888997ie
0.9i
+ 0.193191121888997i

−0.9i 0.9i
⎢ −0.0913267810143413 + 0.193191121888997ie + 0.193191121888997i + 0.408673218985659e ⎥
⎢ ⎥
⎢ 0.9i −0.9i ⎥
⎢ 0.408673218985659 − 0.0913267810143413e + 0.193191121888997ie + 0.193191121888997i ⎥

⎣ −0.9i 0.9i ⎦
−0.0913267810143413 + 0.408673218985659e + 0.193191121888997ie + 0.193191121888997i

localhost:8888/nbconvert/html/Quantum HW.ipynb?download=false 2/7


12/14/2020 Quantum HW

In [8]: # last Part (p = 2)

from sympy.core.rules import Transform


m12 = m8 * m11
lasta = m12[3]
lastc = m12[1]
absolutea = re(lasta) ** 2 + im(lasta)**2
absolutec = re(lastc) ** 2 + im(lastc)**2

lastexpectation = (absolutea - absolutec)


expr = lastexpectation.xreplace(Transform(lambda x: x.round(2), lambda x: isinstance(x, Float)))
str(simplify(expr))

Out[8]: '0.25*(1 - cos(2*x))**2*sin(y) - 7.2e-7*(1 - cos(2*x))**2*sin(2*y) - 0.31*(1 - cos(2*x))**2*cos(y) - 0.31*(1


- cos(2*x))**2 + 0.15*sin(2*x) - 0.075*sin(4*x) - 0.5*sin(y) + 1.4e-6*sin(2*y) + 0.0047*sin(2*x - 2*y) - 0.25
*sin(2*x - y) + 0.25*sin(2*x + y) + 0.0047*sin(2*x + 2*y) + 0.0023*sin(4*x - 2*y) + 0.0039*sin(4*x - y) + 0.0
039*sin(4*x + y) + 0.0023*sin(4*x + 2*y) - 0.8*cos(2*x) + 0.63*cos(y) - 0.0015*cos(2*x - 2*y) - 0.31*cos(2*x
- y) - 0.31*cos(2*x + y) + 0.0015*cos(2*x + 2*y) - 0.00077*cos(4*x - 2*y) + 0.075*cos(4*x - y) - 0.075*cos(4*
x + y) + 0.00077*cos(4*x + 2*y) + 0.3'

In [9]: m14 = m12.subs([(x,2.7),(y,2.2)])


finalabs = m14[1]

simplify(re(finalabs)**2 + im(finalabs)**2)

# probability to get correct state at p = 2

Out[9]: 0.657935111308894

localhost:8888/nbconvert/html/Quantum HW.ipynb?download=false 3/7


12/14/2020 Quantum HW

In [12]: # graph for p = 1


from mpl_toolkits.mplot3d import Axes3D
#import math tools
import numpy as np

# We import the tools to handle general Graphs


import networkx as nx

# We import plotting tools


import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
%config InlineBackend.figure_format = 'svg' # Makes the images look nice

step_size = 0.1;

a_gamma = np.arange(0, np.pi, step_size)


a_beta = np.arange(0, np.pi, step_size)
a_gamma, a_beta = np.meshgrid(a_gamma,a_beta)
F1 = np.sin(a_beta) * np.cos(a_beta) * np.sin(a_gamma)*(2*np.cos(a_beta)**2*np.cos(a_gamma) + 2*np.cos(a_beta
)**2 - 1)
# Grid search for the minimizing variables
result = np.where(F1 == np.amin(F1))
a = list(zip(result[0],result[1]))[0]

beta = a[0]*step_size;
gamma = a[1]*step_size;

# Plot the expetation value F1


fig = plt.figure()
ax = fig.gca(projection='3d')

surf = ax.plot_surface(a_gamma, a_beta, F1, cmap=cm.coolwarm, linewidth=0, antialiased=True)

ax.set_zlim(-0.2,0.5)
ax.zaxis.set_major_locator(LinearLocator(3))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

plt.show()

#The smallest parameters and the expectation can be extracted


print('\n --- OPTIMAL PARAMETERS --- \n')

localhost:8888/nbconvert/html/Quantum HW.ipynb?download=false 4/7


12/14/2020 Quantum HW

print('The minimial expectation value is: M1 = %.03f' % np.amin(F1))


print('This is attained for gamma = %.03f and beta = %.03f' % (gamma,beta))

--- OPTIMAL PARAMETERS ---

The minimial expectation value is: M1 = -0.500


This is attained for gamma = 0.900 and beta = 2.700

localhost:8888/nbconvert/html/Quantum HW.ipynb?download=false 5/7


12/14/2020 Quantum HW

In [11]: #graph for p = 2


#import math tools
import numpy as np

# We import the tools to handle general Graphs


import networkx as nx

# We import plotting tools


import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
from mpl_toolkits.mplot3d import Axes3D
%config InlineBackend.figure_format = 'svg' # Makes the images look nice

step_size = 0.1;

a_gamma = np.arange(0, np.pi, step_size)


a_beta = np.arange(0, np.pi, step_size)
a_gamma, a_beta = np.meshgrid(a_gamma,a_beta)

F1 = 0.25*(1 - np.cos(2*a_beta))**2*np.sin(a_gamma) - 7.2e-7*(1 - np.cos(2*a_beta))**2*np.sin(2*a_gamma) - 0.


31*(1 - np.cos(2*a_beta))**2*np.cos(a_gamma) - 0.31*(1 - np.cos(2*a_beta))**2 + 0.15*np.sin(2*a_beta) - 0.075
*np.sin(4*a_beta) - 0.5*np.sin(a_gamma) + 1.4e-6*np.sin(2*a_gamma) + 0.0047*np.sin(2*a_beta - 2*a_gamma) - 0.
25*np.sin(2*a_beta - a_gamma) + 0.25*np.sin(2*a_beta + a_gamma) + 0.0047*np.sin(2*a_beta + 2*a_gamma) + 0.002
3*np.sin(4*a_beta - 2*a_gamma) + 0.0039*np.sin(4*a_beta - a_gamma) + 0.0039*np.sin(4*a_beta + a_gamma) + 0.00
23*np.sin(4*a_beta + 2*a_gamma) - 0.8*np.cos(2*a_beta) + 0.63*np.cos(a_gamma) - 0.0015*np.cos(2*a_beta - 2*a_
gamma) - 0.31*np.cos(2*a_beta - a_gamma) - 0.31*np.cos(2*a_beta + a_gamma) + 0.0015*np.cos(2*a_beta + 2*a_gam
ma) - 0.00077*np.cos(4*a_beta - 2*a_gamma) + 0.075*np.cos(4*a_beta - a_gamma) - 0.075*np.cos(4*a_beta + a_gam
ma) + 0.00077*np.cos(4*a_beta + 2*a_gamma) + 0.3
result = np.where(F1 == np.amin(F1))
a = list(zip(result[0],result[1]))[0]

beta = a[0]*step_size;
gamma = a[1]*step_size;

# Plot the expetation value F1


fig = plt.figure()
ax = fig.gca(projection='3d')

surf = ax.plot_surface(a_gamma, a_beta, F1, cmap=cm.coolwarm, linewidth=0, antialiased=True)

ax.set_zlim(-0.7,0.5)

localhost:8888/nbconvert/html/Quantum HW.ipynb?download=false 6/7


12/14/2020 Quantum HW

ax.zaxis.set_major_locator(LinearLocator(3))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

plt.show()

#The smallest parameters and the expectation can be extracted


print('\n --- OPTIMAL PARAMETERS --- \n')
print('The minimial expectation value is: M1 = %.03f' % np.amin(F1))
print('This is attained for gamma = %.03f and beta = %.03f' % (gamma,beta))

--- OPTIMAL PARAMETERS ---

The minimial expectation value is: M1 = -0.641


This is attained for gamma = 2.200 and beta = 2.700

localhost:8888/nbconvert/html/Quantum HW.ipynb?download=false 7/7

You might also like