tensorcircuit-nightly 1.2.0.dev20250326__py3-none-any.whl → 1.4.0.dev20251128__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.

Potentially problematic release.


This version of tensorcircuit-nightly might be problematic. Click here for more details.

Files changed (77) hide show
  1. tensorcircuit/__init__.py +5 -1
  2. tensorcircuit/abstractcircuit.py +4 -0
  3. tensorcircuit/analogcircuit.py +413 -0
  4. tensorcircuit/applications/layers.py +1 -1
  5. tensorcircuit/applications/van.py +1 -1
  6. tensorcircuit/backends/abstract_backend.py +312 -5
  7. tensorcircuit/backends/cupy_backend.py +3 -1
  8. tensorcircuit/backends/jax_backend.py +100 -4
  9. tensorcircuit/backends/jax_ops.py +108 -0
  10. tensorcircuit/backends/numpy_backend.py +49 -3
  11. tensorcircuit/backends/pytorch_backend.py +92 -3
  12. tensorcircuit/backends/tensorflow_backend.py +102 -3
  13. tensorcircuit/basecircuit.py +157 -98
  14. tensorcircuit/circuit.py +115 -57
  15. tensorcircuit/cloud/local.py +1 -1
  16. tensorcircuit/cloud/quafu_provider.py +1 -1
  17. tensorcircuit/cloud/tencent.py +1 -1
  18. tensorcircuit/compiler/simple_compiler.py +2 -2
  19. tensorcircuit/cons.py +105 -23
  20. tensorcircuit/densitymatrix.py +16 -11
  21. tensorcircuit/experimental.py +733 -153
  22. tensorcircuit/fgs.py +254 -73
  23. tensorcircuit/gates.py +66 -22
  24. tensorcircuit/interfaces/jax.py +5 -3
  25. tensorcircuit/interfaces/tensortrans.py +6 -2
  26. tensorcircuit/interfaces/torch.py +14 -4
  27. tensorcircuit/keras.py +3 -3
  28. tensorcircuit/mpscircuit.py +154 -65
  29. tensorcircuit/quantum.py +698 -134
  30. tensorcircuit/quditcircuit.py +733 -0
  31. tensorcircuit/quditgates.py +618 -0
  32. tensorcircuit/results/counts.py +131 -18
  33. tensorcircuit/results/readout_mitigation.py +4 -1
  34. tensorcircuit/shadows.py +1 -1
  35. tensorcircuit/simplify.py +3 -1
  36. tensorcircuit/stabilizercircuit.py +29 -17
  37. tensorcircuit/templates/__init__.py +2 -0
  38. tensorcircuit/templates/blocks.py +2 -2
  39. tensorcircuit/templates/hamiltonians.py +174 -0
  40. tensorcircuit/templates/lattice.py +1789 -0
  41. tensorcircuit/timeevol.py +896 -0
  42. tensorcircuit/translation.py +10 -3
  43. tensorcircuit/utils.py +7 -0
  44. {tensorcircuit_nightly-1.2.0.dev20250326.dist-info → tensorcircuit_nightly-1.4.0.dev20251128.dist-info}/METADATA +66 -29
  45. tensorcircuit_nightly-1.4.0.dev20251128.dist-info/RECORD +96 -0
  46. {tensorcircuit_nightly-1.2.0.dev20250326.dist-info → tensorcircuit_nightly-1.4.0.dev20251128.dist-info}/WHEEL +1 -1
  47. {tensorcircuit_nightly-1.2.0.dev20250326.dist-info → tensorcircuit_nightly-1.4.0.dev20251128.dist-info}/top_level.txt +0 -1
  48. tensorcircuit_nightly-1.2.0.dev20250326.dist-info/RECORD +0 -118
  49. tests/__init__.py +0 -0
  50. tests/conftest.py +0 -67
  51. tests/test_backends.py +0 -1035
  52. tests/test_calibrating.py +0 -149
  53. tests/test_channels.py +0 -409
  54. tests/test_circuit.py +0 -1699
  55. tests/test_cloud.py +0 -219
  56. tests/test_compiler.py +0 -147
  57. tests/test_dmcircuit.py +0 -555
  58. tests/test_ensemble.py +0 -72
  59. tests/test_fgs.py +0 -310
  60. tests/test_gates.py +0 -156
  61. tests/test_interfaces.py +0 -562
  62. tests/test_keras.py +0 -160
  63. tests/test_miscs.py +0 -282
  64. tests/test_mpscircuit.py +0 -341
  65. tests/test_noisemodel.py +0 -156
  66. tests/test_qaoa.py +0 -86
  67. tests/test_qem.py +0 -152
  68. tests/test_quantum.py +0 -549
  69. tests/test_quantum_attr.py +0 -42
  70. tests/test_results.py +0 -380
  71. tests/test_shadows.py +0 -160
  72. tests/test_simplify.py +0 -46
  73. tests/test_stabilizer.py +0 -217
  74. tests/test_templates.py +0 -218
  75. tests/test_torchnn.py +0 -99
  76. tests/test_van.py +0 -102
  77. {tensorcircuit_nightly-1.2.0.dev20250326.dist-info → tensorcircuit_nightly-1.4.0.dev20251128.dist-info}/licenses/LICENSE +0 -0
