-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdmrg_U1_test.cpp
417 lines (379 loc) · 16 KB
/
dmrg_U1_test.cpp
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
#include <benchmark/benchmark.h>
#include <array>
#include <itensor/all.h>
#include <cytnx.hpp>
#include <malloc.h>
#include <chrono>
// #include "dmrg.h"
using namespace cytnx;
using namespace itensor;
class Hxx: public LinOp{
public:
Network anet;
UniTensor L;
UniTensor R;
UniTensor M1;
UniTensor M2;
Hxx(Network &anet, UniTensor &L, UniTensor &M1, UniTensor &M2, UniTensor &R):
LinOp("mv", 0, Type.Double, Device.cpu){
this->anet = anet;
this->L = L;
this->R = R;
this->M1 = M1;
this->M2 = M2;
}
UniTensor matvec(const UniTensor &v) override{
auto lbl = v.labels();
// this->anet.PutUniTensor("psi",v);
// auto out = this->anet.Launch(false);
auto L_ = this->L.relabels({"-5","-1","0"});
auto R_ = this->R.relabels({"-7","-4","3"});
auto M1_ = this->M1.relabels({"-5","-6","-2","1"});
auto M2_ = this->M2.relabels({"-6","-7","-3","2"});
auto psi_ = v.relabels({"-1","-2","-3","-4"});
auto out = L_.contract(M1_.contract(M2_.contract(psi_.contract(R_, true, true), true, true), true, true), true, true);
// out.contiguous_();
out.set_labels(lbl);
return out;
}
};
std::vector<UniTensor> optimize_psi(UniTensor &psivec,UniTensor &L, UniTensor &M1,UniTensor &M2, UniTensor &R, int maxit, int krydim){
auto anet = Network();
// anet.FromString({"psi: -1,-2,-3,-4",\
// "L: -5,-1,0",\
// "R: -7,-4,3",\
// "M1: -5,-6,-2,1",\
// "M2: -6,-7,-3,2",\
// "TOUT: 0,1;2,3",\
// "ORDER: (L,(M1,(M2,(psi,R))))"});
// anet.PutUniTensors({"L","M1","M2","R"},{L,M1,M2,R});
auto H = Hxx(anet,L,M1,M2,R);
// auto t1 = std::chrono::high_resolution_clock::now();
// auto res = linalg::Lanczos(&H, psivec, "Gnd", 999, maxit, 1, true, false, 0, false);
auto res = linalg::Lanczos(&H, psivec, "Gnd", 999, maxit, 1, true, false, 0, false);
// auto t2 = std::chrono::high_resolution_clock::now();
// std::cout << "Lanczos took "
// << std::chrono::duration_cast<std::chrono::nanoseconds>(t2-t1).count()
// << " ns\n";
// auto res = linalg::Arnoldi(&H, psivec, "SM", maxit, 999, 1, true, false);
return res;
};
static void cytnx_dmrg_U1(benchmark::State& state){
malloc_trim(0);
int chi = state.range(0); // Maximum allowed bondim (Perform truncation if exceeds)
int Nsites = state.range(1); // Number of sites
int numsweeps = state.range(2); // number of DMRG sweeps
int maxit = 2; // iterations of Lanczos method
int krydim = 4; // dimension of Krylov subspace (Will not be used in Lanczos "Gnd" method)
int d = 2; // 1/2-spin
//// set-up ////
Bond bd_inner = Bond(BD_KET,{{0},{-2},{2},{0}},{1,1,1,1});
Bond bd_phys = Bond(BD_KET,{{1},{-1}},{1,1});
UniTensor M = UniTensor({bd_inner,bd_inner.redirect(),bd_phys, bd_phys.redirect()}); M.set_rowrank(2);
// I
M.set_elem({0,0,0,0},1);
M.set_elem({0,0,1,1},1);
M.set_elem({3,3,0,0},1);
M.set_elem({3,3,1,1},1);
// S-
M.set_elem({0,1,1,0},sqrt(2));
// S+
M.set_elem({0,2,0,1},sqrt(2));
// S+
M.set_elem({1,3,0,1},sqrt(2));
// S-
M.set_elem({2,3,1,0},sqrt(2));
int q = 0; // conserving glb Qn
Bond VbdL = Bond(BD_KET,{{0}},{1});
Bond VbdR = Bond(BD_KET,{{q}},{1});
UniTensor L0 = UniTensor({bd_inner.redirect(),VbdL.redirect(),VbdL}); L0.set_rowrank(1);//Left boundary
UniTensor R0 = UniTensor({bd_inner,VbdR,VbdR.redirect()}); R0.set_rowrank(1); //Right boundary
L0.set_elem({0,0,0},1);
R0.set_elem({3,0,0},1);
std::vector<UniTensor> A(Nsites);
int qcntr = 0;
int cq;
if(qcntr <= q)
cq = 1;
else
cq = -1;
qcntr+=cq;
A[0] = UniTensor({VbdL,bd_phys.redirect(),Bond(BD_BRA,{{qcntr}},{1})}); A[0].set_rowrank(2);
A[0].get_block_(0).at({0,0,0})= 1;
for(int k = 1; k<Nsites;k++){
auto B1 = A[k-1].bonds()[2].redirect();
auto B2 = A[k-1].bonds()[1];
if(qcntr <= q)
cq = 1;
else
cq = -1;
qcntr+=cq;
auto B3 = Bond(BD_BRA,{{qcntr}},{1});
A[k] = UniTensor({B1,B2,B3}); A[k].set_rowrank(2);
A[k].set_labels({2*k,2*k+1,2*k+2});
A[k].get_block_(0).at({0,0,0})= 1;
}
std::vector<UniTensor> LR(Nsites+1);
LR[0] = L0;
LR[Nsites] = R0;
// auto anet = Network();
// anet.FromString({"L: -2,-1,-3",\
// "A: -1,-4,1",\
// "M: -2,0,-4,-5",\
// "A_Conj: -3,-5,2",\
// "TOUT: 0;1,2"});
for(int p = 0; p<Nsites-1;p++){
auto LR_ = LR[p].relabels({"-2","-1","-3"});
auto A_ = A[p].relabels({"-1","-4","1"});
auto Ad_ = A[p].Dagger().relabels({"-3","-5","2"});
auto M_= M.relabels({"-2","0","-4","-5"});
LR[p+1] = Ad_.contract(M_.contract(A_.contract(LR_,true),true),true).permute({1,2,0});
// anet.PutUniTensors({"L","A","A_Conj","M"},{LR[p],A[p],A[p].Dagger(),M});
// LR[p+1] = anet.Launch(true);
}
for(int k = 0; k<numsweeps;k++){
for(int p = Nsites-2; p>=0; p--){
auto dim_l = A[p].shape()[0];
auto dim_r = A[p+1].shape()[2];
auto psi = cytnx::Contract(A[p],A[p+1]);
auto optres = optimize_psi(psi, LR[p],M,M,LR[p+2], maxit, krydim);
psi = optres[1];
auto lbl1 = A[p].labels();
auto lbl2 = A[p+1].labels();
psi.set_rowrank(2);
auto svdres = linalg::Svd_truncate(psi, chi);
auto s = svdres[0];
A[p] = svdres[1];
A[p+1] = svdres[2];
A[p+1].set_labels(lbl2);
A[p] = cytnx::Contract(A[p],s); //// absorb s into next neighbor
A[p].set_labels(lbl1);
// anet.FromString({"R: -2,-1,-3",\
// "B: 1,-4,-1",\
// "M: 0,-2,-4,-5",\
// "B_Conj: 2,-5,-3",\
// "TOUT: 0;1,2"});
// anet.PutUniTensors({"R","B","M","B_Conj"},{LR[p+2],A[p+1],M,A[p+1].Dagger()});
// LR[p+1] = anet.Launch(true);
auto LR_ = LR[p+2].relabels({"-2","-1","-3"});
auto B_ = A[p+1].relabels({"1","-4","-1"});
auto Bd_ = A[p+1].Dagger().relabels({"2","-5","-3"});
auto M_= M.relabels({"0","-2","-4","-5"});
LR[p+1] = Bd_.contract(M_.contract(B_.contract(LR_,true),true),true).permute({1,2,0});;
// std::cout<<"Sweep r->l "<<k<<"/"<<numsweeps<<" loc:"<<p<<" Energy:"<<double(optres[0].item().real())<<std::endl;
}
auto lbl = A[0].labels();
A[0].set_rowrank(1);
auto res = linalg::Svd(A[0], false, true);
A[0] = res[1];
A[0].set_labels(lbl);
for(int p = 0; p<Nsites-1; p++){
auto dim_l = A[p].shape()[0];
auto dim_r = A[p+1].shape()[2];
auto psi = cytnx::Contract(A[p],A[p+1]); //// cytnx::Contract
auto optres = optimize_psi(psi, LR[p],M,M,LR[p+2], maxit, krydim);
psi = optres[1];
auto lbl1 = A[p].labels();
auto lbl2 = A[p+1].labels();
psi.set_rowrank(2);
auto svdres = linalg::Svd_truncate(psi, chi);
auto s = svdres[0];
A[p] = svdres[1];
A[p+1] = svdres[2];
A[p].set_labels(lbl1);
A[p+1] = cytnx::Contract(s,A[p+1]); //// absorb s into next neighbor.
A[p+1].set_labels(lbl2);
// anet = Network();
// anet.FromString({"L: -2,-1,-3",\
// "A: -1,-4,1",\
// "M: -2,0,-4,-5",\
// "A_Conj: -3,-5,2",\
// "TOUT: 0;1,2"});
// anet.PutUniTensors({"L","A","A_Conj","M"},{LR[p],A[p],A[p].Dagger(),M});
// LR[p+1] = anet.Launch(true);
auto LR_ = LR[p].relabels({"-2","-1","-3"});
auto A_ = A[p].relabels({"-1","-4","1"});
auto Ad_ = A[p].Dagger().relabels({"-3","-5","2"});
auto M_= M.relabels({"-2","0","-4","-5"});
LR[p+1] = Ad_.contract(M_.contract(A_.contract(LR_,true),true),true).permute({1,2,0});;
//std::cout<<"Sweep l->r "<<k<<"/"<<numsweeps<<" loc:"<<p<<" Energy:"<<double(optres[0].item().real())<<std::endl;
}
lbl = A[Nsites-1].labels();
A[Nsites-1].set_rowrank(2);
res = linalg::Svd(A[Nsites-1],true,false); //// last one.
A[Nsites-1] = res[1];
A[Nsites-1].set_labels(lbl);
// std::cout<<"Done : "<<k<<std::endl;
}
// std::cout<<A[Nsites/2].shape()<<std::endl;
for (auto _: state) {
std::vector<std::string> oldlbls;
for(int p = Nsites-2; p>=0; p--){
auto dim_l = A[p].shape()[0];
auto dim_r = A[p+1].shape()[2];
auto psi = cytnx::Contract(A[p],A[p+1],true,true);
auto optres = optimize_psi(psi, LR[p],M,M,LR[p+2], maxit, krydim);
psi = optres[1];
auto lbl1 = A[p].labels();
auto lbl2 = A[p+1].labels();
psi.set_rowrank(2);
auto svdres = linalg::Svd_truncate(psi, chi);
auto s = svdres[0];
A[p] = svdres[1];
A[p+1] = svdres[2];
A[p+1].set_labels(lbl2);
A[p] = cytnx::Contract(A[p],s,true,true); //// absorb s into next neighbor
A[p].set_labels(lbl1);
// anet.FromString({"R: -2,-1,-3",\
// "B: 1,-4,-1",\
// "M: 0,-2,-4,-5",\
// "B_Conj: 2,-5,-3",\
// "TOUT: 0;1,2"});
// anet.PutUniTensors({"R","B","M","B_Conj"},{LR[p+2],A[p+1],M,A[p+1].Dagger()});
// LR[p+1] = anet.Launch(true);
auto LR_ = LR[p+2].relabels({"-2","-1","-3"});
auto B_ = A[p+1].relabels({"1","-4","-1"});
A[p+1].Dagger_();
oldlbls = A[p+1].labels();
// auto Bd_ = A[p+1];
A[p+1].set_labels({"2","-5","-3"});
// auto Bd_ = A[p+1].Dagger().relabels({"2","-5","-3"});
auto M_= M.relabels({"0","-2","-4","-5"});
LR[p+1] = A[p+1].contract(M_.contract(B_.contract(LR_,true),true),true).permute({1,2,0});;
// std::cout<<"Sweep r->l "<<k<<"/"<<numsweeps<<" loc:"<<p<<" Energy:"<<double(optres[0].item().real())<<std::endl;
A[p+1].Dagger_();
A[p+1].set_labels(oldlbls);
}
auto lbl = A[0].labels();
A[0].set_rowrank(1);
auto res = linalg::Svd(A[0], false, true);
A[0] = res[1];
A[0].set_labels(lbl);
for(int p = 0; p<Nsites-1; p++){
auto dim_l = A[p].shape()[0];
auto dim_r = A[p+1].shape()[2];
auto psi = cytnx::Contract(A[p],A[p+1],true,true); //// cytnx::Contract
auto optres = optimize_psi(psi, LR[p],M,M,LR[p+2], maxit, krydim);
psi = optres[1];
auto lbl1 = A[p].labels();
auto lbl2 = A[p+1].labels();
psi.set_rowrank(2);
auto svdres = linalg::Svd_truncate(psi, chi);
auto s = svdres[0];
A[p] = svdres[1];
A[p+1] = svdres[2];
A[p].set_labels(lbl1);
A[p+1] = cytnx::Contract(s,A[p+1],true,true); //// absorb s into next neighbor.
A[p+1].set_labels(lbl2);
// anet = Network();
// anet.FromString({"L: -2,-1,-3",\
// "A: -1,-4,1",\
// "M: -2,0,-4,-5",\
// "A_Conj: -3,-5,2",\
// "TOUT: 0;1,2"});
// anet.PutUniTensors({"L","A","A_Conj","M"},{LR[p],A[p],A[p].Dagger(),M});
// LR[p+1] = anet.Launch(true);
auto LR_ = LR[p].relabels({"-2","-1","-3"});
auto A_ = A[p].relabels({"-1","-4","1"});
A[p].Dagger_();
// auto Ad_ = A[p];
oldlbls = A[p].labels();
// auto Ad_ = A[p].Dagger().relabels({"-3","-5","2"});
A[p].set_labels({"-3","-5","2"});
auto M_= M.relabels({"-2","0","-4","-5"});
LR[p+1] = A[p].contract(M_.contract(A_.contract(LR_,true),true),true).permute({1,2,0});
// std::cout<<" Energy:"<<double(optres[0].item().real())<<std::endl;
A[p].Dagger_();
A[p].set_labels(oldlbls);
}
lbl = A[Nsites-1].labels();
A[Nsites-1].set_rowrank(2);
res = linalg::Svd(A[Nsites-1],true,false); //// last one.
A[Nsites-1] = res[1];
A[Nsites-1].set_labels(lbl);
// std::cout<<"Done : "<<k<<std::endl;
}
}
static void itensor_dmrg_U1(benchmark::State& state){
// string infile = argv[1];
// InputGroup input (infile,"basic");
// auto qn = input.getYesNo("quantum_number");
// auto dims = read_vector<int> (infile, "bond_dim");
malloc_trim(0);
int chi = state.range(0);
int N = state.range(1);
int Nsweeps = state.range(2);
auto qn = true;
auto sites = SpinHalf(N, {"ConserveQNs=",false,"ConserveSz=",true}); //make a chain of N spin 1/2's
auto ampo = AutoMPO(sites);
for(auto j : range1(N-1))
{
ampo += 0.5,"S+",j,"S-",j+1;
ampo += 0.5,"S-",j,"S+",j+1;
}
auto H = toMPO(ampo);
auto state_ = InitState(sites);
for(auto i : range1(N))
{
if(i%2 == 1) state_.set(i,"Up");
else state_.set(i,"Dn");
}
auto psi = MPS(state_);
Real energy;
auto sweeps = Sweeps(Nsweeps);
sweeps.maxdim() = chi;
sweeps.mindim() = chi;
sweeps.cutoff() = 1E-15;
sweeps.niter() = 2;
std::tie(energy,psi) = dmrg(H,psi,sweeps,{"Silent",true,"UseSVD",true});
auto psit = psi;
sweeps = Sweeps(1);
sweeps.maxdim() = chi;
sweeps.mindim() = chi;
sweeps.cutoff() = 1E-15;
sweeps.niter() = 2;
for (auto _: state) {
std::tie(energy,psit) = dmrg(H,psit,sweeps,{"Silent",true,"UseSVD",true});
}
}
// BENCHMARK(cytnx_dmrg_U1)->Args({64,32,5});
// BENCHMARK(cytnx_dmrg_U1)->Args({64,32,5});
// BENCHMARK(cytnx_dmrg_U1)->Args({64,32,5});
// BENCHMARK(cytnx_dmrg_U1)->Args({64,32,5});
// BENCHMARK(cytnx_dmrg_U1)->Args({64,32,5});
// BENCHMARK(cytnx_dmrg_U1)->Args({64,32,5});
// BENCHMARK(cytnx_dmrg_U1)->Args({64,32,5});
// BENCHMARK(cytnx_dmrg_U1)->Args({64,32,5});
// BENCHMARK(cytnx_dmrg_U1)->Args({64,32,5});
// BENCHMARK(cytnx_dmrg_U1)->Args({64,32,5});
// BENCHMARK(cytnx_dmrg_U1)->Args({64,32,5});
// BENCHMARK(cytnx_dmrg_U1)->Args({64,32,5});
// BENCHMARK(itensor_dmrg_U1)->Args({64,32,2});
// BENCHMARK(itensor_dmrg_U1)->Args({64,32,2});
// BENCHMARK(itensor_dmrg_U1)->Args({64,32,2});
// BENCHMARK(itensor_dmrg_U1)->Args({64,32,2});
// BENCHMARK(itensor_dmrg_U1)->Args({64,32,2});
// BENCHMARK(itensor_dmrg_U1)->Args({64,32,2});
// BENCHMARK(itensor_dmrg_U1)->Args({64,32,2});
// BENCHMARK(itensor_dmrg_U1)->Args({64,32,2});
// BENCHMARK(itensor_dmrg_U1)->Args({64,32,2});
// BENCHMARK(itensor_dmrg_U1)->Args({64,32,2});
// BENCHMARK(itensor_dmrg_U1)->Args({64,32,2});
// BENCHMARK(itensor_dmrg_U1)->Args({64,32,2});
// BENCHMARK(cytnx_dmrg_U1)->Args({64,32,5});
// BENCHMARK(cytnx_dmrg_U1)->Args({100,32,5});
// BENCHMARK(cytnx_dmrg_U1)->Args({200,32,5});
// BENCHMARK(cytnx_dmrg_U1)->Args({300,32,7});
// BENCHMARK(cytnx_dmrg_U1)->Args({400,32,10});
// BENCHMARK(cytnx_dmrg_U1)->Args({500,32,18});
// BENCHMARK(cytnx_dmrg_U1)->Args({700,32,18});
BENCHMARK(cytnx_dmrg_U1)->Args({2000,32,10});
// BENCHMARK(itensor_dmrg_U1)->Args({64,32,2});
// BENCHMARK(itensor_dmrg_U1)->Args({100,32,5});
// BENCHMARK(itensor_dmrg_U1)->Args({200,32,5});
// BENCHMARK(itensor_dmrg_U1)->Args({300,32,7});
// BENCHMARK(itensor_dmrg_U1)->Args({400,32,10});
// BENCHMARK(itensor_dmrg_U1)->Args({500,32,18});
// BENCHMARK(itensor_dmrg_U1)->Args({700,32,18});
// BENCHMARK(itensor_dmrg_U1)->Args({2000,32,10});
BENCHMARK_MAIN();