Trajectree 0.0.0__py3-none-any.whl → 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. trajectree/__init__.py +3 -0
  2. trajectree/fock_optics/devices.py +1 -1
  3. trajectree/fock_optics/light_sources.py +2 -2
  4. trajectree/fock_optics/measurement.py +3 -3
  5. trajectree/fock_optics/utils.py +6 -6
  6. trajectree/quimb/docs/_pygments/_pygments_dark.py +118 -0
  7. trajectree/quimb/docs/_pygments/_pygments_light.py +118 -0
  8. trajectree/quimb/docs/conf.py +158 -0
  9. trajectree/quimb/docs/examples/ex_mpi_expm_evo.py +62 -0
  10. trajectree/quimb/quimb/__init__.py +507 -0
  11. trajectree/quimb/quimb/calc.py +1491 -0
  12. trajectree/quimb/quimb/core.py +2279 -0
  13. trajectree/quimb/quimb/evo.py +712 -0
  14. trajectree/quimb/quimb/experimental/__init__.py +0 -0
  15. trajectree/quimb/quimb/experimental/autojittn.py +129 -0
  16. trajectree/quimb/quimb/experimental/belief_propagation/__init__.py +109 -0
  17. trajectree/quimb/quimb/experimental/belief_propagation/bp_common.py +397 -0
  18. trajectree/quimb/quimb/experimental/belief_propagation/d1bp.py +316 -0
  19. trajectree/quimb/quimb/experimental/belief_propagation/d2bp.py +653 -0
  20. trajectree/quimb/quimb/experimental/belief_propagation/hd1bp.py +571 -0
  21. trajectree/quimb/quimb/experimental/belief_propagation/hv1bp.py +775 -0
  22. trajectree/quimb/quimb/experimental/belief_propagation/l1bp.py +316 -0
  23. trajectree/quimb/quimb/experimental/belief_propagation/l2bp.py +537 -0
  24. trajectree/quimb/quimb/experimental/belief_propagation/regions.py +194 -0
  25. trajectree/quimb/quimb/experimental/cluster_update.py +286 -0
  26. trajectree/quimb/quimb/experimental/merabuilder.py +865 -0
  27. trajectree/quimb/quimb/experimental/operatorbuilder/__init__.py +15 -0
  28. trajectree/quimb/quimb/experimental/operatorbuilder/operatorbuilder.py +1631 -0
  29. trajectree/quimb/quimb/experimental/schematic.py +7 -0
  30. trajectree/quimb/quimb/experimental/tn_marginals.py +130 -0
  31. trajectree/quimb/quimb/experimental/tnvmc.py +1483 -0
  32. trajectree/quimb/quimb/gates.py +36 -0
  33. trajectree/quimb/quimb/gen/__init__.py +2 -0
  34. trajectree/quimb/quimb/gen/operators.py +1167 -0
  35. trajectree/quimb/quimb/gen/rand.py +713 -0
  36. trajectree/quimb/quimb/gen/states.py +479 -0
  37. trajectree/quimb/quimb/linalg/__init__.py +6 -0
  38. trajectree/quimb/quimb/linalg/approx_spectral.py +1109 -0
  39. trajectree/quimb/quimb/linalg/autoblock.py +258 -0
  40. trajectree/quimb/quimb/linalg/base_linalg.py +719 -0
  41. trajectree/quimb/quimb/linalg/mpi_launcher.py +397 -0
  42. trajectree/quimb/quimb/linalg/numpy_linalg.py +244 -0
  43. trajectree/quimb/quimb/linalg/rand_linalg.py +514 -0
  44. trajectree/quimb/quimb/linalg/scipy_linalg.py +293 -0
  45. trajectree/quimb/quimb/linalg/slepc_linalg.py +892 -0
  46. trajectree/quimb/quimb/schematic.py +1518 -0
  47. trajectree/quimb/quimb/tensor/__init__.py +401 -0
  48. trajectree/quimb/quimb/tensor/array_ops.py +610 -0
  49. trajectree/quimb/quimb/tensor/circuit.py +4824 -0
  50. trajectree/quimb/quimb/tensor/circuit_gen.py +411 -0
  51. trajectree/quimb/quimb/tensor/contraction.py +336 -0
  52. trajectree/quimb/quimb/tensor/decomp.py +1255 -0
  53. trajectree/quimb/quimb/tensor/drawing.py +1646 -0
  54. trajectree/quimb/quimb/tensor/fitting.py +385 -0
  55. trajectree/quimb/quimb/tensor/geometry.py +583 -0
  56. trajectree/quimb/quimb/tensor/interface.py +114 -0
  57. trajectree/quimb/quimb/tensor/networking.py +1058 -0
  58. trajectree/quimb/quimb/tensor/optimize.py +1818 -0
  59. trajectree/quimb/quimb/tensor/tensor_1d.py +4778 -0
  60. trajectree/quimb/quimb/tensor/tensor_1d_compress.py +1854 -0
  61. trajectree/quimb/quimb/tensor/tensor_1d_tebd.py +662 -0
  62. trajectree/quimb/quimb/tensor/tensor_2d.py +5954 -0
  63. trajectree/quimb/quimb/tensor/tensor_2d_compress.py +96 -0
  64. trajectree/quimb/quimb/tensor/tensor_2d_tebd.py +1230 -0
  65. trajectree/quimb/quimb/tensor/tensor_3d.py +2869 -0
  66. trajectree/quimb/quimb/tensor/tensor_3d_tebd.py +46 -0
  67. trajectree/quimb/quimb/tensor/tensor_approx_spectral.py +60 -0
  68. trajectree/quimb/quimb/tensor/tensor_arbgeom.py +3237 -0
  69. trajectree/quimb/quimb/tensor/tensor_arbgeom_compress.py +565 -0
  70. trajectree/quimb/quimb/tensor/tensor_arbgeom_tebd.py +1138 -0
  71. trajectree/quimb/quimb/tensor/tensor_builder.py +5411 -0
  72. trajectree/quimb/quimb/tensor/tensor_core.py +11179 -0
  73. trajectree/quimb/quimb/tensor/tensor_dmrg.py +1472 -0
  74. trajectree/quimb/quimb/tensor/tensor_mera.py +204 -0
  75. trajectree/quimb/quimb/utils.py +892 -0
  76. trajectree/quimb/tests/__init__.py +0 -0
  77. trajectree/quimb/tests/test_accel.py +501 -0
  78. trajectree/quimb/tests/test_calc.py +788 -0
  79. trajectree/quimb/tests/test_core.py +847 -0
  80. trajectree/quimb/tests/test_evo.py +565 -0
  81. trajectree/quimb/tests/test_gen/__init__.py +0 -0
  82. trajectree/quimb/tests/test_gen/test_operators.py +361 -0
  83. trajectree/quimb/tests/test_gen/test_rand.py +296 -0
  84. trajectree/quimb/tests/test_gen/test_states.py +261 -0
  85. trajectree/quimb/tests/test_linalg/__init__.py +0 -0
  86. trajectree/quimb/tests/test_linalg/test_approx_spectral.py +368 -0
  87. trajectree/quimb/tests/test_linalg/test_base_linalg.py +351 -0
  88. trajectree/quimb/tests/test_linalg/test_mpi_linalg.py +127 -0
  89. trajectree/quimb/tests/test_linalg/test_numpy_linalg.py +84 -0
  90. trajectree/quimb/tests/test_linalg/test_rand_linalg.py +134 -0
  91. trajectree/quimb/tests/test_linalg/test_slepc_linalg.py +283 -0
  92. trajectree/quimb/tests/test_tensor/__init__.py +0 -0
  93. trajectree/quimb/tests/test_tensor/test_belief_propagation/__init__.py +0 -0
  94. trajectree/quimb/tests/test_tensor/test_belief_propagation/test_d1bp.py +39 -0
  95. trajectree/quimb/tests/test_tensor/test_belief_propagation/test_d2bp.py +67 -0
  96. trajectree/quimb/tests/test_tensor/test_belief_propagation/test_hd1bp.py +64 -0
  97. trajectree/quimb/tests/test_tensor/test_belief_propagation/test_hv1bp.py +51 -0
  98. trajectree/quimb/tests/test_tensor/test_belief_propagation/test_l1bp.py +142 -0
  99. trajectree/quimb/tests/test_tensor/test_belief_propagation/test_l2bp.py +101 -0
  100. trajectree/quimb/tests/test_tensor/test_circuit.py +816 -0
  101. trajectree/quimb/tests/test_tensor/test_contract.py +67 -0
  102. trajectree/quimb/tests/test_tensor/test_decomp.py +40 -0
  103. trajectree/quimb/tests/test_tensor/test_mera.py +52 -0
  104. trajectree/quimb/tests/test_tensor/test_optimizers.py +488 -0
  105. trajectree/quimb/tests/test_tensor/test_tensor_1d.py +1171 -0
  106. trajectree/quimb/tests/test_tensor/test_tensor_2d.py +606 -0
  107. trajectree/quimb/tests/test_tensor/test_tensor_2d_tebd.py +144 -0
  108. trajectree/quimb/tests/test_tensor/test_tensor_3d.py +123 -0
  109. trajectree/quimb/tests/test_tensor/test_tensor_arbgeom.py +226 -0
  110. trajectree/quimb/tests/test_tensor/test_tensor_builder.py +441 -0
  111. trajectree/quimb/tests/test_tensor/test_tensor_core.py +2066 -0
  112. trajectree/quimb/tests/test_tensor/test_tensor_dmrg.py +388 -0
  113. trajectree/quimb/tests/test_tensor/test_tensor_spectral_approx.py +63 -0
  114. trajectree/quimb/tests/test_tensor/test_tensor_tebd.py +270 -0
  115. trajectree/quimb/tests/test_utils.py +85 -0
  116. trajectree/trajectory.py +2 -2
  117. {trajectree-0.0.0.dist-info → trajectree-0.0.1.dist-info}/METADATA +2 -2
  118. trajectree-0.0.1.dist-info/RECORD +126 -0
  119. trajectree-0.0.0.dist-info/RECORD +0 -16
  120. {trajectree-0.0.0.dist-info → trajectree-0.0.1.dist-info}/WHEEL +0 -0
  121. {trajectree-0.0.0.dist-info → trajectree-0.0.1.dist-info}/licenses/LICENSE +0 -0
  122. {trajectree-0.0.0.dist-info → trajectree-0.0.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,261 @@
1
+ from pytest import mark
2
+ import numpy as np
3
+ from numpy.testing import assert_allclose
4
+ from quimb import (
5
+ tr,
6
+ eye,
7
+ chop,
8
+ ikron,
9
+ expec,
10
+ ptr,
11
+ eigh,
12
+ groundstate,
13
+ concurrence,
14
+ basis_vec,
15
+ up,
16
+ down,
17
+ plus,
18
+ minus,
19
+ yplus,
20
+ yminus,
21
+ thermal_state,
22
+ neel_state,
23
+ ham_j1j2,
24
+ rand_herm,
25
+ graph_state_1d,
26
+ pauli,
27
+ levi_civita,
28
+ bloch_state,
29
+ bell_state,
30
+ singlet,
31
+ singlet_pairs,
32
+ werner_state,
33
+ ghz_state,
34
+ w_state,
35
+ perm_state,
36
+ )
37
+
38
+
39
+ class TestBasisVec:
40
+ def test_basis_vec(self):
41
+ x = basis_vec(1, 2)
42
+ assert_allclose(x, [[0.0], [1.0]])
43
+ x = basis_vec(1, 2, qtype="b")
44
+ assert_allclose(x, [[0.0, 1.0]])
45
+
46
+
47
+ class TestBasicStates:
48
+ def test_up(self):
49
+ p = up(qtype="dop")
50
+ assert_allclose(tr(p @ pauli("z")), 1.0)
51
+
52
+ def test_down(self):
53
+ p = down(qtype="dop")
54
+ assert_allclose(tr(p @ pauli("z")), -1.0)
55
+
56
+ def test_plus(self):
57
+ p = plus(qtype="dop")
58
+ assert_allclose(tr(p @ pauli("x")), 1.0)
59
+
60
+ def test_minus(self):
61
+ p = minus(qtype="dop")
62
+ assert_allclose(tr(p @ pauli("x")), -1.0)
63
+
64
+ def test_yplus(self):
65
+ p = yplus(qtype="dop")
66
+ assert_allclose(tr(p @ pauli("y")), 1.0)
67
+
68
+ def test_yminus(self):
69
+ p = yminus(qtype="dop")
70
+ assert_allclose(tr(p @ pauli("y")), -1.0)
71
+
72
+
73
+ class TestBlochState:
74
+ def test_pure(self):
75
+ for vec, op, val in zip(
76
+ (
77
+ (1, 0, 0),
78
+ (0, 1, 0),
79
+ (0, 0, 1),
80
+ (-1, 0, 0),
81
+ (0, -1, 0),
82
+ (0, 0, -1),
83
+ ),
84
+ ("x", "y", "z", "x", "y", "z"),
85
+ (1, 1, 1, -1, -1, -1),
86
+ ):
87
+ x = tr(bloch_state(*vec) @ pauli(op))
88
+ assert_allclose(x, val)
89
+
90
+ def test_mixed(self):
91
+ for vec, op, val in zip(
92
+ (
93
+ (0.5, 0, 0),
94
+ (0, 0.5, 0),
95
+ (0, 0, 0.5),
96
+ (-0.5, 0, 0),
97
+ (0, -0.5, 0),
98
+ (0, 0, -0.5),
99
+ ),
100
+ ("x", "y", "z", "x", "y", "z"),
101
+ (0.5, 0.5, 0.5, -0.5, -0.5, -0.5),
102
+ ):
103
+ x = tr(bloch_state(*vec) @ pauli(op))
104
+ assert_allclose(x, val)
105
+
106
+ def test_purify(self):
107
+ for vec, op, val in zip(
108
+ (
109
+ (0.5, 0, 0),
110
+ (0, 0.5, 0),
111
+ (0, 0, 0.5),
112
+ (-0.5, 0, 0),
113
+ (0, -0.5, 0),
114
+ (0, 0, -0.5),
115
+ ),
116
+ ("x", "y", "z", "x", "y", "z"),
117
+ (1, 1, 1, -1, -1, -1),
118
+ ):
119
+ x = tr(bloch_state(*vec, purified=True) @ pauli(op))
120
+ assert_allclose(x, val)
121
+
122
+
123
+ class TestBellStates:
124
+ def test_bell_states(self):
125
+ for s, dic in zip(
126
+ ("psi-", "psi+", "phi+", "phi-"),
127
+ ({"qtype": "dop"}, {}, {"sparse": True}, {}),
128
+ ):
129
+ p = bell_state(s, **dic)
130
+ assert_allclose(expec(p, p), 1.0)
131
+ pa = ptr(p, [2, 2], 0)
132
+ assert_allclose(expec(pa, pa), 0.5)
133
+
134
+ def test_bell_state_singlet(self):
135
+ p = singlet(qtype="dop", sparse=True)
136
+ assert_allclose(expec(p, p), 1.0)
137
+ pa = ptr(p, [2, 2], 0)
138
+ assert_allclose(expec(pa, pa), 0.5)
139
+
140
+
141
+ class TestThermalState:
142
+ def test_thermal_state_normalization(self):
143
+ full = rand_herm(2**4)
144
+ for beta in (0, 0.5, 1, 10):
145
+ rhoth = thermal_state(full, beta)
146
+ assert_allclose(tr(rhoth), 1)
147
+
148
+ def test_thermal_state_tuple(self):
149
+ full = rand_herm(2**4)
150
+ l, v = eigh(full)
151
+ for beta in (0, 0.5, 1, 10):
152
+ rhoth1 = thermal_state(full, beta)
153
+ rhoth2 = thermal_state((l, v), beta)
154
+ assert_allclose(rhoth1, rhoth2)
155
+
156
+ def test_thermal_state_hot(self):
157
+ full = rand_herm(2**4)
158
+ rhoth = chop(thermal_state(full, 0.0))
159
+ assert_allclose(rhoth, eye(2**4) / 2**4)
160
+
161
+ def test_thermal_state_cold(self):
162
+ full = ham_j1j2(4, j2=0.1253)
163
+ rhoth = thermal_state(full, 100)
164
+ gs = groundstate(full)
165
+ assert_allclose(tr(gs.H @ rhoth @ gs), 1.0, rtol=1e-4)
166
+
167
+ def test_thermal_state_precomp(self):
168
+ full = rand_herm(2**4)
169
+ beta = 0.624
170
+ rhoth1 = thermal_state(full, beta)
171
+ func = thermal_state(full, None, precomp_func=True)
172
+ rhoth2 = func(beta)
173
+ assert_allclose(rhoth1, rhoth2)
174
+
175
+
176
+ class TestNeelState:
177
+ def test_simple(self):
178
+ p = neel_state(1)
179
+ assert_allclose(p, up())
180
+ p = neel_state(2)
181
+ assert_allclose(p, up() & down())
182
+ p = neel_state(3)
183
+ assert_allclose(p, up() & down() & up())
184
+
185
+
186
+ class TestSingletPairs:
187
+ def test_n2(self):
188
+ p = singlet_pairs(2)
189
+ assert_allclose(p, bell_state("psi-"))
190
+ p = singlet_pairs(4)
191
+ assert_allclose(p, bell_state("psi-") & bell_state("psi-"))
192
+
193
+
194
+ class TestWernerState:
195
+ def test_extremes(self):
196
+ p = werner_state(1)
197
+ assert_allclose(p, bell_state("psi-", qtype="dop"))
198
+ p = werner_state(0)
199
+ assert_allclose(p, eye(4) / 4, atol=1e-14)
200
+
201
+ @mark.parametrize("p", np.linspace(0, 1 / 3, 10))
202
+ def test_no_entanglement(self, p):
203
+ rho = werner_state(p)
204
+ assert concurrence(rho) < 1e-14
205
+
206
+ @mark.parametrize("p", np.linspace(1 / 3 + 0.001, 1.0, 10))
207
+ def test_entanglement(self, p):
208
+ rho = werner_state(p)
209
+ assert concurrence(rho) > 1e-14
210
+
211
+
212
+ class TestGHZState:
213
+ def test_n2(self):
214
+ p = ghz_state(2)
215
+ assert_allclose(p, bell_state("phi+"))
216
+
217
+
218
+ class TestWState:
219
+ def test_n2(self):
220
+ p = w_state(2)
221
+ assert_allclose(p, bell_state("psi+"))
222
+
223
+
224
+ class TestLeviCivita:
225
+ def test_levi_civita_pos(self):
226
+ perm = [0, 1, 2, 3]
227
+ assert levi_civita(perm) == 1
228
+ perm = [2, 3, 0, 1]
229
+ assert levi_civita(perm) == 1
230
+
231
+ def test_levi_civita_neg(self):
232
+ perm = [0, 2, 1, 3]
233
+ assert levi_civita(perm) == -1
234
+ perm = [2, 3, 1, 0]
235
+ assert levi_civita(perm) == -1
236
+
237
+ def test_levi_civita_nzero(self):
238
+ perm = [2, 3, 1, 1]
239
+ assert levi_civita(perm) == 0
240
+ perm = [0, 0, 1, 1]
241
+ assert levi_civita(perm) == 0
242
+
243
+
244
+ class TestPermState:
245
+ def test_n2(self):
246
+ p = perm_state([up(), down()])
247
+ assert_allclose(p, bell_state("psi-"))
248
+
249
+
250
+ class TestGraphState:
251
+ def test_graph_state_1d(self):
252
+ n = 5
253
+ p = graph_state_1d(n, cyclic=True)
254
+ for j in range(n):
255
+ k = ikron(
256
+ [pauli("x"), pauli("z"), pauli("z")],
257
+ dims=[2] * n,
258
+ inds=(j, (j - 1) % n, (j + 1) % n),
259
+ )
260
+ o = p.H @ k @ p
261
+ np.testing.assert_allclose(o, 1)
File without changes
@@ -0,0 +1,368 @@
1
+ import pytest
2
+ import numpy as np
3
+ from numpy.testing import assert_allclose
4
+
5
+ from quimb import (
6
+ prod,
7
+ rand_ket,
8
+ rand_pos,
9
+ rand_herm,
10
+ partial_transpose,
11
+ eigvalsh,
12
+ neel_state,
13
+ ham_heis,
14
+ logneg,
15
+ negativity,
16
+ entropy,
17
+ can_use_mpi_pool,
18
+ )
19
+ from quimb.utils import last
20
+
21
+ from quimb.linalg.approx_spectral import (
22
+ lazy_ptr_ppt_linop,
23
+ construct_lanczos_tridiag,
24
+ lanczos_tridiag_eig,
25
+ approx_spectral_function,
26
+ lazy_ptr_linop,
27
+ tr_abs_approx,
28
+ tr_exp_approx,
29
+ tr_sqrt_approx,
30
+ tr_xlogx_approx,
31
+ entropy_subsys_approx,
32
+ logneg_subsys_approx,
33
+ negativity_subsys_approx,
34
+ norm_fro,
35
+ norm_fro_approx,
36
+ )
37
+
38
+ MPI_PARALLEL = [False]
39
+ if can_use_mpi_pool():
40
+ MPI_PARALLEL.append(True)
41
+
42
+
43
+ np.random.seed(42)
44
+
45
+ DIMS = [5, 6, 7]
46
+ DIMS_MB = [2] * 11
47
+
48
+
49
+ @pytest.fixture
50
+ def psi_abc():
51
+ return rand_ket(prod(DIMS))
52
+
53
+
54
+ @pytest.fixture
55
+ def psi_ab():
56
+ return rand_ket(prod(DIMS[:-1]))
57
+
58
+
59
+ @pytest.fixture
60
+ def psi_ab_mat():
61
+ return np.concatenate(
62
+ [rand_ket(prod(DIMS[:-1])) for _ in range(6)], axis=1
63
+ )
64
+
65
+
66
+ @pytest.fixture
67
+ def psi_mb_abc():
68
+ return rand_ket(prod(DIMS_MB))
69
+
70
+
71
+ @pytest.fixture
72
+ def psi_mb_ab():
73
+ return rand_ket(prod(DIMS_MB[:7]))
74
+
75
+
76
+ @pytest.fixture
77
+ def psi_mb_ab_mat():
78
+ return np.concatenate(
79
+ [rand_ket(prod(DIMS_MB[:7])) for _ in range(6)], axis=1
80
+ )
81
+
82
+
83
+ class TestLazyTensorEval:
84
+ # ------------------------- Just partial trace -------------------------- #
85
+
86
+ def test_lazy_ptr_dot_simple_linear_op(self, psi_abc, psi_ab):
87
+ rho_ab = psi_abc.ptr(DIMS, [0, 1])
88
+ psi_out_expected = rho_ab @ psi_ab
89
+ lo = lazy_ptr_linop(psi_abc, DIMS, [0, 1])
90
+ assert hasattr(lo, "H")
91
+ assert lo.dtype == complex
92
+ assert lo.shape == (DIMS[0] * DIMS[1], DIMS[0] * DIMS[1])
93
+ psi_out_got = lo @ psi_ab
94
+ assert_allclose(psi_out_expected, psi_out_got)
95
+
96
+ def test_lazy_ptr_dot_mat_simple_linear_op(self, psi_abc, psi_ab_mat):
97
+ rho_ab = psi_abc.ptr(DIMS, [0, 1])
98
+ psi_out_expected = rho_ab @ psi_ab_mat
99
+ lo = lazy_ptr_linop(psi_abc, DIMS, [0, 1])
100
+ psi_out_got = lo @ psi_ab_mat
101
+ assert_allclose(psi_out_expected, psi_out_got)
102
+
103
+ def test_lazy_ptr_dot_manybody_linear_op(self, psi_mb_abc, psi_mb_ab):
104
+ sysa = [0, 1, 2, 3, 7, 8, 9]
105
+ rho_ab = psi_mb_abc.ptr(DIMS_MB, sysa)
106
+ psi_out_expected = rho_ab @ psi_mb_ab
107
+ lo = lazy_ptr_linop(psi_mb_abc, DIMS_MB, sysa=sysa)
108
+ assert hasattr(lo, "H")
109
+ assert lo.dtype == complex
110
+ assert lo.shape == (128, 128)
111
+ psi_out_got = lo @ psi_mb_ab
112
+ assert_allclose(psi_out_expected, psi_out_got)
113
+
114
+ def test_lazy_ptr_dot_mat_manybody_linear_op(
115
+ self, psi_mb_abc, psi_mb_ab_mat
116
+ ):
117
+ sysa = [0, 1, 2, 3, 7, 8, 9]
118
+ rho_ab = psi_mb_abc.ptr(DIMS_MB, sysa)
119
+ psi_out_expected = rho_ab @ psi_mb_ab_mat
120
+ lo = lazy_ptr_linop(psi_mb_abc, DIMS_MB, sysa=sysa)
121
+ psi_out_got = lo @ psi_mb_ab_mat
122
+ assert_allclose(psi_out_expected, psi_out_got)
123
+
124
+ # ----------------- partial trace and partial transpose ----------------- #
125
+
126
+ def test_lazy_ptr_ppt_dot_linear_op(self, psi_abc, psi_ab):
127
+ rho_ab = psi_abc.ptr(DIMS, [0, 1])
128
+ rho_ab_pt = partial_transpose(rho_ab, DIMS[:-1])
129
+ psi_out_expected = rho_ab_pt @ psi_ab
130
+ lo = lazy_ptr_ppt_linop(psi_abc, DIMS, 0, 1)
131
+ assert hasattr(lo, "H")
132
+ assert lo.dtype == complex
133
+ assert lo.shape == (DIMS[0] * DIMS[1], DIMS[0] * DIMS[1])
134
+ psi_out_got = lo @ psi_ab
135
+ assert_allclose(psi_out_expected, psi_out_got)
136
+
137
+ def test_lazy_ptr_ppt_dot_mat_linear_op(self, psi_abc, psi_ab_mat):
138
+ rho_ab = psi_abc.ptr(DIMS, [0, 1])
139
+ rho_ab_pt = partial_transpose(rho_ab, DIMS[:-1])
140
+ psi_out_expected = rho_ab_pt @ psi_ab_mat
141
+ lo = lazy_ptr_ppt_linop(psi_abc, DIMS, 0, 1)
142
+ psi_out_got = lo @ psi_ab_mat
143
+ assert_allclose(psi_out_expected, psi_out_got)
144
+
145
+ def test_lazy_ptr_ppt_dot_manybody_linear_op(self, psi_mb_abc, psi_mb_ab):
146
+ sysa = [0, 1, 7, 8]
147
+ sysb = [2, 3, 9]
148
+ rho_ab = psi_mb_abc.ptr(DIMS_MB, sysa + sysb)
149
+ rho_ab = partial_transpose(rho_ab, [2] * 7, sysa=(0, 1, 4, 5))
150
+ psi_out_expected = rho_ab @ psi_mb_ab
151
+ lo = lazy_ptr_ppt_linop(psi_mb_abc, DIMS_MB, sysa, sysb)
152
+ assert hasattr(lo, "H")
153
+ assert lo.dtype == complex
154
+ assert lo.shape == (128, 128)
155
+ psi_out_got = lo.dot(psi_mb_ab)
156
+ assert_allclose(psi_out_expected, psi_out_got)
157
+
158
+ def test_lazy_ptr_ppt_dot_mat_manybody_linear_op(
159
+ self, psi_mb_abc, psi_mb_ab_mat
160
+ ):
161
+ sysa = [0, 1, 7, 8]
162
+ sysb = [2, 3, 9]
163
+ rho_ab = psi_mb_abc.ptr(DIMS_MB, sysa + sysb)
164
+ rho_ab = partial_transpose(rho_ab, [2] * 7, sysa=(0, 1, 4, 5))
165
+ psi_out_expected = rho_ab @ psi_mb_ab_mat
166
+ lo = lazy_ptr_ppt_linop(psi_mb_abc, DIMS_MB, sysa, sysb)
167
+ psi_out_got = lo.dot(psi_mb_ab_mat)
168
+ assert psi_out_got.shape[1] > 1
169
+ assert_allclose(psi_out_expected, psi_out_got)
170
+
171
+
172
+ # --------------------- Test lanczos spectral functions --------------------- #
173
+
174
+
175
+ def np_sqrt(x):
176
+ out = np.empty_like(x)
177
+ mtz = x > 0
178
+ out[~mtz] = 0.0
179
+ out[mtz] = np.sqrt(x[mtz])
180
+ return out
181
+
182
+
183
+ def np_xlogx(x):
184
+ out = np.empty_like(x)
185
+ mtz = x > 0
186
+ out[~mtz] = 0.0
187
+ out[mtz] = x[mtz] * np.log2(x[mtz])
188
+ return out
189
+
190
+
191
+ class TestLanczosApprox:
192
+ @pytest.mark.parametrize("bsz", [1, 2, 5])
193
+ def test_construct_lanczos_tridiag(self, bsz):
194
+ a = rand_herm(2**5)
195
+ alpha, beta, scaling = last(
196
+ construct_lanczos_tridiag(a, bsz=bsz, K=20)
197
+ )
198
+ assert alpha.shape == (20,)
199
+ assert beta.shape == (20,)
200
+
201
+ el, ev = lanczos_tridiag_eig(alpha, beta)
202
+ assert el.shape == (20,)
203
+ assert el.dtype == float
204
+ assert ev.shape == (20, 20)
205
+ assert ev.dtype == float
206
+
207
+ @pytest.mark.parametrize("bsz", [1, 2, 5])
208
+ def test_construct_lanczos_tridiag_beta_breakdown(self, bsz):
209
+ a = rand_herm(8)
210
+ alpha, beta, scaling = last(construct_lanczos_tridiag(a, bsz=bsz, K=9))
211
+ assert alpha.shape == (8,)
212
+ assert beta.shape == (8,)
213
+
214
+ el, ev = lanczos_tridiag_eig(alpha, beta)
215
+ assert el.shape == (8,)
216
+ assert el.dtype == float
217
+ assert ev.shape == (8, 8)
218
+ assert ev.dtype == float
219
+
220
+ @pytest.mark.parametrize("mpi", MPI_PARALLEL)
221
+ @pytest.mark.parametrize("bsz", [1, 2, 5])
222
+ @pytest.mark.parametrize(
223
+ "fn_matrix_rtol",
224
+ [
225
+ (np.abs, rand_herm, 1e-1),
226
+ (np.sqrt, rand_pos, 1e-1),
227
+ (np.log1p, rand_pos, 2e-1),
228
+ (np.exp, rand_herm, 1e-1),
229
+ ],
230
+ )
231
+ def test_approx_spectral_function(self, fn_matrix_rtol, bsz, mpi):
232
+ fn, matrix, rtol = fn_matrix_rtol
233
+ a = matrix(2**7)
234
+ pos = fn == np.sqrt
235
+ actual_x = sum(fn(eigvalsh(a)))
236
+ approx_x = approx_spectral_function(
237
+ a, fn, mpi=mpi, pos=pos, bsz=bsz, verbosity=2
238
+ )
239
+ assert_allclose(actual_x, approx_x, rtol=rtol)
240
+
241
+ @pytest.mark.parametrize("bsz", [1, 2, 5])
242
+ @pytest.mark.parametrize("dist", ["gaussian", "phase", "rademacher"])
243
+ @pytest.mark.parametrize(
244
+ "fn_matrix_rtol",
245
+ [
246
+ (np.abs, rand_herm, 2e-1),
247
+ (np.sqrt, rand_pos, 2e-1),
248
+ (np.log1p, rand_pos, 3e-1),
249
+ (np.exp, rand_herm, 2e-1),
250
+ ],
251
+ )
252
+ def test_approx_spectral_function_with_v0(self, dist, fn_matrix_rtol, bsz):
253
+ fn, matrix, rtol = fn_matrix_rtol
254
+ a = matrix(2**7)
255
+ actual_x = sum(fn(eigvalsh(a)))
256
+ # check un-normalized state work properly
257
+ v0 = neel_state(7) + neel_state(7, down_first=True)
258
+ v0 = v0.toarray().reshape(-1)
259
+ pos = fn == np.sqrt
260
+ v0_opts = {"dist": dist, "orthog": True}
261
+ approx_x = approx_spectral_function(
262
+ a, fn, v0=v0, pos=pos, bsz=bsz, K=20, verbosity=2, v0_opts=v0_opts
263
+ )
264
+ assert_allclose(actual_x, approx_x, rtol=rtol)
265
+
266
+ @pytest.mark.parametrize("bsz", [1, 2, 5])
267
+ @pytest.mark.parametrize(
268
+ "fn_approx_rtol",
269
+ [
270
+ (np_sqrt, tr_sqrt_approx, 0.3),
271
+ (np.exp, tr_exp_approx, 0.03),
272
+ (np_xlogx, tr_xlogx_approx, 0.3),
273
+ ],
274
+ )
275
+ def test_approx_spectral_function_ptr_lin_op(
276
+ self, fn_approx_rtol, psi_abc, psi_ab, bsz
277
+ ):
278
+ fn, approx, rtol = fn_approx_rtol
279
+ sysa = [0, 1]
280
+ rho_ab = psi_abc.ptr(DIMS, sysa)
281
+ actual_x = sum(fn(eigvalsh(rho_ab)))
282
+ lo = lazy_ptr_linop(psi_abc, DIMS, sysa)
283
+ approx_x = approx(lo, R=50, bsz=bsz, verbosity=2)
284
+ assert_allclose(actual_x, approx_x, rtol=rtol)
285
+
286
+ @pytest.mark.parametrize("bsz", [1, 2, 5])
287
+ @pytest.mark.parametrize(
288
+ "fn_approx_rtol",
289
+ [(np.exp, tr_exp_approx, 5e-2), (np.abs, tr_abs_approx, 1e-1)],
290
+ )
291
+ def test_approx_spectral_function_ptr_ppt_lin_op(
292
+ self, fn_approx_rtol, psi_abc, psi_ab, bsz
293
+ ):
294
+ fn, approx, rtol = fn_approx_rtol
295
+ rho_ab_ppt = partial_transpose(psi_abc.ptr(DIMS, [0, 1]), DIMS[:-1], 0)
296
+ actual_x = sum(fn(eigvalsh(rho_ab_ppt)))
297
+ lo = lazy_ptr_ppt_linop(psi_abc, DIMS, sysa=0, sysb=1)
298
+ approx_x = approx_spectral_function(lo, fn, K=20, R=20, bsz=bsz)
299
+ assert_allclose(actual_x, approx_x, rtol=rtol)
300
+
301
+ @pytest.mark.parametrize("bsz", [1, 2, 5])
302
+ def test_approx_spectral_subspaces_with_heis_partition(self, bsz):
303
+ h = ham_heis(10, sparse=True)
304
+ beta = 0.01
305
+ actual_Z = sum(np.exp(-beta * eigvalsh(h.toarray())))
306
+ approx_Z = tr_exp_approx(-beta * h, bsz=bsz)
307
+ assert_allclose(actual_Z, approx_Z, rtol=3e-2)
308
+
309
+ def test_approx_spectral_plot(self):
310
+ import matplotlib
311
+
312
+ matplotlib.use("Template")
313
+ X = rand_herm(1000, sparse=True)
314
+ approx_spectral_function(X, lambda x: abs(x), plot=True)
315
+
316
+
317
+ # ------------------------ Test specific quantities ------------------------- #
318
+
319
+
320
+ class TestSpecificApproxQuantities:
321
+ @pytest.mark.parametrize("bsz", [1, 2, 5])
322
+ def test_entropy_approx_simple(self, psi_abc, bsz):
323
+ np.random.seed(42)
324
+ rho_ab = psi_abc.ptr(DIMS, [0, 1])
325
+ actual_e = entropy(rho_ab)
326
+ approx_e = entropy_subsys_approx(psi_abc, DIMS, [0, 1], bsz=bsz, R=20)
327
+ assert_allclose(actual_e, approx_e, rtol=2e-1)
328
+
329
+ @pytest.mark.parametrize("bsz", [1, 2, 5])
330
+ def test_entropy_approx_many_body(self, psi_mb_abc, bsz):
331
+ rho_ab = psi_mb_abc.ptr(DIMS_MB, [0, 1, 7, 8, 2, 3, 9])
332
+ actual_e = entropy(rho_ab)
333
+ approx_e = entropy_subsys_approx(
334
+ psi_mb_abc, DIMS_MB, [0, 1, 7, 8, 2, 3, 9], bsz=bsz
335
+ )
336
+ assert_allclose(actual_e, approx_e, rtol=2e-1)
337
+
338
+ @pytest.mark.parametrize("bsz", [1, 2, 5])
339
+ def test_logneg_approx_simple(self, psi_abc, bsz):
340
+ rho_ab = psi_abc.ptr(DIMS, [0, 1])
341
+ actual_ln = logneg(rho_ab, DIMS[:-1], 0)
342
+ approx_ln = logneg_subsys_approx(psi_abc, DIMS, 0, 1, bsz=bsz)
343
+ assert_allclose(actual_ln, approx_ln, rtol=2e-1)
344
+
345
+ @pytest.mark.parametrize("bsz", [1, 2, 5])
346
+ def test_logneg_approx_many_body(self, psi_mb_abc, bsz):
347
+ sysa = [0, 1, 7, 8]
348
+ sysb = [2, 3, 9]
349
+ rho_ab = psi_mb_abc.ptr(DIMS_MB, sysa + sysb)
350
+ actual_ln = logneg(rho_ab, [2] * 7, sysa=(0, 1, 4, 5))
351
+ approx_ln = logneg_subsys_approx(
352
+ psi_mb_abc, DIMS_MB, sysa=sysa, sysb=sysb, bsz=bsz
353
+ )
354
+ assert_allclose(actual_ln, approx_ln, rtol=1e-1)
355
+
356
+ @pytest.mark.parametrize("bsz", [1, 2, 5])
357
+ def test_negativity_approx_simple(self, psi_abc, bsz):
358
+ rho_ab = psi_abc.ptr(DIMS, [0, 1])
359
+ actual_neg = negativity(rho_ab, DIMS[:-1], 0)
360
+ approx_neg = negativity_subsys_approx(psi_abc, DIMS, 0, 1, bsz=bsz)
361
+ assert_allclose(actual_neg, approx_neg, rtol=2e-1)
362
+
363
+ @pytest.mark.parametrize("bsz", [1, 2, 5])
364
+ def test_norm_fro_approx(self, bsz):
365
+ A = rand_herm(2**5)
366
+ actual_norm_fro = norm_fro(A)
367
+ approx_norm_fro = norm_fro_approx(A, tol=1e-2, bsz=bsz)
368
+ assert_allclose(actual_norm_fro, approx_norm_fro, rtol=1e-1)