-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_spin_operators.py
287 lines (244 loc) · 9.52 KB
/
mod_spin_operators.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python3
'''
/************************/
/* mod_spin_operators */
/* Version 1.0 */
/* 2024/05/11 */
/************************/
'''
import cmath
import math
import numpy as np
import random
import sys
class SingleSpin:
def __init__(self, basis: str = 'ud'):
self.__basis = basis
self.__state = None
if (basis == 'ud'):
self.__u = np.array([[1], [0]], dtype=complex)
self.__d = np.array([[0], [1]], dtype=complex)
self.__r = np.array(
[[1 / np.sqrt(2)], [1 / np.sqrt(2)]], dtype=complex)
self.__l = np.array(
[[1 / np.sqrt(2)], [-1 / np.sqrt(2)]], dtype=complex)
self.__i = np.array(
[[1 / np.sqrt(2)], [1j / np.sqrt(2)]], dtype=complex)
self.__o = np.array(
[[1 / np.sqrt(2)], [-1j / np.sqrt(2)]], dtype=complex)
self.__sz = np.array([[1, 0], [0, -1]], dtype=complex)
self.__sx = np.array([[0, 1], [1, 0]], dtype=complex)
self.__sy = np.array([[0, -1j], [1j, 0]], dtype=complex)
elif (basis == 'rl'):
self.__u = np.array(
[[1 / np.sqrt(2)], [1 / np.sqrt(2)]], dtype=complex)
self.__d = np.array(
[[1 / np.sqrt(2)], [-1 / np.sqrt(2)]], dtype=complex)
self.__r = np.array([[1], [0]], dtype=complex)
self.__l = np.array([[0], [1]], dtype=complex)
self.__i = np.array(
[[(1 + 1j) / 2], [(1 - 1j) / 2]], dtype=complex)
self.__o = np.array(
[[(1 - 1j) / 2], [(1 + 1j) / 2]], dtype=complex)
elif (basis == 'io'):
self.__u = np.array(
[[1 / np.sqrt(2)], [1 / np.sqrt(2)]], dtype=complex)
self.__d = np.array(
[[1j / np.sqrt(2)], [-1j / np.sqrt(2)]], dtype=complex)
self.__r = np.array(
[[(1 + 1j) / 2], [(1 - 1j) / 2]], dtype=complex)
self.__l = np.array(
[[(1 - 1j) / 2], [(1 + 1j) / 2]], dtype=complex)
self.__o = np.array([[1], [0]], dtype=complex)
self.__i = np.array([[0], [1]], dtype=complex)
else:
raise NotImplementedError(
"Basis " + basis + "not Implemented")
@property
def u(self):
return self.__u
@property
def d(self):
return self.__d
@property
def r(self):
return self.__r
@property
def l(self):
return self.__l
@property
def i(self):
return self.__i
@property
def o(self):
return self.__o
@property
def s_z(self):
if (self.__basis != 'ud'):
raise NotImplementedError(
"S_z for basis " + self.__basis + "not Implemented")
return self.__sz
@property
def s_x(self):
if (self.__basis != 'ud'):
raise NotImplementedError(
"S_x for basis " + self.__basis + "not Implemented")
return self.__sx
@property
def s_y(self):
if (self.__basis != 'ud'):
raise NotImplementedError(
"S_y for basis " + self.__basis + "not Implemented")
return self.__sy
@property
def psi(self):
return self.__state
@psi.setter
def psi(self, value):
# check that length is unitary
assert math.isclose(np.linalg.norm(value), 1)
self.__state = value
def theta(self):
assert self.__state
return 2 * np.arccos(np.linalg.norm(self.__state[0][0]))
def phi(self):
assert self.__state
if self.__state[1][0] != 0:
return cmath.phase(self.__state[1][0])
else:
return 0
def angles(self):
return [self.theta(), self.phi()]
class TwoSpin:
def __init__(self, basis: str = 'ud'):
self.__basis = basis
self.__state = None
if (basis == 'ud'):
u = np.array([[1], [0]], dtype=complex)
d = np.array([[0], [1]], dtype=complex)
self.__b = [
np.kron(u, u), np.kron(u, d),
np.kron(d, u), np.kron(d, d)
]
self.__bmap = {'uu': 0, 'ud': 1, 'du': 2, 'dd': 3}
self.__s = [
np.array([[1, 0], [0, -1]], dtype=complex),
np.array([[0, 1], [1, 0]], dtype=complex),
np.array([[0, -1j], [1j, 0]], dtype=complex),
np.array([[1, 0], [0, 1]], dtype=complex)
]
self.__smap = {'z': 0, 'x': 1, 'y': 2, 'I': 3}
else:
raise NotImplementedError(
"Basis " + basis + "not Implemented")
@property
def psi(self):
return self.__state
@ psi.setter
def psi(self, value):
# check that length is unitary
assert math.isclose(np.linalg.norm(value), 1)
self.__state = value
def BasisVector(self, s):
return self.__b[self.__bmap[s]]
def Sigma(self, sA: str, sB: str):
return np.kron(
self.__s[self.__smap[sA]], self.__s[self.__smap[sB]])
def Sigma_A(self, s: str):
return np.kron(self.__s[self.__smap[s]], self.__s[3])
def Sigma_B(self, s: str):
return np.kron(self.__s[3], self.__s[self.__smap[s]])
def Expectation(self, sA: str, sB: str):
if (sA == 'I'):
# expectation of System B
return np.linalg.multi_dot([
self.__state.conj().T, self.Sigma_B(sB), self.__state])[0, 0]
elif (sB == 'I'):
# expectation of System A
return np.linalg.multi_dot([
self.__state.conj().T, self.Sigma_A(sA), self.__state])[0, 0]
else:
# expectation of the composite system
return np.linalg.multi_dot([
self.__state.conj().T, self.Sigma(sA, sB), self.__state])[0, 0]
def ProductState(self, A: np.array, B: np.array):
# check that length is unitary
assert math.isclose(np.linalg.norm(A), 1)
assert math.isclose(np.linalg.norm(B), 1)
self.psi = np.kron(A, B)
def Singlet(self):
self.psi = 1 / np.sqrt(2) * (self.__b[1] - self.__b[2])
def Triplet(self, i: int):
match i:
case 1:
self.psi = 1 / np.sqrt(2) * (self.__b[1] + self.__b[2])
case 2:
self.psi = 1 / np.sqrt(2) * (self.__b[0] + self.__b[3])
case 3:
self.psi = 1 / np.sqrt(2) * (self.__b[0] - self.__b[3])
case _:
raise ValueError("Incorrect index " + str(i))
def Measure(self, directions1: np.ndarray,
directions2: np.ndarray, simulate_1: bool = True,
update: bool = False):
'''
Perform the measurement of the spin of two directions 1 and 2,
which one to simulate is decided by the caller.
'''
def Rho1(psi: np.ndarray):
return np.outer(psi, psi.conj()).reshape(
(2, 2, 2, 2)).trace(axis1=1, axis2=3)
def Rho2(psi: np.ndarray):
return np.outer(psi, psi.conj()).reshape((
2, 2, 2, 2)).trace(axis1=0, axis2=2)
psi = self.__state
# Define the projector operator for the "+1" state
direction1_p1 = np.array(directions1[0])
direction1_m1 = np.array(directions1[1])
direction2_p1 = np.array(directions2[0])
direction2_m1 = np.array(directions2[1])
projector1_p1 = np.outer(direction1_p1, direction1_p1.conj())
projector1_m1 = np.outer(direction1_m1, direction1_m1.conj())
projector2_p1 = np.outer(direction2_p1, direction2_p1.conj())
projector2_m1 = np.outer(direction2_m1, direction2_m1.conj())
if simulate_1:
# Create 4x4 projectors for the two-spin system
projector_p1_s = np.kron(projector1_p1, np.eye(2))
projector_m1_s = np.kron(projector1_m1, np.eye(2))
projector_p11 = projector1_p1
projector_p21 = projector2_p1
# Calculate the reduced density matrix for the first spin
# which is measured
rho_i = Rho1(psi)
else:
projector_p1_s = np.kron(np.eye(2), projector2_p1)
projector_m1_s = np.kron(np.eye(2), projector2_m1)
projector_p11 = projector2_p1
projector_p21 = projector1_p1
rho_i = Rho2(psi)
# Calculate the probability of this first spin being "+1"
prob_p11 = np.linalg.norm(np.trace(np.dot(projector_p11, rho_i)))
# Generate a random number between 0 and 1
random_number1 = random.uniform(0, 1)
# Perform the measurement in apparatus direction
sp1 = 1 if random_number1 < prob_p11 else -1
# reduce psi projecting on the direction
psi_r = np.dot(projector_p1_s, psi) if sp1 == 1 else \
np.dot(projector_m1_s, psi)
# Normalize psi_r
psi_r = psi_r / np.linalg.norm(psi_r)
# Calculate the reduced density matrix for the second spin
# with the collapsed wave function for the first system
rho_j = Rho2(psi_r) if simulate_1 else Rho1(psi_r)
# Calculate the probability of "system 2" being "+1"
prob_p12 = np.linalg.norm(np.trace(np.dot(projector_p21, rho_j)))
# Generate a random number between 0 and 1
random_number2 = random.uniform(0, 1)
sp2 = 1 if random_number2 < prob_p12 else -1
if update:
self.__state = psi
return (sp1, sp2)
if __name__ == '__main__':
if sys.version_info[0] < 3:
raise RuntimeError('Must be using Python 3')
pass