-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_global_functions.py
429 lines (366 loc) · 20.1 KB
/
test_global_functions.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
from global_functions import *
from spin_system import *
import pytest
def test_clean_line_0() -> None :
'''
Tests that the clean_line() function returns an empty list if provided with an empty string.
'''
vector = clean_line('')
assert len(vector)==0
def test_clean_line_1() -> None :
'''
Tests that the clean_line() function returns an empty list if provided with a string consisting
of empty spaces and a new line character.
'''
vector = clean_line(' \n')
assert len(vector)==0
def test_clean_line_2() -> None :
'''
Tests that the clean_line() function returns the proper list of strings if provided with a string
whose last word finishes with the new line character (common case).
'''
vector = clean_line('2.3 is a real number\n')
exp_vector = ['2.3','is','a','real','number']
assert len(vector)==len(exp_vector)
for i in range(len(vector)) :
assert vector[i]==exp_vector[i]
def test_is_spin_acceptable_0() -> None :
'''
Tests that the is_spin_acceptable() function returns False if provided with a negative half-integer
floating number.
'''
assert is_spin_acceptable(-1.5)==False
def test_is_spin_acceptable_1() -> None :
'''
Tests that the is_spin_acceptable() function returns False if provided with a negative integer number.
'''
assert is_spin_acceptable(-2)==False
def test_is_spin_acceptable_2() -> None :
'''
Tests that the is_spin_acceptable() function returns False if provided with zero.
'''
assert is_spin_acceptable(0)==False
def test_is_spin_acceptable_3() -> None :
'''
Tests that the is_spin_acceptable() function returns False if provided with a positive floating number
which is neither integer nor half-integer.
'''
assert is_spin_acceptable(0.7)==False
def test_adapt_magintmatrix_0() -> None :
'''
Tests that the proper Exception is raised when the adapt_magintmatrix() function is provided with
a 2D array whose shape is not (3,3).
'''
with pytest.raises(ValueError, match='The adapt_magintmatrix function only accepts 3x3 square matrices as argument.') :
new_matrix = adapt_magintmatrix(np.array([[1.0]]))
def test_adapt_magintmatrix_1() -> None :
'''
Tests that the proper Exception is raised when the adapt_magintmatrix() function is provided with
a 1D array whose shape is not (3,3).
'''
with pytest.raises(ValueError, match='The adapt_magintmatrix function only accepts 3x3 square matrices as argument.') :
new_matrix = adapt_magintmatrix(np.array([1.0]))
def test_adapt_magintmatrix_2() -> None :
'''
Tests that the adapt_magintmatrix() function returns the correct transformed matrix when provided with
a 3x3 matrix in the most general form.
'''
# Matrix vs its expected transformation
matrix = np.array([[1.0,2.0,3.0],
[4.0,5.0,6.0],
[7.0,8.0,9.0]])
exp_matrix = np.array([[9.0,7.0,8.0],
[3.0,1.0,2.0],
[6.0,4.0,5.0]])
new_matrix = adapt_magintmatrix(matrix)
assert np.allclose(new_matrix, exp_matrix, atol=1e-10, rtol=1e-10)
def test_adapt_magintmatrix_3() -> None :
'''
Tests that the adapt_magintmatrix() function returns the correct transformed matrix when provided with
a 3x3 diagonal matrix.
'''
# Matrix vs its expected transformation
matrix = np.array([[1.0,0.0,0.0],
[0.0,5.0,0.0],
[0.0,0.0,9.0]])
exp_matrix = np.array([[9.0,0.0,0.0],
[0.0,1.0,0.0],
[0.0,0.0,5.0]])
new_matrix = adapt_magintmatrix(matrix)
assert np.allclose(new_matrix, exp_matrix, atol=1e-10, rtol=1e-10)
def test_map_spin_correlations_0() -> None :
'''
Tests that the function in question correctly provides the length scale for all the NN shells
of the system, starting from the "on-site bond" (e.g. links spin 0 to itself = null distance)
to the closest reproducible "replica bond" (e.g. links spin 0 to its closest replica).
This case involves a 2-sites system and null NN spin-spin correlation values.
'''
# Structural properties of the system
latt_vecs = np.array([[1.0,0.0,0.0],[0.0,10.0,0.0],[0.0,0.0,10.0]])
sites = np.array([[0.0,0.0,0.0],[0.5,0.0,0.0]])
system = SpinSystem(latt_vecs,sites,0.5)
# Spin-spin correlation values in the expected format
SSC_xs = np.eye(2)
SSC_ys = np.eye(2)
SSC_zs = np.eye(2)
SSC_tots = np.eye(2)
spin_correlations = (SSC_xs, SSC_ys, SSC_zs, SSC_tots)
# Final arrangement of the spin-spin correlation values vs expectation
final_SSCs, NN_distances = map_spin_correlations(system,spin_correlations,4,1)
NN_distances = np.array(NN_distances)
exp_NN_distances = np.array([0.0,0.5,1.0])
assert np.allclose(NN_distances, exp_NN_distances, atol=1e-10, rtol=1e-10)
def test_map_spin_correlations_1() -> None :
'''
Tests that the function in question correctly arranges the spin-spin correlation values according to
the associated NN shell of the system, starting from the "on-site correlation" (e.g. spin0-spin0
correlation) to the closest reproducible "replica correlation" (e.g. spin0-replica correlation).
This case involves a 2-sites system and null NN spin-spin correlation values.
'''
# Structural properties of the system
latt_vecs = np.array([[1.0,0.0,0.0],[0.0,10.0,0.0],[0.0,0.0,10.0]])
sites = np.array([[0.0,0.0,0.0],[0.5,0.0,0.0]])
system = SpinSystem(latt_vecs,sites,0.5)
# Spin-spin correlation values in the expected format
SSC_xs = np.eye(2)
SSC_ys = np.eye(2)
SSC_zs = np.eye(2)
SSC_tots = np.eye(2)
spin_correlations = (SSC_xs, SSC_ys, SSC_zs, SSC_tots)
# Final arrangement of the spin-spin correlation values vs expectation
final_SSCs, NN_distances = map_spin_correlations(system,spin_correlations,4,1)
exp_SSCs = np.array([[1.0,0.0,1.0],
[1.0,0.0,1.0],
[1.0,0.0,1.0],
[1.0,0.0,1.0]])
assert np.allclose(final_SSCs, exp_SSCs, atol=1e-10, rtol=1e-10)
def test_map_spin_correlations_2() -> None :
'''
Tests that the function in question correctly provides the length scale for all the NN shells
of the system, starting from the "on-site bond" (e.g. links spin 0 to itself = null distance)
to the closest reproducible "replica bond" (e.g. links spin 0 to its closest replica).
This case involves a 4-sites system and the only non vanishing spin-spin correlation values
are those of the 1°NN shell.
'''
# Structural properties of the system
latt_vecs = np.array([[1.0,0.0,0.0],[0.0,10.0,0.0],[0.0,0.0,10.0]])
sites = np.array([[0.0,0.0,0.0],[0.25,0.0,0.0],[0.5,0.0,0.0],[0.75,0.0,0.0]])
system = SpinSystem(latt_vecs,sites,0.5)
# Spin-spin correlation values in the expected format
SSC_xs = np.eye(4)+0.5*(np.eye(4, k=1)+np.eye(4, k=-1))+0.5*(np.eye(4, k=3)+np.eye(4, k=-3))
SSC_ys = np.eye(4)+0.5*(np.eye(4, k=1)+np.eye(4, k=-1))+0.5*(np.eye(4, k=3)+np.eye(4, k=-3))
SSC_zs = np.eye(4)+0.5*(np.eye(4, k=1)+np.eye(4, k=-1))+0.5*(np.eye(4, k=3)+np.eye(4, k=-3))
SSC_tots = np.eye(4)+0.5*(np.eye(4, k=1)+np.eye(4, k=-1))+0.5*(np.eye(4, k=3)+np.eye(4, k=-3))
spin_correlations = (SSC_xs, SSC_ys, SSC_zs, SSC_tots)
# Final arrangement of the spin-spin correlation values vs expectation
final_SSCs, NN_distances = map_spin_correlations(system,spin_correlations,4,1)
NN_distances = np.array(NN_distances)
exp_NN_distances = np.array([0.0,0.25,0.5,0.75,1.0])
assert np.allclose(NN_distances, exp_NN_distances, atol=1e-10, rtol=1e-10)
def test_map_spin_correlations_3() -> None :
'''
Tests that the function in question correctly arranges the spin-spin correlation values according to
the associated NN shell of the system, starting from the "on-site correlation" (e.g. spin0-spin0
correlation) to the closest reproducible "replica correlation" (e.g. spin0-replica correlation).
This case involves a 4-sites system and the only non vanishing spin-spin correlation values
are those of the 1°NN shell.
'''
# Structural properties of the system
latt_vecs = np.array([[1.0,0.0,0.0],[0.0,10.0,0.0],[0.0,0.0,10.0]])
sites = np.array([[0.0,0.0,0.0],[0.25,0.0,0.0],[0.5,0.0,0.0],[0.75,0.0,0.0]])
system = SpinSystem(latt_vecs,sites,0.5)
# Spin-spin correlation values in the expected format
SSC_xs = np.eye(4)+0.5*(np.eye(4, k=1)+np.eye(4, k=-1))+0.5*(np.eye(4, k=3)+np.eye(4, k=-3))
SSC_ys = np.eye(4)+0.5*(np.eye(4, k=1)+np.eye(4, k=-1))+0.5*(np.eye(4, k=3)+np.eye(4, k=-3))
SSC_zs = np.eye(4)+0.5*(np.eye(4, k=1)+np.eye(4, k=-1))+0.5*(np.eye(4, k=3)+np.eye(4, k=-3))
SSC_tots = np.eye(4)+0.5*(np.eye(4, k=1)+np.eye(4, k=-1))+0.5*(np.eye(4, k=3)+np.eye(4, k=-3))
spin_correlations = (SSC_xs, SSC_ys, SSC_zs, SSC_tots)
# Final arrangement of the spin-spin correlation values vs expectation
final_SSCs, NN_distances = map_spin_correlations(system,spin_correlations,4,1)
exp_SSCs = np.array([[1.0,0.5,0.0,0.5,1.0],
[1.0,0.5,0.0,0.5,1.0],
[1.0,0.5,0.0,0.5,1.0],
[1.0,0.5,0.0,0.5,1.0]])
assert np.allclose(final_SSCs, exp_SSCs, atol=1e-10, rtol=1e-10)
def test_map_spin_correlations_4() -> None :
'''
Tests that the function in question correctly provides the length scale for all the NN shells
of the system, starting from the "on-site bond" (e.g. links spin 0 to itself = null distance)
to the closest reproducible "replica bond" (e.g. links spin 0 to its closest replica).
This case involves a 4-sites system and fully non vanishing spin-spin correlation values.
'''
# Structural properties of the system
latt_vecs = np.array([[1.0,0.0,0.0],[0.0,10.0,0.0],[0.0,0.0,10.0]])
sites = np.array([[0.0,0.0,0.0],[0.25,0.0,0.0],[0.5,0.0,0.0],[0.75,0.0,0.0]])
system = SpinSystem(latt_vecs,sites,0.5)
# Spin-spin correlation values in the expected format
SSC_xs = np.eye(4)+0.5*(np.eye(4, k=1)+np.eye(4, k=-1))+0.5*(np.eye(4, k=3)+np.eye(4, k=-3))-0.25*(np.eye(4, k=2)+np.eye(4, k=-2))
SSC_ys = np.eye(4)+0.5*(np.eye(4, k=1)+np.eye(4, k=-1))+0.5*(np.eye(4, k=3)+np.eye(4, k=-3))-0.25*(np.eye(4, k=2)+np.eye(4, k=-2))
SSC_zs = np.eye(4)+0.5*(np.eye(4, k=1)+np.eye(4, k=-1))+0.5*(np.eye(4, k=3)+np.eye(4, k=-3))-0.25*(np.eye(4, k=2)+np.eye(4, k=-2))
SSC_tots = np.eye(4)+0.5*(np.eye(4, k=1)+np.eye(4, k=-1))+0.5*(np.eye(4, k=3)+np.eye(4, k=-3))-0.25*(np.eye(4, k=2)+np.eye(4, k=-2))
spin_correlations = (SSC_xs, SSC_ys, SSC_zs, SSC_tots)
# Final arrangement of the spin-spin correlation values vs expectation
final_SSCs, NN_distances = map_spin_correlations(system,spin_correlations,4,1)
NN_distances = np.array(NN_distances)
exp_NN_distances = np.array([0.0,0.25,0.5,0.75,1.0])
assert np.allclose(NN_distances, exp_NN_distances, atol=1e-10, rtol=1e-10)
def test_map_spin_correlations_5() -> None :
'''
Tests that the function in question correctly arranges the spin-spin correlation values according to
the associated NN shell of the system, starting from the "on-site correlation" (e.g. spin0-spin0
correlation) to the closest reproducible "replica correlation" (e.g. spin0-replica correlation).
This case involves a 4-sites system and the only non vanishing spin-spin correlation values
are those of the 1°NN shell.
'''
# Structural properties of the system
latt_vecs = np.array([[1.0,0.0,0.0],[0.0,10.0,0.0],[0.0,0.0,10.0]])
sites = np.array([[0.0,0.0,0.0],[0.25,0.0,0.0],[0.5,0.0,0.0],[0.75,0.0,0.0]])
system = SpinSystem(latt_vecs,sites,0.5)
# Spin-spin correlation values in the expected format
SSC_xs = np.eye(4)+0.5*(np.eye(4, k=1)+np.eye(4, k=-1))+0.5*(np.eye(4, k=3)+np.eye(4, k=-3))-0.25*(np.eye(4, k=2)+np.eye(4, k=-2))
SSC_ys = np.eye(4)+0.5*(np.eye(4, k=1)+np.eye(4, k=-1))+0.5*(np.eye(4, k=3)+np.eye(4, k=-3))-0.25*(np.eye(4, k=2)+np.eye(4, k=-2))
SSC_zs = np.eye(4)+0.5*(np.eye(4, k=1)+np.eye(4, k=-1))+0.5*(np.eye(4, k=3)+np.eye(4, k=-3))-0.25*(np.eye(4, k=2)+np.eye(4, k=-2))
SSC_tots = np.eye(4)+0.5*(np.eye(4, k=1)+np.eye(4, k=-1))+0.5*(np.eye(4, k=3)+np.eye(4, k=-3))-0.25*(np.eye(4, k=2)+np.eye(4, k=-2))
spin_correlations = (SSC_xs, SSC_ys, SSC_zs, SSC_tots)
# Final arrangement of the spin-spin correlation values vs expectation
final_SSCs, NN_distances = map_spin_correlations(system,spin_correlations,4,1)
exp_SSCs = np.array([[1.0,0.5,-0.25,0.5,1.0],
[1.0,0.5,-0.25,0.5,1.0],
[1.0,0.5,-0.25,0.5,1.0],
[1.0,0.5,-0.25,0.5,1.0]])
assert np.allclose(final_SSCs, exp_SSCs, atol=1e-10, rtol=1e-10)
def test_map_spin_correlations_6() -> None :
'''
Tests that the function in question correctly raises a ValueError exception when provided
with non-symmetric spin-spin correlation matrices.
'''
# Structural properties of the system
latt_vecs = np.array([[1.0,0.0,0.0],[0.0,10.0,0.0],[0.0,0.0,10.0]])
sites = np.array([[0.0,0.0,0.0],[0.25,0.0,0.0],[0.5,0.0,0.0],[0.75,0.0,0.0]])
system = SpinSystem(latt_vecs,sites,0.5)
# Spin-spin correlation values in the expected format
SSC_xs = np.eye(4)+0.5*(np.eye(4, k=1)-np.eye(4, k=-1))
SSC_ys = np.eye(4)+0.5*(np.eye(4, k=1)-np.eye(4, k=-1))
SSC_zs = np.eye(4)+0.5*(np.eye(4, k=1)-np.eye(4, k=-1))
SSC_tots = np.eye(4)+0.5*(np.eye(4, k=1)-np.eye(4, k=-1))
spin_correlations = (SSC_xs, SSC_ys, SSC_zs, SSC_tots)
with pytest.raises(ValueError, match='The given spin-spin correlation matrices are not symmetric as expected.') :
final_SSCs, NN_distances = map_spin_correlations(system,spin_correlations,4,1)
def test_solve_by_lanczos_0() -> None :
'''
Tests that the function leads to a correct estimation of the ground-state eigenvectors when the given Hamiltonian
consists of a 4x4 diagonal matrix and does not admit any degeneracy.
'''
H = np.array([[1.0,0.0,0.0,0.0],
[0.0,2.3,0.0,0.0],
[0.0,0.0,2.1,0.0],
[0.0,0.0,0.0,11.1]])
approx_eigvecs, approx_eigvals, GS_deg = solve_by_lanczos(H, 0, 4)
approx_eigvecs = np.abs(approx_eigvecs)
assert np.allclose(approx_eigvecs, np.array([[1.0,0.0,0.0,0.0]]).T, atol=1e-10, rtol=1e-10)
def test_solve_by_lanczos_1() -> None :
'''
Tests that the function leads to a correct estimation of the ground-state energy eigenvalue when
the given Hamiltonian consists of a 4x4 diagonal matrix and does not admit any degeneracy.
'''
H = np.array([[1.0,0.0,0.0,0.0],
[0.0,2.3,0.0,0.0],
[0.0,0.0,2.1,0.0],
[0.0,0.0,0.0,11.1]])
approx_eigvecs, approx_eigvals, GS_deg = solve_by_lanczos(H, 0, 4)
assert np.allclose(approx_eigvals, np.array([1.0]), atol=1e-10, rtol=1e-10)
def test_solve_by_lanczos_2() -> None :
'''
Tests that the function leads to a correct estimation of the ground-state degeneracy when
the given Hamiltonian consists of a 4x4 diagonal matrix and does not admit any degeneracy.
'''
H = np.array([[1.0,0.0,0.0,0.0],
[0.0,2.3,0.0,0.0],
[0.0,0.0,2.1,0.0],
[0.0,0.0,0.0,11.1]])
approx_eigvecs, approx_eigvals, GS_deg = solve_by_lanczos(H, 0, 4)
assert GS_deg==1
def test_solve_by_lanczos_3() -> None :
'''
Tests that the function leads to a correct estimation of the lowest energy eigenvalue when
the given Hamiltonian consists of a 4x4 diagonal matrix and the ground-state is also doubly-degenerate.
'''
H = np.array([[1.0,0.0,0.0,0.0],
[0.0,2.3,0.0,0.0],
[0.0,0.0,1.0,0.0],
[0.0,0.0,0.0,11.1]])
approx_eigvecs, approx_eigvals, GS_deg = solve_by_lanczos(H, 0, 4)
assert np.allclose(approx_eigvals, np.array([1.0]), atol=1e-10, rtol=1e-10)
def test_solve_by_lanczos_4() -> None :
'''
Tests that the function leads to a correct estimation of the ground-state degeneracy when
the given Hamiltonian consists of a 4x4 diagonal matrix and the ground-state is also doubly-degenerate.
'''
H = np.array([[1.0,0.0,0.0,0.0],
[0.0,2.3,0.0,0.0],
[0.0,0.0,1.0,0.0],
[0.0,0.0,0.0,11.1]])
approx_eigvecs, approx_eigvals, GS_deg = solve_by_lanczos(H, 0, 4)
assert GS_deg==2
def test_solve_by_lanczos_5() -> None :
'''
Tests that the function leads to a correct estimation of the ground-state eigenvectors when the given Hamiltonian
consists of a generic 4x4 hermitian matrix and does not admit any degeneracy.
'''
H = np.array([[ 1.0, 0.2+1.5j, 1.1-2.2j, -2.1+1.0j],
[ 0.2-1.5j, 2.3, 0.4+0.1j, 4.4+0.1j],
[ 1.1+2.2j, 0.4-0.1j, 2.1, 1.2-0.4j],
[-2.1-1.0j, 4.4-0.1j, 1.2+0.4j, 11.1]], dtype=complex)
# Compare eigenvectors from standard diagonalization vs Lanczos algorithm
exact_eigvals, exact_eigvecs = np.linalg.eigh(H)
approx_eigvecs, approx_eigvals, GS_deg = solve_by_lanczos(H, 0, 4)
# Adjust the (arbitrary) phase factor
first_angle = np.angle(approx_eigvecs[0][0])
approx_eigvecs = np.array([el*np.exp(-first_angle*1.0j) for el in approx_eigvecs])
assert np.allclose(approx_eigvecs.T, exact_eigvecs.T[0], atol=1e-10, rtol=1e-10)
def test_solve_by_lanczos_6() -> None :
'''
Tests that the function leads to a correct estimation of the lowest energy eigenvalue when the given Hamiltonian
consists of a generic 4x4 hermitian matrix and admits a 1D ground-state eigenspace.
'''
H = np.array([[ 1.0, 0.2+1.5j, 1.1-2.2j, -2.1+1.0j],
[ 0.2-1.5j, 2.3, 0.4+0.1j, 4.4+0.1j],
[ 1.1+2.2j, 0.4-0.1j, 2.1, 1.2-0.4j],
[-2.1-1.0j, 4.4-0.1j, 1.2+0.4j, 11.1]], dtype=complex)
# Compare eigenvalues from standard diagonalization vs Lanczos algorithm
exact_eigvals, exact_eigvecs = np.linalg.eigh(H)
approx_eigvecs, approx_eigvals, GS_deg = solve_by_lanczos(H, 0, 10)
assert np.allclose(approx_eigvals, np.array([min(exact_eigvals)]), atol=1e-10, rtol=1e-10)
def test_solve_by_lanczos_7() -> None :
'''
Tests that the function leads to a correct estimation of the ground-state degeneracy when the given Hamiltonian
consists of a generic 4x4 hermitian matrix and admits a 1D ground-state eigenspace.
'''
H = np.array([[ 1.0, 0.2+1.5j, 1.1-2.2j, -2.1+1.0j],
[ 0.2-1.5j, 2.3, 0.4+0.1j, 4.4+0.1j],
[ 1.1+2.2j, 0.4-0.1j, 2.1, 1.2-0.4j],
[-2.1-1.0j, 4.4-0.1j, 1.2+0.4j, 11.1]], dtype=complex)
approx_eigvecs, approx_eigvals, GS_deg = solve_by_lanczos(H, 0, 4)
assert GS_deg==1
def test_solve_by_lanczos_8() -> None :
'''
Tests that the function leads to a correct estimation of the lowest energy eigenvalue when the given Hamiltonian
consists of a generic 4x4 hermitian matrix and admits a 2D ground-state eigenspace.
'''
H = np.array([[ 1.0, 0.0, 0.0, 0.0],
[ 0.0, 1.0, 0.0, 0.0],
[ 0.0, 0.0, 12.1, 1.2-0.4j],
[ 0.0, 0.0, 1.2+0.4j, 11.1]], dtype=complex)
approx_eigvecs, approx_eigvals, GS_deg = solve_by_lanczos(H, 0, 10)
assert np.allclose(approx_eigvals, np.array([1.0]), atol=1e-10, rtol=1e-10)
def test_solve_by_lanczos_9() -> None :
'''
Tests that the function leads to a correct estimation of the ground-state degeneracy when the given Hamiltonian
consists of a generic 4x4 hermitian matrix and admits a 2D ground-state eigenspace.
'''
H = np.array([[ 1.0, 0.0, 0.0, 0.0],
[ 0.0, 1.0, 0.0, 0.0],
[ 0.0, 0.0, 12.1, 1.2-0.4j],
[ 0.0, 0.0, 1.2+0.4j, 11.1]], dtype=complex)
approx_eigvecs, approx_eigvals, GS_deg = solve_by_lanczos(H, 0, 4)
assert GS_deg==2