-
Notifications
You must be signed in to change notification settings - Fork 5
/
idmrg.h
419 lines (339 loc) · 10.3 KB
/
idmrg.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
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
411
412
413
414
415
416
417
418
419
//
// Distributed under the ITensor Library License, Version 1.2
// (See accompanying LICENSE file.)
//
#ifndef __ITENSOR_IDMRG_H
#define __ITENSOR_IDMRG_H
#include "itensor/mps/dmrg.h"
namespace itensor {
struct idmrgRVal
{
Real energy;
ITensor HL;
ITensor HR;
ITensor IL;
ITensor V;
};
void
read(std::istream & s, idmrgRVal & rval);
void
write(std::ostream & s, idmrgRVal const& rval);
idmrgRVal
idmrg(MPS & psi,
MPO const& H,
Sweeps const& sweeps,
Args const& args = Args::global());
idmrgRVal
idmrg(MPS & psi,
MPO const& H,
Sweeps const& sweeps,
DMRGObserver & obs,
Args const& args = Args::global());
//For restarting idmrg calculations
//from a previous run (creates a new DMRGObserver automatically)
idmrgRVal
idmrg(MPS & psi,
MPO const& H,
idmrgRVal const& last_res,
Sweeps const& sweeps,
Args const& args);
idmrgRVal
idmrg(MPS & psi,
MPO H,
idmrgRVal last_rval,
Sweeps const& sweeps,
DMRGObserver & obs,
Args args = Args::global());
//Given an MPS (or MPO) A1 A2 A3 | A4 A5 A6,
//modifies it to A4 A5 A6 | A1 A2 A3
//(treating the left and right halves
//each as one unit cell)
//Very efficient: swap method only uses pointer swaps internally.
template <class MPSType>
void
swapUnitCells(MPSType & psi)
{
auto Nuc = length(psi)/2;
for(auto n : range1(Nuc))
{
psi.ref(n).swap(psi.ref(Nuc+n));
}
}
//
// Implementations
//
namespace detail {
struct PseudoInvert
{
Real cutoff = 0.;
PseudoInvert(Real cut) : cutoff(cut) { }
Real
operator()(Real x) const
{
return (x > cutoff) ? 1./x : 0.;
}
};
} //namespace detail
idmrgRVal inline
idmrg(MPS & psi,
MPO H, //Copies H since algorithm swaps tensors in-place
idmrgRVal last_rval,
Sweeps const& sweeps,
DMRGObserver & obs,
Args args)
{
auto olevel = args.getInt("OutputLevel",0);
auto quiet = args.getBool("Quiet",olevel == 0);
auto nucsweeps = args.getInt("NUCSweeps",1);
auto nucs_decr = args.getInt("NUCSweepsDecrement",0);
auto do_randomize = args.getBool("Randomize",false);
auto show_overlap = args.getBool("ShowOverlap",false);
//inverse_cut is cutoff for computing pseudo inverse
auto inverse_cut = args.getReal("InverseCut",1E-8);
auto actual_nucsweeps = nucsweeps;
int N0 = length(psi); //Number of sites in center
int Nuc = N0/2; //Number of sites in unit cell
int N = N0; //Current system size
if(N0 == 2) args.add("CombineMPO",false);
Real energy = NAN;
auto lastV = last_rval.V;
ITensor D;
if(psi(0))
{
lastV = dag(psi(0));
lastV /= norm(lastV);
lastV.apply(detail::PseudoInvert(0));
}
ITensor HL(last_rval.HL),
HR(last_rval.HR);
auto IL = last_rval.IL;
//If last_rval is trivial,
//get edge tensors from MPO
if(not HL) HL = H(0);
if(not HR) HR = H(N0+1);
int sw = 1;
//Start with two unit cells
{
if(!quiet)
{
printfln("\niDMRG Step = %d, N=%d sites",sw,N);
}
auto ucsweeps = Sweeps(actual_nucsweeps);
ucsweeps.mindim() = sweeps.mindim(sw);
ucsweeps.maxdim() = sweeps.maxdim(sw);
ucsweeps.cutoff() = sweeps.cutoff(sw);
ucsweeps.noise() = sweeps.noise(sw);
ucsweeps.niter() = sweeps.niter(sw);
print(ucsweeps);
auto extra_args = Args("Quiet",olevel < 2,
"iDMRG_Step",sw,
"NSweep",ucsweeps.nsweep());
energy = dmrg(psi,H,HL,HR,ucsweeps,obs,args + extra_args);
if(do_randomize)
{
println("Randomizing psi");
for(int j = 1; j <= length(psi); ++j)
{
psi.ref(j).randomize();
}
psi.normalize();
}
printfln("\n Energy per site = %.14f\n",energy/N0);
psi.position(Nuc);
args.add("Sweep",sw);
args.add("AtBond",Nuc);
args.add("Energy",energy);
obs.measure(args+Args("AtCenter",true,"NoMeasure",true));
svd(psi(Nuc)*psi(Nuc+1),psi.ref(Nuc),D,psi.ref(Nuc+1));
D /= norm(D);
//Prepare MPO for next step
for(int j = 1; j <= Nuc; ++j)
{
HL *= psi(j);
HL *= H(j);
HL *= dag(prime(psi(j)));
IL *= psi(j);
IL *= H(j);
IL *= dag(prime(psi(j)));
HR *= psi(N0-j+1);
HR *= H(N0-j+1);
HR *= dag(prime(psi(N0-j+1)));
}
//H = HG(sw);
swapUnitCells(H);
HL += -energy*IL;
//Prepare MPS for next step
swapUnitCells(psi);
if(lastV) psi.ref(Nuc+1) *= lastV;
psi.ref(1) *= D;
psi.ref(N0) *= D;
psi.position(1);
++sw;
}
Spectrum spec;
for(; sw <= sweeps.nsweep(); ++sw)
{
auto ucsweeps = Sweeps(actual_nucsweeps);
ucsweeps.mindim() = sweeps.mindim(sw);
ucsweeps.maxdim() = sweeps.maxdim(sw);
ucsweeps.cutoff() = sweeps.cutoff(sw);
ucsweeps.noise() = sweeps.noise(sw);
ucsweeps.niter() = sweeps.niter(sw);
args.add("MaxDim",sweeps.maxdim(sw));
print(ucsweeps);
if(actual_nucsweeps > 1) actual_nucsweeps -= nucs_decr;
N += N0;
if(!quiet) printfln("\niDMRG Step = %d, N=%d sites",sw,N);
auto initPsi = psi;
auto PH = LocalMPO(H,HL,HR,args);
if(olevel >= 1)
{
auto E = HL;
for( auto n : range1(length(psi)) )
E = E*dag(prime(psi(n)))*H(n)*psi(n);
E *= HR;
auto ien = real(eltC(E));
printfln("Initial energy = %.20f",ien);
}
auto extra_args = Args("Quiet",olevel<2,"NoMeasure",sw%2==0,"iDMRG_Step",sw,"NSweep",ucsweeps.nsweep());
energy = DMRGWorker(psi,PH,ucsweeps,obs,args + extra_args);
if(show_overlap || olevel >= 1)
{
auto O = dag(initPsi(1))*psi(1);
for( auto n : range1(2,length(psi)) )
O = O*dag(initPsi(n))*psi(n);
auto ovrlap = real(eltC(O));
print("\n Overlap of initial and final psi = ");
printfln((std::fabs(ovrlap) > 1E-4 ? "%.10f" : "%.10E"),std::fabs(ovrlap));
print("\n 1-Overlap of initial and final psi = ");
printfln((1-std::fabs(ovrlap) > 1E-4 ? "%.10f" : "%.10E"),1-std::fabs(ovrlap));
}
printfln(" Energy per site = %.14f",energy/N0);
//Save last center matrix
lastV = dag(D);
lastV /= norm(lastV);
lastV.apply(detail::PseudoInvert(inverse_cut));
//Calculate new center matrix
psi.position(Nuc);
args.add("Sweep",sw);
args.add("AtBond",Nuc);
args.add("Energy",energy);
obs.measure(args+Args("AtCenter",true,"NoMeasure",true));
D = ITensor();
svd(psi(Nuc)*psi(Nuc+1),psi.ref(Nuc),D,psi.ref(Nuc+1),args);
D /= norm(D);
//Prepare MPO for next step
for(int j = 1; j <= Nuc; ++j)
{
HL *= psi(j);
HL *= H(j);
HL *= dag(prime(psi(j)));
IL *= psi(j);
IL *= H(j);
IL *= dag(prime(psi(j)));
HR *= psi(N0-j+1);
HR *= H(N0-j+1);
HR *= dag(prime(psi(N0-j+1)));
}
swapUnitCells(H);
HL += -energy*IL;
//Prepare MPS for next step
swapUnitCells(psi);
psi.ref(N0) *= D;
if((obs.checkDone(args) && sw%2==0)
|| sw == sweeps.nsweep())
{
//Convert A's (left-ortho) to B's by moving D (center matrix)
//through until last V*A_j*D == B_j
for(int b = N0-1; b >= Nuc+1; --b)
{
ITensor d;
svd(psi(b)*psi(b+1),psi.ref(b),d,psi.ref(b+1));
psi.ref(b) *= d;
}
psi.ref(Nuc+1) *= lastV;
psi.ref(0) = D;
break;
}
if(fileExists("WRITE_WF") && sw%2==0)
{
println("File WRITE_WF found: writing out wavefunction after step",sw);
system("rm -f WRITE_WF");
auto wpsi = psi;
for(int b = N0-1; b >= Nuc+1; --b)
{
ITensor d;
svd(wpsi(b)*wpsi(b+1),wpsi.ref(b),d,wpsi.ref(b+1));
wpsi.ref(b) *= d;
}
wpsi.ref(Nuc+1) *= lastV;
wpsi.ref(0) = D;
writeToFile(format("psi_%d",sw),wpsi);
//writeToFile("sites",wpsi.sites());
}
psi.ref(Nuc+1) *= lastV;
psi.ref(1) *= D;
psi.orthogonalize();
psi.normalize();
} //for loop over sw
auto res = idmrgRVal();
res.energy = energy;
res.HL = HL;
res.HR = HR;
res.IL = IL;
res.V = lastV;
return res;
}
idmrgRVal inline
idmrg(MPS & psi,
MPO const& H,
Sweeps const& sweeps,
DMRGObserver & obs,
Args const& args)
{
//Assumes H(N+1) contains vector
//picking out ending state of MPO
//automaton:
auto lval = idmrgRVal();
lval.IL = ITensor(dag(H(length(H)+1)));
return idmrg(psi,H,lval,sweeps,obs,args);
}
idmrgRVal inline
idmrg(MPS & psi,
MPO const& H,
Sweeps const& sweeps,
Args const& args)
{
auto obs = DMRGObserver(psi);
return idmrg(psi,H,sweeps,obs,args);
}
idmrgRVal inline
idmrg(MPS & psi,
MPO const& H,
idmrgRVal const& last_res,
Sweeps const& sweeps,
Args const& args)
{
auto obs = DMRGObserver(psi);
return idmrg(psi,H,last_res,sweeps,obs,args);
}
void inline
read(std::istream & s, idmrgRVal & R)
{
itensor::read(s,R.energy);
itensor::read(s,R.HL);
itensor::read(s,R.HR);
itensor::read(s,R.IL);
itensor::read(s,R.V);
}
void inline
write(std::ostream & s, idmrgRVal const& R)
{
itensor::write(s,R.energy);
itensor::write(s,R.HL);
itensor::write(s,R.HR);
itensor::write(s,R.IL);
itensor::write(s,R.V);
}
} //namespace itensor
#endif