tensorcircuit-nightly 1.3.0.dev20250728__py3-none-any.whl → 1.4.0.dev20251103__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 (72) 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 +92 -3
  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 +123 -82
  14. tensorcircuit/circuit.py +67 -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 +1 -0
  20. tensorcircuit/densitymatrix.py +16 -11
  21. tensorcircuit/experimental.py +7 -152
  22. tensorcircuit/fgs.py +5 -6
  23. tensorcircuit/gates.py +66 -22
  24. tensorcircuit/keras.py +3 -3
  25. tensorcircuit/mpscircuit.py +109 -61
  26. tensorcircuit/quantum.py +697 -133
  27. tensorcircuit/quditcircuit.py +733 -0
  28. tensorcircuit/quditgates.py +618 -0
  29. tensorcircuit/results/counts.py +45 -31
  30. tensorcircuit/shadows.py +1 -1
  31. tensorcircuit/simplify.py +3 -1
  32. tensorcircuit/stabilizercircuit.py +4 -2
  33. tensorcircuit/templates/blocks.py +2 -2
  34. tensorcircuit/templates/hamiltonians.py +29 -8
  35. tensorcircuit/templates/lattice.py +676 -335
  36. tensorcircuit/timeevol.py +896 -0
  37. {tensorcircuit_nightly-1.3.0.dev20250728.dist-info → tensorcircuit_nightly-1.4.0.dev20251103.dist-info}/METADATA +50 -25
  38. tensorcircuit_nightly-1.4.0.dev20251103.dist-info/RECORD +96 -0
  39. {tensorcircuit_nightly-1.3.0.dev20250728.dist-info → tensorcircuit_nightly-1.4.0.dev20251103.dist-info}/top_level.txt +0 -1
  40. tensorcircuit_nightly-1.3.0.dev20250728.dist-info/RECORD +0 -122
  41. tests/__init__.py +0 -0
  42. tests/conftest.py +0 -67
  43. tests/test_backends.py +0 -1035
  44. tests/test_calibrating.py +0 -149
  45. tests/test_channels.py +0 -409
  46. tests/test_circuit.py +0 -1713
  47. tests/test_cloud.py +0 -219
  48. tests/test_compiler.py +0 -147
  49. tests/test_dmcircuit.py +0 -555
  50. tests/test_ensemble.py +0 -72
  51. tests/test_fgs.py +0 -318
  52. tests/test_gates.py +0 -156
  53. tests/test_hamiltonians.py +0 -159
  54. tests/test_interfaces.py +0 -557
  55. tests/test_keras.py +0 -160
  56. tests/test_lattice.py +0 -1666
  57. tests/test_miscs.py +0 -334
  58. tests/test_mpscircuit.py +0 -341
  59. tests/test_noisemodel.py +0 -156
  60. tests/test_qaoa.py +0 -86
  61. tests/test_qem.py +0 -152
  62. tests/test_quantum.py +0 -549
  63. tests/test_quantum_attr.py +0 -42
  64. tests/test_results.py +0 -379
  65. tests/test_shadows.py +0 -160
  66. tests/test_simplify.py +0 -46
  67. tests/test_stabilizer.py +0 -226
  68. tests/test_templates.py +0 -218
  69. tests/test_torchnn.py +0 -99
  70. tests/test_van.py +0 -102
  71. {tensorcircuit_nightly-1.3.0.dev20250728.dist-info → tensorcircuit_nightly-1.4.0.dev20251103.dist-info}/WHEEL +0 -0
  72. {tensorcircuit_nightly-1.3.0.dev20250728.dist-info → tensorcircuit_nightly-1.4.0.dev20251103.dist-info}/licenses/LICENSE +0 -0
