-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScalingDimensions.py
More file actions
325 lines (276 loc) · 11 KB
/
ScalingDimensions.py
File metadata and controls
325 lines (276 loc) · 11 KB
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
import torch
import numpy as np
from tqdm.auto import tqdm
from opt_einsum import contract
svd=torch.linalg.svd
from HOTRGZ2 import gauge_invariant_norm
def _toN(t):
return t.detach().cpu().tolist() if isinstance(t,torch.Tensor) else t
def fix_normalize(T,norms,volume_scaling=2,is_HOTRG=False):
if not is_HOTRG:
# evolve(T/norm)=T
# q T=evolve(q T)=q**scaling * norm**scaling T
q=norms[-1]**(volume_scaling/(1-volume_scaling))
else:
# evolve(...evolve(T/norms[0])/norms[1]...)=T
# q T=evolve(...evolve(q T)...)
# =q**(scaling**dim) * norms[0]**(scaling**dim) *...* norms[-1]**(scaling)
spacial_dim=len(T.shape)//2
norms=([1]*spacial_dim+norms)[-spacial_dim:]
norms=[norms[-1]]+norms[:-1]#why
q=1
for axis in range(spacial_dim):
q=q * norms[axis]**(volume_scaling**(spacial_dim-axis))
q=q**(1/(1-volume_scaling**spacial_dim))
return q*T
def get_transfer_matrix_spectrum_2D(T,loop_length:int=2):
M=T
for i in range(loop_length-2):# Must preserve edge orders
M=contract('kKab,lLbc->klKLac',M,T).reshape(M.shape[0]*T.shape[0],M.shape[1]*T.shape[1],M.shape[2],T.shape[3])
if loop_length>1:
M=contract('kKab,lLba->klKL',M,T).reshape(M.shape[0]*T.shape[0],M.shape[1]*T.shape[1])
else:
M=contract('kKaa->kK',T)
#s2,_=torch.lobpcg(M@M.T,k=k,method="ortho");s=s2**.5
u,s,vh=svd(M)
return u,s,vh
def get_transfer_matrix_spectrum_3D(T,loop_length:'tuple[int]'=(1,1)):
loop_length=tuple(loop_length)
if loop_length==(1,1):
M=contract('ijkkmm->ij',T)
elif loop_length==(2,1):
M=contract('ijklmm,IJlknn->iIjJ',T,T).reshape(T.shape[0]*T.shape[0],T.shape[1]*T.shape[1])
elif loop_length==(1,2):
M=contract('ijkkmn,IJllnm->iIjJ',T,T).reshape(T.shape[0]*T.shape[0],T.shape[1]*T.shape[1])
else:
raise ValueError
#s2,_=torch.lobpcg(M@M.T,k=k,method="ortho");s=s2**.5
u,s,vh=svd(M)
return u,s,vh
#def get_center_charge(spectrum,aspect=1):
# return torch.log(spectrum[0])*12/((2*torch.pi)/aspect)
#def get_scaling_dimensions(spectrum,aspect=1):
# return -torch.log(spectrum/spectrum[0])/((2*torch.pi)/aspect)
def get_central_charge(spectrum,scaling=np.exp(2*np.pi)):
return 12*torch.log(spectrum[0])/torch.log(torch.as_tensor(scaling))
def get_scaling_dimensions(spectrum,scaling=np.exp(2*np.pi)):
return (-torch.log(spectrum/spectrum[0])/torch.log(torch.as_tensor(scaling))).abs()
def get_entanglement_entropy(spectrum):
spectrum=spectrum/spectrum.sum()
logSpectrum=spectrum.log().nan_to_num(nan=0)
return -(logSpectrum*spectrum).sum()
def get_half_circle_density_matrix(u,loop_length):
assert loop_length%2==0
psi=u[:,0]
psi/=torch.norm(psi)
dim=int(psi.shape[0]**.5)
psi=psi.reshape(dim,dim)
rho=contract('ij,Ij->iI',psi,psi.conj())
rho/=contract('ii->',rho)
return rho
import pandas as pd
import matplotlib.pyplot as plt
def show_scaling_dimensions(Ts,loop_length=2,num_scaling_dims=8,volume_scaling=2,is_HOTRG=False,reference_scaling_dimensions=None, reference_center_charge=None,filename=None,display=True,stride=1):
curve=[]
def pad(v):
return np.pad(_toN(v),(0,num_scaling_dims))[:num_scaling_dims]
spacial_dim=len(Ts[0].shape)//2
norms=list(map(gauge_invariant_norm,Ts))
for iLayer,A in tqdm([*enumerate(Ts)]):
if iLayer%stride!=0:
continue
A=fix_normalize(A,is_HOTRG=is_HOTRG,volume_scaling=volume_scaling,norms=norms[:iLayer+1])
if spacial_dim==2:
if is_HOTRG:
aspect=[loop_length,loop_length*2][iLayer%2]
else:
aspect=loop_length
u,s,_=get_transfer_matrix_spectrum_2D(A,loop_length=loop_length)
elif spacial_dim==3:
assert loop_length==1
if is_HOTRG:
loop_length1=[(1,1),(2,1),(1,1)][iLayer%3]
aspect=[1,2,2][iLayer%3]
else:
raise NotImplementedError
u,s,_=get_transfer_matrix_spectrum_3D(A,loop_length=loop_length1)
ss=s**aspect
#print(s[:10])
center_charge=get_central_charge(ss)
scaling_dimensions=get_scaling_dimensions(ss)
min_entropy=-torch.max(ss/ss.sum()).log()
transfer_entropy=get_entanglement_entropy(ss)
#if loop_length%2==1:
# u,_,_=get_transfer_matrix_spectrum_2D(A,loop_length=loop_length-1)
# rho=get_half_circle_density_matrix(u,loop_length-1)
#else:
# rho=get_half_circle_density_matrix(u,loop_length)
#_,s1,_=svd(rho)
#bipartite_entropy=get_entanglement_entropy(s1)
newRow={'layer':iLayer,
'center_charge':center_charge,
'scaling_dimensions':pad(scaling_dimensions),
'min_entropy':min_entropy,
'transfer_entropy':transfer_entropy,
#'bipartite_entropy':bipartite_entropy,
'eigs':pad(s),
'norm':norms[iLayer]}
newRow={k:_toN(v) for k,v in newRow.items()}
curve.append(newRow)
curve=pd.DataFrame(curve)
#plt.plot(curve['layer'],curve['norm'],'.-',color='black')
#plt.xlabel('RG Step')
#plt.ylabel('norm of tensor')
#plt.show()
if display or filename is not None:
eigs=np.array(curve['eigs'].tolist()).T
for eig in eigs:
plt.plot(curve['layer'],eig,'.-',color='black')
plt.xlabel('RG Step')
plt.ylabel('eigenvalues of transfer matrix')
plt.ylim([0,1])
plt.show()
if filename is not None:
plt.savefig(filename+'_eigs.png')
print('saved to',filename+'_eigs.png')
plt.close()
sdsds=np.array(curve['scaling_dimensions'].tolist()).T
if reference_scaling_dimensions is not None:
for sdsd in reference_scaling_dimensions:
plt.plot(curve['layer'],np.ones_like(curve['layer'])*sdsd,'-',color='lightgrey')
plt.ylim([0,max(reference_scaling_dimensions)*1.1])
else:
plt.ylim([np.average(sdsds[-1])*-.1,np.average(sdsds[-1])*1.5])
for sdsd in sdsds:
plt.plot(curve['layer'],sdsd,'.-',color='black')
plt.xlabel('RG Step')
plt.ylabel('scaling dimensions')
plt.show()
if filename is not None:
plt.savefig(filename+'_scDim.png')
print('saved to',filename+'_scDim.png')
plt.close()
if reference_center_charge is not None:
plt.plot(curve['layer'],np.ones_like(curve['layer'])*reference_center_charge,'-',color='lightgrey')
plt.ylim([0,reference_center_charge*2])
else:
avg=np.average(curve['center_charge'])
#nan or inf
if np.isnan(avg) or np.isinf(avg):
avg=.5
plt.ylim([avg*-.1,avg*1.5])
plt.plot(curve['layer'],curve['center_charge'],'.-',color='black')
plt.xlabel('RG Step')
plt.ylabel('central charge')
plt.show()
if filename is not None:
plt.savefig(filename+'_c.png')
print('saved to',filename+'_c.png')
plt.close()
for item in ['min_entropy','transfer_entropy']:
break
plt.plot(curve['layer'],curve[item],'.-',color='black')
plt.xlabel('RG Step')
plt.ylabel(item)
plt.show()
return curve
def NWSE(T):
return contract('nswe->nwse',T).reshape(T.shape[0]*T.shape[2],-1)
def NESW(T):
return contract('nswe->nesw',T).reshape(T.shape[0]*T.shape[3],-1)
def effective_rank(M):
assert len(M.shape)==2
u,s,vh=svd(M)
s=s[s>0]
p=s/torch.sum(s)
entropy=-torch.sum(p*torch.log(p))
return torch.exp(entropy)
def show_effective_rank(Ts,filename=None):
curve=[]
for i,A in tqdm([*enumerate(Ts)]):
_,s,_=torch.linalg.svd(NWSE(A))
s=s/s[0]
s=np.array(_toN(s))
if(s.shape[0]<30):
s=np.pad(s,(0,30-s.shape[0]))
else:
s=s[:30]
er=effective_rank(NWSE(A))
er1=effective_rank(NESW(A))
newRow={'layer':i,'entanglement_spectrum':s,'effective_rank_nwse':er,'effective_rank_nesw':er1}
newRow={k:_toN(v) for k,v in newRow.items()}
curve.append(newRow)
curve=pd.DataFrame(curve)
ss=np.array(curve['entanglement_spectrum'].tolist())
ee=curve['effective_rank_nwse'].tolist()
ee1=curve['effective_rank_nesw'].tolist()
iii=curve['layer']
for sss in ss.T:
plt.plot(iii,sss,'-k')
plt.title(f'')
plt.xlabel('RG Step')
plt.ylabel('normalized eigenvalues')
plt.show()
if filename is not None:
plt.savefig(filename+'_eigsd.png')
print('saved to',filename+'_eigsd.png')
plt.close()
plt.plot(iii,ee,'-k',label='nwse')
#plt.plot(iii,ee1,label='nesw')
plt.ylabel('effective rank')
#plt.legend()
plt.show()
if filename is not None:
plt.savefig(filename+'_rankd.png')
print('saved to',filename+'_rankd.png')
plt.close()
return curve
def show_diff(Ts,stride=1,filename=None):
curve=[]
for i,A in tqdm([*enumerate(Ts)]):
newRow={'layer':i}
if i-stride>=0 and A.shape==Ts[i-stride].shape:
newRow['diff']=(Ts[i]-Ts[i-stride]).norm()/Ts[i].norm()
#newRow['diff1']=contract('ijkk->ij',Ts[i]-Ts[i-stride]).norm()
#newRow['diff2']=contract('iikl->kl',Ts[i]-Ts[i-stride]).norm()
#if (Ts[i]-Ts[i-stride]).norm()>2e-1 and i>10 and i<15:
# print(i)
newRow={k:_toN(v) for k,v in newRow.items()}
curve.append(newRow)
curve=pd.DataFrame(curve)
plt.plot(curve['layer'],curve['diff'],'.-',color='black',label='$|T\'-T|$')
#plt.plot(curve['layer'],curve['diff1'],'.-',label='diff1')
#plt.plot(curve['layer'],curve['diff2'],'.-',label='diff2')
#plt.legend()
plt.xlabel('RG Step')
plt.ylabel('$|T\'-T|/|T|$')
plt.yscale('log')
plt.ylim((1e-7,2))
plt.show()
if filename is not None:
plt.savefig(filename+'_diff.png')
print('saved to',filename+'_diff.png')
plt.close()
return curve
from HOTRGZ2 import reflect_tensor_axis,permute_tensor_axis
def show_asymmetry(Ts):
curve=[]
for i,A in enumerate(Ts):
newRow={'layer':i}
Arot=permute_tensor_axis(A)
Aref=reflect_tensor_axis(A)
if A.shape==Arot.shape:
newRow['asym_rot']=_toN((Arot-A).norm()/A.norm())
newRow['asym_ref']=_toN((Aref-A).norm()/A.norm())
newRow={k:_toN(v) for k,v in newRow.items()}
curve.append(newRow)
curve=pd.DataFrame(curve)
plt.plot(curve['layer'],curve['asym_rot'],'.-',label='rotation')
plt.plot(curve['layer'],curve['asym_ref'],'x-',label='reflection')
plt.legend()
plt.xlabel('RG Step')
plt.ylabel('$|T\'-T|/|T|$')
#plt.yscale('log')
plt.ylim((0,1))
plt.show()
return curve