forked from turleya/Heisenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basis.py
195 lines (147 loc) · 4.17 KB
/
basis.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import numpy as np
#Pauli matrices
x = np.array([[0,1],[1,0]])
y = np.array([[0, -1j], [1j,0]])
z = np.array([[1,0], [0,-1]])
#Spin up and down
Up = np.array([[1],[0]])
Down = np.array([[0], [1]])
#Spin operators
sx = 1./2*x
sy = 1./2*y
sz = 1./2*z
splus = np.array([[0,1],[0,0]])
sminus = np.array([[0,0],[1,0]])
#Identity matrix
I = np.identity(2)
ZeroMat = np.array([[0, 0], [0, 0]])
"""
#Tensor products
#Sz
sz1 = np.tensordot(sz, I, axes=0)
S_1z = np.tensordot(sz1, I, axes=0)
sz2 = np.tensordot(I, sz, axes=0)
S_2z = np.tensordot(sz2, I, axes=0)
sz3 = np.tensordot(I, I, axes=0)
S_3z = np.tensordot(sz3, sz, axes=0)
#Sx
sx1 = np.tensordot(sx, I, axes=0)
S_1x = np.tensordot(sx1, I, axes=0)
sx2 = np.tensordot(I, sx, axes=0)
S_2x = np.tensordot(sx2, I, axes=0)
sx3 = np.tensordot(I, I, axes=0)
S_3x = np.tensordot(sx3, sx, axes=0)
#Sy
sy1 = np.tensordot(sy, I, axes=0)
S_1y = np.tensordot(sy1, I, axes=0)
sy2 = np.tensordot(I, sy, axes=0)
S_2y = np.tensordot(sy2, I, axes=0)
sy3 = np.tensordot(I, I, axes=0)
S_3y = np.tensordot(sy3, sy, axes=0)
"""
"""
def createMatrix(sign, index, particles):
if sign == "plus":
sign = splus
else:
sign = sminus
l = []
mat = np.array(l, ndmin=particles)
for i in range(0, particles):
l.append(ZeroMat)
np.append(mat, l)
print(mat)
"""
#Defining basis for 3 particles
def particleInit(particles):
a = [[0] for i in range(0,1<<particles)]
for i in range(0, 1<<particles):
arr = []
for j in range(0, particles):
if(i & (1<<j)):
arr.append(1)
else:
arr.append(0)
a[i] = arr
return a
def normaliseMatrices(list):
mats = []
for element in list:
l = []
for x in range(0, len(element)):
if element[x][0] == [1]:
l.append([1])
l.append([0])
else:
l.append([0])
l.append([1])
mat = np.array(l)
mats.append(mat)
return mats
#Puts spins in order: up up up first, down down down last for ease
def main(particles):
parts = particleInit(particles)
for part in parts:
part.reverse()
parts.reverse()
#print(parts)
#prints all posssible combination of spins for x number of particles
main(2)
print("-------")
#Spin operators
#Sz_i operators
def spinoperatorsz(particles):
for i in range(0, particles):
W = np.identity(2*particles)
W_i = W
W_i[2*i:2+2*i, 2*i:2+2*i] = sz
#print(W_i)
spinoperatorsz(2)
#Splus_i operators
def spinoperatorsplus(particles):
for i in range(0, particles):
W = np.identity(2*particles)
W_i = W
W_i[2*i:2+2*i, 2*i:2+2*i] = splus
#print(W_i)
spinoperatorsplus(2)
#Sminus_i operators
def spinoperatorsminus(particles):
for i in range(0, particles):
W = np.identity(2*particles)
W_i = W
W_i[2*i:2+2*i, 2*i:2+2*i] = sminus
#print(W_i)
spinoperatorsminus(2)
#def spinoperator(inp,spin,outp):
'''
def _init_(particles,J_xy,J_zz,counts):
#length of matrix
self.J_xy = J_xy #strength of flip-flop term
self.J_zz = J_zz #strength of Ising interaction
self.counts = counts #number of times the code will sweep through states
parts = particleInit(particles)
#Forming matrix containing each part of basis
self.B = np.vstack(part in parts.reshape((3,1<<particles)))
print self.B
l = 0.0
E = 0.0
E_sq = 0.0
for l in range(self.counts):
l += 1
for i in range(0,particles):
for j in range(0,1<<particles):
s = self.B[i,j]
#trying to define all operators to act on basis B
Z_i*Up = (1./2)*Up
Z_i*Down = (1./2)*Down
Plus_i*Up = 0
Plus_i*Down = (1./2)*Up
Minus_i*Up = (1./2)*Down
Minus_i*Down = 0
#Hamiltonian
H = J_zz*Z_i*Z_(i+1)*self.B + J_xy*Plus_i*Minus_(i+1)*self.B + J_xy*Minus_i*Plus_(i+1)*self.B
#How to turn this into the Hamiltonian matrix?
return H
print H
'''