-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathitensor.cc
More file actions
1771 lines (1572 loc) · 41.5 KB
/
Copy pathitensor.cc
File metadata and controls
1771 lines (1572 loc) · 41.5 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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// 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/util/print_macro.h"
//#include "itensor/util/iterate.h"
#include "itensor/util/safe_ptr.h"
#include "itensor/itensor.h"
#include "itensor/tensor/lapack_wrap.h"
#include "itensor/tensor/contract.h"
using std::array;
using std::ostream;
using std::vector;
using std::move;
namespace itensor {
//
// ITensor Constructors
//
ITensor::
ITensor(IndexSet const& is)
: is_(is)
{
IF_USESCALE(scale_ = LogNum(1.);)
}
ITensor::
ITensor(IndexSet iset,
storage_ptr&& pdat,
LogNum const& scale)
:
is_(std::move(iset)),
store_(std::move(pdat))
{
IF_USESCALE(scale_ = scale;)
}
ITensor::
ITensor(std::initializer_list<Index> inds)
: is_(inds)
{
IF_USESCALE(scale_ = LogNum(1.);)
}
ITensor::
ITensor(Cplx val)
{
IF_USESCALE(scale_ = LogNum(1.);)
if(val.imag() == 0)
{
store_ = newITData<ScalarReal>(val.real());
}
else
{
store_ = newITData<ScalarCplx>(val);
}
}
ITensor::
ITensor(QN q, IndexSet const& is)
:
is_(std::move(is))
{
store_ = newITData<QDenseReal>(is,q);
}
Cplx ITensor::
eltC() const
{
if(inds().order() != 0)
{
PrintData(inds());
Error(format("Wrong number of IndexVals passed to elt/eltC (expected 0, got %d)",inds().order()));
}
constexpr size_t size = 0;
auto inds = IntArray(size);
auto z = itensor::doTask(GetElt{is_,inds},store_);
#ifndef USESCALE
return z;
#else
try {
return z*scale_.real0();
}
catch(TooBigForReal const& e)
{
println("too big for real in eltC(...), scale = ",scale());
throw e;
}
catch(TooSmallForReal const&)
{
println("warning: too small for real in eltC(...)");
return Cplx(0.,0.);
}
return Cplx(NAN,NAN);
#endif
}
Cplx ITensor::
eltC(std::vector<IndexVal> const& ivs) const
{
if(!store()) Error("tensor storage unallocated");
auto size = ivs.size();
if(size != size_t(inds().order()))
{
println("---------------------------------------------");
println("Tensor indices = \n",inds(),"\n");
println("---------------------------------------------");
println("Indices provided = ");
for(auto& iv : ivs) println(iv.index);
println("---------------------------------------------");
Error(format("Wrong number of IndexVals passed to elt/eltC (expected %d, got %d)",inds().order(),size));
}
auto ints = IntArray(size);
detail::permute_map(inds(),ivs,ints,
[](IndexVal const& iv) { return iv.val-1; });
auto z = itensor::doTask(GetElt{inds(),ints},store_);
#ifndef USESCALE
return z;
#else
try {
return z*scale().real0();
}
catch(TooBigForReal const& e)
{
println("too big for real in eltC(...), scale = ",scale());
throw e;
}
catch(TooSmallForReal const&)
{
println("warning: too small for real in eltC(...)");
return Cplx(0.,0.);
}
return Cplx(NAN,NAN);
#endif
}
void ITensor::
set(std::vector<IndexVal> const& ivals,
Cplx val)
{
auto size = ivals.size();
if(size != size_t(inds().order()))
{
println("---------------------------------------------");
println("Tensor indices = \n",inds(),"\n");
println("---------------------------------------------");
println("Indices provided = ");
for(auto& iv : ivals) println(iv.index);
println("---------------------------------------------");
Error(format("Wrong number of IndexVals passed to set (expected %d, got %d)",
inds().order(),size));
}
auto inds = IntArray(is_.order(),0);
detail::permute_map(is_,ivals,inds,
[](IndexVal const& iv) { return iv.val-1; });
if(!store_) detail::allocReal(*this,inds);
scaleTo(1.);
if(val.imag()==0.0)
{
doTask(SetElt<Real>{val.real(),is_,inds},store_);
}
else
{
doTask(SetElt<Cplx>{val,is_,inds},store_);
}
}
void ITensor::
set(Cplx val)
{
if(0 != size_t(inds().order()))
{
Error(format("Wrong number of IndexVals passed to set (expected %d, got 0)",
inds().order()));
}
auto inds = IntArray(0,1);
if(!store_) detail::allocReal(*this,inds);
scaleTo(1.);
if(val.imag()==0.)
{
doTask(SetElt<Real>{val.real(),is_,inds},store_);
}
else
{
doTask(SetElt<Cplx>{val,is_,inds},store_);
}
}
void ITensor::
set(std::vector<int> const& ints,
Cplx val)
{
auto size = ints.size();
if(size != size_t(inds().order()))
{
println("---------------------------------------------");
println("Tensor indices = \n",inds(),"\n");
println("---------------------------------------------");
println("Indices provided = ");
for(auto& iv : ints) println(iv);
println("---------------------------------------------");
Error(format("Wrong number of IndexVals passed to set (expected %d, got %d)",
inds().order(),size));
}
auto inds = IntArray(is_.order(),0);
for(auto i : range(size))
inds[i] = ints[i]-1;
//TODO: if !store_ and !is_real, call allocCplx instead
//detail::permute_map(is_,ivals,inds,
// [](IndexVal const& iv) { return iv.val-1; });
if(!store_) detail::allocReal(*this,inds);
scaleTo(1.);
if(val.imag()==0.0)
{
doTask(SetElt<Real>{val.real(),is_,inds},store_);
}
else
{
doTask(SetElt<Cplx>{val,is_,inds},store_);
}
}
ITensor& ITensor::
conj()
{
doTask(Conj{},store_);
return *this;
}
ITensor& ITensor::
dag()
{
if(hasQNs(is_)) is_.dag();
return conj();
}
ITensor& ITensor::
takeReal()
{
doTask(TakeReal{},store_);
return *this;
}
ITensor& ITensor::
takeImag()
{
doTask(TakeImag{},store_);
return *this;
}
ITensor& ITensor::
makeCplx()
{
doTask(MakeCplx{},store_);
return *this;
}
ITensor& ITensor::
randomize(Args const& args)
{
if(!this->store()) detail::allocReal(*this);
#ifdef DEBUG
if(!(*this)) Error("default initialized tensor in randomize");
#endif
auto cplx = args.getBool("Complex",false);
if(cplx) this->generate(detail::quickranCplx);
else this->generate(detail::quickran);
return *this;
}
size_t
nnzblocks(ITensor const& A)
{
if(hasQNs(A)) return doTask(NNZBlocks{},A.store());
return 1;
}
long
nnz(ITensor const& A)
{
return doTask(NNZ{},A.store());
}
ITensor& ITensor::
fixBlockDeficient()
{
if(itensor::hasQNs(*this) && itensor::isDense(*this))
{
// If the ITensor has QNs, we may need to
// expand the storage since it may be
// block deficient
auto itflux = itensor::flux(*this);
auto [bofs,size] = getBlockOffsets(inds(),itflux);
if(bofs.size() != itensor::nnzblocks(*this))
{
// Make a copy of the original ITensor
auto Torig = *this;
// The QDense storage is block deficient,
// need to allocate new memory.
if(isReal(*this))
store_ = newITData<QDense<Real>>(bofs,size);
else
store_ = newITData<QDense<Cplx>>(bofs,size);
*this += Torig;
}
}
return *this;
}
ITensor& ITensor::
fill(Cplx z)
{
if(!store_)
{
if(is_) detail::allocReal(*this);
else Error("Can't fill default-constructed tensor");
}
IF_USESCALE(scale_ = scale_type(1.);)
if(itensor::hasQNs(*this))
{
// If the ITensor has QNs, we may need to
// expand the storage since it may be
// block deficient
auto itflux = itensor::flux(*this);
auto [bofs,size] = getBlockOffsets(inds(),itflux);
if(bofs.size() != itensor::nnzblocks(*this))
{
// The QDense storage is block deficient,
// need to allocate new memory.
// Make the new memory undefined since it
// will be overwritten anyway.
if(z.imag() == 0)
store_ = newITData<QDense<Real>>(undef,bofs,size);
else
store_ = newITData<QDense<Cplx>>(undef,bofs,size);
}
}
if(z.imag() == 0)
doTask(Fill<Real>{z.real()},store_);
else
doTask(Fill<Cplx>{z},store_);
return *this;
}
#ifdef USESCALE
void ITensor::
scaleTo(scale_type const& newscale)
{
if(scale_ == newscale) return;
if(newscale.sign() == 0) Error("Trying to scale an ITensor to a 0 scale");
scale_ /= newscale;
doTask(Mult<Real>{scale_.real0()},store_);
scale_ = newscale;
}
void ITensor::
scaleTo(Real newscale) { scaleTo(LogNum{newscale}); }
#endif
void ITensor::
swap(ITensor & other)
{
is_.swap(other.is_);
store_.swap(other.store_);
IF_USESCALE(scale_.swap(other.scale_);)
}
ITensor
conj(ITensor T)
{
T.conj();
return T;
}
ITensor
dag(ITensor T)
{
T.dag();
return T;
}
bool
hasQNs(ITensor const& T) { return hasQNs(inds(T)); }
long
order(ITensor const& T) { return order(inds(T)); }
IndexSet const&
inds(ITensor const& A) { return A.inds(); }
long
minDim(ITensor const& T) { return minDim(inds(T)); }
long
maxDim(ITensor const& T) { return maxDim(inds(T)); }
std::vector<IndexSet>
inds(std::vector<ITensor> const& A)
{
auto is = std::vector<IndexSet>(A.size());
for( auto i : range(A.size()) )
is[i] = inds(A[i]);
return is;
}
Index const&
index(ITensor const& A, RangeT<Index>::size_type I) { return A.index(I); }
Index
findIndex(ITensor const& T,
TagSet const& tsmatch)
{
return findIndex(inds(T),tsmatch);
}
IndexSet
findInds(ITensor const& T,
TagSet const& tsmatch)
{
return findInds(inds(T),tsmatch);
}
IndexSet
commonInds(ITensor const& A,
ITensor const& B)
{
return commonInds(inds(A),inds(B));
}
IndexSet
commonInds(ITensor const& A,
ITensor const& B,
TagSet const& tsmatch)
{
return findInds(commonInds(inds(A),inds(B)),tsmatch);
}
Index
commonIndex(ITensor const& A,
ITensor const& B)
{
return findIndex(commonInds(inds(A),inds(B)));
}
Index
commonIndex(ITensor const& A,
ITensor const& B,
TagSet const& tsmatch)
{
return findIndex(commonInds(inds(A),inds(B)),tsmatch);
}
IndexSet
uniqueInds(ITensor const& A,
ITensor const& B)
{
return uniqueInds(inds(A),inds(B));
}
IndexSet
uniqueInds(ITensor const& A,
std::vector<ITensor> const& B)
{
return uniqueInds(inds(A),inds(B));
}
IndexSet
uniqueInds(ITensor const& A,
std::initializer_list<ITensor> B)
{
return uniqueInds(A,std::vector<ITensor>(B));
}
Index
uniqueIndex(ITensor const& A,
ITensor const& B)
{
return findIndex(uniqueInds(A,B));
}
Index
uniqueIndex(ITensor const& A,
ITensor const& B,
TagSet const& tsmatch)
{
return findIndex(uniqueInds(A,B),tsmatch);
}
Index
uniqueIndex(ITensor const& A,
std::vector<ITensor> const& B)
{
return findIndex(uniqueInds(A,B));
}
Index
uniqueIndex(ITensor const& A,
std::vector<ITensor> const& B,
TagSet const& tsmatch)
{
return findIndex(uniqueInds(A,B),tsmatch);
}
Index
uniqueIndex(ITensor const& A,
std::initializer_list<ITensor> B)
{
return uniqueIndex(A,std::vector<ITensor>(B));
}
Index
uniqueIndex(ITensor const& A,
std::initializer_list<ITensor> B,
TagSet const& tsmatch)
{
return uniqueIndex(A,std::vector<ITensor>(B),tsmatch);
}
ITensor
setTags(ITensor A,
TagSet const& ts,
IndexSet const& is)
{
A.setTags(ts,is);
return A;
}
ITensor
noTags(ITensor A,
IndexSet const& is)
{
A.noTags(is);
return A;
}
ITensor
addTags(ITensor A,
TagSet const& ts,
IndexSet const& is)
{
A.addTags(ts,is);
return A;
}
ITensor
removeTags(ITensor A,
TagSet const& ts,
IndexSet const& is)
{
A.removeTags(ts,is);
return A;
}
ITensor
replaceTags(ITensor A,
TagSet const& ts1,
TagSet const& ts2,
IndexSet const& is)
{
A.replaceTags(ts1,ts2,is);
return A;
}
ITensor
swapTags(ITensor A,
TagSet const& ts1,
TagSet const& ts2,
IndexSet const& is)
{
A.swapTags(ts1,ts2,is);
return A;
}
ITensor
prime(ITensor A,
int plev,
IndexSet const& is)
{
A.prime(plev,is);
return A;
}
ITensor
prime(ITensor A,
IndexSet const& is)
{
A.prime(is);
return A;
}
ITensor
setPrime(ITensor A,
int plev,
IndexSet const& is)
{
A.setPrime(plev,is);
return A;
}
ITensor
noPrime(ITensor A,
IndexSet const& is)
{
A.noPrime(is);
return A;
}
ITensor& ITensor::
permute(IndexSet const& iset)
{
auto& A = *this;
auto Ais = A.inds();
auto r = Ais.order();
if(size_t(r) != size_t(iset.order()))
{
println("---------------------------------------------");
println("Tensor indices = \n",Ais,"\n");
println("---------------------------------------------");
println("Indices provided = \n",iset,"\n");
println("---------------------------------------------");
Error(format("Wrong number of Indexes passed to permute (expected %d, got %d)",r,iset.order()));
}
// Get permutation
auto P = Permutation(r);
calcPerm(Ais,iset,P);
if(isTrivial(P))
{
return A;
}
// If not trivial, use permutation to get new index set
// This is necessary to preserve the proper arrow direction of QN Index
auto bind = RangeBuilderT<IndexSet>(r);
for(auto i : range(r))
{
bind.setIndex(P.dest(i),Ais[i]);
}
auto Bis = bind.build();
auto O = Order{P,Ais,Bis};
if(A.store())
{
doTask(O, A.store());
}
A.is_.swap(Bis);
return A;
}
ITensor
permute(ITensor A,
IndexSet const& is)
{
A.permute(is);
return A;
}
ITensor& ITensor::
replaceInds(IndexSet const& is1,
IndexSet const& is2)
{
#ifdef DEBUG
if( itensor::order(is1) != itensor::order(is2) ) Error("In replaceInds, must replace with equal number of Indices");
#endif
auto& T = *this;
// Add a random prime to account for possible
// Index swaps
auto plev_temp = 43218432;
auto is2p = itensor::prime(is2,plev_temp);
auto isT = itensor::inds(T);
for(auto& J : isT)
{
for(auto i : range(itensor::order(is1)))
{
if( is1[i] && (J == is1[i]) )
{
if( dim(J) != dim(is2[i]) )
{
printfln("Old dim = %d",dim(J));
printfln("New dim would be = %d",dim(is2[i]));
throw ITError("Mismatch of index dimension in replaceInds");
}
// Make the arrow directions correct
J.dag();
auto& Jnew = is2p[i];
if( dir(J)==dir(Jnew) ) Jnew.dag();
T *= delta(J,Jnew);
break;
}
}
}
// Bring the prime levels back down to the original
// desired ones
T.prime(-plev_temp,is2p);
return T;
}
ITensor
replaceInds(ITensor T,
IndexSet const& is1,
IndexSet const& is2)
{
T.replaceInds(is1,is2);
return T;
}
ITensor& ITensor::
swapInds(IndexSet const& is1,
IndexSet const& is2)
{
#ifdef DEBUG
if( itensor::order(is1) != itensor::order(is2) ) Error("In swapInds, must swap equal numbers of Indices");
#endif
auto& T = *this;
T.replaceInds({is1,is2},{is2,is1});
return T;
}
ITensor
swapInds(ITensor T,
IndexSet const& is1,
IndexSet const& is2)
{
T.swapInds(is1,is2);
return T;
}
Real
norm(ITensor const& T)
{
#ifdef DEBUG
if(!T) Error("Default initialized tensor in norm(ITensor)");
#endif
#ifndef USESCALE
return doTask(NormNoScale{},T.store());
#else
auto fac = std::fabs(T.scale().real0());
return fac * doTask(NormNoScale{},T.store());
#endif
}
void ITensor::
write(std::ostream& s) const
{
itensor::write(s,inds());
itensor::write(s,scale());
auto type = StorageType::Null;
if(store())
{
type = doTask(StorageType{},store());
}
itensor::write(s,type);
if(store())
{
doTask(Write{s},store());
}
}
void ITensor::
read(std::istream& s)
{
itensor::read(s,is_);
LogNum scale;
itensor::read(s,scale);
IF_USESCALE(scale_ = scale;)
auto type = StorageType::Null;
itensor::read(s,type);
if(type==StorageType::Null) { /*intentionally left blank*/ }
else if(type==StorageType::DenseReal) { store_ = readType<DenseReal>(s); }
else if(type==StorageType::DenseCplx) { store_ = readType<DenseCplx>(s); }
else if(type==StorageType::Combiner) { store_ = readType<Combiner>(s); }
else if(type==StorageType::DiagReal) { store_ = readType<Diag<Real>>(s); }
else if(type==StorageType::DiagCplx) { store_ = readType<Diag<Cplx>>(s); }
else if(type==StorageType::QDenseReal) { store_ = readType<QDense<Real>>(s); }
else if(type==StorageType::QDenseCplx) { store_ = readType<QDense<Cplx>>(s); }
else if(type==StorageType::QDiagReal) { store_ = readType<QDiag<Real>>(s); }
else if(type==StorageType::QDiagCplx) { store_ = readType<QDiag<Cplx>>(s); }
else if(type==StorageType::QCombiner) { store_ = readType<QCombiner>(s); }
else if(type==StorageType::ScalarReal) { store_ = readType<ScalarReal>(s); }
else if(type==StorageType::ScalarCplx) { store_ = readType<ScalarCplx>(s); }
else
{
Error("Unrecognized type when reading tensor from istream");
}
}
namespace detail {
void
allocReal(ITensor& T)
{
if(hasQNs(T)) Error("Can't allocate quantum ITensor with undefined divergence");
T.store() = newITData<DenseReal>(dim(inds(T)),0);
}
void
allocReal(ITensor& T, IntArray const& ints)
{
if(not hasQNs(T))
{
T.store() = newITData<DenseReal>(dim(inds(T)),0);
}
else
{
QN div;
for(auto i : range(inds(T)))
{
auto iv = (inds(T)[i])(1+ints[i]);
div += qn(iv)*dir(iv);
}
T.store() = newITData<QDenseReal>(inds(T),div);
}
}
void
allocCplx(ITensor& T)
{
if(hasQNs(T)) Error("Can't allocate quantum ITensor with undefined divergence");
T.store() = newITData<DenseCplx>(dim(inds(T)),0);
}
void
checkArrows(IndexSet const& is1,
IndexSet const& is2,
bool shouldMatch = false)
{
if(hasQNs(is1) && hasQNs(is2))
{
for(auto I1 : is1)
for(auto I2 : is2)
{
if(I1 == I2)
{
auto cond = shouldMatch ^ (I1.dir() == I2.dir());
if(cond)
{
println("----------------------------------------");
println("IndexSet 1 = \n",is1);
println("----------------------------------------");
println("IndexSet 2 = \n",is2);
println("----------------------------------------");
printfln("Mismatched QN Index from set 1 %s",I1);
printfln("Mismatched QN Index from set 2 %s",I2);
Error("Mismatched QN Index arrows");
}
}
}
}
}
void
checkSameDiv(ITensor const& T1,
ITensor const& T2)
{
if(hasQNs(T1) && hasQNs(T2))
{
if(div(T1) != div(T2))
{
Error(format("div(T1)=%s must equal div(T2)=%s when adding T1+T2",div(T1),div(T2)));
}
}
}
} //namespace detail
//TODO: implement proper Dense*QDense to avoid conversion cost
ITensor& ITensor::
operator*=(ITensor const& R)
{
auto& L = *this;
if(!L || !R) Error("Default constructed ITensor in product");
if(L.order() == 0)
{
auto z = L.eltC();
*this = R*z;
return *this;
}
else if(R.order()==0)
{
auto z = R.eltC();
*this *= z;
return *this;
}
if(Global::checkArrows()) detail::checkArrows(L.inds(),R.inds());
//TODO: create a proper doTask(Contract,Dense,QDense)
auto hqL = hasQNs(L);
auto hqR = hasQNs(R);
auto Rdense = R;
if(hqL && !hqR) L = removeQNs(L);
else if(!hqL && hqR) Rdense = removeQNs(Rdense);
auto C = doTask(Contract{L.inds(),Rdense.inds()},
L.store(),
Rdense.store());
#ifdef USESCALE
L.scale_ *= Rdense.scale();
if(!std::isnan(C.scalefac)) L.scale_ *= C.scalefac;
#endif
#ifdef DEBUG
//Check for duplicate indices
checkIndexSet(C.Nis);
#endif
L.is_.swap(C.Nis);
return L;
}
#ifndef USESCALE
//for Diag and QDiag
//Diag -> Dense
//QDiag -> QDense
ITensor
toDense(ITensor T)
{
if(T.store()) doTask(ToDense{T.inds()},T.store());
return ITensor{move(T.inds()),move(T.store()),T.scale()};
}
bool
isDense(ITensor const& T)
{
return doTask(IsDense{},T.store());
}
//TODO: make this use a RemoveQNs task type that does:
//QDense -> Dense
//QDiag -> Diag
ITensor
removeQNs(ITensor T)
{
if(not hasQNs(T)) return T;
if(T.store()) doTask(RemoveQNs{inds(T)},T.store());
auto nis = inds(T);
nis.removeQNs();
return ITensor{move(nis),move(T.store()),T.scale()};
}
ITensor& ITensor::
operator*=(Real r)
{
doTask(Mult<Real>{r},store_);
return *this;
}
ITensor& ITensor::
operator/=(Real r)
{
auto fac = 1./r;
doTask(Mult<Real>{fac},store_);
return *this;
}
#endif
ITensor& ITensor::
operator*=(Cplx z)
{
if(z.imag() == 0) return operator*=(z.real());
doTask(Mult<Cplx>{z},store_);
return *this;
}
//Non-contracting product
ITensor& ITensor::
operator/=(ITensor const& R)
{