-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface_pdaggerq.py
1757 lines (1504 loc) · 52.8 KB
/
interface_pdaggerq.py
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
import pdaggerq
import numpy as np
import re
import sys
# Replace by the correct path
sys.path.append('#PATH_TO_SimpleWick')
import copy
import wick as w
from IPython.display import display, Latex
# Factorial of a number
def factorial(k):
if type(k) != type(1):
print('Error type arg k in factorial')
sys.exit()
f = 1
for i in range(2,k+1):
f = f * i
return f
def gen_left_str(ops):
if type(ops) != type(['aa','bb']):
print('Error type arg ops in gen_left_str')
sys.exit()
acc = 'e'+str(len(ops)//2)+'('
for op in ops:
acc = acc + op + ','
acc = acc[0:len(acc)-1] + ')'
acc = acc.replace('-','')
acc = acc.replace('+','')
return acc
# Class for a single T operator
class T_pq():
def __init__(self,t):
# Init
self.is_ok = True
self.t = t
self.na_i = 0
self.nb_i = 0
self.ng_i = 0
self.na_a = 0
self.nb_a = 0
self.ng_a = 0
self.sign = 1
# clean the T
def clean(self):
T = self.t
#print('Not cleaned T:',T)
T = T.replace('t','')
T = re.sub(r'[()]','',T)
T = re.sub(r'[0-9]+', '', T)
T = T.split(',')
self.t = T
#print('Cleaned T:',T)
# t2 act -> act = 0
# t1 act -> act = 0
def remove_act_exc(self):
acc = []
for op in self.t:
acc.append(op[1])
nb_n = acc.count('n')
nb_m = acc.count('m')
# Remove the t1 act -> act
if len(self.t) == 2:
if nb_m == 1 and nb_n == 1:
self.is_ok = True
# Remove the t2 act -> act
elif len(self.t) == 4:
if nb_m == 2 and nb_n == 2:
self.is_ok = False
# Count alpha, beta and general spin
def count_spin(self):
na_i = self.na_i
nb_i = self.nb_i
ng_i = self.ng_i
na_a = self.na_a
nb_a = self.nb_a
ng_a = self.ng_a
for op in self.t:
if (op[2] == 'a' and op[0] == 'i'):
na_i = na_i + 1
if (op[2] == 'b' and op[0] == 'i'):
nb_i = nb_i + 1
if (op[2] == 'g' and op[0] == 'i'):
ng_i = ng_i + 1
if (op[2] == 'a' and op[0] == 'a'):
na_a = na_a + 1
if (op[2] == 'b' and op[0] == 'a'):
nb_a = nb_a + 1
if (op[2] == 'g' and op[0] == 'a'):
ng_a = ng_a + 1
self.na_i = na_i
self.nb_i = nb_i
self.ng_i = ng_i
self.na_a = na_a
self.nb_a = nb_a
self.ng_a = ng_a
#print('Spin i:',self.na_i,self.nb_i,self.ng_i)
#print('Spin a:',self.na_a,self.nb_a,self.ng_a)
# Check is the term is zero by spin
# alpha -> + 1, beta -> -1
s_i = na_i - nb_i
s_a = na_a - nb_a
if (s_i != s_a) and (abs(s_i - s_a) != ng_i + ng_a):
self.is_ok = False
#print('is ok:', self.is_ok)
# Put the hole indexes before the particle ones
def to_std_order(self):
acc = []
for i in range(len(self.t)//2,len(self.t)):
acc.append(self.t[i])
for i in range(0,len(self.t)//2):
acc.append(self.t[i])
#print('acc',acc)
self.t = acc
# Remove the first index that define the orbital class
def remove_first_idx(self):
acc = []
for t in self.t:
acc.append(t[1:3])
self.t = acc
# Move indexes to end up with: beta g alpha
def move_b_to_left(self):
if len(self.t) == 2:
return
if len(self.t) > 4:
print('Error, only done for t1 and t2')
sys.exit()
sign = 1
idx_spin = 2
t = copy.deepcopy(self.t)
#print(t)
t_i = t[0:(len(t)//2)]
t_a = t[(len(t)//2):len(t)]
#print("ti",t_i)
#print("ta",t_a)
# b=0, g=1, a=2
# For hole part
acc_i = []
for elem in t_i:
acc_i.append(elem[idx_spin])
for i in range(len(acc_i)):
if acc_i[i] == 'b':
acc_i[i] = 0
elif acc_i[i] == 'g':
acc_i[i] = 1
else:
acc_i[i] = 2
#print(acc_i)
if acc_i[1] < acc_i[0]:
sign = -sign
t[0] = t_i[1]
t[1] = t_i[0]
# For particle part
acc_a = []
for elem in t_a:
acc_a.append(elem[idx_spin])
for i in range(len(acc_a)):
if acc_a[i] == 'b':
acc_a[i] = 0
elif acc_a[i] == 'g':
acc_a[i] = 1
else:
acc_a[i] = 2
#print(acc_a)
if acc_a[1] < acc_a[0]:
sign = -sign
t[2] = t_a[1]
t[3] = t_a[0]
#print(self.t,t)
#print(self.sign,sign)
# New t and update the sign
self.t = t
self.sign = sign
# Class for the whole term comming from pdaggerq (prefactor + Ts)
class Term_pq():
def __init__(self,a_term):
self.prefactor = float(a_term[0]) # prefactor
self.str_T = a_term[1:] # output from pdaggerq
self.nb_T = len(self.str_T) # Number of T
self.l_T = None # List of T
self.tex = None # Tex
#self.ref = None
#self.deltas = deltas
# Loop over the Ts
acc = []
for i in range(4):
if i >= self.nb_T:
break
t = T_pq(self.str_T[i])
# Cleaning
t.clean()
# Remove act -> act amplitudes
t.remove_act_exc()
# t(i,j,...a,b,...)
t.to_std_order()
#print("T n°",i,":",t.t)
# To check if the T is zero
t.count_spin()
# Put the beta indexes on the left and change the sign
t.move_b_to_left()
#print(self.prefactor,t.sign)
self.prefactor = self.prefactor * t.sign
#print(self.prefactor)
# The first index for the orbital class is not useful anymore
t.remove_first_idx()
# Nullify if a term is zero by spin
if not t.is_ok:
self.prefactor = 0.0
acc.append(t.t)
# List of Ts
self.l_T = acc
#if self.prefactor != 0.0:
# print('Term:',self.prefactor,self.l_T)
#def remove_disconnected(self):
# Convert the term to latex
def to_latex(self):
# Sign + prefactor
if str(self.prefactor)[0] == '-':
acc = str(self.prefactor)
else:
acc = '+' + str(self.prefactor)
# Ts
self.tex = acc + Ts_to_tex(self.l_T)
#for t in self.l_T:
# acc = acc + ' \\ t_{'
# #print('t',t)
# # Lower indexes
# for i in range(0,len(t)//2):
# acc = acc + t[i][0] + '$' + t[i][1]
# acc = acc + '}^{'
# # Upper indexes
# for i in range(len(t)//2,len(t)):
# acc = acc + t[i][0] + '$' + t[i][1]
# acc = acc + '}'
##print(acc)
## Spin
#acc = acc.replace('$a','_{\\alpha}')
#acc = acc.replace('$b','_{\\beta}')
#acc = acc.replace('$g','_{}')
#self.tex = acc
# To diplay the latex eq
def tex_show(self):
null = self.to_latex()
display(Latex(f'${self.tex}$'))
def prefactor_to_tex(prefactor):
if type(prefactor) != type(1.0):
print('Error type arg prefactor in prefactor_to_tex')
sys.exit()
# Sign + prefactor
if str(prefactor)[0] == '-':
tex = str(prefactor)
else:
tex = '+' + str(prefactor)
return tex
def Ts_to_tex(Ts):
if type(Ts) != type([['aa'],['bb']]):
print('Error type Ts in Ts_to_tex')
sys.exit()
tex = ''
for t in Ts:
tex = tex + ' \\ t_{'
#print('t',t)
# Lower indexes
for i in range(0,len(t)//2):
if t[i][1] == 'b':
tex = tex + '\\bar{' + t[i][0] +'}'
else:
tex = tex + t[i][0]
#tex = tex + t[i][0] + '$' + t[i][1]
tex = tex + '}^{'
# Upper indexes
for i in range(len(t)//2,len(t)):
if t[i][1] == 'b':
tex = tex + '\\bar{' + t[i][0] +'}'
else:
tex = tex + t[i][0]
#tex = tex + t[i][0] + '$' + t[i][1]
tex = tex + '}'
#print(tex)
# Spin
#tex = tex.replace('$a','_{\\alpha}')
#tex = tex.replace('$b','_{\\beta}')
#tex = tex.replace('$g','_{}')
return tex
def deltas_to_tex(deltas):
if type(deltas) != type([['aa','bb'],['aa','bb']]):
print('Error type arg deltas in deltas to tex')
sys.exit()
#tex = w.deltas_to_tex(deltas)
#tex = tex.replace('_{\\alpha}','')
#tex = tex.replace('_{\\beta}','')
tex = ''
for delta in deltas:
if delta[0][3] == 'b':
d1 = '\\bar{'+str(delta[0][1])+'}'
else:
d1 = str(delta[0][1])
if delta[1][3] == 'b':
d2 = '\\bar{'+str(delta[1][1])+'}'
else:
d2 = str(delta[1][1])
tex = tex + '\delta('+d1+','+d2+') \ '
return tex
class T():
def __init__(self,t,ref,list_act_idx):
self.t = t
self.kind = len(self.t)//2
self.list_act_idx = list_act_idx
self.ref = ref
self.is_disconnected = self.check_connection()
def check_connection(self):
n = 0
for idx in self.list_act_idx:
for label in self.t:
n = n + label.count(idx)
if n == 0:
res = True
else:
res = False
return res
def apply_permutation_t(self,list_perm):
for i in range(len(self.t)):
#for perm in list_perm:
for perm in list_perm:
label1 = perm[0]
label2 = perm[1]
#print(self.t[i],label1,label2,spin1,spin2)
if self.t[i][0] == label1:
self.t[i] = label2 + self.t[i][1]
elif self.t[i][0] == label2:
self.t[i] = label1 + self.t[i][1]
#print('a',self.t[i])
class Term():
def __init__(self,deltas,prefactor,Ts):
#print('d:',deltas)
self.deltas = deltas
self.prefactor = prefactor
self.Ts = Ts
self.nb_T = len(Ts)
self.is_disconnected = False
for t in self.Ts:
if t.is_disconnected:
self.is_disconnected = True
def apply_permutation_term(self,sign,list_perm):
# Prefactor
self.prefactor = self.prefactor * sign
# Ts
for t in self.Ts:
#print("t b",t.t)
t.apply_permutation_t(list_perm)
# Delta
for perm in list_perm:
label1 = perm[0]
label2 = perm[1]
#print(perm,spin)
#print('d b',self.deltas)
for i in range(len(self.deltas)):
for j in range(2):
if self.deltas[i][j][0] == label1:
self.deltas[i][j] = label2 + self.deltas[i][j][1]
elif self.deltas[i][j][0] == label2:
self.deltas[i][j] = label1 + self.deltas[i][j][1]
#print('d a',self.deltas)
def spin_flip(self,ref_to_flip,res_ref):
for i in range(len(self.Ts)):
if self.Ts[i].ref != ref_to_flip:
continue
else:
self.Ts[i].ref = res_ref
for j in range(len(self.Ts[i].t)):
if self.Ts[i].t[j][1] == 'a':
self.Ts[i].t[j] = self.Ts[i].t[j][0]+'b'
elif self.Ts[i].t[j][1] == 'b':
self.Ts[i].t[j] = self.Ts[i].t[j][0]+'a'
else:
print('Unknow spin for spin flip')
sys.exit()
def Ts_to_fortran(self,shift):
code = ''
for t in self.Ts:
if len(code) > 60:
code = code + ' & \n' + shift
tmp = str(t.t).replace('\'','')
tmp = tmp.replace('[','(')
tmp = tmp.replace(']',')')
tmp = 't' + str(t.kind) + '_' + t.ref + tmp
code = code + ' * ' + tmp
code = code + ' & \n'
return code
def deltas_to_tex(self):
tex = deltas_2_tex(self.deltas)
return tex
def prefactor_to_tex(self):
# Prefactor
if self.prefactor == 1.0:
tex = '+'
elif self.prefactor == -1.0:
tex = '-'
elif self.prefactor > 1.0:
tex == '+' + str(self.prefactor)
elif self.prefactor < -1.0:
tex == '-' + str(self.prefactor)
else:
print("What ? ", str(self.prefactor))
sys.exit()
return tex
def Ts_to_tex(self):
tex = ''
# Ts
for t in self.Ts:
tex = tex + '\\ ^{'+t.ref+'}t_{'
## Lower indexes
for i in range(0,len(t.t)//2):
if t.t[i][1] == 'b':
tex = tex + '\\bar{' + t.t[i][0] +'}'
else:
tex = tex + t.t[i][0]
## Upper indexes
tex = tex + '}^{'
for i in range(len(t.t)//2,len(t.t)):
if t.t[i][1] == 'b':
tex = tex + '\\bar{' + t.t[i][0] +'}'
else:
tex = tex + t.t[i][0]
tex = tex + '}'
return tex
def to_tex(self):
tex = self.prefactor_to_tex()
tex = tex + self.deltas_to_tex()
tex = tex + self.Ts_to_tex()
return tex
def to_tex_no_delta(self):
tex = self.prefactor_to_tex()
tex = tex + self.Ts_to_tex()
return tex
def eq_show(self):
txt = self.prefactor + ' ' +self.deltas + ' ' + self.Ts.t
def tex_show(self):
tex = self.to_tex()
null = display_tex(tex)
def delta_2_tex(delta):
tex = ''
if delta[0][1] == 'b':
d1 = '\\bar{'+delta[0][0]+'}'
else:
d1 = delta[0][0]
if delta[1][1] == 'b':
d2 = '\\bar{'+delta[1][0]+'}'
else:
d2 = delta[1][0]
tex = tex + '\\delta(' + d1 + ',' + d2 + ') \ '
return tex
def deltas_2_tex(deltas):
tex = ''
for delta in deltas:
tex = tex + delta_2_tex(delta)
return tex
class LTerms():
def __init__(self):
self.terms = []
self.unique_deltas = []
self.factorized_terms = []
self.factorized = False
def append_Term(self,term1):
self.terms.append(term1)
def append_LTerms(self,lterms1):
for term1 in lterms1.terms:
self.terms.append(term1)
def append_prod(self,sign,lterms1,lterms2):
prod = prod_LTerms(self,sign,lterms1,lterms2)
self.append_LTerms(prod)
def prod_LTerms(self,sign,lterms1,lterms2):
for term1 in lterms1.terms:
#print("1",str(term1.deltas),term1.Ts_to_fortran("")[:-4])
for term2 in lterms2.terms:
#print("2 ",str(term2.deltas),term2.Ts_to_fortran("")[:-4])
# Product of the kronecker deltas
is_ok = True
if len(term1.deltas) != 0:
deltas = copy.deepcopy(term1.deltas)
for d2 in term2.deltas:
for d in term1.deltas:
#print(d,d2)
# check is there is two time the same operators in the deltas
is_ok = not(is_conflict_deltas(d,d2))
if not(is_ok):
break
if not(is_ok):
break
else:
#print("append",d2)
deltas.append(d2)
else:
deltas = copy.deepcopy(term2.deltas)
if not(is_ok):
continue
#print("add",deltas)
# Product of prefactors
prefactor = sign * term1.prefactor * term2.prefactor
# Product of Ts
Ts = copy.deepcopy(term1.Ts)
for t2 in term2.Ts:
Ts.append(t2)
self.append_Term(Term(deltas,prefactor,Ts))
def factorize(self):
self.extract_unique_deltas()
self.factorized_terms = [[] for i in range(len(self.unique_deltas))]
for term in self.terms:
idx = search_idx(term.deltas,self.unique_deltas)
self.factorized_terms[idx].append(Term([],term.prefactor,term.Ts))
def show_tex_factorized(self):
k = 0
for deltas in self.unique_deltas:
tex = ""
#if len(deltas) == 0:
# tex = "&"
#else:
# tex = tex + "\\\ &+ "
tex = tex + deltas_2_tex(deltas)
tex = tex + '\\bigl['
l = 0
for term in self.factorized_terms[k]:
tex = tex + term.to_tex_no_delta()
#l += len(term.to_tex_no_delta())
#if l > 300:
# tex = tex + "\\\ & "
# l = 0
#if l == 0:
# tex = tex[:-5]
tex = tex + '\\bigr]'
null = display_tex(tex)
#print(tex)
k = k + 1
def extract_unique_deltas(self):
unique_deltas = []
for term in self.terms:
if not(is_in(term.deltas,unique_deltas)):
unique_deltas.append(term.deltas)
# Sort depending on the number of deltas
## Max number
max_len = 0
for deltas in unique_deltas:
if len(deltas) > max_len:
max_len = len(deltas)
## Split depending on the length
acc = [[] for i in range(max_len+1)]
for deltas in unique_deltas:
acc[len(deltas)].append(deltas)
## Reduction of the number of dimensions
tmp = []
for list_elem in acc:
for elem in list_elem:
tmp.append(elem)
self.unique_deltas = tmp
# Bad function for bad things...
def reverse_deltas_order(self):
for term in self.terms:
if len(term.deltas) >= 2:
stop = 0
while term.deltas[stop][1][0] == 'a' or term.deltas[stop][1][0] == 'b':
stop += 1
if stop == len(term.deltas)-1:
break
if stop >= 1:
term.deltas = self.move_elements(term.deltas,stop)
def move_elements(self, lst, index):
if index < 0 or index >= len(lst):
raise ValueError("Index out of range")
# Move elements before the index to the end
result = lst[index:] + lst[:index]
return result
def ordered_by_t1(self):
if len(self.factorized_terms) == 0:
return
acc = [[] for i in range(len(self.factorized_terms))]
for i in range(len(self.factorized_terms)):
for term in self.factorized_terms[i]:
# Not a T1
if len(term.Ts[0].t) != 2:
continue
# Inactive labels
label_h = term.Ts[0].t[0]
label_p = term.Ts[0].t[0]
h = (label_h == 'i' or label_h == 'j')
p = (label_p == 'a' or label_p == 'b')
if h and p:
acc[i].append(term)
# All the remaining terms
for term in self.factorized_terms[i]:
if not(is_in(term,acc[i])):
acc[i].append(term)
self.factorized_terms = acc
def remove_disconnected(self):
k = 0
for i in range(len(self.terms)):
#null = display_tex(self.terms[k].Ts_to_tex())
#print(self.terms[k].is_disconnected)
if self.terms[k].is_disconnected:
self.terms.pop(k)
else:
k = k + 1
def spin_flip(self,ref_to_flip,res_ref):
for term in self.terms:
term.spin_flip(ref_to_flip,res_ref)
def apply_permutation(self,sign,list_perm):
for term in self.terms:
term.apply_permutation_term(sign,list_perm)
#acc = []
#for term in self.terms:
# if term.prefactor != 0.0:
# acc.append(term)
#
#self.terms = acc
def gen_fortran_M1(self,si,sa,ref):
code = ' ' + '! ### Spin case: i_'+si+', a_'+sa +' ###\n\n'
for deltas,list_term in zip(self.unique_deltas,self.factorized_terms):
d = []
for delta in deltas:
op1 = delta[0]
op2 = delta[1]
d.append([op1,op2])
tmp = ''
if d != []:
tmp = str(d).replace('\'','')
tmp = tmp.replace('[','(')
tmp = tmp.replace(']',')')
code = ' !! Deltas:'+tmp+'\n'
#code += ' !$OMP DO\n'
shift = ' '
code, shift = add_do_fortran(d,sa,'a',code,shift,False)
code, shift = add_do_fortran(d,si,'i',code,shift,False)
code = code + shift + self.which_M1(si,sa,d,ref)
for term in list_term:
p = str(term.prefactor)
if p[0] != '-':
p = '+' + p
p = p + 'd0'
code = code + shift + p + term.Ts_to_fortran(shift)
code = code[:-4] + '\n'
for i in range(len(shift)-2,0,-2):
shft = ' '*i
code = code + shft + 'enddo\n'
#code += ' !$OMP ENDDO NOWAIT\n'
print(code)
def gen_fortran_M2_disconnected(self,si,sj,sa,sb,ref):
code = ' ' + '! ### Spin case: i_'+si+', j_'+sj+', a_'+sa+', b_'+sb+' ###\n\n'
for deltas,list_term in zip(self.unique_deltas,self.factorized_terms):
d = []
for delta in deltas:
op1 = delta[0]
op2 = delta[1]
d.append([op1,op2])
tmp = ''
if d != []:
tmp = str(d).replace('\'','')
tmp = tmp.replace('[','(')
tmp = tmp.replace(']',')')
code = ' !! Deltas:'+tmp+'\n'
#code += ' !$OMP DO\n'
shift = ' '
code, shift = add_do_fortran(d,sb,'b',code,shift,False)
code, shift = add_do_fortran(d,sa,'a',code,shift,False)
code, shift = add_do_fortran(d,sj,'j',code,shift,False)
code, shift = add_do_fortran(d,si,'i',code,shift,False)
code = code + shift + self.which_M2(si,sj,sa,sb,d,ref)
for term in list_term:
p = str(term.prefactor)
if p[0] != '-':
p = '+' + p
p = p + 'd0'
disc = term.Ts[0] # I know that the disconnected term is in first position
#print("disc:",disc.t)
#if len(disc.t) != 2 or disc.t[0][0] == 'n' or disc.t[0][0] == 'm':
# print("That's not normal...)"+str(disc.t))
# sys.exit()
#print('i'+si != disc.t[0] and 'j'+sj != disc.t[0],'i'+si ,'j'+sj, disc.t[0])
#if 'i'+si != disc.t[0] and 'j'+sj != disc.t[0]:
# s1 = disc.t[0][1]
# if s1 == "a":
# l1 = "ma"
# l2 = "na"
# else:
# l1 = "nb"
# l2 = "mb"
# if 'a'+sa != disc.t[1] and 'b'+sb != disc.t[1]:
# str_term = shift + "if ("+str(disc.t[0])+" /= "+l1+" .or "+str(disc.t[1])+" /= "+l2+") then \n"
# str_term = str_term + " " + shift + self.which_M2(si,sj,sa,sb,d,ref) + shift + " " + p + term.Ts_to_fortran(shift)
# str_term = str_term[:-4] + " \n"
# str_term = str_term + shift + "endif \n"
# else:
# # print("Whaaaat???")
# # sys.exit()
# str_term = shift + self.which_M2(si,sj,sa,sb,d,ref) + shift + p + term.Ts_to_fortran(shift)
# str_term = str_term[:-4] + " \n"
#else:
# str_term = shift + self.which_M2(si,sj,sa,sb,d,ref) + shift + p + term.Ts_to_fortran(shift)
# str_term = str_term[:-4] + " \n"
code = code + shift + p + term.Ts_to_fortran(shift)
code = code[:-4] + '\n'
for i in range(len(shift)-2,0,-2):
shft = ' '*i
code = code + shft + 'enddo\n'
#code += ' !$OMP ENDDO NOWAIT\n'
print(code)
def gen_fortran_M2(self,si,sj,sa,sb,ref):
code = ' ' + '! ### Spin case: i_'+si+', j_'+sj+', a_'+sa+', b_'+sb+' ###\n\n'
for deltas,list_term in zip(self.unique_deltas,self.factorized_terms):
d = []
for delta in deltas:
op1 = delta[0]
op2 = delta[1]
d.append([op1,op2])
tmp = ''
if d != []:
tmp = str(d).replace('\'','')
tmp = tmp.replace('[','(')
tmp = tmp.replace(']',')')
code = ' !! Deltas:'+tmp+'\n'
#code += ' !$OMP DO\n'
shift = ' '
code, shift = add_do_fortran(d,sb,'b',code,shift,False)
code, shift = add_do_fortran(d,sa,'a',code,shift,False)
code, shift = add_do_fortran(d,sj,'j',code,shift,False)
code, shift = add_do_fortran(d,si,'i',code,shift,False)
code = code + shift + self.which_M2(si,sj,sa,sb,d,ref)
for term in list_term:
p = str(term.prefactor)
if p[0] != '-':
p = '+' + p
p = p + 'd0'
code = code + shift + p + term.Ts_to_fortran(shift)
code = code[:-4] + '\n'
for i in range(len(shift)-2,0,-2):
shft = ' '*i
code = code + shft + 'enddo\n'
#code += ' !$OMP ENDDO NOWAIT\n'
print(code)
def which_M1(self,si,sa,d,ref):
i = 'i'+si
a = 'a'+sa
M1 = 'M1_'+ref+'('+i+','+a+') = M1_'+ref+'('+i+','+a+') & \n'
label = [i,a]
for l in label:
for elem in d:
if l == elem[0]:
M1 = M1.replace(l,elem[1])
elif l == elem[1]:
M1 = M1.replace(l,elem[0])
return M1
def which_M2(self,si,sj,sa,sb,d,ref):
i = 'i'+si
j = 'j'+sj
a = 'a'+sa
b = 'b'+sb
M2 = 'M2_'+ref+'('+i+','+j+','+a+','+b+') = M2_'+ref+'('+i+','+j+','+a+','+b+') & \n'
label = [i,j,a,b]
for l in label:
for elem in d:
if l == elem[0]:
M2 = M2.replace(l,elem[1])
elif l == elem[1]:
M2 = M2.replace(l,elem[0])
return M2
def add_do_fortran(d,s_label,label,code,shift,disconnected):
is_in = False
for elem in d:
#print(elem)
for op in elem:
#print(op,label+s_label)
if label+s_label == op:
is_in = True
if not is_in:
code = code + shift + 'do '+label+s_label+' = i_'+label+s_label+', f_'+label+s_label+'\n'
shift = shift + ' '
if label+s_label == 'ia' or label+s_label == 'ja':
l = 'ma'
elif label+s_label == 'aa' or label+s_label == 'ba':
l = 'na'
elif label+s_label == 'ib' or label+s_label == 'jb':
l = 'nb'
elif label+s_label == 'ab' or label+s_label == 'bb':
l = 'mb'
else:
print('ooops')
sys.exit()
if s_label == 'a':
if label == 'i' or label == 'j':
txt = label+'b = '+label+'a + cc_nOa'
elif label == 'a' or label == 'b':
txt = label+'b = '+label+'a + cc_nVa'
else:
print('no such s_label')
sys.exit()
if s_label == 'b':
if label == 'i' or label == 'j':
txt = label+'a = '+label+'b - cc_nOa'
elif label == 'a' or label == 'b':
txt = label+'a = '+label+'b - cc_nVa'
else:
print('no such s_label')
sys.exit()
if s_label != 'a' and s_label != 'b':
print('Well, we have a problem here')
sys.exit()
if not disconnected:
code = code + shift + 'if ('+label+s_label+' == '+l+') cycle \n'
code = code + shift + txt + '\n'
return code, shift
def is_conflict_deltas(delta1,delta2):
is_conflict = False
for op1 in delta1:
for op2 in delta2:
#print(op1,op2,op1==op2)
if op1 == op2:
is_conflict = True
#if op1[0] == op2[0] or op1[1] == op2[0]:
# count = count + 1
#if op1[0] == op2[1] or op1[1] == op2[1]:
# count = count + 1
#if count != 0:
# is_conflict = True
#print(delta1,delta2,is_conflict)
return is_conflict
def delta4_to_delta2(delta):
d1 = delta[0][1] + delta[0][3]
d2 = delta[1][1] + delta[1][3]
d = [d1,d2]
return d
def display_tex(tex):
display(Latex(f'${tex}$'))
def apply_ops_eT(ops,Ts):
if type(ops) != type(['aa','bb']):
print('Error type arg ops in apply_ops_eT')
sys.exit()
if type(Ts) != type([['t1','t1','t2']]):
print('Error type arg Ts in apply_ops_eT')
sys.exit()
# Init
pq = pdaggerq.pq_helper("fermi")
op_str = gen_left_str(ops)
#print('Left ops:',op_str)
# Set left operators
pq.set_left_operators([[op_str]])
#pq.set_left_operators([['e3(ira,isb,iig,aqa,apb,aag)']])
#print('If there are many T, set the prefactor to 1/k! ...\n')
# Set Ts operators
#Ts = ['t1','t2']
prefactor = 1.0/factorial(Ts.count('t1')) * 1.0/factorial(Ts.count('t2'))
pq.add_operator_product(prefactor,Ts)
#print(prefactor)
#pq.add_operator_product(1.0/factorial(len(Ts)),Ts)
#pq.add_operator_product(1.0,['t1','t1'])
pq.simplify()
# list of fully-contracted strings, then print
terms = pq.fully_contracted_strings()
#print(1,terms)
#for term in terms:
# print(term)
# #pq.clear()
# obj = Term_pq(term)
# #print('prefactor',obj.prefactor)
# #print('T:',obj.l_T)
# obj.to_latex()
return terms
def gen_all_T(max_rank,nb_min_op,nb_max_op):
if type(max_rank) != type(1):