forked from ValeevGroup/SeQuant1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeQuant.m
2634 lines (2290 loc) · 85.8 KB
/
SeQuant.m
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
(* ::Package:: *)
BeginPackage["SeQuant`"];
(* turn on Assert *)
On[Assert];
(* Setting up internal things *)
Off[General::spell];
Off[General::spell1];
$RecursionLimit = 8192;
$IterationLimit = Infinity;
(*SetOptions[$FrontEnd,CommonDefaultFormatTypes->{"Output"->TraditionalForm}];*)
NCM = NonCommutativeMultiply;
(* ::Section::Closed:: *)
(* Global Parameters *)
(*
Special Operators
*)
Unprotect[defaultHamiltonianOpers];
Unprotect[ignoreDisconnectedOpers];
Unprotect[ignoreConnectedOpers];
defaultHamiltonianOpers = {"F","g","\!\(\*OverscriptBox[\(g\), \(_\)]\)"};
ignoreDisconnectedOpers = {};
ignoreConnectedOpers = {};
Protect[ignoreConnectedOpers];
Protect[ignoreDisconnectedOpers];
Protect[defaultHamiltonianOpers];
(*
Global SeQuant Parameters
*)
If[ !ValueQ[SeQuantDebugLevel],
SeQuantDebugLevel = 0
];
SeQuantVacuumChoices["Physical"] = 0;
SeQuantVacuumChoices["SingleConfiguration"] = 1;
SeQuantVacuumChoices["MultiConfiguration"] = 2;
If[ !ValueQ[SeQuantVacuum],
SeQuantVacuum = SeQuantVacuumChoices["SingleConfiguration"]
];
(* Definition of spaces *)
occ = particleSpace[occupied];
virt = particleSpace[virtual];
othervirt = particleSpace[othervirtual];
any = Union[occ,virt];
allvirt = Union[ virt,othervirt];
allany = Union[occ,allvirt];
spacesEqualIgnoreParticleType[a_particleSpace,b_particleSpace] :=
(Select[a,Head[#]=!=particleType&]==Select[b,Head[#]=!=particleType&]);
spacesOverlapIgnoreParticleType[a_particleSpace,b_particleSpace] :=
(Select[Intersection[a,b],Head[#]=!=particleType&]=!=particleSpace[]);
spacesOverlapIgnoreSpinAndParticleType[a_particleSpace,b_particleSpace] :=
(Select[Intersection[a,b],Head[#]=!=particleType&&Head[#]=!=particleSpin&]=!=particleSpace[]);
spacesOverlap[a_particleSpace,b_particleSpace] :=
(spacesOverlapIgnoreSpinAndParticleType[a,b] && Select[a,Head[#]===particleType&]===Select[b,Head[#]===particleType&]&& Select[a,Head[#]===particleSpin&]===Select[b,Head[#]===particleSpin&]);
(* return +1 if above, -1 if below, 0 if neither or both *)
spaceWRTFermiLevel[a_particleSpace] :=
If[ spacesOverlapIgnoreSpinAndParticleType[a,allvirt],
If[ spacesOverlapIgnoreSpinAndParticleType[a,occ],
0,
+1
],
If[ spacesOverlapIgnoreSpinAndParticleType[a,occ],
-1,
0
]
];
(* Global index for most recently generated index
Generated indices are named iXX, where i is generated by DefaultSpaceSymbol *)
GlobalIndexCounter = 0;
DefaultSpaceSymbol[occ] = "i";
DefaultSpaceSymbol[virt] = "a";
DefaultSpaceSymbol[allvirt] = "\[Alpha]";
DefaultSpaceSymbol[othervirt] = "\[Alpha]'";
DefaultSpaceSymbol[any] = "p";
DefaultSpaceSymbol[allany] = "\[Kappa]";
(* particle space with spin *)
occA = Append[occ,particleSpin[A]];
virtA=Append[virt,particleSpin[A]];
othervirtA=Append[othervirt,particleSpin[A]];
allvirtA=Append[allvirt,particleSpin[A]];
anyA = Append[any,particleSpin[A]];
allanyA = Append[allany,particleSpin[A]];
occB = Append[occ,particleSpin[B]];
virtB=Append[virt,particleSpin[B]];
othervirtB=Append[othervirt,particleSpin[B]];
allvirtB=Append[allvirt,particleSpin[B]];
anyB = Append[any,particleSpin[B]];
allanyB = Append[allany,particleSpin[B]];
DefaultSpaceSymbol[occA]="\!\(\*SubscriptBox[\(i\), \(\[Alpha]\)]\)";
DefaultSpaceSymbol[virtA]="\!\(\*SubscriptBox[\(a\), \(\[Alpha]\)]\)";
DefaultSpaceSymbol[allvirtA]="\!\(\*SubscriptBox[\(\[Alpha]\), \(\[Alpha]\)]\)";
DefaultSpaceSymbol[othervirtA]="\!\(\*SubsuperscriptBox[\(\[Alpha]\), \(\[Alpha]\), \(,\)]\)";
DefaultSpaceSymbol[anyA]="\!\(\*SubscriptBox[\(p\), \(\[Alpha]\)]\)";
DefaultSpaceSymbol[allanyA]="\!\(\*SubscriptBox[\(\[Kappa]\), \(\[Alpha]\)]\)";
DefaultSpaceSymbol[occB]="\!\(\*SubscriptBox[\(i\), \(\[Beta]\)]\)";
DefaultSpaceSymbol[virtB]="\!\(\*SubscriptBox[\(a\), \(\[Beta]\)]\)";
DefaultSpaceSymbol[allvirtB]="\!\(\*SubscriptBox[\(\[Alpha]\), \(\[Beta]\)]\)";
DefaultSpaceSymbol[othervirtB]="\!\(\*SubsuperscriptBox[\(\[Alpha]\), \(\[Beta]\), \(,\)]\)";
DefaultSpaceSymbol[anyB]="\!\(\*SubscriptBox[\(p\), \(\[Beta]\)]\)";
DefaultSpaceSymbol[allanyB]="\!\(\*SubscriptBox[\(\[Kappa]\), \(\[Beta]\)]\)";
(* ::Section::Closed:: *)
(* particleIndex Class *)
(* create particleIndex with space and symbol i *)
createParticleIndex[i_String,space_particleSpace] :=
Module[ {},
particleIndex[i,space]
];
(* create particleIndex with DefaultSpaceSymbol *)
createParticleIndex[space_particleSpace] :=
createParticleIndex[ToString[Subscript[DefaultSpaceSymbol[space],
GlobalIndexCounter++],TraditionalForm],space];
(* takes A and replaces name and space with i and s, respectively *)
createParticleIndex[A_particleIndex,i_String,s_particleSpace] :=
Module[ {},
If[ Length[A]==3,
particleIndex[i,s,A[[3]]],
particleIndex[i,s]
]
];
(* if a has particleIndex b ignoring indexType *)
indexQ[a_,b_particleIndex] :=
MemberQ[a,c_particleIndex/;(c[[1]]==b[[1]])&&(c[[2]]==b[[2]]),Infinity];
(* if particleIndex a has indexType[cre]*)
indexCreQ[a_particleIndex] :=
MemberQ[a,indexType[cre]];
(* if particleIndex a has indexType[ann]*)
indexAnnQ[a_particleIndex] :=
MemberQ[a,indexType[ann]];
(* if particleIndex a has indexType[bra]*)
indexBraQ[a_particleIndex] :=
MemberQ[a,indexType[bra]];
(* if particleIndex a has indexType[ket]*)
indexKetQ[a_particleIndex] :=
MemberQ[a,indexType[ket]];
(* return indexType of particleIndex with indexType *)
indexType[a_particleIndex] :=
Cases[a,_indexType][[1,1]]
(* return particleSpace of particleIndex *)
indexSpace[a_particleIndex] :=
Cases[a,_particleSpace][[1]];
(* return the particleType of a particleIndex *)
indexParticle[a_particleIndex] :=
Module[ {typeList},
typeList = Cases[a[[2]],_particleType];
If[ typeList=={},
Return[particleType[default]],
Return[typeList[[1]]]
]
];
(* return the symbol of particleIndex *)
indexSymbol[a_particleIndex] :=
a[[1]];
(* remove indexType *)
indexLight[a_particleIndex] :=
createParticleIndex[indexSymbol[a],indexSpace[a]];
(* if particleIndex equiv ignoring indexType *)
indexEquiv[a_particleIndex,b_particleIndex] :=
(a[[1]]===b[[1]])&&(a[[2]]===b[[2]]);
Unprotect[Equal];
Equal[a_particleIndex,b_particleIndex] :=
(a[[1]]===b[[1]])&&(a[[2]]===b[[2]]);
Protect[Equal];
(* canonical ordering of particleIndex objects is defined as follows: order by particleSpace first, then alphabetically by the symbol *)
Unprotect[OrderedQ];
OrderedQ[A_particleSpace,B_particleSpace] :=
If[ OrderedQ[A[[2]],B[[2]]],
OrderedQ[A[[1]],B[[1]]],
True
];
Protect[OrderedQ];
(* this functions throws out all multiple occurences of the same index from inds *)
uniqueIndexList[inds_List] :=
Module[ {tmpinds},
(* Old code *)
(*tmpinds={inds[[1]]};
Do[
ind=inds[[i]];
If[!indexQ[tmpinds,ind],
tmpinds=Append[tmpinds,ind]
],
{i,Length[inds]}
];*)
(* Union does it now *)
tmpinds = Union[inds];
Return[tmpinds];
];
(* "tag " and "untag " all indices in expr which also appear in intInds. uses indexQ *)
tagIndex[index_particleIndex] :=
Append[index,mysecrettag[]];
tagIndex[expr_] :=
expr;
untagIndex[index_particleIndex] :=
DeleteCases[index,_mysecrettag];
untagIndex[expr_] :=
expr;
tagIndices[expr_,inds_List] :=
Module[ {result,ninds,pos,ind},
ninds = Length[inds];
result = expr;
Do[
ind = inds[[i]];
pos = Position[result,c_particleIndex/;indexEquiv[c,ind]];
result = MapAt[tagIndex,result,pos],{i,1,ninds}];
Return[result];
];
untagIndices[expr_,inds_List] :=
Module[ {result,ninds,pos,ind},
ninds = Length[inds];
result = expr;
Do[
ind = inds[[i]];
pos = Position[result,c_particleIndex/;indexEquiv[c,ind]];
result = MapAt[untagIndex,result,pos],{i,1,ninds}];
Return[result];
];
(* ::Section::Closed:: *)
(* SQS Class *)
(*
SQS is a string of creation/annihilation operators
only normal ordered strings can be utilized at the moment
*)
inorder = normalOrder[True];
noorder = normalOrder[False];
createSQS[creInds_List,annInds_List,norm_normalOrder:inorder] :=
Module[ {},
If[ SeQuantVacuum === SeQuantVacuumChoices["MultiConfiguration"],
Return[mSQS[norm, flattenSQS[SQS[creInds,annInds,norm]]]]
];
Return[flattenSQS[SQS[creInds,annInds,norm]]]
];
createSQS[creInds_List, annInds_List ] :=
Return[flattenSQS[SQS[creInds,annInds]]]
SQS::wrongdepth = "Argument has wrong depth";
flattenSQS[a_SQS] :=
Module[ {nc,na,result,creInds,annInds,x},
Off[Append::normal];
If[ Depth[a]<5,
Return[a]
];
Clear[result];
Clear[creInds];
Clear[annInds];
Clear[x];
na = Length[a[[2]]];
nc = Length[a[[1]]];
creInds = Cases[a[[1]],x_particleIndex->Append[x,indexType[cre]]];
annInds = Cases[a[[2]],x_particleIndex->Append[x,indexType[ann]]];
result = FlattenAt[SQS[FlattenAt[{creInds,Reverse[annInds]},{{1},{2}}]],{1}];
Return[result];
];
(* SQS in MultiConfiguration case has non normal ordered form *)
(*
createmultiSQS[creInds_List,annInds_List,norm_normalOrder:inorder] :=
Module[ {},
Return[multiSQS[norm, createSQS[creInds,annInds,norm]]]
];
*)
(* find all index types in a *)
indexTypesSQS[a_SQS] :=
Module[ {ninds,ind,types,type},
ninds = Length[a];
types = {};
Do[
type = indexParticle[a[[ind]]];
types = Append[types,type]
,{ind,1,ninds}
];
types = Union[Flatten[types]];
Return[types];
];
(* find the number of ptype operator in a *)
numIndicesOfType[a_SQS,ptype_particleType] :=
Module[ {ninds,result},
ninds = Length[a];
result = 0;
Do[
result+=If[ indexParticle[a[[i]]]===ptype,
1,
0
]
,{i,1,ninds}
];
Return[result];
];
(*Find the first index of ptype, else return -1*)
firstIndexOfType[a_SQS,ptype_particleType] :=
Module[ {ninds,ind,index},
ninds = Length[a];
ind = -1;
Do[
index = a[[i]];
If[ indexParticle[index]===ptype,
ind = i;
Break[]
]
,{i,1,ninds}
];
Return[ind];
];
(* selects creation/annihilation indices from SQS
while the indexType was droped
*)
creIndices[a_SQS] :=
Cases[a,x_particleIndex/;indexCreQ[x]->indexLight[x] ];
annIndices[a_SQS] :=
Reverse[Cases[a,x_particleIndex/;indexAnnQ[x]->indexLight[x] ]];
(* ::Section::Closed:: *)
(* SQM class *)
(*
SQM is a matrix element of an operator
operators can be classified as antisymm, symm, and nonsymmetric, which will help in reducing expressions to their simplest form
*)
antisymm = indexSymm[-1];
symm = indexSymm[1];
nonsymm = indexSymm[0];
createSQM[O_String,braInds_List,ketInds_List,symm_indexSymm:antisymm] :=
Module[ {},
Return[flattenSQM[SQM[O,braInds,ketInds,symm]]]
];
SQM::wrongdepth = "Argument has wrong depth";
flattenSQM[a_SQM] :=
Module[ {nb,nk,result,braInds,ketInds,x},
Off[Append::normal];
If[ Depth[a]<5,
Return[a]
];
Clear[result];
Clear[braInds];
Clear[ketInds];
Clear[x];
nb = Length[a[[2]]];
nk = Length[a[[3]]];
braInds = Cases[a[[2]],x_particleIndex->Append[x,indexType[bra] ] ];
ketInds = Cases[a[[3]],x_particleIndex->Append[x,indexType[ket] ] ];
result = FlattenAt[SQM[FlattenAt[{braInds,ketInds},{{1},{2}}]],{1}];
result = Prepend[result,OHead[a[[1]],a[[4]]]];
Return[result];
];
(* selects bra/ket indices from SQM
while the indexType was droped
*)
braIndices[a_SQM] :=
Cases[a,x_particleIndex/;indexBraQ[x]->indexLight[x] ];
ketIndices[a_SQM] :=
Cases[a,x_particleIndex/;indexKetQ[x]->indexLight[x] ];
(*
the following two functions are used to deal with \Eta SQM in MultiConfigution
*)
(* expand \eta to \lamda and \delta *)
expandEta[a_SQM] :=
Module[{bra, ket, result},
bra = braIndices[a];
ket = ketIndices[a];
result = Plus[deltaIndex[bra[[1]], ket[[1]]], -createSQM["\[Lambda]", bra, ket, antisymm] ];
Return[result];
];
(* expand every \eta in expr *)
expandExp[expr_] :=
expr /. x_SQM /; x[[1, 1]] == "\[Eta]" -> expandEta[x]
(* convert cumulant to density matricies *)
(* this function is not universal,
it only works for rank 1 and 2 cumulant
*)
convertLambda[a_SQM] :=
Module[{bra, ket, result},
bra = braIndices[a];
ket = ketIndices[a];
Assert[ Length[bra] === Length[ket] ];
If [ Length[bra] === 1,
result = createSQM["\[Gamma]",bra,ket,antisymm];
Return[result]
];
If [Length[bra] === 2,
If [Depth[a] === 4,
result = createSQM["\[Gamma]",bra,ket,antisymm] - createSQM["\[Gamma]",{bra[[1]]},{ket[[1]]},antisymm]*createSQM["\[Gamma]",{bra[[2]]},{ket[[2]]},antisymm]
+ createSQM["\[Gamma]",{bra[[1]]},{ket[[2]]},antisymm]*createSQM["\[Gamma]",{bra[[2]]},{ket[[1]]},antisymm];
Return[result]
];
(*in spin free case, the rank one density with different spin will be zero *)
If [Depth[a] === 5,
result = createSQM["\[Gamma]",bra,ket,antisymm] - spinConvertGamma[{bra[[1]]},{ket[[1]]}]*spinConvertGamma[{bra[[2]]},{ket[[2]]}]
+ spinConvertGamma[{bra[[1]]},{ket[[2]]}]*spinConvertGamma[{bra[[2]]},{ket[[1]]}];
Return[result]
];
];
If[ Length[bra] > 2 ,
Return[a]
];
];
spinConvertGamma[bra_List, ket_List] :=
Module[{tmpbra, tmpket, spinsame, result},
spinsame = True;
tmpbra = bra[[1]];
tmpket = ket[[1]];
If[ tmpbra[[2,-1]] =!= tmpket[[2,-1]],
spinsame = False;
];
If [ spinsame,
result = createSQM["\[Gamma]",bra,ket,antisymm],
result = 0;
];
Return[result];
];
convertExp[expr_] :=
expr /. x_SQM /; x[[1,1]] == "\[Lambda]" -> convertLambda[x]
(* ::Section:: *)
(* Visualize function *)
(* these functions display particleIndex in string *)
visualizeIndex[a_particleIndex] :=
a[[1]];
Format[particleIndex[a__],TraditionalForm] :=
visualizeIndex[particleIndex[a]];
(* these functions display SQ expressions in tensor notation *)
visualizeSQE[a_*b_] :=
visualizeSQE[a]*visualizeSQE[b];
visualizeSQE[a_**b_] :=
visualizeSQE[a]**visualizeSQE[b];
visualizeSQE[a_^n_Integer] :=
visualizeSQE[a]^n;
visualizeSQE[a_+b_] :=
visualizeSQE[a]+visualizeSQE[b];
visualizeSQE[a_/;(Head[a]=!=SQS&&Head[a]=!=deltaIndex&&Head[a]=!=SQM&&Head[a]=!=mSQS)] :=
a;
visualizeSQE[a_deltaIndex] :=
Subsuperscript["\[Delta]",a[[1,1]],a[[2,1]] ];
visualizeSQE[a_SQS] :=
Module[ {bodyLabel,i,supInds,subInds},
(* convention labels strings normal-ordered wrt to nonphysical vacuum as tilde{a} *)
bodyLabel = If[SeQuantVacuum==SeQuantVacuumChoices["Physical"],"a","\[ATilde]"];
supInds = "";
subInds = "";
Do[
If[ Cases[a[[i]],_indexType][[1,1]]===cre,
supInds = StringJoin[supInds,a[[i,1]] ],
subInds = StringJoin[a[[i,1]],subInds ]
],{i,1,Length[a] }
];
Return[Subsuperscript[bodyLabel,subInds,supInds]]
];
visualizeSQE[a_mSQS] :=
Module[ {sqs,bodyLabel,i,supInds,subInds},
If [ Length[a[[2]]] === 0,
Return[a[[2]]]
];
bodyLabel = If[ a[[1]] === normalOrder[True],
OverTilde["a"],
"a"
];
sqs = a[[2]];
supInds = "";
subInds = "";
Do[
If[ Cases[sqs[[i]],_indexType][[1,1]]===cre,
supInds = StringJoin[supInds,sqs[[i,1]] ],
subInds = StringJoin[sqs[[i,1]],subInds ]
],
{i,1,Length[sqs] }
];
Return[Subsuperscript[bodyLabel,subInds,supInds]]
];
visualizeSQE[a_SQM] :=
Module[ {bodyLabel,i,supInds,subInds},
bodyLabel = a[[1,1]];
supInds = "";
subInds = "";
Do[
If[ indexKetQ[a[[i]]],
supInds = StringJoin[supInds,a[[i,1]] ],
subInds = StringJoin[subInds,a[[i,1]] ]
],{i,2,Length[a] }
];
Return[Subsuperscript[bodyLabel,subInds,supInds]]
];
Format[deltaIndex[a__],TraditionalForm] :=
visualizeSQE[deltaIndex[a]];
Format[SQM[a__],TraditionalForm] :=
visualizeSQE[SQM[a]];
Format[SQS[a__],TraditionalForm] :=
visualizeSQE[SQS[a]];
Format[mSQS[a__],TraditionalForm] :=
visualizeSQE[mSQS[a]];
(* ::Section:: *)
(* Contraction functions *)
(*
Low-level contraction routines
contractIndex contracts 2 indices
contractSQS contracts a List of SQ strings of operators
deltaIndex is our representation of Kroneker delta
*)
contractIndex[L_particleIndex,R_particleIndex] :=
Module[ {typeL,typeR, fermiL, fermiR,intIndex,result},
(* check if particles are the same *)
typeL = Cases[L[[2]],_particleType];
typeR = Cases[R[[2]],_particleType];
If[ typeL=!=typeR,
Return[0]
];
(*if both creation or both annihilation - result is 0*)
If[ L[[3]]==R[[3]],
Return[0]
];
(* the case of a multiconfiguration vacuum cannot be handled by simple Wick-type rules *)
If[ SeQuantVacuum==SeQuantVacuumChoices["MultiConfiguration"],
Print["Wick theorem for MultiConfiguration vacuum is not expressed as simple contraction"];
Abort[]
];
(* discard zero contractions for the case of a determinant vacuum *)
If[ SeQuantVacuum==SeQuantVacuumChoices["SingleConfiguration"],
fermiL = spaceWRTFermiLevel[L[[2]]];
If[ L[[3]]==indexType[ann]&&fermiL==-1,
Return[0]
];
If[ L[[3]]==indexType[cre]&&fermiL==+1,
Return[0]
];
fermiR = spaceWRTFermiLevel[R[[2]]];
If[ R[[3]]==indexType[cre]&&fermiR==-1,
Return[0]
];
If[ R[[3]]==indexType[ann]&&fermiR==+1,
Return[0]
];
];
(* discard zero contractions for the case of a physical vacuum *)
If[ SeQuantVacuum==SeQuantVacuumChoices["Physical"],
If[ L[[3]]==indexType[cre],
Return[0]
];
];
If[ !spacesOverlap[L[[2]],R[[2]]],
Return[0]
];
If[ SeQuantDebugLevel>=5,
Print["In contractIndex: ",L//TraditionalForm," and ", R//TraditionalForm]
];
(* single-configuration contractions involving general (non-particle and non-hole) indices involve particle and hole density matrices *)
If[ SeQuantVacuum==SeQuantVacuumChoices["SingleConfiguration"]&&(fermiL==0&&fermiR==0),
(* must be unoccupied *)
If[ R[[3]]==indexType[cre],
intIndex = createParticleIndex[allvirt];
result = deltaIndex[Drop[L,{3}],intIndex ] * deltaIndex[intIndex,Drop[R,{3}] ];
];
(* must be occupied *)
If[ R[[3]]==indexType[ann],
intIndex = createParticleIndex[occ];
result = deltaIndex[Drop[R,{3}],intIndex ] * deltaIndex[intIndex,Drop[L,{3}] ];
];
Return[result];
];
If[ L[[1]]==R[[1]],
Return[1]
];
If[ R[[3]]===indexType[cre],
Return[deltaIndex[Drop[L,{3}],Drop[R,{3}] ] ],
Return[deltaIndex[Drop[R,{3}],Drop[L,{3}] ] ]
];
];
(* contractIndex function used in MultiConfiguration
return True if it is nonzero contration
return False if it is zero contraction
*)
contractIndex[a_List] :=
Module[{bra, ket, spinmatch, tmpresult, result},
If [ Depth[a] === 4,
result = !MemberQ[a, y_particleIndex /; spaceWRTFermiLevel[indexSpace[y]] === +1];
Return[result]
];
(* for spin free cases *)
(* only rank one cumulant will be zero if the spin is different *)
If [ Depth[a] === 5,
tmpresult = !MemberQ[a, y_particleIndex /; spaceWRTFermiLevel[indexSpace[y]] === +1];
If[ !tmpresult,
Return[tmpresult];
];
spinmatch = True;
If[ Length[a] === 2,
ket = a[[1]];
bra = a[[2]];
If [ bra[[2,-1]] =!= ket[[2,-1]],
spinmatch = False;
]
];
result = tmpresult && spinmatch;
Return[result];
];
];
(* if a has member deltaIndex b *)
deltaQ[a_,b_deltaIndex] :=
MemberQ[a,c_deltaIndex/;(indexQ[c,b[[1]] ]&&indexQ[c,b[[2]] ])];
(* ::Subsection:: *)
(* CR Class *)
(*
CR is the head for a general result of a contraction (unless it's a zero)
such a result consists of a prefactor and a noncommutative product of SQ strings:
CR[pfac,body]
*)
factorIntoCR[a_,0] :=
0;
factorIntoCR[a_,b_CR] :=
CR[a*b[[1]],b[[2]]];
factorIntoCR[a_,b:Plus[__]] :=
Module[ {result,nterms},
result = 0;
nterms = Length[b];
Do[result+=factorIntoCR[a,b[[i]]],{i,1,nterms}];
Return[result];
];
AddCR[a_CR,b_CR] :=
CR[1,a+b];
(* returns c if it does not equal to t *)
uniqueCR[t_CR,c_CR] :=
If[ t=!=c,
c,
0
];
(* returns members of c that do not equal to t *)
uniqueCR[t_CR,c_Plus] :=
Module[ {uc},
uc = 0;
Do[uc+=uniqueCR[t,c[[i]]],
{i,1,Length[c]}
];
Return[uc];
];
(* returns c if it does not appear in t *)
uniqueCR[t_Plus,c_CR] :=
If[ FreeQ[t,c],
c,
0
];
(* returns the terms in c that do not appear in t *)
uniqueCR[t_Plus,c_Plus] :=
Module[ {uc},
uc = 0;
Do[uc+=uniqueCR[t,c[[i]]],
{i,1,Length[c]}
];
Return[uc];
];
(* return the number of CR in Plus *)
numberCR[t_Plus] :=
Module[ {len, num},
num = 0;
len = Length[t];
Do[
If[ Head[t[[i]] ] === CR,
num += 1
];
,{i,1,len}
];
Return[num]
];
numberCR[t_CR] :=
1;
(* ::Subsection:: *)
(* Contract SQS *)
(*
If fullContract
find the first index
contract recursively, i.e. result = contraction[i,j]*contractSQS[str/ij]
else
loop over all pairs of indices i and j
result += normalOrderedForm[str] + contraction[i,j]*contractSQS[str/ij]
*)
contractSQS[str:NCM[__SQS],ptype_particleType,contractOptions_List] :=
Module[ {result,newcontractOptions,sqsL,sqsR,indexL,indexR,indL,indR,iR,indRoff,strLoff,contr1,contr2,newstr,sqsLlast,sqsLfirst,indLfirst,indLlast,newcontrib,f,nstr},
If[ Depth[str]==2,
Return[CR[1,1]]
];
If[ SeQuantDebugLevel>=5,
Print["called contract SQS for str=",str//TraditionalForm," ptype=",ptype//TraditionalForm]
];
(*Recursively drop empty strings*)
result = {};
Do[
sqsL = sL;
If[ Length[str[[sL]]]==0,
result = contractSQS[Drop[str,{sL}],ptype,contractOptions ];
Break[]
],
{sL,1,Length[str]}
];
If[ result=!={},
Return[result]
];
(*Cannot contract single string*)
If[ Length[str]==1,
If [ SeQuantVacuum==SeQuantVacuumChoices["MultiConfiguration"],
result = If[ !(finalfullContract/.contractOptions),
CR[1,str[[1]]],
0
],
result = If[ !(fullContract/.contractOptions),
CR[1,str[[1]]],
0
]
];
Return[result]
];
If[ SeQuantDebugLevel>=7,
Print["in contractSQS: dropped all empty strings str=",str//TraditionalForm]
];
nstr = Length[str];
If [ SeQuantVacuum==SeQuantVacuumChoices["MultiConfiguration"],
If [ nstr === 2,
(* If the last contraction, do fullContraction if finalfullContraction is True *)
If [ finalfullContract/. contractOptions,
newcontractOptions = Cases[contractOptions, a_ /; FreeQ[a, fullContract]];
newcontractOptions = Append[ newcontractOptions, fullContract->True ],
newcontractOptions = Cases[contractOptions, a_ /; FreeQ[a, fullContract]];
newcontractOptions = Append[ newcontractOptions, fullContract->False ]
];
result = contractSQS[str[[1]],str[[2]],newcontractOptions];
Return[result],
(* If not the last contraciton, do not do fullContraction *)
If [ fullContract/. contractOptions,
newcontractOptions = Cases[contractOptions, a_ /; FreeQ[a, fullContract]];
newcontractOptions = Append[ newcontractOptions, fullContract->False ],
newcontractOptions = contractOptions
];
result = contractSQS[str[[1]],str[[2]], newcontractOptions];
newstr = Take[ str, {3,nstr}];
f[a_CR] := (
If[ a[[2]] === 1,
CR[ a[[1]], newstr ],
CR[ a[[1]], a[[2]]**newstr ]
]
);
(* when there is only one CR[] result, map function no longer works *)
If [ numberCR[result] === 1,
result = f[result],
result = Map[f, result]
];
If[ SeQuantDebugLevel>=5,
Print["new contraction ", result//TraditionalForm];
];
result = contractSQS[result, ptype, newcontractOptions]
];
Return[result];
];
(*Find the first string that has an index of ptype*)
indRoff = 0;
sqsL = -1;
Do[
indL = firstIndexOfType[str[[sL]],ptype];
If[ indL!=-1,
sqsL = sL;
Break[]
],
{sL,1,Length[str]-1}
];
If[ SeQuantDebugLevel>=7,
Print["in contractSQS: first substring that contains index of ptype = ",sqsL]
];
(*exit if such string is not found*)
If[ sqsL==-1,
result = If[ (fullContract/.contractOptions),
0,
CR[1,str]
];
Return[result]
];
(* If not doing full contraction, then first add the strings reordered into normal order (taking into account the signs) *)
If[ SeQuantDebugLevel>=7,
Print["in contractSQS: ",(fullContract/.contractOptions)]
];
result = If[ !(fullContract/.contractOptions),
normalOrderedForm[str,ptype],
0
];
(* Only one string needs to be considered if full contraction is sought *)
sqsLfirst = sqsL;
sqsLlast = If[ !(fullContract/.contractOptions),
Length[str]-1,
sqsL
];
Do[
sqsL = sL;
indL = firstIndexOfType[str[[sqsL]],ptype];
If[ indL==-1,
Continue[]
];
(* only 1 index need to be considered if seeking a full contraction *)
indLfirst = indL;
indLlast = If[ !(fullContract/.contractOptions),
Length[str[[sqsL]]],
indL
];
(* strLoff keeps track of the distance between the first ptype index in strL and the end of the string. It's used to initialize indRoff.
indRoff is the distance between left and right indices *)
strLoff = numIndicesOfType[str[[sqsL]],ptype];
Do[
indL = iL;
indexL = str[[sqsL,indL]];
If[ indexParticle[indexL]=!=ptype,
Continue[]
];
strLoff--;
indRoff = strLoff+1;
If[ SeQuantDebugLevel>=7,
Print["Chose left index: indexL = ",indexL//TraditionalForm," indRoff = ",indRoff];
];
(*Contract with every other index of ptype*)
Do[
iR = 0;
Do[
indexR = str[[sqsR,indR]];
If[ SeQuantDebugLevel>=7,
Print["Chose right index: indexL = ",indexL//TraditionalForm," indexR = ",indexR//TraditionalForm," indRoff = ",indRoff];
];
If[ indexParticle[indexR]=!=ptype,
Continue[],
iR+=1
];
If[ SeQuantDebugLevel>=5,
Print["Calling contractIndex for ",indexL//TraditionalForm," and ", indexR//TraditionalForm]
];
contr1 = contractIndex[indexL,indexR];
If[ SeQuantDebugLevel>=5,
Print["result = ",contr1//TraditionalForm]
];
If[ contr1=!=0,
(newstr = Join[
Take[str,{1,sqsL-1}],NCM[Drop[str[[sqsL]],{indL}]],Take[str,{sqsL+1,sqsR-1}],NCM[Drop[str[[sqsR]],{indR}]],Take[str,{sqsR+1,Length[str]}]
];
If[ SeQuantDebugLevel>=5,
Print["Prefactor: ",(-1)^(iR+indRoff-2)*contr1//TraditionalForm];
Print["Dropped string: ",newstr//TraditionalForm];
Print["Contracting: ",sqsL," ",indL," ",sqsR," ",indR," iR=",iR," indRoff=",indRoff];
];
contr2 = contractSQS[newstr,ptype,contractOptions];
newcontrib = factorIntoCR[(-1)^(iR+indRoff-2)*contr1,contr2];
(* before adding to the result, need to canonizalize if seeking operator rather than full contraction
the same term can appear multiple times if fullContract is false, hence must detect such cases reliably
*)
If[ !(fullContract/.contractOptions),
newcontrib = Map[maporder,newcontrib,2];
If[ SeQuantDebugLevel>=5,
Print["contractSQS(",str//TraditionalForm,"): result (before accumulate) = ",result//TraditionalForm]
];
If[ SeQuantDebugLevel>=5,
Print["contractSQS(",str//TraditionalForm,"): newcontrib (before accumulate) = ",newcontrib//TraditionalForm]
];
result+=uniqueCR[result,newcontrib];
If[ SeQuantDebugLevel>=5,
Print["contractSQS(",str//TraditionalForm,"): result (after accumulate) = ",result//TraditionalForm]
];,
result+=newcontrib
];
If[ SeQuantDebugLevel>=5,
Print["contractSQS(",str//TraditionalForm,"): Contracted result: ",contr2//TraditionalForm];
Print["contractSQS(",str//TraditionalForm,"): Total result: ",result//TraditionalForm]
];
)
],
{indR,1,Length[str[[sqsR]]]}
];
indRoff+=numIndicesOfType[str[[sqsR]],ptype],
{sqsR,sL+1,Length[str]}
],