tests/test_cloud.py DELETED
@@ -1,219 +0,0 @@
1
- import sys
2
- import os
3
- import time
4
- import pytest
5
- import numpy as np
6
-
7
- thisfile = os.path.abspath(__file__)
8
- modulepath = os.path.dirname(os.path.dirname(thisfile))
9
-
10
- sys.path.insert(0, modulepath)
11
- import tensorcircuit as tc
12
- from tensorcircuit.cloud import apis, wrapper
13
- from tensorcircuit.results import counts
14
-
15
- if "TC_CLOUD_TEST" not in os.environ:
16
- pytest.skip(allow_module_level=True)
17
- # skip on CI due to no token
18
-
19
-
20
- def test_get_token():
21
- print(apis.get_token(provider="Tencent"))
22
- p = apis.get_provider("tencent")
23
- print(p.get_token())
24
- print(p.get_device("simulator:tc").get_token())
25
-
26
-
27
- def test_list_devices():
28
- print(apis.list_devices())
29
- p = apis.get_provider()
30
- print(p.list_devices())
31
- print(p.list_devices(state="on"))
32
-
33
-
34
- def test_get_device():
35
- d1 = apis.get_device(device="tencent::hello")
36
- assert d1.name == "hello"
37
- assert d1.provider.name == "tencent"
38
- d2 = apis.get_device(device="hello")
39
- assert d2.name == "hello"
40
- assert d2.provider.name == "tencent"
41
- p = apis.get_provider()
42
- d3 = p.get_device("tencent::hello")
43
- assert d3.name == "hello"
44
- assert d3.provider.name == "tencent"
45
- d4 = p.get_device("hello")
46
- assert d4.name == "hello"
47
- assert d4.provider.name == "tencent"
48
-
49
-
50
- def test_get_device_cache():
51
- d1 = apis.get_device("local::testing")
52
- d2 = apis.get_device(provider="local", device="testing")
53
- apis.set_provider("local")
54
- d3 = apis.get_device("testing")
55
- assert id(d1) == id(d2)
56
- assert id(d3) == id(d1)
57
- apis.set_provider("tencent")
58
- d4 = apis.get_device("testing")
59
- assert d4.provider.name == "tencent"
60
- assert id(d4) != id(d1)
61
-
62
-
63
- def test_list_properties():
64
- d = apis.get_device(device="simulator:aer")
65
- print(d.list_properties())
66
- print(apis.list_properties(device="simulator:aer"))
67
- with pytest.raises(ValueError):
68
- apis.list_properties(device="hell")
69
-
70
-
71
- def test_submit_task():
72
- c = tc.Circuit(3)
73
- c.H(0)
74
- c.H(1)
75
- c.H(2)
76
- t = apis.submit_task(device="simulator:tc", circuit=c)
77
- r = t.details()
78
- assert r["state"] in ["pending", "completed"]
79
- print(t.results(blocked=True))
80
- assert t.get_logical_physical_mapping() == {0: 0, 1: 1, 2: 2}
81
-
82
-
83
- def test_resubmit_task():
84
- c = tc.Circuit(3)
85
- c.H(0)
86
- c.H(1)
87
- t = apis.submit_task(device="simulator:aer", circuit=c)
88
- time.sleep(15)
89
- t1 = apis.resubmit_task(t)
90
- print(t.details())
91
- print(t1.details(wait=True))
92
-
93
-
94
- def test_get_task():
95
- apis.set_device("simulator:tcn1")
96
- c = tc.Circuit(2)
97
- c.cx(0, 1)
98
- t = apis.submit_task(circuit=c)
99
- t1 = apis.get_task(t.id_)
100
- assert t1.id_ == t.id_
101
- t2 = apis.get_device("tencent::simulator:tcn1").get_task(t.id_)
102
- assert t2.id_ == t.id_
103
-
104
- apis.set_device()
105
-
106
-
107
- def test_list_tasks():
108
- d = apis.get_device(device="simulator:aer")
109
- print(d.list_tasks())
110
- print(apis.list_tasks(device="simulator:tc"))
111
-
112
-
113
- def test_local_list_device():
114
- dd = apis.list_devices(provider="local")
115
- assert dd[0].name == "testing"
116
-
117
-
118
- def test_local_submit_task():
119
- c = tc.Circuit(2)
120
- c.h(0)
121
- c.cx(0, 1)
122
-
123
- t = apis.submit_task(device="local::testing", circuit=c, shots=2048)
124
- r = t.results(blocked=True)
125
- assert counts.kl_divergence({"00": 0.5, "11": 0.5}, r) < 0.1
126
- print(t.details())
127
- print(t.get_device())
128
-
129
-
130
- def test_local_list_tasks():
131
- print(apis.list_tasks(provider="local"))
132
-
133
-
134
- def test_local_batch_submit():
135
- apis.set_provider("local")
136
- c = tc.Circuit(2)
137
- c.h(1)
138
- c.ry(1, theta=0.8)
139
-
140
- ts = apis.submit_task(device="testing", circuit=[c, c])
141
- print(ts[0].results())
142
-
143
- apis.set_device("testing")
144
- ts = apis.submit_task(circuit=[c, c])
145
- print(ts[1].results())
146
- print(ts[1].details())
147
- apis.set_provider("tencent")
148
-
149
-
150
- def test_batch_exp_ps():
151
- pss = [[1, 0], [0, 3]]
152
- c = tc.Circuit(2)
153
- c.h(0)
154
- c.x(1)
155
- np.testing.assert_allclose(wrapper.batch_expectation_ps(c, pss), [1, -1], atol=1e-5)
156
- np.testing.assert_allclose(
157
- wrapper.batch_expectation_ps(c, pss, ws=[1, -0.5]), 1.5, atol=1e-5
158
- )
159
- np.testing.assert_allclose(
160
- wrapper.batch_expectation_ps(c, pss, device="simulator:tcn1"),
161
- [1, -1],
162
- atol=1e-1,
163
- )
164
- np.testing.assert_allclose(
165
- wrapper.batch_expectation_ps(c, pss, device="local::default", with_rem=False),
166
- [1, -1],
167
- atol=1e-1,
168
- )
169
-
170
-
171
- def test_batch_submit_template():
172
- run = tc.cloud.wrapper.batch_submit_template(
173
- device="simulator:tc", batch_limit=2, prior=10
174
- )
175
- cs = []
176
- for i in range(4):
177
- c = tc.Circuit(4)
178
- c.h(i)
179
- cs.append(c)
180
-
181
- rs = run(cs[:3], prior=1)
182
- assert len(rs) == 3
183
- rs = run(cs[:4])
184
- assert len(rs) == 4
185
-
186
-
187
- def test_allz_batch():
188
- n = 5
189
-
190
- def makec(inputs, params):
191
- c = tc.Circuit(n)
192
- for i in range(n):
193
- c.rx(i, theta=inputs[i])
194
- for i in range(n):
195
- c.rz(i, theta=params[0, i])
196
- for i in range(n - 1):
197
- c.cx(i, i + 1)
198
- for i in range(n):
199
- c.rx(i, theta=params[1, i])
200
- return c
201
-
202
- pss = []
203
- for i in range(n):
204
- ps = [0 for _ in range(n)]
205
- ps[i] = 3
206
- pss.append(ps)
207
-
208
- def exp_val(c, device=None):
209
- rs = tc.cloud.wrapper.batch_expectation_ps(c, pss, device)
210
- return tc.backend.stack(rs)
211
-
212
- def qmlf(inputs, params, device=None):
213
- c = makec(inputs, params)
214
- return exp_val(c, device)
215
-
216
- inputs = tc.array_to_tensor(np.array([0, 1, 0, 1, 0]))
217
- params = np.ones([2, n])
218
- print(qmlf(inputs, params, device="9gmon"))
219
- print(qmlf(inputs, params))
tests/test_compiler.py DELETED
@@ -1,147 +0,0 @@
1
- import sys
2
- import os
3
- import pytest
4
- import numpy as np
5
-
6
- # from pytest_lazyfixture import lazy_fixture as lf
7
-
8
-
9
- thisfile = os.path.abspath(__file__)
10
- modulepath = os.path.dirname(os.path.dirname(thisfile))
11
-
12
- sys.path.insert(0, modulepath)
13
- import tensorcircuit as tc
14
-
15
-
16
- def test_qsikit_compiler():
17
- try:
18
- import qiskit as _
19
- except ImportError:
20
- pytest.skip("qiskit is not installed")
21
-
22
- from tensorcircuit.compiler.qiskit_compiler import qiskit_compile
23
-
24
- c = tc.Circuit(2)
25
- c.x(1)
26
- c.cx(0, 1)
27
-
28
- c1, info = qiskit_compile(
29
- c,
30
- info=None,
31
- output="qasm",
32
- compiled_options={
33
- "basis_gates": ["cz", "rz", "h"],
34
- "optimization_level": 3,
35
- "coupling_map": [[0, 2], [2, 0], [1, 0], [0, 1]],
36
- },
37
- )
38
- assert "cz" in c1
39
- print(info["logical_physical_mapping"])
40
-
41
- c = tc.Circuit(2)
42
- c.x(1)
43
- c.cx(0, 1)
44
- c.measure_instruction(1)
45
- c1, info = qiskit_compile(
46
- c,
47
- info=None,
48
- output="tc",
49
- compiled_options={
50
- "basis_gates": ["cx", "rz", "h"],
51
- "optimization_level": 3,
52
- "coupling_map": [[0, 2], [2, 0], [1, 0], [0, 1]],
53
- "initial_layout": [1, 2],
54
- },
55
- )
56
- for inst in c1.to_qir():
57
- if inst["name"] == "h":
58
- assert inst["index"][0] == 2
59
- print(c1.draw())
60
- assert info["logical_physical_mapping"][1] in [0, 2]
61
- print(info)
62
- c2, info2 = qiskit_compile(
63
- c1,
64
- info=info,
65
- output="tc",
66
- compiled_options={
67
- "basis_gates": ["cx", "rz", "h"],
68
- "optimization_level": 3,
69
- "coupling_map": [[0, 2], [2, 0], [1, 0], [0, 1]],
70
- },
71
- )
72
- assert info2["positional_logical_mapping"] == {0: 1}
73
- print(c2.draw())
74
-
75
-
76
- def test_composed_compiler():
77
- from tensorcircuit.compiler import DefaultCompiler
78
-
79
- c = tc.Circuit(3)
80
- c.rx(0)
81
- c.cx(0, 1)
82
- c.cz(1, 0)
83
- c.rxx(0, 2, theta=0.2)
84
- c.measure_instruction(2)
85
- c.measure_instruction(0)
86
- default_compiler = DefaultCompiler()
87
- c1, info = default_compiler(c)
88
- print(c1.draw())
89
- assert c1.gate_count_by_condition(lambda qir: qir["name"] == "cnot") == 3
90
- assert info["positional_logical_mapping"][0] == 2
91
-
92
- default_compiler = DefaultCompiler(
93
- {
94
- "basis_gates": ["h", "rz", "cz"],
95
- "optimization_level": 2,
96
- "coupling_map": [[0, 1], [1, 2]],
97
- }
98
- )
99
-
100
- c1, info = default_compiler(c)
101
- assert c1.gate_count_by_condition(lambda qir: qir["name"] == "cnot") == 0
102
- print(info)
103
-
104
-
105
- def test_replace_r():
106
- c = tc.Circuit(3)
107
- c.rz(0, theta=0.1)
108
- c.cx(0, 2)
109
- c.ry(1)
110
- c.rxx(1, 0, theta=0.2)
111
- c.rx(0, theta=3.9)
112
- c.ry(1, theta=-0.2)
113
- c.rzz(1, 0, theta=-0.3)
114
- c.ryy(1, 0, theta=-0.6)
115
- c.rx(2)
116
-
117
- print(c.draw())
118
- c1 = tc.compiler.simple_compiler.replace_r(c)
119
- print(c1.draw())
120
- np.testing.assert_allclose(c.matrix(), c1.matrix(), atol=1e-5)
121
-
122
-
123
- def test_default_compiler():
124
- c = tc.Circuit(3)
125
- c.cx(0, 1)
126
- c.rx(0, theta=1e-5)
127
- c.x(1)
128
- c.y(1)
129
- c.z(1)
130
- c.h(1)
131
- c.cz(2, 0)
132
- c.h(1)
133
- c.cz(2, 0)
134
- c.s(2)
135
- c.sd(2)
136
- c.s(2)
137
- c.s(2)
138
- c.y(2)
139
- c.ry(2, theta=0.1)
140
- c.t(2)
141
- c.td(2)
142
- c.ry(2, theta=-0.1)
143
- c.rz(1, theta=0.3)
144
-
145
- c1, _ = tc.compiler.simple_compiler.simple_compile(c)
146
- print(c1.draw())
147
- assert c1.gate_count() == 3