tensorcircuit-nightly 1.0.2.dev20250108__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 (76) hide show
  1. tensorcircuit/__init__.py +18 -2
  2. tensorcircuit/about.py +46 -0
  3. tensorcircuit/abstractcircuit.py +4 -0
  4. tensorcircuit/analogcircuit.py +413 -0
  5. tensorcircuit/applications/layers.py +1 -1
  6. tensorcircuit/applications/van.py +1 -1
  7. tensorcircuit/backends/abstract_backend.py +320 -7
  8. tensorcircuit/backends/cupy_backend.py +3 -1
  9. tensorcircuit/backends/jax_backend.py +102 -4
  10. tensorcircuit/backends/jax_ops.py +110 -1
  11. tensorcircuit/backends/numpy_backend.py +49 -3
  12. tensorcircuit/backends/pytorch_backend.py +92 -3
  13. tensorcircuit/backends/tensorflow_backend.py +102 -3
  14. tensorcircuit/basecircuit.py +157 -98
  15. tensorcircuit/circuit.py +115 -57
  16. tensorcircuit/cloud/local.py +1 -1
  17. tensorcircuit/cloud/quafu_provider.py +1 -1
  18. tensorcircuit/cloud/tencent.py +1 -1
  19. tensorcircuit/compiler/simple_compiler.py +2 -2
  20. tensorcircuit/cons.py +142 -21
  21. tensorcircuit/densitymatrix.py +43 -14
  22. tensorcircuit/experimental.py +387 -129
  23. tensorcircuit/fgs.py +282 -81
  24. tensorcircuit/gates.py +66 -22
  25. tensorcircuit/interfaces/__init__.py +1 -3
  26. tensorcircuit/interfaces/jax.py +189 -0
  27. tensorcircuit/keras.py +3 -3
  28. tensorcircuit/mpscircuit.py +154 -65
  29. tensorcircuit/quantum.py +868 -152
  30. tensorcircuit/quditcircuit.py +733 -0
  31. tensorcircuit/quditgates.py +618 -0
  32. tensorcircuit/results/counts.py +147 -20
  33. tensorcircuit/results/readout_mitigation.py +4 -1
  34. tensorcircuit/shadows.py +1 -1
  35. tensorcircuit/simplify.py +3 -1
  36. tensorcircuit/stabilizercircuit.py +479 -0
  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.0.2.dev20250108.dist-info → tensorcircuit_nightly-1.4.0.dev20251103.dist-info}/METADATA +73 -23
  45. tensorcircuit_nightly-1.4.0.dev20251103.dist-info/RECORD +96 -0
  46. {tensorcircuit_nightly-1.0.2.dev20250108.dist-info → tensorcircuit_nightly-1.4.0.dev20251103.dist-info}/WHEEL +1 -1
  47. {tensorcircuit_nightly-1.0.2.dev20250108.dist-info → tensorcircuit_nightly-1.4.0.dev20251103.dist-info}/top_level.txt +0 -1
  48. tensorcircuit_nightly-1.0.2.dev20250108.dist-info/RECORD +0 -115
  49. tests/__init__.py +0 -0
  50. tests/conftest.py +0 -67
  51. tests/test_backends.py +0 -1031
  52. tests/test_calibrating.py +0 -149
  53. tests/test_channels.py +0 -365
  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 -429
  62. tests/test_keras.py +0 -160
  63. tests/test_miscs.py +0 -277
  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 -526
  69. tests/test_quantum_attr.py +0 -42
  70. tests/test_results.py +0 -347
  71. tests/test_shadows.py +0 -160
  72. tests/test_simplify.py +0 -46
  73. tests/test_templates.py +0 -218
  74. tests/test_torchnn.py +0 -99
  75. tests/test_van.py +0 -102
  76. {tensorcircuit_nightly-1.0.2.dev20250108.dist-info → tensorcircuit_nightly-1.4.0.dev20251103.dist-info/licenses}/LICENSE +0 -0
