-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathiterativesolvers_test.cc
More file actions
448 lines (340 loc) · 11 KB
/
iterativesolvers_test.cc
File metadata and controls
448 lines (340 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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "test.h"
#include "itensor/iterativesolvers.h"
#include "sample/Heisenberg.h"
#include "itensor/mps/sites/spinhalf.h"
#include "itensor/mps/localmpo.h"
#include "itensor/mps/autompo.h"
using namespace itensor;
using namespace std;
class ITensorMap
{
ITensor const& A_;
mutable long size_;
public:
ITensorMap(ITensor const& A)
: A_(A)
{
size_ = 1;
for(auto& I : A_.inds())
{
if(I.primeLevel() > 0)
size_ *= dim(I);
}
}
void
product(ITensor const& x, ITensor& b) const
{
b = A_*x;
b.noPrime();
}
long
size() const { return size_; }
};
TEST_CASE("EigenSolverTest")
{
SECTION("FourSite")
{
//Exact 4 site energy is -1.6160254038 from DMRG
const int N = 4;
SpinHalf sites(N);
MPO H = Heisenberg(sites);
InitState initState(sites);
for(int i = 1; i <= N; ++i)
initState.set(i,i%2==1 ? "Up" : "Dn");
MPS psi(initState);
LocalMPO PH(H);
psi.position(2);
PH.position(2,psi);
ITensor phi1 = psi(2) * psi(3);
Real En1 = davidson(PH,phi1,"MaxIter=9");
CHECK_CLOSE(En1,-0.95710678118);
cout << endl << endl;
//TODO: should this be put back in?
//ITensor mpoh = H(2)*H(3);
//ITensor phi2 = psi(2)*psi(3);
//Real En2 = doDavidson(phi2,mpoh,PH.L(),PH.R(),9,2,1E-4);
//cout << format("Energy from matrix Davidson (b=2) = %.20f")%En2 << endl;
//cout << endl << endl;
//cout << endl << "Overlap = " << Dot(phi1,phi2) << endl;
//cout << "---------------------------------------" << endl << endl;
//psi.doSVD(2,phi1,Fromleft);
//psi.position(3);
//PH.position(3,psi);
//ITensor phi3 = psi(3) * psi(4);
//Real En3 = d.davidson(PH,phi3);
//cout << format("Energy from tensor Davidson (b=3) = %.20f")%En3 << endl;
//cout << endl << endl;
//mpoh = H(3)*H(4);
//ITensor phi3m = psi(3)*psi(4);
//Real En3m = doDavidson(phi3m,mpoh,PH.L(),PH.R(),9,2,1E-4);
//cout << format("Energy from matrix Davidson (b=3) = %.20f")%En3m << endl;
//cout << "---------------------------------------" << endl << endl;
//psi.doSVD(3,phi3,Fromright);
//psi.position(3);
//PH.position(2,psi);
//ITensor phi4 = psi(2) * psi(3);
//Real En4 = d.davidson(PH,phi4);
//cout << format("Energy from tensor Davidson (b=2) = %.20f")%En4 << endl;
//cout << endl << endl;
//mpoh = H(2)*H(3);
//ITensor phi4m = psi(2)*psi(3);
//Real En4m = doDavidson(phi4m,mpoh,PH.L(),PH.R(),9,2,1E-4);
//cout << format("Energy from matrix Davidson (b=2) = %.20f")%En4m << endl;
//cout << "---------------------------------------" << endl << endl;
//psi.doSVD(2,phi4,Fromright);
//psi.position(1);
//PH.position(1,psi);
////With doDavidson
//mpoh = H(1)*H(2);
//ITensor phi5 = psi(1) * psi(2);
//Real En5 = doDavidson(phi5,mpoh,PH.L(),PH.R(),9,2,1E-4);
//cout << format("Energy from matrix Davidson (b=1) = %.20f")%En5 << endl;
//cout << endl << endl;
//ITensor phi6 = psi(1) * psi(2);
////Print(phi6);
////Print(PH.L());
////Print(PH.R());
////ITensor AB = phi6 * PH.R();
////AB *= H(2);
////AB *= H(1);
////AB.noprime();
////ITensor AB; PH.product(phi6,AB);
////Print(Dot(phi6,AB));
//Real En6 = d.davidson(PH,phi6);
//cout << format("Energy from tensor Davidson (b=1) = %.20f")%En6 << endl;
}
SECTION("IQFourSite")
{
//Exact 4 site energy is -1.6160254038 from DMRG
const int N = 4;
SpinHalf sites(N);
MPO H = Heisenberg(sites);
InitState initState(sites);
for(int i = 1; i <= N; ++i)
initState.set(i,i%2==1 ? "Up" : "Dn");
MPS psi(initState);
LocalMPO PH(H);
psi.position(2);
PH.position(2,psi);
ITensor phi1 = psi(2) * psi(3);
Real En1 = davidson(PH,phi1,"MaxIter=9");
//cout << format("Energy from tensor Davidson (b=2) = %.20f")%En1 << endl;
CHECK_CLOSE(En1,-0.95710678118);
}
SECTION("Davidson (Custom Linear Map)")
{
auto a1 = Index(3,"Site,a1");
auto a2 = Index(4,"Site,a2");
auto a3 = Index(5,"Site,a3");
auto A = randomITensor(prime(a1),prime(a2),prime(a3),a1,a2,a3);
A = 0.5*(A + swapPrime(dag(A),0,1));
auto x = randomITensor(a1,a2,a3);
// ITensorMap is defined above, it simply wraps an ITensor that is of the
// form of a matrix (i.e. has indices of the form {i,j,k,...,i',j',k',...})
auto lambda = davidson(ITensorMap(A),x,{"MaxIter",40,"ErrGoal",1e-14});
CHECK_CLOSE(norm(noPrime(A*x)-lambda*x)/norm(x),0.0);
}
SECTION("GMRES (ITensor, Real)")
{
auto a1 = Index(3,"Site,a1");
auto a2 = Index(4,"Site,a2");
auto a3 = Index(3,"Site,a3");
auto A = randomITensor(prime(a1),prime(a2),prime(a3),a1,a2,a3);
auto x = randomITensor(a1,a2,a3);
auto b = randomITensor(a1,a2,a3);
// ITensorMap is defined above, it simply wraps an ITensor that is of the
// form of a matrix (i.e. has indices of the form {i,j,k,...,i',j',k',...})
gmres(ITensorMap(A),b,x,{"MaxIter",36,"ErrGoal",1e-10});
CHECK_CLOSE(norm(noPrime(A*x)-b)/norm(b),0.0);
}
SECTION("GMRES (ITensor, Real), size 1")
{
auto a1 = Index(1,"Site,a1");
auto a2 = Index(1,"Site,a2");
auto a3 = Index(1,"Site,a3");
auto A = randomITensor(prime(a1),prime(a2),prime(a3),a1,a2,a3);
auto x = randomITensor(a1,a2,a3);
auto b = randomITensor(a1,a2,a3);
// ITensorMap is defined above, it simply wraps an ITensor that is of the
// form of a matrix (i.e. has indices of the form {i,j,k,...,i',j',k',...})
gmres(ITensorMap(A),b,x,{"MaxIter",36,"ErrGoal",1e-10});
CHECK_CLOSE(norm(noPrime(A*x)-b)/norm(b),0.0);
}
SECTION("GMRES (ITensor, Complex)")
{
auto a1 = Index(3,"Site,a1");
auto a2 = Index(4,"Site,a2");
auto a3 = Index(3,"Site,a3");
auto A = randomITensorC(prime(a1),prime(a2),prime(a3),a1,a2,a3);
auto x = randomITensorC(a1,a2,a3);
auto b = randomITensorC(a1,a2,a3);
gmres(ITensorMap(A),b,x,{"MaxIter",100,"ErrGoal",1e-10});
CHECK_CLOSE(norm(noPrime(A*x)-b)/norm(b),0.0);
}
SECTION("GMRES (ITensor, QN)")
{
auto i = Index(QN(+1),5,
QN(0),5,
QN(-1),5);
auto j = Index(QN(+1),5,
QN(0),5,
QN(-1),5,
In);
auto A = randomITensor(QN(0),prime(dag(i)),prime(dag(j)),i,j);
auto x = randomITensor(QN(0),dag(i),dag(j));
auto b = randomITensor(QN(0),dag(i),dag(j));
gmres(ITensorMap(A),b,x,{"MaxIter",100,"DebugLevel",0,"ErrGoal",1e-10});
CHECK_CLOSE(norm(noPrime(A*x)-b)/norm(b),0.0);
}
SECTION("Arnoldi (No QN)")
{
const int N = 50;
const int Nc = N/2;
SpinHalf sites(N,{"ConserveQNs=",false});
// Create random MPO
auto ampo = AutoMPO(sites);
for(int j = 1; j < N; ++j)
{
ampo += 0.5,"S+",j,"S-",j+1;
ampo += 0.5,"S-",j,"S+",j+1;
ampo += "Sz",j,"Sz",j+1;
}
auto H = toMPO(ampo);
for(int j = 1; j <= N; ++j)
{
H.ref(j).randomize();
H.ref(j) /= norm(H(j));
}
auto normH = sqrt(trace(H,prime(H)));
for(int j = 1; j <= N; ++j)
H.ref(j) /= pow(normH,1.0/N);
// Create random starting MPS
auto initState = InitState(sites);
for(int i = 1; i <= N; ++i)
initState.set(i,i%2==1 ? "Up" : "Dn");
auto psi = MPS(initState);
for(int j = 1; j < 2; ++j)
{
psi = applyMPO(H,psi);
psi.noPrime();
psi.normalize();
}
for(int j = 1; j <= N; ++j)
{
psi.ref(j).randomize();
psi.ref(j) /= norm(psi(j));
}
psi.position(Nc);
psi.normalize();
LocalMPO PH(H);
PH.position(Nc,psi);
auto x = psi(Nc) * psi(Nc+1);
x.randomize();
auto lambda = arnoldi(PH,x,{"MaxIter",20,"ErrGoal",1e-14,"DebugLevel",0,"WhichEig","LargestMagnitude"});
auto PHx = x;
PH.product(x,PHx);
CHECK_CLOSE(norm(PHx-lambda*x)/norm(PHx),0.0);
}
SECTION("Arnoldi (QN)")
{
const int N = 50;
const int Nc = N/2;
SpinHalf sites(N);
// Create random MPO
auto ampo = AutoMPO(sites);
for(int j = 1; j < N; ++j)
{
ampo += 0.5,"S+",j,"S-",j+1;
ampo += 0.5,"S-",j,"S+",j+1;
ampo += "Sz",j,"Sz",j+1;
}
auto H = toMPO(ampo);
for(int j = 1; j <= N; ++j)
{
H.ref(j).randomize();
H.ref(j) /= norm(H(j));
}
auto normH = sqrt(trace(H,prime(H)));
for(int j = 1; j <= N; ++j)
H.Aref(j) /= pow(normH,1.0/N);
// Create random starting MPS
auto initState = InitState(sites);
for(int i = 1; i <= N; ++i)
initState.set(i,i%2==1 ? "Up" : "Dn");
auto psi = MPS(initState);
for(int j = 1; j < 2; ++j)
{
psi = applyMPO(H,psi);
psi.noPrime();
psi.normalize();
}
for(int j = 1; j <= N; ++j)
{
psi.ref(j).randomize();
psi.ref(j) /= norm(psi(j));
}
psi.position(Nc);
psi.normalize();
LocalMPO PH(H);
psi.position(Nc);
PH.position(Nc,psi);
auto x = psi.A(Nc) * psi.A(Nc+1);
x.randomize();
auto lambda = arnoldi(PH,x,{"MaxIter",20,"ErrGoal",1e-14,"DebugLevel",0,"WhichEig","LargestMagnitude"});
auto PHx = x;
PH.product(x,PHx);
CHECK_CLOSE(norm(PHx-lambda*x)/norm(PHx),0.0);
}
SECTION("applyExp (QNs)")
{
auto i = Index(QN(-1),10,QN(1),10,"i");
auto A = randomITensor(QN(0),dag(i),prime(i));
A += swapPrime(dag(A),0,1);
A *= 0.5;
auto x0 = randomITensor(QN(-1),i);
auto Ac = randomITensorC(QN(0),dag(i),prime(i));
Ac += swapPrime(dag(Ac),0,1);
Ac *= 0.5;
auto x0c = randomITensorC(QN(-1),i);
SECTION("Real timestep")
{
auto t = 0.1;
auto x = x0;
applyExp(ITensorMap(A),x,-t,{"ErrGoal=",1E-14,
"MaxIter=",10});
auto exptA = expHermitian(A,-t);
auto exptAx = noPrime(exptA*x0);
CHECK_CLOSE(norm(exptAx - x), 0.);
}
SECTION("Complex timestep")
{
auto t = 0.1*1_i;
auto x = x0;
applyExp(ITensorMap(A),x,-t,{"ErrGoal=",1E-14,
"MaxIter=",10});
auto exptA = expHermitian(A,-t);
auto exptAx = noPrime(exptA*x0);
CHECK_CLOSE(norm(exptAx - x), 0.);
}
SECTION("Complex tensors, Real timestep")
{
auto t = 0.1;
auto x = x0;
applyExp(ITensorMap(Ac),x,-t,{"ErrGoal=",1E-14,
"MaxIter=",10});
auto exptA = expHermitian(Ac,-t);
auto exptAx = noPrime(exptA*x0);
CHECK_CLOSE(norm(exptAx - x), 0.);
}
SECTION("Complex tensors Complex timestep")
{
auto t = 0.1*1_i;
auto x = x0c;
applyExp(ITensorMap(Ac),x,-t,{"ErrGoal=",1E-14,
"MaxIter=",10});
auto exptA = expHermitian(Ac,-t);
auto exptAx = noPrime(exptA*x0c);
CHECK_CLOSE(norm(exptAx - x), 0.);
}
}
}