3
3
import numpy as np
4
4
import time
5
5
from forte import det
6
+ import pytest
6
7
7
8
8
9
def test_sparse_exp_1 ():
9
- import pytest
10
-
11
10
### Test the linear operator ###
12
11
op = forte .SparseOperator ()
13
12
ref = forte .SparseState ({det ("22" ): 1.0 })
@@ -26,6 +25,8 @@ def test_sparse_exp_1():
26
25
assert wfn [det ("0220" )] == pytest .approx (0.15 , abs = 1e-9 )
27
26
assert wfn [det ("2002" )] == pytest .approx (- 0.21 , abs = 1e-9 )
28
27
28
+
29
+ def test_sparse_exp_2 ():
29
30
### Test the exponential operator with excitation operator ###
30
31
op = forte .SparseOperator ()
31
32
ref = forte .SparseState ({det ("22" ): 1.0 })
@@ -53,6 +54,8 @@ def test_sparse_exp_1():
53
54
assert wfn [det ("+0-2" )] == pytest .approx (- 0.0077 , abs = 1e-9 )
54
55
assert wfn [det ("-0+2" )] == pytest .approx (- 0.0077 , abs = 1e-9 )
55
56
57
+
58
+ def test_sparse_exp_3 ():
56
59
### Test the exponential operator with antihermitian operator ###
57
60
op = forte .SparseOperator ()
58
61
ref = forte .SparseState ({det ("22" ): 1.0 })
@@ -75,6 +78,8 @@ def test_sparse_exp_1():
75
78
assert wfn2 [det ("+2-0" )] == pytest .approx (0.0 , abs = 1e-9 )
76
79
assert wfn2 [det ("-2+0" )] == pytest .approx (0.0 , abs = 1e-9 )
77
80
81
+
82
+ def test_sparse_exp_4 ():
78
83
### Test the factorized exponential operator with an antihermitian operator ###
79
84
op = forte .SparseOperatorList ()
80
85
op .add ("[2a+ 0a-]" , 0.1 )
@@ -112,7 +117,6 @@ def test_sparse_exp_1():
112
117
op .add ("[1b+ 0b-]" , 0.05 )
113
118
op .add ("[2a+ 2b+ 1b- 1a-]" , - 0.07 )
114
119
115
- dtest = det ("20" )
116
120
ref = forte .SparseState ({det ("20" ): 0.5 , det ("02" ): 0.8660254038 })
117
121
factexp = forte .SparseFactExp ()
118
122
wfn = factexp .apply_antiherm (op , ref )
@@ -123,56 +127,114 @@ def test_sparse_exp_1():
123
127
assert wfn [det ("020" )] == pytest .approx (0.676180171388 , abs = 1e-9 )
124
128
assert wfn [det ("-+0" )] == pytest .approx (0.016058887563 , abs = 1e-9 )
125
129
126
- ### Test the factorized exponential operator with an antihermitian operator with complex coefficients ###
130
+ ### Test idempotent operators with complex coefficients ###
127
131
op = forte .SparseOperatorList ()
128
- op .add ("[1a+ 0a-]" , 0.1 + 0.2j )
132
+ op .add ("[0a+ 0a-]" , np .pi * 0.25j )
133
+ exp = forte .SparseExp (maxk = 100 , screen_thresh = 1e-15 )
134
+ factexp = forte .SparseFactExp ()
135
+ ref = forte .SparseState ({forte .det ("20" ): 1.0 })
136
+ s1 = exp .apply_op (op , ref )
137
+ s2 = factexp .apply_op (op , ref )
138
+ assert s1 [det ("20" )] == pytest .approx (s2 [det ("20" )], abs = 1e-9 )
139
+ assert s2 [det ("20" )] == pytest .approx (np .sqrt (2 ) * (1.0 + 1.0j ) / 2 , abs = 1e-9 )
140
+ s1 = exp .apply_antiherm (op , ref )
141
+ s2 = factexp .apply_antiherm (op , ref )
142
+ assert s1 [det ("20" )] == pytest .approx (s2 [det ("20" )], abs = 1e-9 )
143
+ assert s2 [det ("20" )] == pytest .approx (1.0j , abs = 1e-9 )
144
+ op = forte .SparseOperatorList ()
145
+ op .add ("[1a+ 1a-]" , np .pi * 0.25j )
146
+ s1 = exp .apply_antiherm (op , ref )
147
+ s2 = factexp .apply_antiherm (op , ref )
148
+ assert s1 [det ("20" )] == pytest .approx (s2 [det ("20" )], abs = 1e-9 )
149
+ assert s2 [det ("20" )] == pytest .approx (1.0 , abs = 1e-9 )
129
150
130
- op_inv = forte .SparseOperatorList ()
131
- op_inv .add ("[0a+ 1a-]" , 0.1 - 0.2j )
132
151
133
- exp = forte . SparseFactExp ()
134
- ref = forte . SparseState ({ forte . det ( "20" ): 0.5 , forte . det ( "02" ): 0.8660254038 })
152
+ def test_sparse_exp_5 ():
153
+ ### Test the reverse argument of factorized exponential operator ###
135
154
136
- s1 = exp .apply_antiherm (op , ref )
137
- s2 = exp .apply_antiherm (op_inv , s1 )
138
- assert s2 [det ("20" )] == pytest .approx (0.5 , abs = 1e-9 )
139
- assert s2 [det ("02" )] == pytest .approx (0.8660254038 , abs = 1e-9 )
155
+ # this is the manually reversed op from test_sparse_exp_4
156
+ op = forte .SparseOperatorList ()
157
+ op .add ("[2a+ 2b+ 0b- 0a-]" , 0.15 )
158
+ op .add ("[2b+ 0b-]" , 0.2 )
159
+ op .add ("[2a+ 0a-]" , 0.1 )
160
+ ref = forte .SparseState ({det ("22" ): 1.0 })
140
161
141
- s1 = exp .apply_antiherm (op , ref , inverse = True )
142
- s2 = exp .apply_antiherm (op_inv , ref , inverse = False )
143
- assert s1 == s2
162
+ factexp = forte .SparseFactExp ()
163
+ wfn = factexp .apply_antiherm (op , ref , reverse = True )
164
+
165
+ assert wfn [det ("+2-0" )] == pytest .approx (- 0.197676811654 , abs = 1e-9 )
166
+ assert wfn [det ("-2+0" )] == pytest .approx (- 0.097843395007 , abs = 1e-9 )
167
+ assert wfn [det ("0220" )] == pytest .approx (+ 0.165338757995 , abs = 1e-9 )
168
+ assert wfn [det ("2200" )] == pytest .approx (+ 0.961256283877 , abs = 1e-9 )
169
+
170
+ wfn2 = factexp .apply_antiherm (op , wfn , inverse = True , reverse = True )
171
+
172
+ assert wfn2 [det ("2200" )] == pytest .approx (1.0 , abs = 1e-9 )
144
173
145
- ### Test the exponential operator with an antihermitian operator with complex coefficients ###
174
+ op = forte .SparseOperatorList ()
175
+ op .add ("[2a+ 2b+ 1b- 1a-]" , - 0.07 )
176
+ op .add ("[1b+ 0b-]" , 0.05 )
177
+ op .add ("[1a+ 1b+ 0b- 0a-]" , - 0.3 )
178
+ op .add ("[1a+ 0a-]" , 0.1 )
179
+
180
+ ref = forte .SparseState ({det ("20" ): 0.5 , det ("02" ): 0.8660254038 })
181
+ factexp = forte .SparseFactExp ()
182
+ wfn = factexp .apply_antiherm (op , ref , reverse = True )
183
+
184
+ assert wfn [det ("200" )] == pytest .approx (0.733340213919 , abs = 1e-9 )
185
+ assert wfn [det ("+-0" )] == pytest .approx (- 0.049868863373 , abs = 1e-9 )
186
+ assert wfn [det ("002" )] == pytest .approx (- 0.047410073759 , abs = 1e-9 )
187
+ assert wfn [det ("020" )] == pytest .approx (0.676180171388 , abs = 1e-9 )
188
+ assert wfn [det ("-+0" )] == pytest .approx (0.016058887563 , abs = 1e-9 )
189
+
190
+ op = forte .SparseOperatorList ()
191
+ ref = forte .SparseState ({det ("22" ): 1.0 })
192
+ op .add ("[2a+ 0a-]" , 0.1 )
193
+ op .add ("[2b+ 0b-]" , 0.1 )
194
+ op .add ("[2a+ 2b+ 0b- 0a-]" , 0.15 )
195
+ op .add ("[3a+ 3b+ 1b- 1a-]" , - 0.077 )
196
+
197
+ exp = forte .SparseFactExp ()
198
+ wfn = exp .apply_op (op , ref , reverse = True )
199
+ assert wfn [det ("2200" )] == pytest .approx (1.0 , abs = 1e-9 )
200
+ assert wfn [det ("0220" )] == pytest .approx (0.16 , abs = 1e-9 )
201
+ assert wfn [det ("+2-0" )] == pytest .approx (- 0.1 , abs = 1e-9 )
202
+ assert wfn [det ("-2+0" )] == pytest .approx (- 0.1 , abs = 1e-9 )
203
+ assert wfn [det ("2002" )] == pytest .approx (- 0.077 , abs = 1e-9 )
204
+ assert wfn [det ("+0-2" )] == pytest .approx (- 0.0077 , abs = 1e-9 )
205
+ assert wfn [det ("-0+2" )] == pytest .approx (- 0.0077 , abs = 1e-9 )
206
+
207
+
208
+ def test_sparse_exp_6 ():
209
+ ### Test the factorized exponential operator with an antihermitian operator with complex coefficients ###
146
210
op = forte .SparseOperatorList ()
147
211
op .add ("[1a+ 0a-]" , 0.1 + 0.2j )
148
- op_explicit = forte .SparseOperatorList ()
149
- op_explicit .add ("[1a+ 0a-]" , 0.1 + 0.2j )
150
- op_explicit .add ("[0a+ 1a-]" , - 0.1 + 0.2j )
151
212
152
213
op_inv = forte .SparseOperatorList ()
153
214
op_inv .add ("[0a+ 1a-]" , 0.1 - 0.2j )
154
- op_inv_explicit = forte .SparseOperatorList ()
155
- op_inv_explicit .add ("[0a+ 1a-]" , 0.1 - 0.2j )
156
- op_inv_explicit .add ("[1a+ 0a-]" , - 0.1 - 0.2j )
157
215
158
- exp = forte .SparseExp ()
216
+ exp = forte .SparseExp (maxk = 100 , screen_thresh = 1e-15 )
217
+ factexp = forte .SparseFactExp ()
159
218
ref = forte .SparseState ({forte .det ("20" ): 0.5 , forte .det ("02" ): 0.8660254038 })
160
219
161
220
s1 = exp .apply_antiherm (op , ref )
162
- s1_explicit = exp .apply_op (op_explicit , ref )
163
- assert s1 == s1_explicit
221
+ s2 = factexp .apply_antiherm (op , ref )
222
+ assert s1 [det ("20" )] == pytest .approx (s2 [det ("20" )], abs = 1e-9 )
223
+ assert s1 [det ("02" )] == pytest .approx (s2 [det ("02" )], abs = 1e-9 )
224
+ assert s1 [det ("+-" )] == pytest .approx (s2 [det ("+-" )], abs = 1e-9 )
225
+ assert s1 [det ("-+" )] == pytest .approx (s2 [det ("-+" )], abs = 1e-9 )
226
+
227
+ s1 = exp .apply_antiherm (op , ref )
164
228
s2 = exp .apply_antiherm (op_inv , s1 )
165
229
assert s2 [det ("20" )] == pytest .approx (0.5 , abs = 1e-9 )
166
230
assert s2 [det ("02" )] == pytest .approx (0.8660254038 , abs = 1e-9 )
167
- s2_explicit = exp .apply_op (op_inv_explicit , s1 )
168
- assert s2 == s2_explicit
169
231
170
- s1 = exp .apply_antiherm (op , ref )
171
- s2 = exp .apply_antiherm (op_inv , ref , scaling_factor = - 1.0 )
232
+ s1 = factexp .apply_antiherm (op , ref , inverse = True )
233
+ s2 = factexp .apply_antiherm (op_inv , ref , inverse = False )
172
234
assert s1 == s2
173
235
174
236
175
- def test_sparse_exp_2 ():
237
+ def test_sparse_exp_7 ():
176
238
# Compare the performance of the two methods to apply an operator to a state
177
239
# when the operator all commute with each other
178
240
norb = 10
@@ -239,3 +301,8 @@ def test_sparse_exp_2():
239
301
if __name__ == "__main__" :
240
302
test_sparse_exp_1 ()
241
303
test_sparse_exp_2 ()
304
+ test_sparse_exp_3 ()
305
+ test_sparse_exp_4 ()
306
+ test_sparse_exp_5 ()
307
+ test_sparse_exp_6 ()
308
+ test_sparse_exp_7 ()
0 commit comments