-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathindex.cc
More file actions
779 lines (657 loc) · 15.3 KB
/
index.cc
File metadata and controls
779 lines (657 loc) · 15.3 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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
//
// Copyright 2018 The Simons Foundation, Inc. - All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "itensor/index.h"
#include "itensor/util/readwrite.h"
#include "itensor/util/print_macro.h"
namespace itensor {
using std::string;
using std::stringstream;
string
putprimes(string s, int plev)
{
stringstream str;
str << s;
if(plev < 0) Error("Negative prime level");
if(plev > 3)
{
str << "'" << plev;
}
else
{
for(int i = 1; i <= plev; ++i) str << "\'";
}
return str.str();
}
//
// class Index
//
Index::id_type Index::
generateID()
{
static thread_local Index::IDGenerator G;
return G();
}
Index::
Index()
:
id_(0),
dim_(1),
tags_(TagSet("0"))
{
if(primeLevel() < 0) setPrime(0);
}
Index::
Index(long m,
TagSet const& t)
: id_(generateID()),
dim_(m),
tags_(t)
{
if(primeLevel() < 0) setPrime(0);
}
Index::
Index(id_type id,
long dim,
Arrow dir,
TagSet const& ts)
: id_(id),
dim_(dim),
dir_(dir),
tags_(ts)
{
if(primeLevel() < 0) setPrime(0);
}
Index& Index::
setPrime(int plev)
{
tags_.setPrime(plev);
#ifdef DEBUG
if(this->primeLevel() < 0)
Error("Negative primeLevel");
#endif
return *this;
}
Index& Index::
noPrime()
{
tags_.setPrime(0);
return *this;
}
Index& Index::
prime(int inc)
{
tags_.prime(inc);
#ifdef DEBUG
if(this->primeLevel() < 0)
{
Error("Negative primeLevel");
}
#endif
return *this;
}
Index::
operator bool() const { return (id_!=0); }
IndexVal Index::
operator()(long val) const
{
return IndexVal(*this,val);
}
IndexVal Index::
operator=(long val) const
{
return operator()(val);
}
bool
operator==(Index const& i1, Index const& i2)
{
return (id(i1) == id(i2)) && (tags(i1) == tags(i2));
}
bool
operator!=(Index const& i1, Index const& i2)
{
return not operator==(i1,i2);
}
Index::id_type
id(Index const& I) { return I.id(); }
long
dim(Index const& I) { return I.dim(); }
long
dim(IndexVal const& I) { return I.dim(); }
int
primeLevel(Index const& I) { return I.primeLevel(); }
TagSet
tags(const Index& I) { return I.tags(); }
Index
addTags(Index I, TagSet const& t) { I.addTags(t); return I; }
Index
removeTags(Index I, TagSet const& t) { I.removeTags(t); return I; }
Index
setTags(Index I, TagSet const& t) { I.setTags(t); return I; }
Index
noTags(Index I) { I.noTags(); return I; }
Index
replaceTags(Index I, TagSet const& tsold, TagSet const& tsnew) { I.replaceTags(tsold,tsnew); return I; }
Index
prime(Index I, int plinc) { I.prime(plinc); return I; }
Index
setPrime(Index I, int plev) { I.setPrime(plev); return I; }
Index
noPrime(Index I) { I.noPrime(); return I; }
IndexVal
addTags(IndexVal I, TagSet const& t) { I.addTags(t); return I; }
IndexVal
removeTags(IndexVal I, TagSet const& t) { I.removeTags(t); return I; }
IndexVal
setTags(IndexVal I, TagSet const& t) { I.setTags(t); return I; }
IndexVal
noTags(IndexVal I) { I.noTags(); return I; }
IndexVal
replaceTags(IndexVal I, TagSet const& tsold, TagSet const& tsnew) { I.replaceTags(tsold,tsnew); return I; }
IndexVal
prime(IndexVal I, int plinc) { I.prime(plinc); return I; }
IndexVal
setPrime(IndexVal I, int plev) { I.setPrime(plev); return I; }
IndexVal
noPrime(IndexVal I) { I.noPrime(); return I; }
//
// Check if Index I contains the tags tsmatch.
//
bool
hasTags(Index I, const TagSet& tsmatch) { return hasTags(tags(I),tsmatch); }
bool
hasQNs(Index const& I) { return nblock(I)!=0; }
Index
removeQNs(Index I) { if(hasQNs(I)) I.removeQNs(); return I; }
Index const&
index(IndexVal const& res) { return res.index; }
long
val(IndexVal const& res) { return res.val; }
bool
hasQNs(IndexVal const& iv) { return hasQNs(index(iv)); }
Index
dag(Index res) { res.dag(); return res; }
IndexVal
dag(IndexVal res) { res.dag(); return res; }
QN const&
qn(IndexVal const& iv) { return iv.qn(); }
Arrow
dir(Index const& res) { return res.dir(); }
Arrow
dir(IndexVal const& res) { return dir(index(res)); }
//TODO: what is this for? It doesn't make as much sense with
//tags, but I guess we can compare tags with inequalities
bool
operator>(Index const& i1, Index const& i2)
{
if(dim(i1) == dim(i2))
{
if(id(i1) == id(i2)) return primeLevel(i1) > primeLevel(i2);
return id(i1) > id(i2);
}
return dim(i1) > dim(i2);
}
bool
operator<(Index const& i1, Index const& i2)
{
if(dim(i1) == dim(i2))
{
if(id(i1) == id(i2)) return primeLevel(i1) < primeLevel(i2);
return id(i1) < id(i2);
}
return dim(i1) < dim(i2);
}
std::ostream&
operator<<(std::ostream & s, Index const& I)
{
s << "(";
s << dim(I);
if(Global::showIDs())
{
s << "|id=" << (id(I) % 1000);
//s << "," << id(I);
}
auto ts = tags(I);
if(size(ts) > 0)
{
s << "|";
auto ts = tags(I);
for(auto i : range(size(ts)))
{
s << ts[i];
if( i < (size(ts)-1) ) s << ",";
}
}
s << ")";
if(primeLevel(I) > 0)
{
if(primeLevel(I) > 3)
{
s << "'" << primeLevel(I);
}
else
{
for(int n = 1; n <= primeLevel(I); ++n)
s << "'";
}
}
else if(primeLevel(I) < 0)
{
s << "'" << primeLevel(I) << " (WARNING: prime level of Index is negative, not well defined)";
}
if(hasQNs(I))
{
s << " <" << I.dir() << ">\n";
for(auto j : range1(nblock(I)))
{
s << " " << j << ": " << blocksize(I,j) << " " << qn(I,j);
if(j != nblock(I)) s << "\n";
}
}
return s;
}
IndexVal::
IndexVal()
:
val(0)
{ }
IndexVal::
IndexVal(Index const& index_,
long val_)
:
index(index_),
val(val_)
{
#ifdef DEBUG
if(!index) Error("IndexVal initialized with default initialized Index");
//Can also use IndexVal's to indicate prime increments:
//if(val_ < 1 || val_ > dim(index))
// {
// println("val = ",val_);
// println("index = ",index);
// Error("val out of range");
// }
#endif
}
bool
operator==(IndexVal const& iv1, IndexVal const& iv2)
{
return (index(iv1) == index(iv2) && iv1.val == iv2.val);
}
bool
operator!=(IndexVal const& iv1, IndexVal const& iv2)
{
return not operator==(iv1,iv2);
}
bool
operator==(Index const& I, IndexVal const& iv)
{
return index(iv) == I;
}
bool
operator==(IndexVal const& iv, Index const& I)
{
return index(iv) == I;
}
string
showDim(Index const& I) { return format("dim=%d",dim(I)); }
std::ostream&
operator<<(std::ostream& s, IndexVal const& iv)
{
return s << "IndexVal: val = " << iv.val
<< ", ind = " << index(iv);
}
void
add(Args & args,
Args::Name const& name,
TagSet const& ts)
{
args.add(name,ts);
}
TagSet
getTagSet(Args const& args,
Args::Name const& name)
{
if(!args.defined(name)) Error(format("Name %s not found in Args",name));
return TagSet(args.getString(name));
}
TagSet
getTagSet(const Args& args,
const Args::Name& name,
TagSet const& default_val)
{
if(!args.defined(name)) return default_val;
return TagSet(args.getString(name));
}
struct IndSector
{
long sector = 0l;
long sind = 0l;
IndSector(long sec, long si) : sector(sec), sind(si) { }
};
IndSector
sectorInfo(IndexVal const& iv)
{
auto is = IndSector(1,iv.val);
while(is.sind > blocksize(index(iv),is.sector))
{
is.sind -= blocksize(index(iv),is.sector);
is.sector += 1;
}
return is;
}
QN const& IndexVal::
qn() const
{
auto is = sectorInfo(*this);
return index.qn(is.sector);
}
IndexVal& IndexVal::
dag() { index.dag(); return *this; }
class IQIndexDat
{
public:
using storage = std::vector<QNInt>;
using iterator = storage::iterator;
using const_iterator = storage::const_iterator;
private:
storage iq_;
public:
IQIndexDat() { }
explicit
IQIndexDat(storage const& ind_qn)
: iq_(ind_qn)
{ }
explicit
IQIndexDat(storage&& ind_qn)
: iq_(std::move(ind_qn))
{ }
//Disallow copying
IQIndexDat(IQIndexDat const&) = delete;
void
operator=(IQIndexDat const&) = delete;
void
setStore(storage && iq) { iq_ = std::move(iq); }
storage const&
inds() const { return iq_; }
long
size() const { return iq_.size(); }
long
blocksize(long i) { return iq_[i-1].second; }
long
blocksize0(long i) { return iq_[i].second; }
QN const&
qn(long i) { return iq_[i-1].first; }
iterator
begin() { return iq_.begin(); }
iterator
end() { return iq_.end(); }
const_iterator
begin() const { return iq_.begin(); }
const_iterator
end() const { return iq_.end(); }
storage const&
store() const { return iq_; }
};
#ifdef DEBUG
#define IQINDEX_CHECK_NULL if(pd == 0) throw ITError("IQIndex storage unallocated");
#else
#define IQINDEX_CHECK_NULL
#endif
Index
directSum(Index const& i,
Index const& j,
Args const& args)
{
auto tags = getTagSet(args,"Tags","sum");
if(not hasQNs(i) && not hasQNs(j))
{
auto dim_ij = dim(i) + dim(j);
if(dim_ij <= 0) dim_ij = 1;
return Index(dim_ij,tags);
}
else
{
#ifdef DEBUG
if( dir(i) != dir(j) ) Error("In directSum(Index, Index), input indices must have same arrow direction");
#endif
auto nblock_i = nblock(i);
auto nblock_j = nblock(j);
auto siq = stdx::reserve_vector<QNInt>(nblock_i+nblock_j);
for(auto iqn : range1(nblock_i))
siq.emplace_back(qn(i,iqn),blocksize(i,iqn));
for(auto jqn : range1(nblock_j))
siq.emplace_back(qn(j,jqn),blocksize(j,jqn));
#ifdef DEBUG
if(siq.empty()) Error("siq is empty in plussers");
#endif
return Index(std::move(siq),dir(i),tags);
}
return Index();
}
Index
sim(Index const& I)
{
Index J;
J.sim(I);
return J;
}
void
write(std::ostream & s, QNInt const& q)
{
write(s,q.first);
write(s,q.second);
}
void
read(std::istream & s, QNInt & q)
{
read(s,q.first);
read(s,q.second);
}
void
write(std::ostream & s, IQIndexDat const& d)
{
write(s,d.store());
}
void
read(std::istream & s, IQIndexDat & d)
{
IQIndexDat::storage store;
read(s,store);
d.setStore(std::move(store));
}
long Index::
nblock() const
{
if(not pd) return 0;
return static_cast<long>(pd->size());
}
long
nblock(Index const& i) { return i.nblock(); }
QN const& Index::
qn(long i) const
{
IQINDEX_CHECK_NULL
#ifdef DEBUG
if(i > nblock())
{
Print(nblock());
Print(i);
Error("IQIndex::qn arg out of range");
}
#endif
return pd->qn(i);
}
QN const&
qn(Index const& i, long b) { return i.qn(b); }
long Index::
blocksize(long i) const
{
IQINDEX_CHECK_NULL
#ifdef DEBUG
if(i > nblock())
{
Print(nblock());
Print(i);
Error("Index::blocksize arg out of range");
}
#endif
return pd->blocksize(i);
}
long
blocksize(Index const& i, long b) { return i.blocksize(b); }
long Index::
blocksize0(long i) const
{
IQINDEX_CHECK_NULL
#ifdef DEBUG
if(i >= nblock())
{
Print(nblock());
Print(i);
Error("Index::blocksize0 arg out of range");
}
#endif
return pd->blocksize0(i);
}
void Index::
makeStorage(qnstorage && qi)
{
pd = std::make_shared<IQIndexDat>(std::move(qi));
}
long
totalM(Index::qnstorage const& storage)
{
long tm = 0;
for(auto& iq : storage)
{
tm += iq.second;
}
return tm;
}
Index::
Index(qnstorage && ind_qn,
TagSet const& ts)
: Index(totalM(ind_qn),ts)
{
dir_ = Out;
makeStorage(std::move(ind_qn));
if(primeLevel() < 0) setPrime(0);
}
Index::
Index(qnstorage && ind_qn,
Arrow dir,
TagSet const& ts)
: Index(totalM(ind_qn),ts)
{
dir_ = dir;
makeStorage(std::move(ind_qn));
if(primeLevel() < 0) setPrime(0);
}
long
QNblock(Index const& I,
QN const& Q)
{
for(auto n : range1(nblock(I)))
{
if(qn(I,n) == Q) return n;
}
if(not hasQNs(I)) Error("Index does not contain any QN blocks");
println("I = ",I);
println("Q = ",Q);
Error("Index does not contain given QN block.");
return 0l;
}
long
QNblockSize(Index const& I,
QN const& Q)
{
return blocksize(I,QNblock(I,Q));
}
void Index::
write(std::ostream& s) const
{
if(!bool(*this)) Error("Index::write: Index is default initialized");
itensor::write(s,tags_);
itensor::write(s,id_);
itensor::write(s,dim_);
itensor::write(s,dir_);
if(pd) itensor::write(s,*pd);
else itensor::write(s,IQIndexDat());
}
Index& Index::
read(std::istream& s)
{
itensor::read(s,tags_);
if(Global::read32BitIDs())
{
using ID32 = std::mt19937::result_type;
ID32 oldid = 0;
itensor::read(s,oldid);
id_ = oldid;
}
else
{
itensor::read(s,id_);
}
itensor::read(s,dim_);
itensor::read(s,dir_);
IQIndexDat dat;
itensor::read(s,dat);
if(dat.size()>0)
pd = std::make_shared<IQIndexDat>(std::move(dat.store()));
#ifdef DEBUG
if(tags_.primeLevel() < 0) Error("Negative primeLevel");
#endif
return *this;
}
bool
isFermionic(Index const& I)
{
for(auto i : range1(I.nblock()))
{
for(auto& qnum : I.qn(i).store())
{
if(isFermionic(qnum)) return true;
}
}
return false;
}
#ifdef ITENSOR_USE_HDF5
void
h5_write(h5::group parent, std::string const& name, Index const& I)
{
auto g = parent.create_group(name);
h5_write_attribute(g,"type","Index",true);
h5_write_attribute(g,"version",long(1));
h5_write(g,"id",static_cast<unsigned long>(I.id()));
h5_write(g,"dim",long(I.dim()));
h5_write(g,"dir",long(I.dir()));
h5_write(g,"tags",I.tags());
}
void
h5_read(h5::group parent, std::string const& name, Index & I)
{
auto g = parent.open_group(name);
auto type = h5_read_attribute<string>(g,"type");
if(type != "Index") Error("Group does not contain Index data in HDF5 file");
auto id = h5_read<unsigned long>(g,"id");
auto dim = h5_read<long>(g,"dim");
auto dir = h5_read<long>(g,"dir");
auto tags = h5_read<TagSet>(g,"tags");
I = Index(id,dim,toArrow(dir),tags);
}
#endif
} //namespace itensor