-
Notifications
You must be signed in to change notification settings - Fork 2
/
pSC.py
410 lines (378 loc) · 15.7 KB
/
pSC.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import numpy as np
import scipy.linalg as la
import numpy.linalg as nla
import numpy.matlib
class Params:
def __init__(self,
Lx=4,
Ly=16,
Delta=1,
t=1,
m=1,
bcx=-1,
bcy=1,
T=0,
dxmax=16,
dymax=16,
history=False,
):
self.Lx = Lx
self.Ly = Ly
self.Delta = Delta
self.t = t
self.m = m
self.bcx = bcx
self.bcy = bcy
self.T = T
self.history = history
self.sigmax = np.array([[0, 1], [1, 0]])
self.sigmay = np.array([[0, -1j], [1j, 0]])
self.sigmaz = np.array([[1, 0], [0, -1]])
# check which one is faster, use sparse or dense?
if Lx<np.inf and Ly<np.inf:
hopx = np.diag(np.ones(Lx-1), -1)
hopx[0, -1] = bcx
hopy = np.diag(np.ones(Ly-1), -1)
hopy[0, -1] = bcy
hopxmat = np.kron( np.eye(Ly),hopx)
hopymat = np.kron(hopy,np.eye(Lx))
onsitemat = np.eye(Lx*Ly)
self.Hamiltonian = ((np.kron(hopxmat-hopxmat.T, self.sigmax)+np.kron(hopymat-hopymat.T, self.sigmay))* 1j*Delta-t*np.kron(hopxmat+hopxmat.T+hopymat+hopymat.T, self.sigmaz))/2+m*np.kron(onsitemat, self.sigmaz)
elif Lx==np.inf and Ly==np.inf:
self.dxmax=dxmax
self.dymax=dymax
self.dx=lambda kx: Delta*np.sin(kx)
self.dy=lambda ky: Delta*np.sin(ky)
self.dz=lambda kx,ky: m-t*np.cos(kx)-t*np.cos(ky)
else:
raise ValueError('The size of system {:d,:d} is not supported.'.format(Lx,Ly))
def bandstructure(self):
val, vec = nla.eigh(self.Hamiltonian)
sortindex = np.argsort(val)
self.val = val[sortindex]
self.vec = vec[:, sortindex]
def fermi_dist_k(self,kx,ky,branch,E_F=0):
if self.T==0:
return np.heaviside(E_F-self.E_k(kx,ky,branch),0)
else:
return 1/(1+np.exp((self.E_k(kx,ky,branch)-E_F)/self.T))
def fermi_dist(self, energy, E_F):
if self.T == 0:
return np.heaviside(E_F-energy, 0)
else:
return 1/(1+np.exp((energy-E_F)/self.T))
def correlation_matrix_inf_fft(self,threshold=[1024,512]):
'''
self.dxmax/self.dymax: the maximal distance in x/y direction (in terms of unit cell)
Directly call fft to evaluate the integral
'''
assert self.Lx==np.inf and self.Ly==np.inf, "Wire length should be inf"
# cov_mat=[]
Nxmax=max(2*self.dxmax,threshold[0])
Nymax=max(2*self.dymax,threshold[1])
if self.T>0:
pass #to be filled
else:
kxlist=np.arange(0,2*np.pi,2*np.pi/Nxmax)
kylist=np.arange(0,2*np.pi,2*np.pi/Nymax)
kxmap,kymap=np.meshgrid(kxlist,kylist)
dxmap=self.dx(kxmap)
dymap=self.dy(kymap)
dzmap=self.dz(kxmap,kymap)
Ekmap=np.sqrt(dxmap**2+dymap**2+dzmap**2)
Ekxymap=np.sqrt(dxmap**2+dymap**2)
costheta=dzmap/Ekmap
sintheta=Ekxymap/Ekmap
Ekxymap[0,0]=np.inf #to avoid 0/0 in cos(phi) & sin(phi); the order of this line matters
cosphi=dxmap/Ekxymap
sinphi=dymap/Ekxymap
fftcostheta=np.fft.ifft2(costheta)
constmap=np.zeros((Nymax,Nxmax))
constmap[0,0]=0.5
A_11=constmap-fftcostheta/2
A_22=constmap+fftcostheta/2
A_12=np.fft.ifft2(-(cosphi-1j*sinphi)/2*sintheta)
A_21=np.fft.ifft2(-(cosphi+1j*sinphi)/2*sintheta)
mat=np.stack([[A_11,A_12],[A_21,A_22]])
C_f=np.zeros((2*self.dxmax*self.dymax,2*self.dxmax*self.dymax))*1j
for i in range(self.dxmax*self.dymax):
for j in range(i):
ix,iy=i%self.dxmax,i//self.dxmax
jx,jy=j%self.dxmax,j//self.dxmax
dx,dy=(ix-jx)%Nxmax,(iy-jy)%Nymax
C_f[2*i:2*i+2,2*j:2*j+2]=mat[:,:,dy,dx]
C_f=C_f+C_f.T.conj()
for i in range(self.dxmax*self.dymax):
C_f[2*i:2*i+2,2*i:2*i+2]=mat[:,:,0,0]
self.C_f=C_f
def correlation_matrix(self, E_F=0):
'''
'''
if not (hasattr(self, 'val') and hasattr(self, 'vec')):
self.bandstructure()
occupancy_mat = np.matlib.repmat(self.fermi_dist(self.val, E_F), self.vec.shape[0], 1)
self.C_f=((occupancy_mat*self.vec)@self.vec.T.conj())
def covariance_matrix(self, E_F=0):
'''
c.f. notes
Maybe differs by a minus sign
'''
if not hasattr(self, 'C_f'):
if self.Lx < np.inf and self.Ly<np.inf:
self.correlation_matrix()
else:
self.correlation_matrix_inf_fft()
# G=self.C_f[::2,::2]
# F=self.C_f[::2,1::2]
G=self.C_f[1::2,1::2]
F=self.C_f[1::2,::2]
self.G,self.F=G,F
Gamma_11 = 1j*(F.conj().T+F+G-G.T)
Gamma_21 = -(np.eye(G.shape[0])+F.conj().T-F-G-G.T)
Gamma_12 = -Gamma_21.T
Gamma_22 = -1j*(F.conj().T+F+G.T-G)
Gamma = np.zeros((2*G.shape[0], 2*G.shape[0]), dtype=complex)
even = np.arange(2*G.shape[0])[::2]
odd = np.arange(2*G.shape[0])[1::2]
Gamma[np.ix_(even, even)] = Gamma_11
Gamma[np.ix_(even, odd)] = Gamma_12
Gamma[np.ix_(odd, even)] = Gamma_21
Gamma[np.ix_(odd, odd)] = Gamma_22
assert np.abs(np.imag(Gamma)).max() < 1e-10, "Covariance matrix not real {:.5f}".format(np.abs(np.imag(Gamma)).max())
self.C_m = np.real(Gamma-Gamma.T.conj())/2
self.C_m_history = [self.C_m]
def linearize_index(self, subregion, n, proj=False):
'''
If proj ==True, then the index used for projection operator will be returned
'''
subregion_x, subregion_y = (subregion)
subregion_x = np.array(subregion_x)
subregion_y = np.array(subregion_y)
X, Y = np.meshgrid(subregion_x, subregion_y)
if self.Ly<np.inf:
assert subregion_x.max()<self.Lx and subregion_y.max()<self.Ly, '({:d},{:d}) exceed ({:d},{:d})'.format(subregion_x.max(),self.Lx,subregion_y.max(),self.Ly)
linear_index = ((X+Y*self.Lx).flatten('F'))
else:
assert subregion_x.max()<self.dxmax and subregion_y.max()<self.dymax, '({:f},{:f}) exceed ({:f},{:f})'.format(subregion_x.max(),(self.dxmax),subregion_y.max(),(self.dymax))
linear_index = ((X+Y*self.dxmax).flatten('F'))
if proj:
return sorted(np.concatenate([n*linear_index+i for i in range(0, n, 2)]))
else:
return sorted(np.concatenate([n*linear_index+i for i in range(n)]))
def square_index(self, subregion):
subregion=np.unique(np.array(subregion)//2)
if self.Lx<np.inf and self.Ly<np.inf:
return subregion%self.Lx,subregion//self.Lx
else:
return subregion%self.dxmax,subregion//self.dxmax
def c_subregion_f(self, subregion, linear=True):
'''
subregion: [subregoin_x, subregion_y] index of unit cell
'''
if not hasattr(self, 'C_f'):
self.correlation_matrix()
try:
subregion = list(subregion)
except:
raise ValueError("The subregion is ill-defined"+subregion)
if len(subregion) == 2:
if isinstance(subregion[0], int) and isinstance(subregion[1], int):
linear = True
elif (not isinstance(subregion[0], int)) and (not isinstance(subregion[1], int)):
linear = False
else:
raise ValueError('Illegal subregion')
else:
linear = True
if linear:
# linearized index
subregion_index = subregion
else:
# 2D index
subregion_index = self.linearize_index(subregion, 2)
# This is a dumb way...
return self.C_f[np.ix_(subregion_index, subregion_index)]
def von_Neumann_entropy_f(self, subregion):
c_A = self.c_subregion_f(subregion)
val = nla.eigvalsh(c_A)
self.val_sh = val
val = np.sort(val)
val=np.sort(val)[:val.shape[0]//2]
return np.real(-np.sum(val*np.log(val+1e-18j))-np.sum((1-val)*np.log(1-val+1e-18j)))
def c_subregion_m(self, subregion, Gamma=None):
'''
subregion: [subregoin_x, subregion_y] index of unit cell
'''
if not hasattr(self, 'C_m'):
self.covariance_matrix()
if Gamma is None:
Gamma = self.C_m_history[-1]
try:
subregion = list(subregion)
except:
raise ValueError("The subregion is ill-defined"+subregion)
if len(subregion) == 2:
if isinstance(subregion[0], int) and isinstance(subregion[1], int):
linear = True
elif (not isinstance(subregion[0], int)) and (not isinstance(subregion[1], int)):
linear = False
else:
raise ValueError('Illegal subregion')
else:
linear = True
if linear:
# linearized index
subregion_index = subregion
else:
# 2D index
subregion_index = self.linearize_index(subregion, 2)
return Gamma[np.ix_(subregion_index, subregion_index)]
def von_Neumann_entropy_m(self, subregion):
c_A = self.c_subregion_m(subregion)
val = nla.eigvalsh(1j*c_A)
self.val_sh = val
val = np.sort(val)
val = (1-val)/2+1e-18j # \lambda=(1-\xi)/2
return np.real(-np.sum(val*np.log(val))-np.sum((1-val)*np.log(1-val)))/2
def mutual_information_f(self, subregion_A, subregion_B):
subregion_A = self.linearize_index(subregion_A, 2)
subregion_B = self.linearize_index(subregion_B, 2)
s_A = self.von_Neumann_entropy_f(subregion_A)
s_B = self.von_Neumann_entropy_f(subregion_B)
assert np.intersect1d(
subregion_A, subregion_B).size == 0, "Subregion A and B overlap"
subregion_AB = np.concatenate([subregion_A, subregion_B])
s_AB = self.von_Neumann_entropy_f(subregion_AB)
return s_A+s_B-s_AB
def mutual_information_m(self, subregion_A, subregion_B,linear=False):
if not linear:
subregion_A = self.linearize_index(subregion_A, 2)
subregion_B = self.linearize_index(subregion_B, 2)
assert np.intersect1d(
subregion_A, subregion_B).size == 0, "Subregion A and B overlap"
s_A = self.von_Neumann_entropy_m(subregion_A)
s_B = self.von_Neumann_entropy_m(subregion_B)
subregion_AB = np.concatenate([subregion_A, subregion_B])
s_AB = self.von_Neumann_entropy_m(subregion_AB)
return s_A+s_B-s_AB
def log_neg(self, subregion_A, subregion_B, Gamma=None,linear=False):
if not linear:
subregion_A = self.linearize_index(subregion_A, 2)
subregion_B = self.linearize_index(subregion_B, 2)
assert np.intersect1d(
subregion_A, subregion_B).size == 0, "Subregion A and B overlap"
if not hasattr(self, 'C_m'):
self.covariance_matrix()
if Gamma is None:
Gamma = self.C_m_history[-1]
subregion_A = np.array(subregion_A)
subregion_B = np.array(subregion_B)
Gm_p = np.block([
[-Gamma[np.ix_(subregion_A, subregion_A)], 1j *
Gamma[np.ix_(subregion_A, subregion_B)]],
[1j*Gamma[np.ix_(subregion_B, subregion_A)],
Gamma[np.ix_(subregion_B, subregion_B)]]
])
Gm_n = np.block([
[-Gamma[np.ix_(subregion_A, subregion_A)], -1j *
Gamma[np.ix_(subregion_A, subregion_B)]],
[-1j*Gamma[np.ix_(subregion_B, subregion_A)],
Gamma[np.ix_(subregion_B, subregion_B)]]
])
idm = np.eye(Gm_p.shape[0])
# Gm_x=idm-(idm+1j*Gm_p)@nla.inv(idm-Gm_n@Gm_p)@(idm+1j*Gm_n)
Gm_x = idm-(idm+1j*Gm_p)@(la.solve((idm-Gm_n@Gm_p), (idm+1j*Gm_n)))
Gm_x = (Gm_x+Gm_x.T.conj())/2
xi = nla.eigvalsh(Gm_x)
subregion_AB = np.concatenate([subregion_A, subregion_B])
eA = np.sum(np.log(((1+xi+0j)/2)**0.5+((1-xi+0j)/2)**0.5))/2
chi = nla.eigvalsh(1j*Gamma[np.ix_(subregion_AB, subregion_AB)])
sA = np.sum(np.log(((1+chi)/2)**2+((1-chi)/2)**2))/4
self.eA=eA
self.sA=sA
return np.real(eA+sA)
def projection(self, s):
'''
For type:'onsite'
occupancy number: s= 0,1
(-1)^0 even parity, (-1)^1 odd parity
'''
assert (s == 0 or s == 1), "s={} is either 0 or 1".format(s)
blkmat = np.array([[0, -(-1)**s, 0, 0],
[(-1)**s, 0, 0, 0],
[0, 0, 0, (-1)**s],
[0, 0, -(-1)**s, 0]])
return blkmat
def measure(self, s, ix):
if not hasattr(self, 'C_m'):
self.covariance_matrix()
if not hasattr(self, 's_history'):
self.s_history = []
if not hasattr(self, 'i_history'):
self.i_history = []
mat = self.C_m_history[-1].copy()
for i_ind, i in enumerate(ix):
mat[[i, -(len(ix)-i_ind)]] = mat[[-(len(ix)-i_ind), i]]
mat[:, [i, -(len(ix)-i_ind)]] = mat[:, [-(len(ix)-i_ind), i]]
self.mat = mat
Gamma_LL = mat[:-len(ix), :-len(ix)]
Gamma_LR = mat[:-len(ix), -len(ix):]
Gamma_RR = mat[-len(ix):, -len(ix):]
proj = self.projection(s)
Upsilon_LL = proj[:-len(ix), :-len(ix)]
Upsilon_RR = proj[-len(ix):, -len(ix):]
Upsilon_RL = proj[-len(ix):, :-len(ix)]
zero = np.zeros((mat.shape[0]-len(ix), len(ix)))
zero0 = np.zeros((len(ix), len(ix)))
mat1 = np.block([[Gamma_LL, zero], [zero.T, Upsilon_RR]])
mat2 = np.block([[Gamma_LR, zero], [zero0, Upsilon_RL]])
mat3 = np.block([[Gamma_RR, np.eye(len(ix))],
[-np.eye(len(ix)), Upsilon_LL]])
self.mat2 = mat2
if np.count_nonzero(mat2):
Psi = mat1+mat2@(la.solve(mat3, mat2.T))
# Psi=mat1+mat2@(la.lstsq(mat3,mat2.T)[0])
assert np.abs(
np.trace(Psi)) < 1e-5, "Not trace zero {:e}".format(np.trace(Psi))
else:
Psi = mat1
for i_ind, i in enumerate(ix):
Psi[[i, -(len(ix)-i_ind)]] = Psi[[-(len(ix)-i_ind), i]]
Psi[:, [i, -(len(ix)-i_ind)]] = Psi[:, [-(len(ix)-i_ind), i]]
Psi = (Psi-Psi.T)/2 # Anti-symmetrize
if self.history:
self.C_m_history.append(Psi)
self.s_history.append(s)
self.i_history.append(ix[0])
else:
self.C_m_history = [Psi]
self.s_history = [s]
self.i_history = [ix[0]]
def measure_all_Born(self, proj_range,prob=None,linear=False):
if not linear:
proj_range = self.linearize_index(proj_range, 2, proj=True)
self.proj_range=proj_range
# print(proj_range)
self.P_0_list = []
self.f_parity= []
self.covariance_matrix()
for i in proj_range:
if prob is None:
P_0 = (self.C_m_history[-1][i, i+1]+1)/2 # Use Born rule
else:
P_0=prob
self.P_0_list.append(P_0)
if np.random.rand() < P_0:
self.measure(0, [i, i+1])
self.f_parity.append(0)
else:
self.measure(1, [i, i+1])
self.f_parity.append(1)
return self
def cross_ratio(x,L):
if L<np.inf:
xx=lambda i,j: (np.sin(np.pi/(L)*np.abs(x[i]-x[j])))
else:
xx=lambda i,j: np.abs(x[i]-x[j])
eta=(xx(0,1)*xx(2,3))/(xx(0,2)*xx(1,3))
return eta