-
Notifications
You must be signed in to change notification settings - Fork 5
/
power_projection.py
370 lines (318 loc) · 12.7 KB
/
power_projection.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
""" main program: power projection procedure
"""
import numpy as np
import torch
import os, sys, io, subprocess
import os.path
torch.set_num_threads(int(os.environ['OMP_NUM_THREADS']))
torch.manual_seed(42)
from cmpo import *
import model
def F(psi, Lpsi, T, beta):
""" calculate the free energy by
-(1/beta) * log [<Lpsi|T|psi> / <Lpsi|psi>]
T: cMPO
psi: cMPS (right eigenvector)
Lpsi: cMPS (left eigenvector)
beta: inverse temperature
"""
Tpsi = act(T, psi)
up = ln_ovlp(Lpsi, Tpsi, beta)
dn = ln_ovlp(Lpsi, psi, beta)
return (- up + dn) / beta
def Obsv(psi, Lpsi, T, O, beta):
""" calculate the thermal average of the observable O as
<I \otimes O \otimes I>_{K}
where K is the K-matrix corresponding to <Lpsi|T|psi>
K plays the role of effective Hamiltonian
T: cMPO
psi: cMPS (right eigenvector)
Lpsi: cMPS (left eigenvector)
O: the observable
beta: inverse temperature
"""
dtype, device = psi.dtype, psi.device
totalD = O.shape[0]*psi.dim*psi.dim
matI = torch.eye(psi.dim, dtype=dtype, device=device)
matO = torch.einsum('ab,cd,ef->acebdf', matI, O, matI).contiguous().view(totalD, totalD)
Tpsi = act(T, psi)
M = density_matrix(Lpsi, Tpsi)
w, v = eigensolver(M)
w -= w.max().item()
expw = torch.diag(torch.exp(beta*w))
return torch.trace(expw @ v.t() @ matO @ v).item() / torch.trace(expw).item()
def Corr(psi, Lpsi, T, O1, O2, beta, tau):
""" calculate unequal-imaginary-time correlator
tr[exp(-beta*K) O1(0) O2(tau)]
where K is the K-matrix corresponding to <Lpsi|T|psi>
K plays the role of effective Hamiltonian
T: cMPO
psi: cMPS (right eigenvector)
Lpsi: cMPS (left eigenvector)
O1, O2: observables
beta: inverse temperature
"""
dtype, device = psi.dtype, psi.device
totalD = O1.shape[0]*psi.dim*psi.dim
matI = torch.eye(psi.dim, dtype=dtype, device=device)
matO1 = torch.einsum('ab,cd,ef->acebdf', matI, O1, matI).contiguous().view(totalD, totalD)
matO2 = torch.einsum('ab,cd,ef->acebdf', matI, O2, matI).contiguous().view(totalD, totalD)
Tpsi = act(T, psi)
M = density_matrix(Lpsi, Tpsi)
w, v = eigensolver(M)
matO1 = v.t() @ matO1 @ v
matO2 = v.t() @ matO2 @ v
w -= w.max().item()
expw_a = torch.diag(torch.exp((beta-tau)*w))
expw_b = torch.diag(torch.exp(tau*w))
expw = torch.diag(torch.exp(beta*w))
return torch.trace(expw_a @ matO1 @ expw_b @ matO2).item() / torch.trace(expw).item()
def chi(psi, Lpsi, T, O1, O2, beta, iomega=0):
""" calculate the local susceptibility with
\int_0^{\beta} d tau tr[exp(-beta*K) O1(0) O2(tau)]
where K is the K-matrix corresponding to <Lpsi|T|psi>
K plays the role of effective Hamiltonian
T: cMPO
psi: cMPS (right eigenvector)
Lpsi: cMPS (left eigenvector)
O1, O2: observables
beta: inverse temperature
"""
dtype, device = psi.dtype, psi.device
totalD = O1.shape[0]*psi.dim*psi.dim
matI = torch.eye(psi.dim, dtype=dtype, device=device)
matO1 = torch.einsum('ab,cd,ef->acebdf', matI, O1, matI).contiguous().view(totalD, totalD)
matO2 = torch.einsum('ab,cd,ef->acebdf', matI, O2, matI).contiguous().view(totalD, totalD)
Tpsi = act(T, psi)
M = density_matrix(Lpsi, Tpsi)
w, v = eigensolver(M)
w -= w.max().item()
matO1 = v.t() @ matO1 @ v
matO2 = v.t() @ matO2 @ v
expw = torch.diag(torch.exp(beta*w))
Envec, Emvec = np.meshgrid(w.detach().numpy(), (w+1e-8).detach().numpy())
F = (np.exp(beta*Envec) - np.exp(beta*Emvec)) / (iomega + Envec - Emvec)
F = torch.tensor(F, dtype=dtype, device=device)
result = torch.trace(matO1 @ (matO2 * F)) / torch.trace(expw)
return result.item()
def chi2(psi, Lpsi, T, O1, O2, beta, omega=0, eta=0.05):
dtype, device = psi.dtype, psi.device
totalD = O1.shape[0]*psi.dim*psi.dim
matI = torch.eye(psi.dim, dtype=dtype, device=device)
matO1 = torch.einsum('ab,cd,ef->acebdf', matI, O1, matI).contiguous().view(totalD, totalD)
matO2 = torch.einsum('ab,cd,ef->acebdf', matI, O2, matI).contiguous().view(totalD, totalD)
Tpsi = act(T, psi)
M = density_matrix(Lpsi, Tpsi)
w, v = eigensolver(M)
w -= w.max().item()
matO1 = v.t() @ matO1 @ v
matO2 = v.t() @ matO2 @ v
expw = torch.diag(torch.exp(beta*w))
delta = lambda x, eta: 1/np.pi * eta/(x**2 + eta**2)
Envec, Emvec = np.meshgrid(w.detach().numpy(), w.detach().numpy())
F = (np.exp(beta*Envec) - np.exp(beta*Emvec)) * delta(omega + Envec - Emvec, eta)
F = torch.tensor(F, dtype=dtype, device=device)
result = -np.pi * torch.trace(matO1 @ (matO2 * F)) / torch.trace(expw)
return result.item()
def spectral(psi, W, T, O1, O2, beta, omega=1e-6, eta=0.05):
""" calculate spectral function
"""
return 2*chi2(psi, W, T, O1, O2, beta, omega, eta) / (1-np.exp(-beta*omega))
def E(psi, Lpsi, T, beta):
""" calculate the energy
T: cMPO
psi: cMPS (right eigenvector)
Lpsi: cMPS (left eigenvector)
beta: inverse temperature
"""
Tpsi = act(T, psi)
M = density_matrix(Lpsi, Tpsi)
w, _ = eigensolver(M)
w_nm = w - torch.logsumexp(beta * w, dim=0)/beta
up = torch.exp(beta * w_nm) @ w
M = density_matrix(Lpsi, psi)
w, _ = eigensolver(M)
w_nm = w - torch.logsumexp(beta * w, dim=0)/beta
dn = torch.exp(beta * w_nm) @ w
return (- up + dn).item()
def Cv(psi, Lpsi, T, beta):
""" calculate the specific heat
T: cMPO
psi: cMPS (right eigenvector)
Lpsi: cMPS (left eigenvector)
beta: inverse temperature
"""
Tpsi = act(T, psi)
M = density_matrix(Lpsi, Tpsi)
w, _ = eigensolver(M)
w_nm = w - torch.logsumexp(beta * w, dim=0)/beta
up = torch.exp(beta * w_nm) @ (w*w) - (torch.exp(beta * w_nm) @ w)**2
M = density_matrix(Lpsi, psi)
w, _ = eigensolver(M)
w_nm = w - torch.logsumexp(beta * w, dim=0)/beta
dn = torch.exp(beta * w_nm) @ (w*w) - (torch.exp(beta * w_nm) @ w)**2
return (beta**2) * (up - dn).item()
def reduced_density_matrix(psi, tau, beta):
bondD = psi.dim
M = density_matrix(psi, psi)
w0, v0 = eigensolver(M)
w0 = w0 - torch.logsumexp(beta*w0, dim=0) / beta
expM = lambda x: v0 @ torch.diag(torch.exp(x*w0)) @ v0.t()
def reshapeM(M):
M1 = M.reshape(bondD, bondD, bondD, bondD)
M1 = M1.permute(0,2,1,3).reshape(bondD**2, bondD**2)
return M1
expM1 = reshapeM(expM(tau))
expM2 = reshapeM(expM(beta - tau))
w, v = eigensolver(expM1)
w_sqrt = torch.diag(torch.sqrt(w))
return w_sqrt @ v.t() @ expM2.t() @ v @ w_sqrt
def entanglement_entropy(psi, tau, beta):
""" use the boundary cMPS to calculate von Neumann entropy
on interval tau
"""
rho = reduced_density_matrix(psi, tau, beta)
w, _ = eigensolver(rho)
result = -torch.sum(w * torch.log(w))
# fot test (renyi-2)
#result = torch.log(torch.sum(w**2)) / (1-2)
return result
def klein(psi, W, beta):
""" calculate the klein bottle entropy
psi: cMPS (right eigenvector)
W: transform psi to the left eigenvector
beta: inverse temperature
"""
bondD = psi.dim
Wpsi = multiply(W, psi)
M = density_matrix(psi, Wpsi)
w, v = eigensolver(M)
w_nm = w - torch.logsumexp(beta * w, dim=0)/beta
exp_M = v @ torch.diag(torch.exp(0.5*beta*w_nm)) @ v.t()
gk = torch.einsum('abba->', exp_M.reshape(bondD,bondD,bondD,bondD))
sk = 2*torch.log(gk)
return sk.item()
def effectiveH(psi, Lpsi, num):
""" calculate the spectrum of the effective Hamiltonian
psi: cMPS (right eigenvector)
Lpsi: cMPS (left eigenvector)
num: number of states
"""
H = density_matrix(Lpsi, psi)
w, _ = eigensolver(H)
return -w[-num:] + w[-1]
def name_gen(args):
""" generate the name for the datafile with the parameters
"""
fl_to_str = lambda x: '{:.4f}'.format(x)
s = ''
s += 'bondD' + str(args.bondD)
s += '_beta' + fl_to_str(args.beta)
s += '_Gamma' + fl_to_str(args.Gamma)
s += '_J' + fl_to_str(args.J)
return s
if __name__=='__main__':
import argparse
parser = argparse.ArgumentParser(description='')
parser.add_argument("-bondD", type=int, default=2, help="tau direction bond dimension")
parser.add_argument("-beta", type=float, default=4.0 , help="beta")
parser.add_argument("-Gamma", type=float, default=1.0, help="Gamma")
parser.add_argument("-J", type=float, default=1.0, help="J")
parser.add_argument("-alpha", type=float, default=1.0, help="alpha")
parser.add_argument("-Nmax", type=int, default=6, help="Nmax")
parser.add_argument("-resultdir", type=str, default='tmpdata', help="result folder")
parser.add_argument("-init", type=str, default='none', help="init")
parser.add_argument("-float32", action='store_true', help="use float32")
parser.add_argument("-cuda", type=int, default=-1, help="use GPU")
args = parser.parse_args()
device = torch.device("cpu" if args.cuda<0 else "cuda:"+str(args.cuda))
dtype = torch.float32 if args.float32 else torch.float64
bondD = args.bondD
beta = args.beta
### datafile
if (not os.path.exists(args.resultdir)):
print("---> creating directory to save the result: " + args.resultdir)
key = args.resultdir+'/'+name_gen(args)
cmd = ['mkdir', '-p', key]
subprocess.check_call(cmd)
print("---> log and chkp saved to ", key)
### construct cMPO for more models see model.py
s = model.spin_half(dtype, device)
ising = model.ising(Gamma=args.Gamma, J=args.J, dtype=dtype, device=device) # cmpo def
T = ising.T
W = ising.W
ph_leg = ising.ph_leg
d = ising.d
### initialize
if args.init == 'none':
### after initialization bondD/ph_leg < psi.dim < or = bondD
init_step = int(np.floor(np.log(bondD) / np.log(ph_leg)))
psi = cmps(T.Q, T.L)
Lpsi = cmps(T.Q, T.R)
for ix in range(init_step-1):
psi = act(T, psi)
Lpsi = act(T.t(), psi)
psi = psi.diagQ()
Lpsi = Lpsi.diagQ()
else:
f_meas = io.open(args.init, 'r')
data_arr = np.loadtxt(f_meas)
optim_step = int(data_arr[np.argmin(data_arr[:, 1]), 0])
print('optim step', optim_step)
D0 = int(args.init.split('bondD')[1].split('_beta')[0])
beta0 = float(args.init.split('_beta')[1].split('_Gamma')[0])
# read out right eigenvector
psidata_name = args.init[:-9]+'/psi_{:03d}.pt'.format(optim_step)
Q = torch.rand(D0, dtype=dtype, device=device)
R = torch.rand(d,D0,D0, dtype=dtype, device=device)
Q = torch.nn.Parameter(Q)
R = torch.nn.Parameter(R)
psidata = data_cmps(Q, R)
dataload(psidata, psidata_name)
factor = beta0/beta
psi = cmps(torch.diag(Q)*factor, R*np.sqrt(factor)).detach()
# read out left eigenvector
if W is None:
Lpsidata_name = args.init[:-9]+'/Lpsi_{:03d}.pt'.format(optim_step)
Ql = torch.rand(D0, dtype=dtype, device=device)
Rl = torch.rand(d,D0,D0, dtype=dtype, device=device)
Ql = torch.nn.Parameter(Ql)
Rl = torch.nn.Parameter(Rl)
Lpsidata = data_cmps(Ql, Rl)
dataload(Lpsidata, Lpsidata_name)
Lpsi = cmps(torch.diag(Ql), Rl).detach()
else:
Lpsi = multiply(W, psi)
# power procedure
power_counter, step = 0, 0
Fmin = 9.9e9
while power_counter < 3:
if psi.dim <= bondD:
Tpsi = act(T, psi)
else:
Tpsi = psi
psi = variational_compr(Tpsi, beta, bondD, chkp_loc=key+'/psi_{:03d}.pt'.format(step))
if W is None: # this part is outdated
LpsiT = act(T.t(), Lpsi)
Lpsi = variational_compr(LpsiT, beta, bondD, chkp_loc=key+'/Lpsi_{:03d}.pt'.format(step))
else:
Lpsi = multiply(W, psi)
# output measurements
F_value = F(psi, Lpsi, T, beta)
E_value = E(psi, Lpsi, T, beta)
Cv_value = Cv(psi, Lpsi, T, beta)
Sk_value = klein(psi, W, beta)
#chi_value = chi(psi, Lpsi, T, s.Z/2, s.Z/2, beta) / beta
# output measurement results
logfile_meas = io.open(key+'-meas.log', 'a')
message = ('{} ' + 4*'{:.12f} ').format(step, F_value, E_value, Cv_value, Sk_value)
print('step, F, E, Cv, Sk '+ message, end=' \n')
logfile_meas.write(message + u'\n')
logfile_meas.close()
step += 1
if F_value < Fmin - 1e-11:
power_counter = 0
Fmin = F_value
else:
power_counter += 1
print(' ')