forked from turleya/Heisenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeisenberg2.py
173 lines (135 loc) · 4.66 KB
/
Heisenberg2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import numpy as np
from numpy import linalg as la
#Spin operators
sx = np.array([[0,1./2],[1./2,0]])
sy = np.array([[0, -1./2j], [1./2j,0]])
sz = np.array([[1./2,0], [0,-1./2]])
splus = np.array([[0,1],[0,0]])
sminus = np.array([[0,0],[1,0]])
#Spin up and down
Up = np.array([[1],[0]])
Down = np.array([[0], [1]])
I = np.identity(2)
#Defining basis of all possible combinations of spins for n particles
def particleInit(particles): # not necessary
for i in range(0,1<<particles):
B = np.arange(1<<particles)
B = B.reshape((1<<particles,1))
C = np.zeros_like(B)
C_i = C
C_i[1-i,0] = 1
#print(C_i)
particleInit(3)
#Spin operators
#Sz Operator
def spinoperatorz(particles,index):
if particles == index:
P_i = sz
else:
P_i = I
for i in range(1, particles):
if (particles-i) == index:
P_i = np.bmat([[sz[0,0]*P_i, sz[0,1]*P_i], [sz[1,0]*P_i, sz[1,1]*P_i]])
else:
P_i = np.bmat([[I[0,0]*P_i, I[0,1]*P_i], [I[1,0]*P_i, I[1,1]*P_i]])
return(P_i)
#Splus operator
def spinoperatorplus(particles,index):
if particles == index:
P_i = splus
else:
P_i = I
for i in range(1, particles):
if (particles-i) == index:
P_i = np.bmat([[splus[0,0]*P_i, splus[0,1]*P_i], [splus[1,0]*P_i, splus[1,1]*P_i]])
else:
P_i = np.bmat([[I[0,0]*P_i, I[0,1]*P_i], [I[1,0]*P_i, I[1,1]*P_i]])
return(P_i)
#Sminus operator
def spinoperatorminus(particles,index):
if particles == index:
P_i = sminus
else:
P_i = I
for i in range(1, particles):
if (particles-i) == index:
P_i = np.bmat([[sminus[0,0]*P_i, sminus[0,1]*P_i], [sminus[1,0]*P_i, sminus[1,1]*P_i]])
else:
P_i = np.bmat([[I[0,0]*P_i, I[0,1]*P_i], [I[1,0]*P_i, I[1,1]*P_i]])
return(P_i)
#Sx operator
def spinoperatorx(particles,index):
if particles == index:
P_i = sx
else:
P_i = I
for i in range(1, particles):
if (particles-i) == index:
P_i = np.bmat([[sx[0,0]*P_i, sx[0,1]*P_i], [sx[1,0]*P_i, sx[1,1]*P_i]])
else:
P_i = np.bmat([[I[0,0]*P_i, I[0,1]*P_i], [I[1,0]*P_i, I[1,1]*P_i]])
return(P_i)
#Sy operator
def spinoperatory(particles,index):
if particles == index:
P_i = sy
else:
P_i = I
for i in range(1, particles):
if (particles-i) == index:
P_i = np.bmat([[sy[0,0]*P_i, sy[0,1]*P_i], [sy[1,0]*P_i, sy[1,1]*P_i]])
else:
P_i = np.bmat([[I[0,0]*P_i, I[0,1]*P_i], [I[1,0]*P_i, I[1,1]*P_i]])
return(P_i)
#Heisenberg Hamiltonian
def Hamiltonian(particles):
J_ij = 1.0 #anistropy parameter
B_j = 0.0 #local magnetic field
#Chain
H = np.zeros([2**particles,2**particles])
#Loop
#H = 0.5*J_ij*(spinoperatorplus(particles,1)*spinoperatorminus(particles,particles) + spinoperatorplus(particles,particles)*spinoperatorminus(particles,1)) + J_ij*(spinoperatorz(particles,1)*spinoperatorz(particles,particles))
#Particles not interacting
#H = -(0.5*J_ij*(spinoperatorplus(particles,2)*spinoperatorminus(particles,3) + spinoperatorplus(particles,3)*spinoperatorminus(particles,2)) + J_ij*(spinoperatorz(particles,2)*spinoperatorz(particles,3)))
for i in range(particles-1):
H_i = 0.5*J_ij*(spinoperatorplus(particles,i+1)*spinoperatorminus(particles,i+2) + spinoperatorplus(particles,i+2)*spinoperatorminus(particles,i+1)) + J_ij*(spinoperatorz(particles,i+1)*spinoperatorz(particles,i+2))
H = H + H_i
for j in range(particles):
H_j = B_j*spinoperatorz(particles, j+1)
H = H + H_j
return(H)
Ham = Hamiltonian(2)
w, v = la.eig(Ham)
#Computing eigenvalues of Hamiltonian
def eigenvalues(particles):
for i in range(0, 2**particles):
w_i = w[i]
return(round(w_i,8))
eigenvalues(2)
#Computing column eigenvectors of Hamiltonian
def eigenvectorsc(particles,index):
for i in range(0, 2**particles):
v_i = v[i]
l = v[i].reshape(2**particles,1)
if i == (index-1):
return(l)
eigenvectorsc(2,1)
#Computing row eigenvectors of Hamiltonian
def eigenvectorsr(particles,index):
for i in range(0, 2**particles):
v_i = v[i]
if i == (index-1):
return(v_i)
eigenvectorsr(2,4)
def spinoperator(particles,index):
so = [spinoperatorx(particles,index), spinoperatory(particles,index), spinoperatorz(particles,index)]
return(so)
spinoperator(2,1)
#Computing expectation values for
def expecval(particles):
for i in range(particles):
for j in range(2**particles):
Ket_i = spinoperator(particles, i+1)*eigenvectorsc(particles,j+1)
Bra_i = eigenvectorsr(particles,j+1)*Ket_i
print(Bra_i)
expecval(2)