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,788 @@
1
+ import pytest
2
+ import itertools
3
+ import math
4
+ import numpy as np
5
+ from numpy.testing import assert_allclose
6
+ import quimb as qu
7
+
8
+
9
+ @pytest.fixture
10
+ def p1():
11
+ return qu.rand_rho(3)
12
+
13
+
14
+ @pytest.fixture
15
+ def p2():
16
+ return qu.rand_rho(3)
17
+
18
+
19
+ @pytest.fixture
20
+ def k1():
21
+ return qu.rand_ket(3)
22
+
23
+
24
+ @pytest.fixture
25
+ def k2():
26
+ return qu.rand_ket(3)
27
+
28
+
29
+ @pytest.fixture
30
+ def orthog_ks():
31
+ p = qu.rand_rho(3)
32
+ v = qu.eigvecsh(p)
33
+ return (v[:, [0]], v[:, [1]], v[:, [2]])
34
+
35
+
36
+ # --------------------------------------------------------------------------- #
37
+ # TESTS #
38
+ # --------------------------------------------------------------------------- #
39
+
40
+
41
+ class TestFidelity:
42
+ def test_both_pure(self, k1, k2):
43
+ f = qu.fidelity(k1, k1)
44
+ assert_allclose(f, 1.0)
45
+ f = qu.fidelity(k1, k2)
46
+ assert f > 0 and f < 1
47
+
48
+ def test_both_mixed(self, p1, p2):
49
+ f = qu.fidelity(qu.eye(3) / 3, qu.eye(3) / 3)
50
+ assert_allclose(f, 1.0)
51
+ f = qu.fidelity(p1, p1)
52
+ assert_allclose(f, 1.0)
53
+ f = qu.fidelity(p1, p2)
54
+ assert f > 0 and f < 1
55
+
56
+ def test_orthog_pure(self, orthog_ks):
57
+ k1, k2, k3 = orthog_ks
58
+ for (
59
+ s1,
60
+ s2,
61
+ ) in (
62
+ [k1, k2],
63
+ [k2, k3],
64
+ [k3, k1],
65
+ [k1 @ k1.H, k2],
66
+ [k1, k2 @ k2.H],
67
+ [k3 @ k3.H, k2],
68
+ [k3, k2 @ k2.H],
69
+ [k1 @ k1.H, k3],
70
+ [k1, k3 @ k3.H],
71
+ [k1 @ k1.H, k2 @ k2.H],
72
+ [k2 @ k2.H, k3 @ k3.H],
73
+ [k1 @ k1.H, k3 @ k3.H],
74
+ ):
75
+ f = qu.fidelity(s1, s2)
76
+ assert_allclose(f, 0.0, atol=1e-6)
77
+
78
+
79
+ class TestPurify:
80
+ def test_d2(self):
81
+ rho = qu.eye(2) / 2
82
+ psi = qu.purify(rho)
83
+ assert qu.expec(psi, qu.bell_state("phi+")) > 1 - 1e-14
84
+
85
+ def test_pure(self):
86
+ rho = qu.up(qtype="dop")
87
+ psi = qu.purify(rho)
88
+ assert abs(qu.concurrence(psi)) < 1e-14
89
+
90
+
91
+ class TestDephase:
92
+ @pytest.mark.parametrize("rand_rank", [None, 0.3, 2])
93
+ def test_basic(self, rand_rank):
94
+ rho = qu.rand_rho(9)
95
+ ln = qu.logneg(rho, [3, 3])
96
+ for p in (0.2, 0.5, 0.8, 1.0):
97
+ rho_d = qu.dephase(rho, p, rand_rank=rand_rank)
98
+ assert qu.logneg(rho_d, [3, 3]) <= ln
99
+ assert rho_d.tr() == pytest.approx(1.0)
100
+
101
+
102
+ class TestKrausOp:
103
+ @pytest.mark.parametrize("stack", [False, True])
104
+ def test_depolarize(self, stack):
105
+ rho = qu.rand_rho(2)
106
+ I, X, Y, Z = (qu.pauli(s) for s in "IXYZ")
107
+ es = [qu.expec(rho, A) for A in (X, Y, Z)]
108
+ p = 0.1
109
+ Ek = [
110
+ (1 - p) ** 0.5 * I,
111
+ (p / 3) ** 0.5 * X,
112
+ (p / 3) ** 0.5 * Y,
113
+ (p / 3) ** 0.5 * Z,
114
+ ]
115
+ if stack:
116
+ Ek = np.stack(Ek, axis=0)
117
+ sigma = qu.kraus_op(rho, Ek, check=True)
118
+ es2 = [qu.expec(sigma, A) for A in (X, Y, Z)]
119
+ assert qu.tr(sigma) == pytest.approx(1.0)
120
+ assert all(abs(e2) < abs(e) for e, e2 in zip(es, es2))
121
+ sig_exp = sum(E @ rho @ qu.dag(E) for E in Ek)
122
+ assert_allclose(sig_exp, sigma)
123
+
124
+ def test_subsystem(self):
125
+ rho = qu.rand_rho(6)
126
+ dims = [3, 2]
127
+ I, X, Y, Z = (qu.pauli(s) for s in "IXYZ")
128
+ mi_i = qu.mutual_information(rho, dims)
129
+ p = 0.1
130
+ Ek = [
131
+ (1 - p) ** 0.5 * I,
132
+ (p / 3) ** 0.5 * X,
133
+ (p / 3) ** 0.5 * Y,
134
+ (p / 3) ** 0.5 * Z,
135
+ ]
136
+
137
+ with pytest.raises(ValueError):
138
+ qu.kraus_op(
139
+ rho, qu.randn((3, 2, 2)), check=True, dims=dims, where=1
140
+ )
141
+
142
+ sigma = qu.kraus_op(rho, Ek, check=True, dims=dims, where=1)
143
+ mi_f = qu.mutual_information(sigma, dims)
144
+ assert mi_f < mi_i
145
+ assert qu.tr(sigma) == pytest.approx(1.0)
146
+ sig_exp = sum(
147
+ (qu.eye(3) & E) @ rho @ qu.dag(qu.eye(3) & E) for E in Ek
148
+ )
149
+ assert_allclose(sig_exp, sigma)
150
+
151
+ def test_multisubsystem(self):
152
+ qu.seed_rand(42)
153
+ dims = [2, 2, 2]
154
+ IIX = qu.ikron(qu.rand_matrix(2), dims, 2)
155
+ dcmp = qu.pauli_decomp(IIX, mode="c")
156
+ for p, x in dcmp.items():
157
+ if abs(x) < 1e-12:
158
+ assert (p[0] != "I") or (p[1] != "I")
159
+ else:
160
+ assert p[0] == p[1] == "I"
161
+ K = qu.rand_iso(3 * 4, 4).reshape(3, 4, 4)
162
+ KIIXK = qu.kraus_op(IIX, K, dims=dims, where=[0, 2], check=True)
163
+ dcmp = qu.pauli_decomp(KIIXK, mode="c")
164
+ for p, x in dcmp.items():
165
+ if abs(x) > 1e-12:
166
+ assert (p == "III") or p[1] == "I"
167
+
168
+ @pytest.mark.parametrize("subsystem", [(0, 1), (1, 2), (2, 0)])
169
+ def test_multisubsytem_kraus_identity(self, subsystem):
170
+ n = 3
171
+ qu.seed_rand(7)
172
+ rho = qu.rand_rho(2**n)
173
+ Ek = np.array([qu.eye(2 ** len(subsystem))])
174
+ sigma = qu.kraus_op(rho, Ek, dims=[2] * n, where=[0, 1], check=True)
175
+ assert qu.fidelity(rho, sigma) == pytest.approx(1.0)
176
+
177
+
178
+ class TestProjector:
179
+ def test_simple(self):
180
+ Z = qu.pauli("Z")
181
+ P = qu.projector(Z & Z)
182
+ uu = qu.dop(qu.up()) & qu.dop(qu.up())
183
+ dd = qu.dop(qu.down()) & qu.dop(qu.down())
184
+ assert_allclose(P, uu + dd)
185
+ assert qu.expec(P, qu.bell_state("phi+")) == pytest.approx(1.0)
186
+ assert qu.expec(P, qu.bell_state("psi+")) == pytest.approx(0.0)
187
+
188
+
189
+ class TestMeasure:
190
+ def test_pure(self):
191
+ psi = qu.bell_state("psi-")
192
+ IZ = qu.pauli("I") & qu.pauli("Z")
193
+ ZI = qu.pauli("Z") & qu.pauli("I")
194
+ res, psi_after = qu.measure(psi, IZ)
195
+ # normalized
196
+ assert qu.expectation(psi_after, psi_after) == pytest.approx(1.0)
197
+ # anticorrelated
198
+ assert qu.expectation(psi_after, IZ) == pytest.approx(res)
199
+ assert qu.expectation(psi_after, ZI) == pytest.approx(-res)
200
+ assert isinstance(psi_after, qu.qarray)
201
+
202
+ def test_bigger(self):
203
+ psi = qu.rand_ket(2**5)
204
+ assert np.sum(abs(psi) < 1e-12) == 0
205
+ A = qu.kronpow(qu.pauli("Z"), 5)
206
+ res, psi_after = qu.measure(psi, A, eigenvalue=-1.0)
207
+ # should have projected to half subspace
208
+ assert np.sum(abs(psi_after) < 1e-12) == 2**4
209
+ assert res == -1.0
210
+
211
+ def test_mixed(self):
212
+ rho = qu.dop(qu.bell_state("psi-"))
213
+ IZ = qu.pauli("I") & qu.pauli("Z")
214
+ ZI = qu.pauli("Z") & qu.pauli("I")
215
+ res, rho_after = qu.measure(rho, IZ)
216
+ # normalized
217
+ assert qu.tr(rho_after) == pytest.approx(1.0)
218
+ # anticorrelated
219
+ assert qu.expectation(rho_after, IZ) == pytest.approx(res)
220
+ assert qu.expectation(rho_after, ZI) == pytest.approx(-res)
221
+ assert isinstance(rho_after, qu.qarray)
222
+
223
+
224
+ class TestSimulateCounts:
225
+ @pytest.mark.parametrize("qtype", ["ket", "dop"])
226
+ def test_ghz(self, qtype):
227
+ psi = qu.ghz_state(3, qtype=qtype)
228
+ results = qu.simulate_counts(psi, 1024)
229
+ assert len(results) == 2
230
+ assert "000" in results
231
+ assert "111" in results
232
+
233
+
234
+ class TestCPrint:
235
+ def test_basic(self):
236
+ psi = qu.ghz_state(2)
237
+ qu.cprint(psi)
238
+
239
+
240
+ class TestEntropy:
241
+ def test_entropy_pure(self):
242
+ a = qu.bell_state(1, qtype="dop")
243
+ assert_allclose(0.0, qu.entropy(a), atol=1e-12)
244
+
245
+ def test_entropy_mixed(self):
246
+ a = 0.5 * (
247
+ qu.bell_state(1, qtype="dop") + qu.bell_state(2, qtype="dop")
248
+ )
249
+ assert_allclose(1.0, qu.entropy(a), atol=1e-12)
250
+
251
+ @pytest.mark.parametrize(
252
+ "evals, e",
253
+ [
254
+ ([0, 1, 0, 0], 0),
255
+ ([0, 0.5, 0, 0.5], 1),
256
+ ([0.25, 0.25, 0.25, 0.25], 2),
257
+ ],
258
+ )
259
+ def test_list(self, evals, e):
260
+ assert_allclose(qu.entropy(evals), e)
261
+
262
+ @pytest.mark.parametrize(
263
+ "evals, e",
264
+ [
265
+ ([0, 1, 0, 0], 0),
266
+ ([0, 0.5, 0, 0.5], 1),
267
+ ([0.25, 0.25, 0.25, 0.25], 2),
268
+ ],
269
+ )
270
+ def test_1darray(self, evals, e):
271
+ assert_allclose(qu.entropy(np.asarray(evals)), e)
272
+
273
+ @pytest.mark.parametrize("m", [1, 2, 3])
274
+ def test_rank(self, m):
275
+ k = qu.rand_ket(2**4)
276
+ pab = qu.ptr(k, [2, 2, 2, 2], range(m))
277
+ ef = qu.entropy(pab)
278
+ er = qu.entropy(pab, rank=2**m)
279
+ assert_allclose(ef, er)
280
+
281
+ def test_entropy_subsystem(self):
282
+ p = qu.rand_ket(2**9)
283
+ # exact
284
+ e1 = qu.entropy_subsys(p, (2**5, 2**4), 0, approx_thresh=1e30)
285
+ # approx
286
+ e2 = qu.entropy_subsys(p, (2**5, 2**4), 0, approx_thresh=1)
287
+ assert e1 != e2
288
+ assert_allclose(e1, e2, rtol=0.2)
289
+
290
+ assert (
291
+ qu.entropy_subsys(p, (2**5, 2**4), [0, 1], approx_thresh=1) == 0.0
292
+ )
293
+
294
+
295
+ class TestMutualInformation:
296
+ def test_mutual_information_pure(self):
297
+ a = qu.bell_state(0)
298
+ assert_allclose(qu.mutual_information(a), 2.0)
299
+ a = qu.rand_product_state(2)
300
+ assert_allclose(qu.mutual_information(a), 0.0, atol=1e-12)
301
+
302
+ def test_mutual_information_pure_sub(self):
303
+ a = qu.up() & qu.bell_state(1)
304
+ ixy = qu.mutual_information(a, [2, 2, 2], 0, 1)
305
+ assert_allclose(0.0, ixy, atol=1e-12)
306
+ ixy = qu.mutual_information(a, [2, 2, 2], 0, 2)
307
+ assert_allclose(0.0, ixy, atol=1e-12)
308
+ ixy = qu.mutual_information(a, [2, 2, 2], 2, 1)
309
+ assert_allclose(2.0, ixy, atol=1e-12)
310
+
311
+ @pytest.mark.parametrize("inds", [(0, 1), (1, 2), (0, 2)])
312
+ def test_mixed_sub(self, inds):
313
+ a = qu.rand_rho(2**3)
314
+ rho_ab = qu.ptr(a, [2, 2, 2], inds)
315
+ ixy = qu.mutual_information(rho_ab, (2, 2))
316
+ assert 0 <= ixy <= 2.0
317
+
318
+ def test_mutinf_interleave(self):
319
+ p = qu.dop(qu.singlet() & qu.singlet())
320
+ ixy = qu.mutual_information(p, [2] * 4, sysa=(0, 2))
321
+ assert_allclose(ixy, 4)
322
+
323
+ def test_mutinf_interleave_pure(self):
324
+ p = qu.singlet() & qu.singlet()
325
+ ixy = qu.mutual_information(p, [2] * 4, sysa=(0, 2))
326
+ assert_allclose(ixy, 4)
327
+
328
+ def test_mutinf_subsys(self):
329
+ p = qu.rand_ket(2**9)
330
+ dims = (2**3, 2**2, 2**4)
331
+ # exact
332
+ rho_ab = qu.ptr(p, dims, [0, 2])
333
+ mi0 = qu.mutual_information(rho_ab, [8, 16])
334
+ mi1 = qu.mutinf_subsys(p, dims, sysa=0, sysb=2, approx_thresh=1e30)
335
+ assert_allclose(mi1, mi0)
336
+ # approx
337
+ mi2 = qu.mutinf_subsys(p, dims, sysa=0, sysb=2, approx_thresh=1)
338
+ assert_allclose(mi1, mi2, rtol=0.1)
339
+
340
+ def test_mutinf_subsys_pure(self):
341
+ p = qu.rand_ket(2**7)
342
+ dims = (2**3, 2**4)
343
+ # exact
344
+ mi0 = qu.mutual_information(p, dims, sysa=0)
345
+ mi1 = qu.mutinf_subsys(p, dims, sysa=0, sysb=1, approx_thresh=1e30)
346
+ assert_allclose(mi1, mi0)
347
+ # approx
348
+ mi2 = qu.mutinf_subsys(
349
+ p, dims, sysa=0, sysb=1, approx_thresh=1, tol=5e-3
350
+ )
351
+ assert_allclose(mi1, mi2, rtol=0.1)
352
+
353
+
354
+ class TestSchmidtGap:
355
+ def test_bell_state(self):
356
+ p = qu.bell_state("psi-")
357
+ assert_allclose(qu.schmidt_gap(p, [2, 2], 0), 0.0)
358
+ p = qu.up() & qu.down()
359
+ assert_allclose(qu.schmidt_gap(p, [2, 2], 0), 1.0)
360
+ p = qu.rand_ket(2**3)
361
+ assert 0 < qu.schmidt_gap(p, [2] * 3, sysa=[0, 1]) < 1.0
362
+
363
+
364
+ class TestPartialTranspose:
365
+ def test_partial_transpose(self):
366
+ a = qu.bell_state(0, qtype="dop")
367
+ b = qu.partial_transpose(a)
368
+ assert isinstance(b, qu.qarray)
369
+ assert_allclose(
370
+ b,
371
+ np.array(
372
+ [
373
+ [0, 0, 0, -0.5],
374
+ [0, 0.5, 0, 0],
375
+ [0, 0, 0.5, 0],
376
+ [-0.5, 0, 0, 0],
377
+ ]
378
+ ),
379
+ )
380
+
381
+ def test_tr_sqrt_rank(self):
382
+ psi = qu.rand_ket(2**5)
383
+ rhoa = psi.ptr([2] * 5, range(4))
384
+ assert_allclose(qu.tr_sqrt(rhoa), qu.tr_sqrt(rhoa, rank=2))
385
+
386
+
387
+ class TestNegativity:
388
+ @pytest.mark.parametrize("bs", ["psi-", "phi-", "psi+", "phi+"])
389
+ @pytest.mark.parametrize("qtype", ["ket", "dop"])
390
+ def test_simple(self, qtype, bs):
391
+ p = qu.bell_state(bs, qtype=qtype)
392
+ assert qu.negativity(p) > 0.5 - 1e-14
393
+
394
+ def test_subsystem(self):
395
+ p = qu.singlet_pairs(4)
396
+ rhoab = p.ptr([2, 2, 2, 2], [0, 1])
397
+ assert qu.negativity(rhoab, [2] * 2) > 0.5 - 1e-14
398
+ rhoab = p.ptr([2, 2, 2, 2], [1, 2])
399
+ assert qu.negativity(rhoab, [2] * 2) < 1e-14
400
+ rhoab = p.ptr([2, 2, 2, 2], [2, 3])
401
+ assert qu.negativity(rhoab, [2] * 2) > 0.5 - 1e-14
402
+
403
+
404
+ class TestLogarithmicNegativity:
405
+ @pytest.mark.parametrize("bs", ["psi-", "phi-", "psi+", "phi+"])
406
+ @pytest.mark.parametrize("qtype", ["ket", "dop"])
407
+ def test_bell_states(self, qtype, bs):
408
+ p = qu.bell_state(bs, qtype=qtype)
409
+ assert qu.logneg(p) > 1.0 - 1e-14
410
+
411
+ def test_subsystem(self):
412
+ p = qu.singlet_pairs(4)
413
+ rhoab = p.ptr([2, 2, 2, 2], [0, 1])
414
+ assert qu.logneg(rhoab, [2] * 2) > 1 - 1e-14
415
+ rhoab = p.ptr([2, 2, 2, 2], [1, 2])
416
+ assert qu.logneg(rhoab, [2] * 2) < 1e-14
417
+ rhoab = p.ptr([2, 2, 2, 2], [2, 3])
418
+ assert qu.logneg(rhoab, [2] * 2) > 1 - 1e-14
419
+
420
+ def test_interleaving(self):
421
+ p = qu.permute(qu.singlet() & qu.singlet(), [2, 2, 2, 2], [0, 2, 1, 3])
422
+ assert qu.logneg(p, [2] * 4, sysa=[0, 3]) > 2 - 1e-13
423
+
424
+ def test_logneg_subsys(self):
425
+ p = qu.rand_ket(2 ** (2 + 3 + 1 + 2))
426
+ dims = (2**2, 2**3, 2**1, 2**2)
427
+ sysa = [0, 3]
428
+ sysb = 1
429
+ # exact 1
430
+ ln0 = qu.logneg(qu.ptr(p, dims, [0, 1, 3]), [4, 8, 4], [0, 2])
431
+ # exact 2
432
+ ln1 = qu.logneg_subsys(p, dims, sysa, sysb, approx_thresh=1e30)
433
+ assert_allclose(ln0, ln1)
434
+ # approx
435
+ ln2 = qu.logneg_subsys(p, dims, sysa, sysb, approx_thresh=1)
436
+ assert ln1 != ln2
437
+ assert_allclose(ln1, ln2, rtol=5e-2)
438
+
439
+ def test_logneg_subsys_pure(self):
440
+ p = qu.rand_ket(2 ** (3 + 4))
441
+ dims = (2**3, 2**4)
442
+ sysa = 0
443
+ sysb = 1
444
+ # exact 1
445
+ ln0 = qu.logneg(p, dims, 0)
446
+ # exact 2
447
+ ln1 = qu.logneg_subsys(p, dims, sysa, sysb, approx_thresh=1e30)
448
+ assert_allclose(ln0, ln1)
449
+ # approx
450
+ ln2 = qu.logneg_subsys(p, dims, sysa, sysb, approx_thresh=1, tol=5e-3)
451
+ assert ln1 != ln2
452
+ assert_allclose(ln1, ln2, rtol=1e-1)
453
+
454
+ def test_logneg_subsys_pure_should_swap_subsys(self):
455
+ p = qu.rand_ket(2 ** (5 + 2))
456
+ dims = (2**5, 2**2)
457
+ sysa = 0
458
+ sysb = 1
459
+ # exact 1
460
+ ln0 = qu.logneg(p, dims, 0)
461
+ # exact 2
462
+ ln1 = qu.logneg_subsys(p, dims, sysa, sysb, approx_thresh=1e30)
463
+ assert_allclose(ln0, ln1)
464
+ # approx
465
+ ln2 = qu.logneg_subsys(p, dims, sysa, sysb, approx_thresh=1, tol=0.005)
466
+ assert ln1 != ln2
467
+ assert_allclose(ln1, ln2, rtol=0.2)
468
+
469
+
470
+ class TestConcurrence:
471
+ @pytest.mark.parametrize("bs", ["psi-", "phi-", "psi+", "phi+"])
472
+ @pytest.mark.parametrize("qtype", ["ket", "dop"])
473
+ def test_bell_states(self, qtype, bs):
474
+ p = qu.bell_state(bs, qtype=qtype)
475
+ assert qu.concurrence(p) > 1.0 - 1e-14
476
+
477
+ def test_subsystem(self):
478
+ p = qu.rand_rho(2**4)
479
+ e = qu.concurrence(p, [2, 2, 2, 2], 1, 2)
480
+ assert 0 <= e <= 1
481
+
482
+
483
+ class TestQuantumDiscord:
484
+ def test_owci(self):
485
+ a = qu.qu([1, 0], qtype="op")
486
+ b = qu.qu([0, 1], qtype="op")
487
+ for _ in (0, 1, 2, 3):
488
+ p = qu.rand_product_state(2)
489
+ ci = qu.one_way_classical_information(p @ p.H, [a, b])
490
+ assert_allclose(ci, 0.0, atol=1e-12)
491
+ for i in (0, 1, 2, 3):
492
+ p = qu.bell_state(i)
493
+ ci = qu.one_way_classical_information(p @ p.H, [a, b])
494
+ assert_allclose(ci, 1.0, atol=1e-12)
495
+
496
+ def test_quantum_discord_sep(self):
497
+ for _ in range(10):
498
+ p = qu.rand_product_state(2)
499
+ p = p @ p.H
500
+ qd = qu.quantum_discord(p)
501
+ assert_allclose(0.0, qd, atol=1e-12)
502
+
503
+ def test_quantum_discord_pure(self):
504
+ for _ in range(10):
505
+ p = qu.rand_ket(4)
506
+ p = p @ p.H
507
+ iab = qu.mutual_information(p)
508
+ qd = qu.quantum_discord(p)
509
+ assert_allclose(iab / 2, qd)
510
+
511
+ def test_quantum_discord_mixed(self):
512
+ for _ in range(10):
513
+ p = qu.rand_mix(4)
514
+ p = p @ p.H
515
+ qd = qu.quantum_discord(p)
516
+ assert 0 <= qd and qd <= 1
517
+
518
+ def test_auto_trace_out(self):
519
+ p = qu.rand_rho(2**3)
520
+ qd = qu.quantum_discord(p, [2, 2, 2], 0, 2)
521
+ assert 0 <= qd and qd <= 1
522
+
523
+ @pytest.mark.parametrize("seed", range(10))
524
+ def test_qu_discord_diagonal(self, seed):
525
+ rng = np.random.RandomState(seed)
526
+ p = rng.random(size=4)
527
+ p /= np.sum(p)
528
+ rho = np.diag(p)
529
+ assert qu.quantum_discord(rho) < 1e-10
530
+
531
+
532
+ class TestTraceDistance:
533
+ def test_types(self, k1, k2):
534
+ td1 = qu.trace_distance(k1, k2)
535
+ td2 = qu.trace_distance(qu.dop(k1), k2)
536
+ td3 = qu.trace_distance(k1, qu.dop(k2))
537
+ td4 = qu.trace_distance(qu.dop(k1), qu.dop(k2))
538
+ assert_allclose([td1] * 3, [td2, td3, td4])
539
+
540
+ def test_same(self, p1):
541
+ assert abs(qu.trace_distance(p1, p1)) < 1e-14
542
+
543
+ @pytest.mark.parametrize("uqtype", ["ket", "dop"])
544
+ @pytest.mark.parametrize("dqtype", ["ket", "dop"])
545
+ def test_distinguishable(self, uqtype, dqtype):
546
+ assert (
547
+ qu.trace_distance(qu.up(qtype=uqtype), qu.down(qtype=dqtype))
548
+ > 1 - 1e-10
549
+ )
550
+
551
+
552
+ class TestDecomp:
553
+ @pytest.mark.parametrize("qtype", ["ket", "dop"])
554
+ def test_pauli_decomp_singlet(self, qtype):
555
+ p = qu.singlet(qtype=qtype)
556
+ names_cffs = qu.pauli_decomp(p, mode="cp")
557
+ assert_allclose(names_cffs["II"], 0.25)
558
+ assert_allclose(names_cffs["ZZ"], -0.25)
559
+ assert_allclose(names_cffs["YY"], -0.25)
560
+ assert_allclose(names_cffs["ZZ"], -0.25)
561
+ for name in itertools.permutations("IXYZ", 2):
562
+ assert_allclose(names_cffs["".join(name)], 0.0)
563
+
564
+ def test_pauli_reconstruct(self):
565
+ p1 = qu.rand_rho(4)
566
+ names_cffs = qu.pauli_decomp(p1, mode="c")
567
+ pr = sum(
568
+ qu.kron(*(qu.pauli(s) for s in name)) * names_cffs["".join(name)]
569
+ for name in itertools.product("IXYZ", repeat=2)
570
+ )
571
+ assert_allclose(pr, p1)
572
+
573
+ @pytest.mark.parametrize(
574
+ "state, out",
575
+ [
576
+ (qu.up() & qu.down(), {0: 0.5, 1: 0.5, 2: 0, 3: 0}),
577
+ (qu.down() & qu.down(), {0: 0, 1: 0, 2: 0.5, 3: 0.5}),
578
+ (qu.singlet() & qu.singlet(), {"00": 1.0, "23": 0.0}),
579
+ ],
580
+ )
581
+ def test_bell_decomp(self, state, out):
582
+ names_cffs = qu.bell_decomp(state, mode="c")
583
+ for key in out:
584
+ assert_allclose(names_cffs[str(key)], out[key])
585
+
586
+
587
+ class TestCorrelation:
588
+ @pytest.mark.parametrize("pre_c", [False, True])
589
+ @pytest.mark.parametrize("p_sps", [True, False])
590
+ @pytest.mark.parametrize("op_sps", [True, False])
591
+ @pytest.mark.parametrize("dims", (None, [2, 2]))
592
+ def test_types(self, dims, op_sps, p_sps, pre_c):
593
+ p = qu.rand_rho(4, sparse=p_sps)
594
+ c = qu.correlation(
595
+ p,
596
+ qu.pauli("x", sparse=op_sps),
597
+ qu.pauli("z", sparse=op_sps),
598
+ 0,
599
+ 1,
600
+ dims=dims,
601
+ precomp_func=pre_c,
602
+ )
603
+ c = c(p) if pre_c else c
604
+ assert c >= -1.0
605
+ assert c <= 1.0
606
+
607
+ @pytest.mark.parametrize("pre_c", [False, True])
608
+ @pytest.mark.parametrize("qtype", ["ket", "dop"])
609
+ @pytest.mark.parametrize("s", ["x", "y", "z"])
610
+ def test_classically_no_correlated(self, s, qtype, pre_c):
611
+ p = qu.up(qtype=qtype) & qu.up(qtype=qtype)
612
+ c = qu.correlation(
613
+ p, qu.pauli(s), qu.pauli(s), 0, 1, precomp_func=pre_c
614
+ )
615
+ c = c(p) if pre_c else c
616
+ assert_allclose(c, 0.0)
617
+
618
+ @pytest.mark.parametrize("pre_c", [False, True])
619
+ @pytest.mark.parametrize("s, ct", [("x", 0), ("y", 0), ("z", 1)])
620
+ def test_classically_correlated(self, s, ct, pre_c):
621
+ p = 0.5 * (
622
+ (qu.up(qtype="dop") & qu.up(qtype="dop"))
623
+ + (qu.down(qtype="dop") & qu.down(qtype="dop"))
624
+ )
625
+ c = qu.correlation(
626
+ p, qu.pauli(s), qu.pauli(s), 0, 1, precomp_func=pre_c
627
+ )
628
+ c = c(p) if pre_c else c
629
+ assert_allclose(c, ct)
630
+
631
+ @pytest.mark.parametrize("pre_c", [False, True])
632
+ @pytest.mark.parametrize("s, ct", [("x", -1), ("y", -1), ("z", -1)])
633
+ def test_entangled(self, s, ct, pre_c):
634
+ p = qu.bell_state("psi-")
635
+ c = qu.correlation(
636
+ p, qu.pauli(s), qu.pauli(s), 0, 1, precomp_func=pre_c
637
+ )
638
+ c = c(p) if pre_c else c
639
+ assert_allclose(c, ct)
640
+
641
+ def test_reuse_precomp(self):
642
+ cfn = qu.correlation(
643
+ None,
644
+ qu.pauli("z"),
645
+ qu.pauli("z"),
646
+ 0,
647
+ 1,
648
+ dims=[2, 2],
649
+ precomp_func=True,
650
+ )
651
+ assert_allclose(cfn(qu.bell_state("psi-")), -1.0)
652
+ assert_allclose(cfn(qu.bell_state("phi+")), 1.0)
653
+
654
+ @pytest.mark.parametrize("pre_c", [False, True])
655
+ def test_pauli_correlations_sum_abs(self, pre_c):
656
+ p = qu.bell_state("psi-")
657
+ ct = qu.pauli_correlations(p, sum_abs=True, precomp_func=pre_c)
658
+ ct = ct(p) if pre_c else ct
659
+ assert_allclose(ct, 3.0)
660
+
661
+ @pytest.mark.parametrize("pre_c", [False, True])
662
+ def test_pauli_correlations_no_sum_abs(self, pre_c):
663
+ p = qu.bell_state("psi-")
664
+ ct = qu.pauli_correlations(p, sum_abs=False, precomp_func=pre_c)
665
+ assert_allclose(list(c(p) for c in ct) if pre_c else ct, (-1, -1, -1))
666
+
667
+
668
+ class TestEntCrossMatrix:
669
+ def test_bell_state(self):
670
+ p = qu.bell_state("phi+")
671
+ ecm = qu.ent_cross_matrix(p, ent_fn=qu.concurrence, calc_self_ent=True)
672
+ assert_allclose(ecm, [[1, 1], [1, 1]])
673
+
674
+ def test_bell_state_no_self_ent(self):
675
+ p = qu.bell_state("phi+")
676
+ ecm = qu.ent_cross_matrix(
677
+ p, ent_fn=qu.concurrence, calc_self_ent=False
678
+ )
679
+ assert_allclose(ecm, [[np.nan, 1], [1, np.nan]])
680
+
681
+ def test_block2(self):
682
+ p = qu.bell_state("phi+") & qu.bell_state("phi+")
683
+ ecm = qu.ent_cross_matrix(p, ent_fn=qu.logneg, sz_blc=2)
684
+ assert_allclose(ecm[1, 1], 0)
685
+ assert_allclose(ecm[0, 1], 0)
686
+ assert_allclose(ecm[1, 0], 0)
687
+
688
+ def test_block2_no_self_ent(self):
689
+ p = qu.bell_state("phi+") & qu.bell_state("phi+")
690
+ ecm = qu.ent_cross_matrix(
691
+ p, ent_fn=qu.logneg, calc_self_ent=False, sz_blc=2
692
+ )
693
+ assert_allclose(ecm[0, 1], 0)
694
+ assert_allclose(ecm[0, 0], np.nan)
695
+ assert_allclose(ecm[1, 0], 0)
696
+
697
+ def test_block2_upscale(self):
698
+ p = qu.bell_state("phi+") & qu.bell_state("phi+")
699
+ ecm = qu.ent_cross_matrix(
700
+ p, ent_fn=qu.logneg, calc_self_ent=False, sz_blc=2
701
+ )
702
+ assert ecm.shape == (2, 2)
703
+ ecm = qu.ent_cross_matrix(
704
+ p, ent_fn=qu.logneg, calc_self_ent=False, sz_blc=2, upscale=True
705
+ )
706
+ assert ecm.shape == (4, 4)
707
+
708
+
709
+ class TestEntCrossMatrixBlocked:
710
+ @pytest.mark.parametrize("sz_p", [2**2 for i in [2, 3, 4, 5, 6, 9, 12]])
711
+ @pytest.mark.parametrize("sz_blc", [1, 2, 3, 4, 5])
712
+ @pytest.mark.parametrize("calc_self_ent", [True, False])
713
+ def test_shapes_and_blocks(self, sz_blc, sz_p, calc_self_ent):
714
+ if sz_p // sz_blc > 0:
715
+ p = qu.rand_rho(2**sz_p)
716
+ n = sz_p // sz_blc
717
+ ecm = qu.ent_cross_matrix(p, sz_blc, calc_self_ent=calc_self_ent)
718
+ assert ecm.shape[0] == n
719
+ if not calc_self_ent:
720
+ assert_allclose(np.diag(ecm), [np.nan] * n, equal_nan=True)
721
+
722
+
723
+ class TestQID:
724
+ @pytest.mark.parametrize("bs", [0, 1, 2, 3])
725
+ @pytest.mark.parametrize("pre_c", [False, True])
726
+ def test_bell_state(self, bs, pre_c):
727
+ p = qu.bell_state(bs)
728
+ qids = qu.qid(p, dims=[2, 2], inds=[0, 1], precomp_func=pre_c)
729
+ assert_allclose(qids(p) if pre_c else qids, [3, 3])
730
+
731
+ @pytest.mark.parametrize("pre_c", [False, True])
732
+ def test_random_product_state(self, pre_c):
733
+ p = qu.rand_product_state(3)
734
+ qids = qu.qid(p, dims=[2, 2, 2], inds=[0, 1, 2], precomp_func=pre_c)
735
+ assert_allclose(qids(p) if pre_c else qids, [2, 2, 2])
736
+
737
+
738
+ class TestIsDegenerate:
739
+ def test_known_degenerate(self):
740
+ h = qu.ham_heis(2)
741
+ assert qu.is_degenerate(h) == 2
742
+
743
+ def test_known_nondegen(self):
744
+ h = qu.ham_heis(2, b=0.3)
745
+ assert qu.is_degenerate(h) == 0
746
+
747
+ def test_supply_list(self):
748
+ evals = [0, 1, 2, 2.0, 3]
749
+ assert qu.is_degenerate(evals)
750
+
751
+ def test_tol(self):
752
+ evals = [0, 1, 1.001, 3, 4, 5, 6, 7, 8, 9]
753
+ assert not qu.is_degenerate(evals)
754
+ assert qu.is_degenerate(evals, tol=1e-2)
755
+
756
+
757
+ class TestPageEntropy:
758
+ def test_known_qubit_qubit(self):
759
+ assert abs(qu.page_entropy(2, 4) - 0.4808983469629878) < 1e-12
760
+
761
+ def test_large_m_approx(self):
762
+ pe = qu.page_entropy(2**10, 2**20)
763
+ ae = 0.5 * (20 - math.log2(math.e))
764
+
765
+ assert abs(pe - ae) < 1e-5
766
+
767
+ def test_bigger_than_half(self):
768
+ assert_allclose(qu.page_entropy(4, 24), qu.page_entropy(6, 24))
769
+
770
+
771
+ class TestIsEigenvector:
772
+ def test_dense_true(self):
773
+ a = qu.rand_herm(10)
774
+ v = qu.eigvecsh(a)
775
+ for i in range(10):
776
+ assert qu.is_eigenvector(v[:, [i]], a)
777
+
778
+ def test_dense_false(self):
779
+ a = qu.rand_herm(10)
780
+ v = qu.rand_ket(10)
781
+ assert not qu.is_eigenvector(v, a)
782
+
783
+ def test_sparse(self):
784
+ a = qu.rand_herm(10, sparse=True, density=0.9)
785
+ vt = qu.eigvecsh(a, sigma=0, k=1)
786
+ assert qu.is_eigenvector(vt, a)
787
+ vf = qu.rand_ket(10)
788
+ assert not qu.is_eigenvector(vf, a)