tests/test_results.py DELETED
@@ -1,380 +0,0 @@
1
- import pytest
2
- import numpy as np
3
-
4
-
5
- import tensorcircuit as tc
6
- from tensorcircuit.results import counts
7
- from tensorcircuit.results.readout_mitigation import ReadoutMit
8
-
9
- d = {"000": 2, "101": 3, "100": 4}
10
-
11
-
12
- def test_marginal_count():
13
- assert counts.marginal_count(d, [1, 2])["00"] == 6
14
- assert counts.marginal_count(d, [1])["0"] == 9
15
- assert counts.marginal_count(d, [2, 1, 0])["001"] == 4
16
-
17
-
18
- def test_merge_count():
19
-
20
- c1 = {"00": 10, "01": 20, "11": 30}
21
- c2 = {"00": 5, "10": 15, "11": 25}
22
- c3 = {"01": 10, "10": 20}
23
-
24
- # Test merging two count dicts
25
- merged = counts.merge_count(c1, c2)
26
- assert merged["00"] == 15
27
- assert merged["01"] == 20
28
- assert merged["10"] == 15
29
- assert merged["11"] == 55
30
-
31
- # Test merging three count dicts
32
- merged = counts.merge_count(c1, c2, c3)
33
- assert merged["00"] == 15
34
- assert merged["01"] == 30
35
- assert merged["10"] == 35
36
- assert merged["11"] == 55
37
-
38
- # Test merging single count dict
39
- merged = counts.merge_count(c1)
40
- assert merged == c1
41
-
42
- # Test merging empty dicts
43
- merged = counts.merge_count({}, {})
44
- assert merged == {}
45
-
46
- # Test merging empty with non-empty
47
- merged = counts.merge_count({}, c1)
48
- assert merged == c1
49
-
50
-
51
- def test_count2vec():
52
- assert counts.vec2count(counts.count2vec(d, normalization=False), prune=True) == d
53
-
54
-
55
- def test_kl():
56
- a = {"00": 512, "11": 512}
57
- assert counts.kl_divergence(a, a) == 0
58
-
59
-
60
- def test_expectation():
61
- assert counts.expectation(d, [0, 1]) == -5 / 9
62
- assert counts.expectation(d, None, [[1, -1], [1, 0], [1, 1]]) == -5 / 9
63
-
64
-
65
- def test_plot_histogram():
66
- d = {"00": 10, "01": 2, "11": 8}
67
- d1 = {"00": 11, "11": 9}
68
- print(counts.plot_histogram([d, d1]))
69
-
70
-
71
- def test_readout():
72
- nqubit = 4
73
- shots = 4096
74
- c = tc.Circuit(nqubit)
75
- c.H(0)
76
- c.cnot(0, 1)
77
- c.x(3)
78
-
79
- idea_count = c.sample(batch=shots, allow_state=True, format="count_dict_bin")
80
- raw_count = run([c], shots)[0]
81
-
82
- # test "inverse", "constrained_least_square", "M3"
83
- mit = ReadoutMit(execute=run)
84
- mit.cals_from_system([0, 1, 2, 3, 6], shots=10000, method="local")
85
-
86
- mit_count1 = mit.apply_correction(
87
- raw_count, [1, 3, 2], method="inverse"
88
- ) # direct(Max2),iterative(Max3), inverse,square
89
- mit_count2 = mit.apply_correction(
90
- raw_count, [1, 3, 2], method="constrained_least_square"
91
- )
92
- idea_count2 = counts.marginal_count(idea_count, [1, 3, 2])
93
-
94
- assert counts.kl_divergence(idea_count2, mit_count1) < 0.05
95
- assert counts.kl_divergence(idea_count2, mit_count2) < 0.05
96
-
97
- # test "global" and "equal"
98
- mit = ReadoutMit(execute=run)
99
- mit.cals_from_system([0, 1, 2, 3], shots=100000, method="global")
100
- A_global = mit.get_matrix([1, 3, 2])
101
- mit_countg = mit.apply_correction(
102
- raw_count, [1, 3, 2], method="constrained_least_square"
103
- )
104
-
105
- mit = ReadoutMit(execute=run)
106
- mit.cals_from_system([0, 1, 2, 3], shots=100000, method="local")
107
- A_local = mit.get_matrix([1, 3, 2])
108
- mit_countl = mit.apply_correction(
109
- raw_count, [1, 3, 2], method="constrained_least_square"
110
- )
111
-
112
- np.testing.assert_allclose(A_global, A_local, atol=1e-2)
113
- assert counts.kl_divergence(mit_countg, mit_countl) < 0.05
114
-
115
-
116
- def test_readout_masks():
117
- mit = ReadoutMit(execute=run)
118
- mit.cals_from_system(
119
- [1, 2, 4], shots=8192, method="local", masks=["01010", "10101", "11111"]
120
- )
121
- np.testing.assert_allclose(
122
- mit.single_qubit_cals[1][0, 0], 0.02 * np.sin(2) + 0.978, atol=1e-2
123
- )
124
-
125
-
126
- def test_readout_expv():
127
- nqubit = 4
128
- c = tc.Circuit(nqubit)
129
- c.H(0)
130
- c.cnot(0, 1)
131
- c.x(3)
132
-
133
- idea_count = c.sample(batch=100000, allow_state=True, format="count_dict_bin")
134
- raw_count = run([c], 100000)[0]
135
-
136
- cal_qubits = [0, 1, 2, 3]
137
- use_qubits = [0, 1]
138
-
139
- # idea_value = c.expectation_ps(z=[0,1])
140
- idea_count2 = counts.marginal_count(idea_count, use_qubits)
141
- idea_value = counts.expectation(idea_count2, z=[0, 1])
142
-
143
- mit = ReadoutMit(execute=run)
144
- mit.cals_from_system(cal_qubits, shots=100000, method="local")
145
- mit_count = mit.apply_correction(raw_count, use_qubits, method="inverse")
146
- mit_value = counts.expectation(mit_count, z=[0, 1])
147
-
148
- mit = ReadoutMit(execute=run)
149
- mit.cals_from_system(cal_qubits, shots=100000, method="local")
150
- mit_value1 = mit.expectation(raw_count, z=[0, 1], method="inverse")
151
-
152
- mit = ReadoutMit(execute=run)
153
- mit.cals_from_system(cal_qubits, shots=100000, method="global")
154
- mit_value2 = mit.expectation(raw_count, z=[0, 1], method="square")
155
-
156
- np.testing.assert_allclose(idea_value, mit_value, atol=1e-2)
157
- np.testing.assert_allclose(idea_value, mit_value1, atol=1e-2)
158
- np.testing.assert_allclose(idea_value, mit_value2, atol=1e-2)
159
-
160
- # test large size
161
- nqubit = 20
162
- c = tc.Circuit(nqubit)
163
- c.H(0)
164
- for i in range(nqubit - 1):
165
- c.cnot(i, i + 1)
166
- c.rx(1, theta=0.9)
167
-
168
- idea_count = c.sample(batch=100000, allow_state=True, format="count_dict_bin")
169
- raw_count = run([c], 100000)[0]
170
-
171
- cal_qubits = list(range(nqubit))
172
- use_qubits = list(range(nqubit))
173
-
174
- # idea_value = c.expectation_ps(z=[0,1])
175
- idea_count2 = counts.marginal_count(idea_count, use_qubits)
176
- idea_value = counts.expectation(idea_count2, z=list(range(nqubit)))
177
-
178
- mit = ReadoutMit(execute=run)
179
- mit.cals_from_system(cal_qubits, shots=100000, method="local")
180
- mit_value1 = mit.expectation(raw_count, z=list(range(nqubit)), method="inverse")
181
-
182
- np.testing.assert_allclose(idea_value, mit_value1, atol=1e-1)
183
-
184
-
185
- def test_M3():
186
- try:
187
- import mthree # pylint: disable=unused-import
188
- except ImportError:
189
- pytest.skip("****** No mthree, skipping test suit *******")
190
-
191
- nqubit = 20
192
- c = tc.Circuit(nqubit)
193
- c.H(0)
194
- for i in range(nqubit - 1):
195
- c.cnot(i, i + 1)
196
- c.rx(1, theta=0.9)
197
-
198
- idea_count = c.sample(batch=100000, allow_state=True, format="count_dict_bin")
199
- raw_count = run([c], 100000)[0]
200
-
201
- cal_qubits = list(range(nqubit))
202
- use_qubits = list(range(nqubit))
203
-
204
- idea_count2 = counts.marginal_count(idea_count, use_qubits)
205
- idea_value = counts.expectation(idea_count2, z=list(range(nqubit)))
206
-
207
- mit = ReadoutMit(execute=run)
208
- mit.cals_from_system(cal_qubits, shots=100000, method="local")
209
- mit_count = mit.apply_correction(raw_count, use_qubits, method="M3_auto")
210
- mit_value = counts.expectation(mit_count, z=list(range(nqubit)))
211
- np.testing.assert_allclose(idea_value, mit_value, atol=1e-1)
212
-
213
- nqubit = 4
214
- shots = 4096
215
- c = tc.Circuit(nqubit)
216
- c.H(0)
217
- c.cnot(0, 1)
218
- c.x(3)
219
-
220
- idea_count = c.sample(batch=shots, allow_state=True, format="count_dict_bin")
221
- raw_count = run([c], shots)[0]
222
-
223
- mit_count3 = mit.apply_correction(raw_count, [1, 3, 2], method="M3_direct")
224
- mit_count4 = mit.apply_correction(raw_count, [1, 3, 2], method="M3_iterative")
225
- idea_count2 = counts.marginal_count(idea_count, [1, 3, 2])
226
- assert counts.kl_divergence(idea_count2, mit_count3) < 0.05
227
- assert counts.kl_divergence(idea_count2, mit_count4) < 0.05
228
-
229
-
230
- def partial_sample(c, batch, readout_error=None):
231
- measure_index = []
232
- for inst in c._extra_qir:
233
- if inst["name"] == "measure":
234
- measure_index.append(inst["index"][0])
235
- if len(measure_index) == 0:
236
- measure_index = list(range(c._nqubits))
237
-
238
- ct = c.sample(
239
- allow_state=True,
240
- batch=batch,
241
- readout_error=readout_error,
242
- format="count_dict_bin",
243
- )
244
- return tc.results.counts.marginal_count(ct, measure_index)
245
-
246
-
247
- def run(cs, shots):
248
- # customized backend for mitigation test
249
- ts = []
250
- for c in cs:
251
- count = simulator(c, shots)
252
- ts.append(count)
253
- return ts
254
-
255
-
256
- def simulator(c, shots, logical_physical_mapping=None):
257
- # with readout_error noise
258
- nqubit = c._nqubits
259
- if logical_physical_mapping is None:
260
- logical_physical_mapping = {i: i for i in range(nqubit)}
261
-
262
- gg = []
263
- for i in range(200):
264
- gg.append(np.sin(i) * 0.02 + 0.978)
265
- # gg.append(0.98 - i * 0.01)
266
- readout_error = np.reshape(gg[0 : nqubit * 2], (nqubit, 2))
267
- mapped_readout_error = [[1, 1]] * nqubit
268
- for lq, phyq in logical_physical_mapping.items():
269
- mapped_readout_error[lq] = readout_error[phyq]
270
- return partial_sample(c, shots, mapped_readout_error)
271
-
272
-
273
- def test_mapping():
274
- nqubit = 15
275
- shots = 100000
276
- c = tc.Circuit(nqubit)
277
- c.H(4)
278
- c.cnot(4, 5)
279
- c.cnot(5, 6)
280
- c.cnot(6, 7)
281
- c.rx(4, theta=0.8)
282
- c.rx(7, theta=1.8)
283
- c.measure_instruction(4)
284
- c.measure_instruction(5)
285
- c.measure_instruction(6)
286
- c.measure_instruction(7)
287
-
288
- mit = ReadoutMit(execute=run)
289
- mit.cals_from_system(list(range(15)), shots=100000, method="local")
290
-
291
- show_qubits = [6, 7, 5]
292
-
293
- idea_count = c.sample(batch=shots, allow_state=True, format="count_dict_bin")
294
- idea_count1 = counts.marginal_count(idea_count, show_qubits)
295
-
296
- def miti_kl_mean(logical_physical_mapping):
297
- ls = []
298
- for _ in range(10):
299
- raw_count = simulator(c, shots, logical_physical_mapping)
300
- mit_count1 = mit.apply_correction(
301
- raw_count,
302
- qubits=show_qubits,
303
- positional_logical_mapping={1: 5, 0: 4, 2: 6, 3: 7},
304
- logical_physical_mapping=logical_physical_mapping,
305
- method="square",
306
- )
307
- ls.append(counts.kl_divergence(idea_count1, mit_count1))
308
- # print("std", np.std(listtt), np.mean(listtt)) # smaller error rate and larger shots, better mititation.
309
- np.testing.assert_allclose(np.mean(ls), 0.01, atol=1e-2)
310
-
311
- logical_physical_mapping = {4: 0, 6: 2, 7: 3, 5: 1}
312
- miti_kl_mean(logical_physical_mapping)
313
-
314
- logical_physical_mapping = {4: 4, 5: 5, 6: 6, 7: 7}
315
- miti_kl_mean(logical_physical_mapping)
316
-
317
- logical_physical_mapping = {4: 8, 5: 9, 6: 10, 7: 11}
318
- miti_kl_mean(logical_physical_mapping)
319
-
320
-
321
- def test_readout_expv_map():
322
- shots = 100000
323
- nqubit = 7
324
- c = tc.Circuit(nqubit)
325
- c.H(3)
326
- c.cnot(3, 4)
327
- c.cnot(4, 5)
328
- c.rx(3, theta=0.8)
329
- c.rx(4, theta=1.2)
330
- c.measure_instruction(3)
331
- c.measure_instruction(4)
332
- c.measure_instruction(5)
333
-
334
- idea_count = c.sample(batch=100000, allow_state=True, format="count_dict_bin")
335
- idea_value = counts.expectation(idea_count, z=[4, 5])
336
-
337
- # logical_physical_mapping = {3: 3, 4: 4, 5: 5}
338
- logical_physical_mapping = {3: 1, 5: 3, 4: 6}
339
- positional_logical_mapping = {1: 4, 0: 3, 2: 5}
340
-
341
- raw_count = simulator(c, shots, logical_physical_mapping)
342
-
343
- cal_qubits = list(range(nqubit))
344
-
345
- mit = ReadoutMit(execute=run)
346
- mit.cals_from_system(cal_qubits, shots=100000, method="local")
347
- mit_value1 = mit.expectation(
348
- raw_count,
349
- z=[4, 5],
350
- positional_logical_mapping=positional_logical_mapping,
351
- logical_physical_mapping=logical_physical_mapping,
352
- method="inverse",
353
- )
354
-
355
- mit_count = mit.apply_correction(
356
- raw_count,
357
- qubits=[3, 4, 5],
358
- positional_logical_mapping=positional_logical_mapping,
359
- logical_physical_mapping=logical_physical_mapping,
360
- method="inverse",
361
- )
362
- mit_value = counts.expectation(mit_count, z=[1, 2])
363
- # print("idea", idea_value)
364
- # print("mit", mit_value)
365
-
366
- mit = ReadoutMit(execute=run)
367
- mit.cals_from_system(cal_qubits, shots=100000, method="global")
368
- mit_value2 = mit.expectation(
369
- raw_count,
370
- z=[4, 5],
371
- positional_logical_mapping=positional_logical_mapping,
372
- logical_physical_mapping=logical_physical_mapping,
373
- method="inverse",
374
- )
375
-
376
- # print("mit1", mit_value1)
377
- # print("mit2", mit_value2)
378
- np.testing.assert_allclose(idea_value, mit_value, atol=3 * 1e-2)
379
- np.testing.assert_allclose(idea_value, mit_value1, atol=3 * 1e-2)
380
- np.testing.assert_allclose(idea_value, mit_value2, atol=3 * 1e-2)
tests/test_shadows.py DELETED
@@ -1,160 +0,0 @@
1
- import pytest
2
- from pytest_lazyfixture import lazy_fixture as lf
3
- import numpy as np
4
- import tensorcircuit as tc
5
- from tensorcircuit.shadows import (
6
- shadow_bound,
7
- shadow_snapshots,
8
- global_shadow_state,
9
- entropy_shadow,
10
- renyi_entropy_2,
11
- expectation_ps_shadow,
12
- global_shadow_state1,
13
- )
14
-
15
-
16
- @pytest.mark.parametrize("backend", [lf("tfb"), lf("jaxb")])
17
- def test_jit(backend):
18
- nq, repeat = 8, 5
19
- ps = [1, 0, 0, 0, 2, 0, 0, 0]
20
- sub = (1, 3, 6, 7)
21
- error = 0.1
22
- ns, k = shadow_bound(ps, error)
23
- ns //= repeat
24
-
25
- thetas = 2 * np.random.rand(2, nq) - 1
26
-
27
- c = tc.Circuit(nq)
28
- for i in range(nq):
29
- c.H(i)
30
- for i in range(2):
31
- for j in range(nq):
32
- c.cnot(j, (j + 1) % nq)
33
- for j in range(nq):
34
- c.rz(j, theta=thetas[i, j] * np.pi)
35
-
36
- psi = c.state()
37
- pauli_strings = tc.backend.convert_to_tensor(np.random.randint(1, 4, size=(ns, nq)))
38
- status = tc.backend.convert_to_tensor(np.random.rand(ns, repeat))
39
-
40
- def classical_shadow(psi, pauli_strings, status):
41
- lss_states = shadow_snapshots(psi, pauli_strings, status)
42
- expc = expectation_ps_shadow(lss_states, ps=ps, k=k)
43
- ent = entropy_shadow(lss_states, sub=sub, alpha=2)
44
- return expc, ent
45
-
46
- csjit = tc.backend.jit(classical_shadow)
47
-
48
- exact_expc = c.expectation_ps(ps=ps)
49
- exact_rdm = tc.quantum.reduced_density_matrix(psi, cut=[0, 2, 4, 5])
50
- exact_ent = tc.quantum.renyi_entropy(exact_rdm, k=2)
51
- expc, ent = csjit(psi, pauli_strings, status)
52
- expc = np.median(expc)
53
-
54
- np.testing.assert_allclose(expc, exact_expc, atol=error)
55
- np.testing.assert_allclose(ent, exact_ent, atol=5 * error)
56
-
57
-
58
- @pytest.mark.parametrize("backend", [lf("tfb"), lf("jaxb")])
59
- def test_state(backend):
60
- nq, ns = 2, 10000
61
-
62
- c = tc.Circuit(nq)
63
- c.H(0)
64
- c.cnot(0, 1)
65
-
66
- psi = c.state()
67
- bell_state = psi[:, None] @ psi[None, :]
68
-
69
- pauli_strings = tc.backend.convert_to_tensor(np.random.randint(1, 4, size=(ns, nq)))
70
- status = tc.backend.convert_to_tensor(np.random.rand(ns, 5))
71
- lss_states = shadow_snapshots(c.state(), pauli_strings, status)
72
- sdw_state = global_shadow_state(lss_states)
73
- sdw_state1 = global_shadow_state1(lss_states)
74
-
75
- np.testing.assert_allclose(sdw_state, bell_state, atol=0.1)
76
- np.testing.assert_allclose(sdw_state1, bell_state, atol=0.1)
77
-
78
-
79
- @pytest.mark.parametrize("backend", [lf("tfb"), lf("jaxb")])
80
- def test_ent(backend):
81
- nq, ns, repeat = 6, 1000, 500
82
-
83
- thetas = 2 * np.random.rand(2, nq) - 1
84
-
85
- c = tc.Circuit(nq)
86
- for i in range(nq):
87
- c.H(i)
88
- for i in range(2):
89
- for j in range(nq):
90
- c.cnot(j, (j + 1) % nq)
91
- for j in range(nq):
92
- c.rz(j, theta=thetas[i, j] * np.pi)
93
-
94
- sub = [1, 4]
95
- psi = c.state()
96
-
97
- pauli_strings = tc.backend.convert_to_tensor(np.random.randint(1, 4, size=(ns, nq)))
98
- status = tc.backend.convert_to_tensor(np.random.rand(ns, repeat))
99
- snapshots = shadow_snapshots(psi, pauli_strings, status, measurement_only=True)
100
-
101
- exact_rdm = tc.quantum.reduced_density_matrix(
102
- psi, cut=[i for i in range(nq) if i not in sub]
103
- )
104
- exact_ent = tc.quantum.renyi_entropy(exact_rdm, k=2)
105
- ent = entropy_shadow(snapshots, pauli_strings, sub, alpha=2)
106
- ent2 = renyi_entropy_2(snapshots, sub)
107
-
108
- np.testing.assert_allclose(ent, exact_ent, atol=0.1)
109
- np.testing.assert_allclose(ent2, exact_ent, atol=0.1)
110
-
111
-
112
- # @pytest.mark.parametrize("backend", [lf("tfb"), lf("jaxb")])
113
- # def test_expc(backend):
114
- # import pennylane as qml
115
- #
116
- # nq, ns, repeat = 6, 2000, 1000
117
- #
118
- # thetas = 2 * np.random.rand(2, nq) - 1
119
- #
120
- # c = tc.Circuit(nq)
121
- # for i in range(nq):
122
- # c.H(i)
123
- # for i in range(2):
124
- # for j in range(nq):
125
- # c.cnot(j, (j + 1) % nq)
126
- # for j in range(nq):
127
- # c.rz(j, theta=thetas[i, j] * np.pi)
128
- #
129
- # ps = [1, 0, 0, 0, 0, 3]
130
- # sub = [1, 4]
131
- # psi = c.state()
132
- #
133
- # pauli_strings = tc.backend.convert_to_tensor(np.random.randint(1, 4, size=(ns, nq)))
134
- # status = tc.backend.convert_to_tensor(np.random.rand(ns, repeat))
135
- # snapshots = shadow_snapshots(psi, pauli_strings, status, measurement_only=True)
136
- #
137
- # exact_expc = c.expectation_ps(ps=ps)
138
- # exact_rdm = tc.quantum.reduced_density_matrix(
139
- # psi, cut=[i for i in range(nq) if i not in sub]
140
- # )
141
- # exact_ent = tc.quantum.renyi_entropy(exact_rdm, k=2)
142
- # print(exact_expc, exact_ent)
143
- #
144
- # expc = np.median(expection_ps_shadow(snapshots, pauli_strings, ps=ps, k=9))
145
- # ent = entropy_shadow(snapshots, pauli_strings, sub, alpha=2)
146
- # ent2 = renyi_entropy_2(snapshots, sub)
147
- # print(expc, ent, ent2)
148
- #
149
- # pl_snapshots = np.asarray(snapshots).reshape(ns * repeat, nq)
150
- # pl_ps = np.tile(np.asarray(pauli_strings - 1)[:, None, :], (1, repeat, 1)).reshape(
151
- # ns * repeat, nq
152
- # )
153
- # shadow = qml.ClassicalShadow(pl_snapshots, pl_ps)
154
- # H = qml.PauliX(0) @ qml.PauliZ(5)
155
- # pl_expc = shadow.expval(H, k=9)
156
- # pl_ent = shadow.entropy(sub, alpha=2)
157
- # print(pl_expc, pl_ent)
158
- #
159
- # assert np.isclose(expc, pl_expc)
160
- # assert np.isclose(ent, pl_ent)
tests/test_simplify.py DELETED
@@ -1,46 +0,0 @@
1
- import os
2
- import sys
3
-
4
- thisfile = os.path.abspath(__file__)
5
- modulepath = os.path.dirname(os.path.dirname(thisfile))
6
-
7
- sys.path.insert(0, modulepath)
8
- import numpy as np
9
- import tensornetwork as tn
10
- from tensorcircuit import simplify
11
-
12
-
13
- def test_infer_shape():
14
- a = tn.Node(np.ones([2, 3, 5]))
15
- b = tn.Node(np.ones([3, 5, 7]))
16
- a[1] ^ b[0]
17
- a[2] ^ b[1]
18
- assert simplify.infer_new_shape(a, b) == ((2, 7), (2, 3, 5), (3, 5, 7))
19
-
20
-
21
- def test_rank_simplify():
22
- a = tn.Node(np.ones([2, 2]), name="a")
23
- b = tn.Node(np.ones([2, 2]), name="b")
24
- c = tn.Node(np.ones([2, 2, 2, 2]), name="c")
25
- d = tn.Node(np.ones([2, 2, 2, 2, 2, 2]), name="d")
26
- e = tn.Node(np.ones([2, 2]), name="e")
27
-
28
- a[1] ^ c[0]
29
- b[1] ^ c[1]
30
- c[2] ^ d[0]
31
- c[3] ^ d[1]
32
- d[4] ^ e[0]
33
-
34
- nodes = simplify._full_rank_simplify([a, b, c, d, e])
35
- assert nodes[0].shape == tuple([2 for _ in range(6)])
36
- assert len(nodes) == 1
37
-
38
- f = tn.Node(np.ones([2, 2]), name="f")
39
- g = tn.Node(np.ones([2, 2, 2, 2]), name="g")
40
- h = tn.Node(np.ones([2, 2, 2, 2]), name="h")
41
-
42
- f[1] ^ g[0]
43
- g[2] ^ h[1]
44
-
45
- nodes = simplify._full_rank_simplify([f, g, h])
46
- assert len(nodes) == 2