tests/test_noisemodel.py DELETED
@@ -1,156 +0,0 @@
1
- import pytest
2
- from pytest_lazyfixture import lazy_fixture as lf
3
- import numpy as np
4
-
5
- import tensorcircuit as tc
6
- from tensorcircuit.noisemodel import (
7
- NoiseConf,
8
- circuit_with_noise,
9
- sample_expectation_ps_noisfy,
10
- expectation_noisfy,
11
- )
12
- from tensorcircuit.channels import composedkraus
13
-
14
-
15
- @pytest.mark.parametrize("backend", [lf("tfb"), lf("jaxb")])
16
- def test_noisemodel(backend):
17
- # test data structure
18
- # noise_conf = NoiseConf()
19
- # noise_conf.add_noise("h1", "t0")
20
- # noise_conf.add_noise("h1", ["t1", "t2"], [[0], [1]])
21
- # noise_conf.add_noise("h1", ["t3"], [[0]])
22
- # noise_conf.add_noise("h1", "t4")
23
- # noise_conf.add_noise("h1", ["t5"], [[3]])
24
- # noise_conf.add_noise("h2", ["v1", "v2"], [[0], [1]])
25
- # noise_conf.add_noise("h2", ["v3"], [[0]])
26
- # noise_conf.add_noise("h2", "v4")
27
-
28
- c = tc.Circuit(2)
29
- c.cnot(0, 1)
30
- c.rx(0, theta=0.4)
31
- c.rx(1, theta=0.8)
32
- c.h(0)
33
- c.h(1)
34
-
35
- dmc = tc.DMCircuit(2)
36
- dmc.cnot(0, 1)
37
- dmc.rx(0, theta=0.4)
38
- dmc.rx(1, theta=0.8)
39
- dmc.h(0)
40
- dmc.h(1)
41
-
42
- error1 = tc.channels.generaldepolarizingchannel(0.1, 1)
43
- error2 = tc.channels.generaldepolarizingchannel(0.01, 2)
44
- error3 = tc.channels.thermalrelaxationchannel(300, 400, 100, "ByChoi", 0)
45
-
46
- readout_error = []
47
- readout_error.append([0.9, 0.75]) # readout error of qubit 0
48
- readout_error.append([0.4, 0.7]) # readout error of qubit 1
49
-
50
- noise_conf = NoiseConf()
51
- noise_conf.add_noise("rx", error1)
52
- noise_conf.add_noise("rx", [error3], [[0]])
53
- noise_conf.add_noise("h", [error3, error1], [[0], [1]])
54
- noise_conf.add_noise("x", [error3], [[0]])
55
- noise_conf.add_noise("cnot", [error2], [[0, 1]])
56
- noise_conf.add_noise("readout", readout_error)
57
-
58
- cnoise = circuit_with_noise(c, noise_conf, [0.1] * 7)
59
- value = cnoise.expectation_ps(x=[0, 1])
60
-
61
- # value = expectation_ps_noisfy(c, x=[0, 1], noise_conf=noise_conf, nmc=10000)
62
- # np.testing.assert_allclose(value, 0.09, atol=1e-1)
63
-
64
- # value = expectation_ps_noisfy(dmc, x=[0, 1], noise_conf=noise_conf)
65
- # np.testing.assert_allclose(value, 0.09, atol=1e-1)
66
-
67
- # with readout_error
68
- value = sample_expectation_ps_noisfy(dmc, x=[0, 1], noise_conf=noise_conf)
69
- np.testing.assert_allclose(value, -0.12, atol=1e-2)
70
-
71
- value = sample_expectation_ps_noisfy(c, x=[0, 1], noise_conf=noise_conf, nmc=100000)
72
- np.testing.assert_allclose(value, -0.12, atol=1e-2)
73
-
74
- # test composed channel and general condition
75
- newerror = composedkraus(error1, error3)
76
- noise_conf1 = NoiseConf()
77
- noise_conf1.add_noise("rx", [newerror, error1], [[0], [1]])
78
- noise_conf1.add_noise("h", [error3, error1], [[0], [1]])
79
- noise_conf1.add_noise("x", [error3], [[0]])
80
-
81
- def condition(d):
82
- return d["name"] == "cnot" and d["index"] == (0, 1)
83
-
84
- noise_conf1.add_noise_by_condition(condition, error2)
85
- noise_conf1.add_noise("readout", readout_error)
86
-
87
- value = sample_expectation_ps_noisfy(dmc, x=[0, 1], noise_conf=noise_conf1)
88
- np.testing.assert_allclose(value, -0.12, atol=1e-2)
89
-
90
- # test standardized gate
91
- newerror = composedkraus(error1, error3)
92
- noise_conf2 = NoiseConf()
93
- noise_conf2.add_noise("Rx", [newerror, error1], [[0], [1]])
94
- noise_conf2.add_noise("H", [error3, error1], [[0], [1]])
95
- noise_conf2.add_noise("x", [error3], [[0]])
96
- noise_conf2.add_noise("cx", [error2], [[0, 1]])
97
- noise_conf2.add_noise("readout", readout_error)
98
-
99
- value = sample_expectation_ps_noisfy(
100
- c, x=[0, 1], noise_conf=noise_conf2, nmc=100000
101
- )
102
- np.testing.assert_allclose(value, -0.12, atol=1e-2)
103
-
104
-
105
- @pytest.mark.parametrize("backend", [lf("tfb"), lf("jaxb")])
106
- def test_general_noisemodel(backend):
107
- c = tc.Circuit(2)
108
- c.cnot(0, 1)
109
- c.rx(0, theta=0.4)
110
- c.rx(1, theta=0.8)
111
- c.h(0)
112
- c.h(1)
113
-
114
- dmc = tc.DMCircuit(2)
115
- dmc.cnot(0, 1)
116
- dmc.rx(0, theta=0.4)
117
- dmc.rx(1, theta=0.8)
118
- dmc.h(0)
119
- dmc.h(1)
120
-
121
- error1 = tc.channels.generaldepolarizingchannel(0.1, 1)
122
- error2 = tc.channels.generaldepolarizingchannel(0.06, 2)
123
- error3 = tc.channels.thermalrelaxationchannel(300, 400, 100, "ByChoi", 0)
124
-
125
- readout_error = []
126
- readout_error.append([0.9, 0.75])
127
- readout_error.append([0.4, 0.7])
128
-
129
- noise_conf = NoiseConf()
130
- noise_conf.add_noise("rx", error1)
131
- noise_conf.add_noise("rx", [error3], [[0]])
132
- noise_conf.add_noise("h", [error3, error1], [[0], [1]])
133
- noise_conf.add_noise("x", [error3], [[0]])
134
- noise_conf.add_noise("cnot", [error2], [[0, 1]])
135
- noise_conf.add_noise("readout", readout_error)
136
-
137
- nmc = 100000
138
- # # test sample_expectation_ps
139
- value1 = sample_expectation_ps_noisfy(c, x=[0, 1], noise_conf=noise_conf, nmc=nmc)
140
- value2 = c.sample_expectation_ps(x=[0, 1], noise_conf=noise_conf, nmc=nmc)
141
- value3 = dmc.sample_expectation_ps(x=[0, 1], noise_conf=noise_conf)
142
- np.testing.assert_allclose(value1, value2, atol=1e-2)
143
- np.testing.assert_allclose(value3, value2, atol=1e-2)
144
-
145
- # test expectation
146
- value1 = expectation_noisfy(c, (tc.gates.z(), [0]), noise_conf=noise_conf, nmc=nmc)
147
- value2 = c.expectation((tc.gates.z(), [0]), noise_conf=noise_conf, nmc=nmc)
148
- value3 = dmc.expectation((tc.gates.z(), [0]), noise_conf=noise_conf)
149
- np.testing.assert_allclose(value1, value2, atol=1e-2)
150
- np.testing.assert_allclose(value3, value2, atol=1e-2)
151
-
152
- # test expectation_ps
153
- # value = expectation_ps_noisfy(c, x=[0], noise_conf=noise_conf, nmc=10000)
154
- value1 = c.expectation_ps(x=[0], noise_conf=noise_conf, nmc=nmc)
155
- value2 = dmc.expectation_ps(x=[0], noise_conf=noise_conf)
156
- np.testing.assert_allclose(value1, value2, atol=1e-2)
tests/test_qaoa.py DELETED
@@ -1,86 +0,0 @@
1
- import sys
2
- import os
3
-
4
- thisfile = os.path.abspath(__file__)
5
- modulepath = os.path.dirname(os.path.dirname(thisfile))
6
-
7
- sys.path.insert(0, modulepath)
8
-
9
- import numpy as np
10
- import pytest
11
-
12
- from tensorcircuit.applications.dqas import set_op_pool
13
- from tensorcircuit.applications.graphdata import get_graph
14
- from tensorcircuit.applications.layers import Hlayer, rxlayer, zzlayer
15
- from tensorcircuit.applications.vags import evaluate_vag
16
- from tensorcircuit.templates.ansatz import QAOA_ansatz_for_Ising
17
- from tensorcircuit.circuit import Circuit
18
-
19
-
20
- def test_vag(tfb):
21
- set_op_pool([Hlayer, rxlayer, zzlayer])
22
- expene, ene, eneg, p = evaluate_vag(
23
- np.array([0.0, 0.3, 0.5, 0.7, -0.8]),
24
- [0, 2, 1, 2, 1],
25
- get_graph("10A"),
26
- lbd=0,
27
- overlap_threhold=11,
28
- )
29
- print(expene, eneg, p)
30
- np.testing.assert_allclose(ene.numpy(), -7.01, rtol=1e-2)
31
-
32
-
33
- cases = [
34
- ("X", True),
35
- ("X", False),
36
- ("XY", True),
37
- ("XY", False),
38
- ("ZZ", True),
39
- ("ZZ", False),
40
- ]
41
-
42
-
43
- @pytest.fixture
44
- def example_inputs():
45
- params = [0.1, 0.2, 0.3, 0.4]
46
- nlayers = 2
47
- pauli_terms = [[0, 1, 0], [1, 0, 1]]
48
- weights = [0.5, -0.5]
49
- return params, nlayers, pauli_terms, weights
50
-
51
-
52
- @pytest.mark.parametrize("mixer, full_coupling", cases)
53
- def test_QAOA_ansatz_for_Ising(example_inputs, full_coupling, mixer):
54
- params, nlayers, pauli_terms, weights = example_inputs
55
- circuit = QAOA_ansatz_for_Ising(
56
- params, nlayers, pauli_terms, weights, full_coupling, mixer
57
- )
58
- n = len(pauli_terms[0])
59
- assert isinstance(circuit, Circuit)
60
- assert circuit._nqubits == n
61
-
62
- if mixer == "X":
63
- assert circuit.gate_count() == n + nlayers * (len(pauli_terms) + n)
64
- elif mixer == "XY":
65
- if full_coupling is False:
66
- assert circuit.gate_count() == n + nlayers * (len(pauli_terms) + 2 * n)
67
- else:
68
- assert circuit.gate_count() == n + nlayers * (
69
- len(pauli_terms) + sum(range(n + 1))
70
- )
71
- else:
72
- if full_coupling is False:
73
- assert circuit.gate_count() == n + nlayers * (len(pauli_terms) + n)
74
- else:
75
- assert circuit.gate_count() == n + nlayers * (
76
- len(pauli_terms) + sum(range(n + 1)) / 2
77
- )
78
-
79
-
80
- @pytest.mark.parametrize("mixer, full_coupling", [("AB", True), ("XY", 1), ("TC", 5)])
81
- def test_QAOA_ansatz_errors(example_inputs, full_coupling, mixer):
82
- params, nlayers, pauli_terms, weights = example_inputs
83
- with pytest.raises(ValueError):
84
- QAOA_ansatz_for_Ising(
85
- params, nlayers, pauli_terms, weights, full_coupling, mixer
86
- )
tests/test_qem.py DELETED
@@ -1,152 +0,0 @@
1
- from functools import partial
2
- import pytest
3
- from pytest_lazyfixture import lazy_fixture as lf
4
- import numpy as np
5
- import networkx as nx
6
-
7
- import tensorcircuit as tc
8
- from tensorcircuit.noisemodel import NoiseConf
9
- from tensorcircuit.results import qem
10
- from tensorcircuit.results.qem import (
11
- zne_option,
12
- apply_zne,
13
- dd_option,
14
- apply_dd,
15
- apply_rc,
16
- )
17
- from tensorcircuit.results.qem import benchmark_circuits
18
-
19
-
20
- @pytest.mark.parametrize("backend", [lf("tfb"), lf("jaxb")])
21
- def test_benchmark_circuits(backend):
22
- # QAOA
23
- graph = [(2, 0), (0, 3), (1, 2)]
24
- weight = [1] * len(graph)
25
- params = np.array([[1, 1]])
26
-
27
- _ = benchmark_circuits.QAOA_circuit(graph, weight, params)
28
-
29
- # mirror circuit
30
- # return circuit and ideal counts {"01000":1}
31
- _, _ = benchmark_circuits.mirror_circuit(
32
- depth=5, two_qubit_gate_prob=1, connectivity_graph=nx.complete_graph(3), seed=20
33
- )
34
-
35
- # GHZ circuit
36
- _ = benchmark_circuits.generate_ghz_circuit(10)
37
-
38
- # Werner-state with linear complexity
39
- # {'1000': 0.25, '0100': 0.25, '0010': 0.25, '0001': 0.25}
40
- _ = benchmark_circuits.generate_w_circuit(5)
41
-
42
- # RB cirucit
43
- _ = benchmark_circuits.generate_rb_circuits(2, 7)[0]
44
-
45
-
46
- @pytest.mark.parametrize("backend", [lf("tfb"), lf("jaxb")])
47
- def test_zne(backend):
48
- c = tc.Circuit(2)
49
- for _ in range(3):
50
- c.rx(range(2), theta=0.4)
51
-
52
- error1 = tc.channels.generaldepolarizingchannel(0.01, 1)
53
- noise_conf = NoiseConf()
54
- noise_conf.add_noise("rx", error1)
55
-
56
- def execute(circuit):
57
- value = circuit.expectation_ps(z=[0], noise_conf=noise_conf, nmc=10000)
58
- return value
59
-
60
- random_state = np.random.RandomState(0)
61
- noise_scaling_function = partial(
62
- zne_option.scaling.fold_gates_at_random,
63
- # fidelities = {"single": 1.0},
64
- random_state=random_state,
65
- )
66
- factory = zne_option.inference.PolyFactory(scale_factors=[1, 3, 5], order=1)
67
- # factory = zne_option.inference.ExpFactory(scale_factors=[1,1.5,2],asymptote=0.)
68
- # factory = zne_option.inference.RichardsonFactory(scale_factors=[1,1.5,2])
69
- # factory = zne_option.inference.AdaExpFactory(steps=5, asymptote=0.)
70
-
71
- result = apply_zne(
72
- circuit=c,
73
- executor=execute,
74
- factory=factory,
75
- scale_noise=noise_scaling_function,
76
- num_to_average=1,
77
- )
78
-
79
- ideal_value = c.expectation_ps(z=[0])
80
- mit_value = result
81
-
82
- np.testing.assert_allclose(ideal_value, mit_value, atol=4e-2)
83
-
84
-
85
- @pytest.mark.parametrize("backend", [lf("tfb"), lf("jaxb")])
86
- def test_dd(backend):
87
- c = tc.Circuit(2)
88
- for _ in range(3):
89
- c.rx(range(2), theta=0.4)
90
-
91
- def execute(circuit):
92
- value = circuit.expectation_ps(z=[0])
93
- return value
94
-
95
- def execute2(circuit):
96
- key = tc.backend.get_random_state(42)
97
- count = circuit.sample(
98
- batch=1000, allow_state=True, format_="count_dict_bin", random_generator=key
99
- )
100
- return count
101
-
102
- _ = apply_dd(
103
- circuit=c,
104
- executor=execute,
105
- rule=["X", "X"],
106
- rule_args={"spacing": -1},
107
- full_output=True,
108
- ignore_idle_qubit=True,
109
- fulldd=False,
110
- )
111
-
112
- _ = apply_dd(
113
- circuit=c,
114
- executor=execute2,
115
- rule=dd_option.rules.xyxy,
116
- rule_args={"spacing": -1},
117
- full_output=True,
118
- ignore_idle_qubit=True,
119
- fulldd=True,
120
- iscount=True,
121
- )
122
-
123
- # wash circuit based on use_qubits and washout iden gates
124
- _ = qem.prune_ddcircuit(c, qlist=list(range(c.circuit_param["nqubits"])))
125
-
126
-
127
- @pytest.mark.parametrize("backend", [lf("tfb"), lf("jaxb")])
128
- def test_rc(backend):
129
- c = tc.Circuit(2)
130
- for _ in range(3):
131
- c.rx(range(2), theta=0.4)
132
- c.cnot(0, 1)
133
-
134
- def execute(circuit):
135
- value = circuit.expectation_ps(z=[0])
136
- return value
137
-
138
- def execute2(circuit):
139
- key = tc.backend.get_random_state(42)
140
- count = circuit.sample(
141
- batch=1000, allow_state=True, format_="count_dict_bin", random_generator=key
142
- )
143
- return count
144
-
145
- _ = apply_rc(circuit=c, executor=execute, num_to_average=6, simplify=False)
146
-
147
- _ = apply_rc(
148
- circuit=c, executor=execute2, num_to_average=6, simplify=True, iscount=True
149
- )
150
-
151
- # generate a circuit with rc
152
- _ = qem.rc_circuit(c)