-
Notifications
You must be signed in to change notification settings - Fork 8
/
tdvp.h
333 lines (285 loc) · 8.36 KB
/
tdvp.h
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
#ifndef __ITENSOR_TDVP_H
#define __ITENSOR_TDVP_H
#include "itensor/iterativesolvers.h"
#include "itensor/mps/localmposet.h"
#include "itensor/mps/sweeps.h"
#include "itensor/mps/DMRGObserver.h"
#include "itensor/util/cputime.h"
namespace itensor {
template <class LocalOpT>
Real
TDVPWorker(MPS & psi,
LocalOpT& PH,
Cplx t,
const Sweeps& sweeps,
const Args& args = Args::global());
template <class LocalOpT>
Real
TDVPWorker(MPS & psi,
LocalOpT& PH,
Cplx t,
const Sweeps& sweeps,
DMRGObserver & obs,
Args args = Args::global());
//
// Available TDVP methods:
// second order integrator: sweep left-to-right and right-to-left
//
//
//TDVP with an MPO
//
Real inline
tdvp(MPS & psi,
MPO const& H,
Cplx t,
const Sweeps& sweeps,
const Args& args = Args::global())
{
LocalMPO PH(H,args);
Real energy = TDVPWorker(psi,PH,t,sweeps,args);
return energy;
}
//
//TDVP with an MPO and custom DMRGObserver
//
Real inline
tdvp(MPS & psi,
MPO const& H,
Cplx t,
const Sweeps& sweeps,
DMRGObserver & obs,
const Args& args = Args::global())
{
LocalMPO PH(H,args);
Real energy = TDVPWorker(psi,PH,t,sweeps,obs,args);
return energy;
}
//
//TDVP with an MPO and boundary tensors LH, RH
// LH - H1 - H2 - ... - HN - RH
//(ok if one or both of LH, RH default constructed)
//
Real inline
tdvp(MPS & psi,
MPO const& H,
Cplx t,
ITensor const& LH,
ITensor const& RH,
const Sweeps& sweeps,
const Args& args = Args::global())
{
LocalMPO PH(H,LH,RH,args);
Real energy = TDVPWorker(psi,PH,t,sweeps,args);
return energy;
}
//
//TDVP with an MPO and boundary tensors LH, RH
//and a custom observer
//
Real inline
tdvp(MPS & psi,
MPO const& H,
Cplx t,
ITensor const& LH,
ITensor const& RH,
const Sweeps& sweeps,
DMRGObserver& obs,
const Args& args = Args::global())
{
LocalMPO PH(H,LH,RH,args);
Real energy = TDVPWorker(psi,PH,t,sweeps,obs,args);
return energy;
}
//
//TDVP with a set of MPOs (lazily summed)
//(H vector is 0-indexed)
//
Real inline
tdvp(MPS& psi,
std::vector<MPO> const& Hset,
Cplx t,
const Sweeps& sweeps,
const Args& args = Args::global())
{
LocalMPOSet PH(Hset,args);
Real energy = TDVPWorker(psi,PH,t,sweeps,args);
return energy;
}
//
//TDVP with a set of MPOs and a custom DMRGObserver
//(H vector is 0-indexed)
//
Real inline
tdvp(MPS & psi,
std::vector<MPO> const& Hset,
Cplx t,
const Sweeps& sweeps,
DMRGObserver& obs,
const Args& args = Args::global())
{
LocalMPOSet PH(Hset,args);
Real energy = TDVPWorker(psi,PH,t,sweeps,obs,args);
return energy;
}
//
// TDVPWorker
//
template <class LocalOpT>
Real
TDVPWorker(MPS & psi,
LocalOpT& PH,
Cplx t,
Sweeps const& sweeps,
Args const& args)
{
DMRGObserver obs(psi,args);
Real energy = TDVPWorker(psi,PH,t,sweeps,obs,args);
return energy;
}
template <class LocalOpT>
Real
TDVPWorker(MPS & psi,
LocalOpT& H,
Cplx t,
Sweeps const& sweeps,
DMRGObserver& obs,
Args args)
{
// Truncate blocks of degenerate singular values (or not)
args.add("RespectDegenerate",args.getBool("RespectDegenerate",true));
const bool silent = args.getBool("Silent",false);
if(silent)
{
args.add("Quiet",true);
args.add("PrintEigs",false);
args.add("NoMeasure",true);
args.add("DebugLevel",-1);
}
const bool quiet = args.getBool("Quiet",false);
const int debug_level = args.getInt("DebugLevel",(quiet ? -1 : 0));
const int numCenter = args.getInt("NumCenter",2);
if(numCenter != 1)
args.add("Truncate",args.getBool("Truncate",true));
else
args.add("Truncate",args.getBool("Truncate",false));
const int N = length(psi);
Real energy = NAN;
if((!isOrtho(psi)) || (psi.leftLim() != 0))
psi.position(1);
args.add("DebugLevel",debug_level);
for(int sw = 1; sw <= sweeps.nsweep(); ++sw)
{
cpu_time sw_time;
args.add("Sweep",sw);
args.add("NSweep",sweeps.nsweep());
args.add("Cutoff",sweeps.cutoff(sw));
args.add("MinDim",sweeps.mindim(sw));
args.add("MaxDim",sweeps.maxdim(sw));
args.add("MaxIter",sweeps.niter(sw));
if(!H.doWrite()
&& args.defined("WriteDim")
&& maxLinkDim(psi) >= args.getInt("WriteDim"))
{
if(!quiet)
{
println("\nTurning on write to disk, write_dir = ",
args.getString("WriteDir","./"));
}
//psi.doWrite(true);
H.doWrite(true,args);
}
// 0, 1 and 2-site wavefunctions
ITensor phi0,phi1;
Spectrum spec;
for(int b = 1, ha = 1; ha <= 2; sweepnext(b,ha,N,{"NumCenter=",numCenter}))
{
if(!quiet)
printfln("Sweep=%d, HS=%d, Bond=%d/%d",sw,ha,b,(N-1));
H.numCenter(numCenter);
H.position(b,psi);
if(numCenter == 2)
phi1 = psi(b)*psi(b+1);
else if(numCenter == 1)
phi1 = psi(b);
applyExp(H,phi1,t/2,args);
if(args.getBool("DoNormalize",true))
phi1 /= norm(phi1);
if(numCenter == 2)
spec = psi.svdBond(b,phi1,(ha==1 ? Fromleft : Fromright),H,args);
else if(numCenter == 1)
psi.ref(b) = phi1;
if((ha == 1 && b+numCenter-1 != N) || (ha == 2 && b != 1))
{
auto b1 = (ha == 1 ? b+1 : b);
if(numCenter == 2)
{
phi0 = psi(b1);
}
else if(numCenter == 1)
{
Index l;
if(ha == 1) l = commonIndex(psi(b),psi(b+1));
else l = commonIndex(psi(b-1),psi(b));
ITensor U,S,V(l);
spec = svd(phi1,U,S,V,args);
psi.ref(b) = U;
phi0 = S*V;
}
H.numCenter(numCenter-1);
H.position(b1,psi);
applyExp(H,phi0,-t/2,args);
if(args.getBool("DoNormalize",true))
phi0 /= norm(phi0);
if(numCenter == 2)
{
psi.ref(b1) = phi0;
}
if(numCenter == 1)
{
if(ha == 1) psi.ref(b+1) *= phi0;
else psi.ref(b-1) *= phi0;
}
// Calculate energy
ITensor H_phi0;
H.product(phi0,H_phi0);
energy = real(eltC(dag(phi0)*H_phi0));
}
else
{
// Calculate energy
ITensor H_phi1;
H.product(phi1,H_phi1);
energy = real(eltC(dag(phi1)*H_phi1));
}
if(!quiet)
{
printfln(" Truncated to Cutoff=%.1E, Min_dim=%d, Max_dim=%d",
sweeps.cutoff(sw),
sweeps.mindim(sw),
sweeps.maxdim(sw) );
printfln(" Trunc. err=%.1E, States kept: %s",
spec.truncerr(),
showDim(linkIndex(psi,b)) );
}
obs.lastSpectrum(spec);
args.add("AtBond",b);
args.add("HalfSweep",ha);
args.add("Energy",energy);
args.add("Truncerr",spec.truncerr());
obs.measure(args);
} //for loop over b
if(!silent)
{
auto sm = sw_time.sincemark();
printfln(" Sweep %d/%d CPU time = %s (Wall time = %s)",
sw,sweeps.nsweep(),showtime(sm.time),showtime(sm.wall));
}
if(obs.checkDone(args)) break;
} //for loop over sw
psi.rightLim(psi.leftLim()+2);
if(args.getBool("DoNormalize",true))
psi.normalize();
return energy;
}
} //namespace itensor
#endif