Just for completeness’ sake: the full code I’m dealing with is the following.
import numpy as np
import math as math
import matplotlib.pyplot as plt
def sigma_x(j,n):
if j>n:
raise Exception('j should not exceed n')
elif j==n:
return sigma_x(0,n)
else:
m=1
for k in range(j):
m=np.kron(m,np.eye(2))
m=np.kron(m,np.array([[0,1],[1,0]]))
for k in range (j+1,n):
m=np.kron(m,np.eye(2))
return m
def sigma_z(j,n):
if j>n:
raise Exception('j should not exceed n')
elif j==n:
return sigma_z(0,n)
else:
m=1
for k in range(j):
m=np.kron(m,np.eye(2))
m=np.kron(m,np.array([[1,0],[0,-1]]))
for k in range (j+1,n):
m=np.kron(m,np.eye(2))
return m
def H(n,J,g,h,bound): #MAIN FUNCTION
term1=0.
term2=0.
term3=0.
if bound=='PBC':
for k in range(n):
term1+=np.dot(sigma_z(k,n),sigma_z(k+1,n))
for k in range(n):
term2+=sigma_x(k,n)
for k in range(n):
term3+=sigma_z(k,n)
return -J*term1-g*term2-h*term3 #PROBLEM HERE
elif bound=='OBC':
for k in range(n-1):
term1+=np.dot(sigma_z(k,n),sigma_z(k+1,n))
for k in range(n):
term2+=sigma_x(k,n)
for k in range(n):
term3+=sigma_z(k,n)
return -J*term1-g*term2-h*term3 #SAME AS ABOVE
else:
raise Exception('Choose PBC or OBC')
def energy(i,n,J,g,h,bound):
array=np.linalg.eigh(H(n,J,g,h,bound))[0]
return array[i]
def delta_E(n,J,g,h,bound):
return energy(1,n,J,g,h,bound)-energy(0,n,J,g,h,bound)
def plot_gap(n,J,bound):
g=np.linspace(0,1,50)
E=delta_E(n,J,g,0,bound)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.plot(g,E,'b')
plt.show()
The problem here is the variable g
in the function H
, which multiplies term2
(a 2D array). When I’m trying to plot the function delta_E
with g=np.linspace(...)
, I get the issue above.