cirq-core 1.6.0.dev20250702182429__py3-none-any.whl → 1.6.0.dev20250702211226__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 cirq-core might be problematic. Click here for more details.
- cirq/_version.py +1 -1
- cirq/_version_test.py +1 -1
- cirq/circuits/circuit.py +4 -2
- cirq/circuits/circuit_operation.py +2 -2
- cirq/circuits/circuit_test.py +157 -148
- cirq/circuits/moment_test.py +61 -51
- cirq/circuits/optimization_pass.py +1 -1
- cirq/circuits/text_diagram_drawer.py +6 -6
- cirq/conftest.py +4 -3
- cirq/contrib/acquaintance/bipartite_test.py +10 -8
- cirq/contrib/acquaintance/devices_test.py +3 -3
- cirq/contrib/acquaintance/executor_test.py +8 -6
- cirq/contrib/acquaintance/gates.py +1 -1
- cirq/contrib/acquaintance/gates_test.py +18 -16
- cirq/contrib/acquaintance/inspection_utils.py +7 -3
- cirq/contrib/acquaintance/permutation_test.py +24 -17
- cirq/contrib/acquaintance/shift_swap_network_test.py +7 -6
- cirq/contrib/acquaintance/strategies/quartic_paired_test.py +8 -6
- cirq/contrib/bayesian_network/bayesian_network_gate.py +2 -2
- cirq/contrib/bayesian_network/bayesian_network_gate_test.py +10 -8
- cirq/contrib/circuitdag/circuit_dag.py +2 -2
- cirq/contrib/circuitdag/circuit_dag_test.py +15 -15
- cirq/contrib/custom_simulators/custom_state_simulator_test.py +7 -3
- cirq/contrib/graph_device/graph_device.py +6 -5
- cirq/contrib/graph_device/graph_device_test.py +16 -14
- cirq/contrib/graph_device/hypergraph.py +2 -2
- cirq/contrib/graph_device/hypergraph_test.py +11 -11
- cirq/contrib/graph_device/uniform_graph_device_test.py +9 -3
- cirq/contrib/hacks/disable_validation.py +4 -1
- cirq/contrib/json.py +2 -2
- cirq/contrib/noise_models/noise_models.py +5 -5
- cirq/contrib/noise_models/noise_models_test.py +4 -0
- cirq/contrib/qcircuit/qcircuit_diagram_info.py +1 -1
- cirq/contrib/qcircuit/qcircuit_diagram_info_test.py +2 -1
- cirq/contrib/qcircuit/qcircuit_pdf.py +1 -1
- cirq/contrib/quantum_volume/quantum_volume_test.py +17 -16
- cirq/contrib/quimb/density_matrix.py +1 -1
- cirq/contrib/quimb/density_matrix_test.py +7 -7
- cirq/devices/noise_model_test.py +16 -14
- {cirq_core-1.6.0.dev20250702182429.dist-info → cirq_core-1.6.0.dev20250702211226.dist-info}/METADATA +1 -1
- {cirq_core-1.6.0.dev20250702182429.dist-info → cirq_core-1.6.0.dev20250702211226.dist-info}/RECORD +44 -44
- {cirq_core-1.6.0.dev20250702182429.dist-info → cirq_core-1.6.0.dev20250702211226.dist-info}/WHEEL +0 -0
- {cirq_core-1.6.0.dev20250702182429.dist-info → cirq_core-1.6.0.dev20250702211226.dist-info}/licenses/LICENSE +0 -0
- {cirq_core-1.6.0.dev20250702182429.dist-info → cirq_core-1.6.0.dev20250702211226.dist-info}/top_level.txt +0 -0
|
@@ -30,7 +30,9 @@ class ComputationalBasisState(cirq.qis.QuantumStateRepresentation):
|
|
|
30
30
|
def copy(self, deep_copy_buffers: bool = True) -> ComputationalBasisState:
|
|
31
31
|
return ComputationalBasisState(self.basis) # pragma: no cover
|
|
32
32
|
|
|
33
|
-
def measure(
|
|
33
|
+
def measure(
|
|
34
|
+
self, axes: Sequence[int], seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None
|
|
35
|
+
) -> list[int]:
|
|
34
36
|
return [self.basis[i] for i in axes]
|
|
35
37
|
|
|
36
38
|
|
|
@@ -49,7 +51,7 @@ class ComputationalBasisSimState(cirq.SimulationState[ComputationalBasisState]):
|
|
|
49
51
|
return True
|
|
50
52
|
|
|
51
53
|
|
|
52
|
-
def create_test_circuit():
|
|
54
|
+
def create_test_circuit() -> cirq.Circuit:
|
|
53
55
|
q0, q1 = cirq.LineQid.range(2, dimension=3)
|
|
54
56
|
x = cirq.XPowGate(dimension=3)
|
|
55
57
|
return cirq.Circuit(
|
|
@@ -143,7 +145,9 @@ class ComputationalBasisProductState(cirq.qis.QuantumStateRepresentation):
|
|
|
143
145
|
def copy(self, deep_copy_buffers: bool = True) -> ComputationalBasisProductState:
|
|
144
146
|
return ComputationalBasisProductState(self.basis)
|
|
145
147
|
|
|
146
|
-
def measure(
|
|
148
|
+
def measure(
|
|
149
|
+
self, axes: Sequence[int], seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None
|
|
150
|
+
) -> list[int]:
|
|
147
151
|
return [self.basis[i] for i in axes]
|
|
148
152
|
|
|
149
153
|
def kron(self, other: ComputationalBasisProductState) -> ComputationalBasisProductState:
|
|
@@ -16,7 +16,7 @@ from __future__ import annotations
|
|
|
16
16
|
|
|
17
17
|
import abc
|
|
18
18
|
import itertools
|
|
19
|
-
from typing import cast, Iterable, TYPE_CHECKING
|
|
19
|
+
from typing import Any, cast, Hashable, Iterable, NoReturn, TYPE_CHECKING
|
|
20
20
|
|
|
21
21
|
from cirq import devices, ops, value
|
|
22
22
|
from cirq.contrib.graph_device.hypergraph import UndirectedHypergraph
|
|
@@ -98,7 +98,7 @@ def is_crosstalk_graph(graph: UndirectedHypergraph) -> bool:
|
|
|
98
98
|
return True
|
|
99
99
|
|
|
100
100
|
|
|
101
|
-
def raise_crosstalk_error(*ops: ops.Operation):
|
|
101
|
+
def raise_crosstalk_error(*ops: ops.Operation) -> NoReturn:
|
|
102
102
|
raise ValueError(f'crosstalk on {ops}')
|
|
103
103
|
|
|
104
104
|
|
|
@@ -156,11 +156,11 @@ class UndirectedGraphDevice(devices.Device):
|
|
|
156
156
|
return cast(tuple['cirq.Qid', ...], tuple(sorted(self.device_graph.vertices)))
|
|
157
157
|
|
|
158
158
|
@property
|
|
159
|
-
def edges(self):
|
|
159
|
+
def edges(self) -> tuple[frozenset[Hashable], ...]:
|
|
160
160
|
return tuple(sorted(self.device_graph.edges))
|
|
161
161
|
|
|
162
162
|
@property
|
|
163
|
-
def labelled_edges(self):
|
|
163
|
+
def labelled_edges(self) -> dict[frozenset, Any]:
|
|
164
164
|
return self.device_graph.labelled_edges
|
|
165
165
|
|
|
166
166
|
def get_device_edge_from_op(self, operation: ops.Operation) -> UndirectedGraphDeviceEdge:
|
|
@@ -185,6 +185,7 @@ class UndirectedGraphDevice(devices.Device):
|
|
|
185
185
|
self.crosstalk_graph._adjacency_lists.get(frozenset(operation.qubits), ())
|
|
186
186
|
)
|
|
187
187
|
for crosstalk_edge in adjacent_crosstalk_edges:
|
|
188
|
+
# pylint: disable=unreachable
|
|
188
189
|
label = self.crosstalk_graph.labelled_edges[crosstalk_edge]
|
|
189
190
|
validator = (
|
|
190
191
|
raise_crosstalk_error(operation, *other_operations) if (label is None) else label
|
|
@@ -194,7 +195,7 @@ class UndirectedGraphDevice(devices.Device):
|
|
|
194
195
|
):
|
|
195
196
|
validator(operation, *crosstalk_operations)
|
|
196
197
|
|
|
197
|
-
def validate_moment(self, moment: cirq.Moment):
|
|
198
|
+
def validate_moment(self, moment: cirq.Moment) -> None:
|
|
198
199
|
super().validate_moment(moment)
|
|
199
200
|
ops = moment.operations
|
|
200
201
|
for i, op in enumerate(ops):
|
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
|
|
15
15
|
from __future__ import annotations
|
|
16
16
|
|
|
17
|
+
from typing import Any, Hashable, Iterable
|
|
18
|
+
|
|
17
19
|
import pytest
|
|
18
20
|
|
|
19
21
|
import cirq
|
|
@@ -21,7 +23,7 @@ import cirq.contrib.graph_device as ccgd
|
|
|
21
23
|
import cirq.contrib.graph_device.graph_device as ccgdgd
|
|
22
24
|
|
|
23
25
|
|
|
24
|
-
def test_fixed_duration_undirected_graph_device_edge_eq():
|
|
26
|
+
def test_fixed_duration_undirected_graph_device_edge_eq() -> None:
|
|
25
27
|
e = ccgd.FixedDurationUndirectedGraphDeviceEdge(cirq.Duration(picos=4))
|
|
26
28
|
f = ccgd.FixedDurationUndirectedGraphDeviceEdge(cirq.Duration(picos=4))
|
|
27
29
|
g = ccgd.FixedDurationUndirectedGraphDeviceEdge(cirq.Duration(picos=5))
|
|
@@ -30,15 +32,15 @@ def test_fixed_duration_undirected_graph_device_edge_eq():
|
|
|
30
32
|
assert e != 4
|
|
31
33
|
|
|
32
34
|
|
|
33
|
-
def test_unconstrained_undirected_graph_device_edge_eq():
|
|
35
|
+
def test_unconstrained_undirected_graph_device_edge_eq() -> None:
|
|
34
36
|
e = ccgdgd._UnconstrainedUndirectedGraphDeviceEdge()
|
|
35
37
|
f = ccgd.UnconstrainedUndirectedGraphDeviceEdge
|
|
36
38
|
assert e == f
|
|
37
39
|
assert e != 3
|
|
38
40
|
|
|
39
41
|
|
|
40
|
-
def test_is_undirected_device_graph():
|
|
41
|
-
assert not ccgd.is_undirected_device_graph('abc')
|
|
42
|
+
def test_is_undirected_device_graph() -> None:
|
|
43
|
+
assert not ccgd.is_undirected_device_graph('abc') # type: ignore[arg-type]
|
|
42
44
|
graph = ccgd.UndirectedHypergraph()
|
|
43
45
|
assert ccgd.is_undirected_device_graph(graph)
|
|
44
46
|
a, b, c, d, e = cirq.LineQubit.range(5)
|
|
@@ -52,9 +54,9 @@ def test_is_undirected_device_graph():
|
|
|
52
54
|
assert not ccgd.is_undirected_device_graph(graph)
|
|
53
55
|
|
|
54
56
|
|
|
55
|
-
def test_is_crosstalk_graph():
|
|
57
|
+
def test_is_crosstalk_graph() -> None:
|
|
56
58
|
a, b, c, d, e, f = cirq.LineQubit.range(6)
|
|
57
|
-
assert not ccgd.is_crosstalk_graph('abc')
|
|
59
|
+
assert not ccgd.is_crosstalk_graph('abc') # type: ignore[arg-type]
|
|
58
60
|
graph = ccgd.UndirectedHypergraph()
|
|
59
61
|
graph.add_vertex('abc')
|
|
60
62
|
assert not ccgd.is_crosstalk_graph(graph)
|
|
@@ -76,14 +78,14 @@ def test_is_crosstalk_graph():
|
|
|
76
78
|
assert not ccgd.is_crosstalk_graph(graph)
|
|
77
79
|
|
|
78
80
|
|
|
79
|
-
def test_unconstrained_undirected_graph_device_edge():
|
|
81
|
+
def test_unconstrained_undirected_graph_device_edge() -> None:
|
|
80
82
|
edge = ccgd.UnconstrainedUndirectedGraphDeviceEdge
|
|
81
83
|
qubits = cirq.LineQubit.range(2)
|
|
82
84
|
assert edge.duration_of(cirq.X(qubits[0])) == cirq.Duration(picos=0)
|
|
83
85
|
assert edge.duration_of(cirq.CZ(*qubits[:2])) == cirq.Duration(picos=0)
|
|
84
86
|
|
|
85
87
|
|
|
86
|
-
def test_graph_device():
|
|
88
|
+
def test_graph_device() -> None:
|
|
87
89
|
one_qubit_duration = cirq.Duration(picos=10)
|
|
88
90
|
two_qubit_duration = cirq.Duration(picos=1)
|
|
89
91
|
one_qubit_edge = ccgd.FixedDurationUndirectedGraphDeviceEdge(one_qubit_duration)
|
|
@@ -95,7 +97,7 @@ def test_graph_device():
|
|
|
95
97
|
|
|
96
98
|
n_qubits = 4
|
|
97
99
|
qubits = cirq.LineQubit.range(n_qubits)
|
|
98
|
-
edges = {
|
|
100
|
+
edges: dict[Iterable[Hashable], Any] = {
|
|
99
101
|
(cirq.LineQubit(i), cirq.LineQubit((i + 1) % n_qubits)): two_qubit_edge
|
|
100
102
|
for i in range(n_qubits)
|
|
101
103
|
}
|
|
@@ -111,8 +113,8 @@ def test_graph_device():
|
|
|
111
113
|
|
|
112
114
|
assert ccgd.is_undirected_device_graph(device_graph)
|
|
113
115
|
with pytest.raises(TypeError):
|
|
114
|
-
ccgd.UndirectedGraphDevice('abc')
|
|
115
|
-
constraint_edges = {
|
|
116
|
+
ccgd.UndirectedGraphDevice('abc') # type: ignore[arg-type]
|
|
117
|
+
constraint_edges: dict[Iterable[Hashable], Any] = {
|
|
116
118
|
(frozenset(cirq.LineQubit.range(2)), frozenset(cirq.LineQubit.range(2, 4))): None,
|
|
117
119
|
(
|
|
118
120
|
frozenset(cirq.LineQubit.range(1, 3)),
|
|
@@ -123,7 +125,7 @@ def test_graph_device():
|
|
|
123
125
|
assert ccgd.is_crosstalk_graph(crosstalk_graph)
|
|
124
126
|
|
|
125
127
|
with pytest.raises(TypeError):
|
|
126
|
-
ccgd.UndirectedGraphDevice(device_graph, crosstalk_graph='abc')
|
|
128
|
+
ccgd.UndirectedGraphDevice(device_graph, crosstalk_graph='abc') # type: ignore[arg-type]
|
|
127
129
|
|
|
128
130
|
graph_device = ccgd.UndirectedGraphDevice(device_graph)
|
|
129
131
|
assert graph_device.crosstalk_graph == ccgd.UndirectedHypergraph()
|
|
@@ -141,7 +143,7 @@ def test_graph_device():
|
|
|
141
143
|
with pytest.raises(ValueError):
|
|
142
144
|
graph_device.validate_operation(cirq.CNOT(qubits[0], qubits[2]))
|
|
143
145
|
with pytest.raises(AttributeError):
|
|
144
|
-
graph_device.validate_operation(list((2, 3)))
|
|
146
|
+
graph_device.validate_operation(list((2, 3))) # type: ignore[arg-type]
|
|
145
147
|
|
|
146
148
|
moment = cirq.Moment([cirq.CNOT(*qubits[:2]), cirq.CNOT(*qubits[2:])])
|
|
147
149
|
with pytest.raises(ValueError):
|
|
@@ -155,7 +157,7 @@ def test_graph_device():
|
|
|
155
157
|
graph_device.validate_moment(moment)
|
|
156
158
|
|
|
157
159
|
|
|
158
|
-
def test_graph_device_copy_and_add():
|
|
160
|
+
def test_graph_device_copy_and_add() -> None:
|
|
159
161
|
a, b, c, d, e, f = cirq.LineQubit.range(6)
|
|
160
162
|
device_graph = ccgd.UndirectedHypergraph(labelled_edges={(a, b): None, (c, d): None})
|
|
161
163
|
crosstalk_graph = ccgd.UndirectedHypergraph(
|
|
@@ -71,7 +71,7 @@ class UndirectedHypergraph:
|
|
|
71
71
|
self._adjacency_lists[neighbor].difference_update((edge,))
|
|
72
72
|
del self._adjacency_lists[vertex]
|
|
73
73
|
|
|
74
|
-
def remove_vertices(self, vertices):
|
|
74
|
+
def remove_vertices(self, vertices) -> None:
|
|
75
75
|
for vertex in vertices:
|
|
76
76
|
self.remove_vertex(vertex)
|
|
77
77
|
|
|
@@ -82,7 +82,7 @@ class UndirectedHypergraph:
|
|
|
82
82
|
self._adjacency_lists[vertex].update((vertices,))
|
|
83
83
|
self._labelled_edges[vertices] = label
|
|
84
84
|
|
|
85
|
-
def add_edges(self, edges: dict[Iterable[Hashable], Any]):
|
|
85
|
+
def add_edges(self, edges: dict[Iterable[Hashable], Any]) -> None:
|
|
86
86
|
for vertices, label in edges.items():
|
|
87
87
|
self.add_edge(vertices, label)
|
|
88
88
|
|
|
@@ -21,7 +21,7 @@ import pytest
|
|
|
21
21
|
import cirq.contrib.graph_device as ccgd
|
|
22
22
|
|
|
23
23
|
|
|
24
|
-
def test_update_edge_label():
|
|
24
|
+
def test_update_edge_label() -> None:
|
|
25
25
|
edge = frozenset(range(3))
|
|
26
26
|
graph = ccgd.UndirectedHypergraph(labelled_edges={edge: 'a'})
|
|
27
27
|
assert graph.labelled_edges[edge] == 'a'
|
|
@@ -29,7 +29,7 @@ def test_update_edge_label():
|
|
|
29
29
|
assert graph.labelled_edges[edge] == 'b'
|
|
30
30
|
|
|
31
31
|
|
|
32
|
-
def test_hypergraph():
|
|
32
|
+
def test_hypergraph() -> None:
|
|
33
33
|
vertices = range(4)
|
|
34
34
|
graph = ccgd.UndirectedHypergraph(vertices=vertices)
|
|
35
35
|
assert graph.vertices == tuple(vertices)
|
|
@@ -55,7 +55,7 @@ def test_hypergraph():
|
|
|
55
55
|
for _ in range(10)
|
|
56
56
|
],
|
|
57
57
|
)
|
|
58
|
-
def test_eq(vertices, edges):
|
|
58
|
+
def test_eq(vertices, edges) -> None:
|
|
59
59
|
vertices = set(vertices).union(*edges)
|
|
60
60
|
graph_initialized = ccgd.UndirectedHypergraph(vertices=vertices, labelled_edges=edges)
|
|
61
61
|
graph_added_parallel = ccgd.UndirectedHypergraph()
|
|
@@ -69,15 +69,15 @@ def test_eq(vertices, edges):
|
|
|
69
69
|
assert graph_initialized == graph_added_parallel == graph_added_sequential
|
|
70
70
|
|
|
71
71
|
|
|
72
|
-
def test_random_hypergraph():
|
|
72
|
+
def test_random_hypergraph() -> None:
|
|
73
73
|
n_vertices = 4
|
|
74
74
|
graph = ccgd.UndirectedHypergraph.random(n_vertices, {1: 1.0})
|
|
75
|
-
assert sorted(graph.vertices) == sorted(range(n_vertices))
|
|
75
|
+
assert sorted(graph.vertices) == sorted(range(n_vertices)) # type: ignore[type-var]
|
|
76
76
|
assert set(graph.labelled_edges.values()) == set((None,))
|
|
77
77
|
assert tuple(len(edge) for edge in graph.edges) == (1,) * n_vertices
|
|
78
78
|
|
|
79
79
|
|
|
80
|
-
def test_copy():
|
|
80
|
+
def test_copy() -> None:
|
|
81
81
|
graph_original = ccgd.UndirectedHypergraph(labelled_edges={(0, 1): None})
|
|
82
82
|
graph_copy = graph_original.__copy__()
|
|
83
83
|
assert graph_copy == graph_original
|
|
@@ -85,20 +85,20 @@ def test_copy():
|
|
|
85
85
|
assert graph_copy != graph_original
|
|
86
86
|
|
|
87
87
|
|
|
88
|
-
def test_iadd():
|
|
88
|
+
def test_iadd() -> None:
|
|
89
89
|
graph = ccgd.UndirectedHypergraph(labelled_edges={(0, 1): None})
|
|
90
90
|
addend = ccgd.UndirectedHypergraph(labelled_edges={(1, 2): None})
|
|
91
91
|
graph += addend
|
|
92
92
|
assert set(graph.edges) == set(frozenset(e) for e in ((0, 1), (1, 2)))
|
|
93
|
-
assert sorted(graph.vertices) == [0, 1, 2]
|
|
93
|
+
assert sorted(graph.vertices) == [0, 1, 2] # type: ignore[type-var]
|
|
94
94
|
|
|
95
95
|
|
|
96
|
-
def test_add():
|
|
96
|
+
def test_add() -> None:
|
|
97
97
|
first_addend = ccgd.UndirectedHypergraph(labelled_edges={('a', 'b'): None})
|
|
98
98
|
second_addend = ccgd.UndirectedHypergraph(labelled_edges={('c', 'b'): None})
|
|
99
99
|
graph_sum = first_addend + second_addend
|
|
100
|
-
assert sorted(first_addend.vertices) == list('ab')
|
|
101
|
-
assert sorted(second_addend.vertices) == list('bc')
|
|
100
|
+
assert sorted(first_addend.vertices) == list('ab') # type: ignore[type-var]
|
|
101
|
+
assert sorted(second_addend.vertices) == list('bc') # type: ignore[type-var]
|
|
102
102
|
assert sorted(graph_sum.vertices) == list('abc')
|
|
103
103
|
assert sorted(first_addend.edges) == [frozenset('ab')]
|
|
104
104
|
assert sorted(second_addend.edges) == [frozenset('bc')]
|
|
@@ -14,21 +14,27 @@
|
|
|
14
14
|
|
|
15
15
|
from __future__ import annotations
|
|
16
16
|
|
|
17
|
+
from typing import TYPE_CHECKING
|
|
18
|
+
|
|
17
19
|
import pytest
|
|
18
20
|
|
|
19
21
|
import cirq
|
|
20
22
|
import cirq.contrib.graph_device as ccgd
|
|
21
23
|
|
|
24
|
+
if TYPE_CHECKING:
|
|
25
|
+
from cirq.contrib.graph_device.graph_device import UndirectedGraphDeviceEdge
|
|
26
|
+
|
|
22
27
|
|
|
23
|
-
def test_empty_uniform_undirected_linear_device():
|
|
28
|
+
def test_empty_uniform_undirected_linear_device() -> None:
|
|
24
29
|
n_qubits = 4
|
|
30
|
+
edge_labels: dict[int, UndirectedGraphDeviceEdge | None]
|
|
25
31
|
edge_labels = {}
|
|
26
32
|
device = ccgd.uniform_undirected_linear_device(n_qubits, edge_labels)
|
|
27
33
|
assert device.qubits == tuple()
|
|
28
34
|
assert device.edges == tuple()
|
|
29
35
|
|
|
30
36
|
|
|
31
|
-
def test_negative_arity_arg_uniform_undirected_linear_device():
|
|
37
|
+
def test_negative_arity_arg_uniform_undirected_linear_device() -> None:
|
|
32
38
|
with pytest.raises(ValueError):
|
|
33
39
|
ccgd.uniform_undirected_linear_device(5, {-1: None})
|
|
34
40
|
with pytest.raises(ValueError):
|
|
@@ -36,7 +42,7 @@ def test_negative_arity_arg_uniform_undirected_linear_device():
|
|
|
36
42
|
|
|
37
43
|
|
|
38
44
|
@pytest.mark.parametrize('arity', range(1, 5))
|
|
39
|
-
def test_regular_uniform_undirected_linear_device(arity):
|
|
45
|
+
def test_regular_uniform_undirected_linear_device(arity) -> None:
|
|
40
46
|
n_qubits = 10
|
|
41
47
|
edge_labels = {arity: None}
|
|
42
48
|
device = ccgd.uniform_undirected_linear_device(n_qubits, edge_labels)
|
|
@@ -17,10 +17,13 @@
|
|
|
17
17
|
from __future__ import annotations
|
|
18
18
|
|
|
19
19
|
import contextlib
|
|
20
|
+
from collections.abc import Generator
|
|
20
21
|
|
|
21
22
|
|
|
22
23
|
@contextlib.contextmanager
|
|
23
|
-
def disable_op_validation(
|
|
24
|
+
def disable_op_validation(
|
|
25
|
+
*, accept_debug_responsibility: bool = False
|
|
26
|
+
) -> Generator[None, None, None]:
|
|
24
27
|
if not accept_debug_responsibility:
|
|
25
28
|
raise ValueError(
|
|
26
29
|
"WARNING! Using disable_op_validation with invalid ops can cause "
|
cirq/contrib/json.py
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
|
|
4
4
|
from __future__ import annotations
|
|
5
5
|
|
|
6
|
-
from cirq.protocols.json_serialization import DEFAULT_RESOLVERS
|
|
6
|
+
from cirq.protocols.json_serialization import DEFAULT_RESOLVERS, ObjectFactory
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
def contrib_class_resolver(cirq_type: str):
|
|
9
|
+
def contrib_class_resolver(cirq_type: str) -> ObjectFactory | None:
|
|
10
10
|
"""Extend cirq's JSON API with resolvers for cirq contrib classes."""
|
|
11
11
|
from cirq.contrib.acquaintance import SwapPermutationGate
|
|
12
12
|
from cirq.contrib.bayesian_network import BayesianNetworkGate
|
|
@@ -42,7 +42,7 @@ class DepolarizingNoiseModel(devices.NoiseModel):
|
|
|
42
42
|
self.qubit_noise_gate = ops.DepolarizingChannel(depol_prob)
|
|
43
43
|
self._prepend = prepend
|
|
44
44
|
|
|
45
|
-
def noisy_moment(self, moment: cirq.Moment, system_qubits: Sequence[cirq.Qid]):
|
|
45
|
+
def noisy_moment(self, moment: cirq.Moment, system_qubits: Sequence[cirq.Qid]) -> cirq.OP_TREE:
|
|
46
46
|
if validate_all_measurements(moment) or self.is_virtual_moment(moment): # pragma: no cover
|
|
47
47
|
return moment
|
|
48
48
|
|
|
@@ -78,7 +78,7 @@ class ReadoutNoiseModel(devices.NoiseModel):
|
|
|
78
78
|
self.readout_noise_gate = ops.BitFlipChannel(bitflip_prob)
|
|
79
79
|
self._prepend = prepend
|
|
80
80
|
|
|
81
|
-
def noisy_moment(self, moment: cirq.Moment, system_qubits: Sequence[cirq.Qid]):
|
|
81
|
+
def noisy_moment(self, moment: cirq.Moment, system_qubits: Sequence[cirq.Qid]) -> cirq.OP_TREE:
|
|
82
82
|
if self.is_virtual_moment(moment):
|
|
83
83
|
return moment
|
|
84
84
|
if validate_all_measurements(moment):
|
|
@@ -115,7 +115,7 @@ class DampedReadoutNoiseModel(devices.NoiseModel):
|
|
|
115
115
|
self.readout_decay_gate = ops.AmplitudeDampingChannel(decay_prob)
|
|
116
116
|
self._prepend = prepend
|
|
117
117
|
|
|
118
|
-
def noisy_moment(self, moment: cirq.Moment, system_qubits: Sequence[cirq.Qid]):
|
|
118
|
+
def noisy_moment(self, moment: cirq.Moment, system_qubits: Sequence[cirq.Qid]) -> cirq.OP_TREE:
|
|
119
119
|
if self.is_virtual_moment(moment):
|
|
120
120
|
return moment
|
|
121
121
|
if validate_all_measurements(moment):
|
|
@@ -148,7 +148,7 @@ class DepolarizingWithReadoutNoiseModel(devices.NoiseModel):
|
|
|
148
148
|
self.qubit_noise_gate = ops.DepolarizingChannel(depol_prob)
|
|
149
149
|
self.readout_noise_gate = ops.BitFlipChannel(bitflip_prob)
|
|
150
150
|
|
|
151
|
-
def noisy_moment(self, moment: cirq.Moment, system_qubits: Sequence[cirq.Qid]):
|
|
151
|
+
def noisy_moment(self, moment: cirq.Moment, system_qubits: Sequence[cirq.Qid]) -> cirq.OP_TREE:
|
|
152
152
|
if validate_all_measurements(moment):
|
|
153
153
|
return [circuits.Moment(self.readout_noise_gate(q) for q in system_qubits), moment]
|
|
154
154
|
return [moment, circuits.Moment(self.qubit_noise_gate(q) for q in system_qubits)]
|
|
@@ -178,7 +178,7 @@ class DepolarizingWithDampedReadoutNoiseModel(devices.NoiseModel):
|
|
|
178
178
|
self.readout_noise_gate = ops.BitFlipChannel(bitflip_prob)
|
|
179
179
|
self.readout_decay_gate = ops.AmplitudeDampingChannel(decay_prob)
|
|
180
180
|
|
|
181
|
-
def noisy_moment(self, moment: cirq.Moment, system_qubits: Sequence[cirq.Qid]):
|
|
181
|
+
def noisy_moment(self, moment: cirq.Moment, system_qubits: Sequence[cirq.Qid]) -> cirq.OP_TREE:
|
|
182
182
|
if validate_all_measurements(moment):
|
|
183
183
|
return [
|
|
184
184
|
circuits.Moment(self.readout_decay_gate(q) for q in system_qubits),
|
|
@@ -25,6 +25,7 @@ def test_depol_noise() -> None:
|
|
|
25
25
|
qubits = cirq.LineQubit.range(2)
|
|
26
26
|
moment = cirq.Moment([cirq.X(qubits[0]), cirq.Y(qubits[1])])
|
|
27
27
|
noisy_mom = noise_model.noisy_moment(moment, system_qubits=qubits)
|
|
28
|
+
assert isinstance(noisy_mom, list)
|
|
28
29
|
assert len(noisy_mom) == 2
|
|
29
30
|
assert noisy_mom[0] == moment
|
|
30
31
|
for g in noisy_mom[1]:
|
|
@@ -36,6 +37,7 @@ def test_depol_noise_prepend() -> None:
|
|
|
36
37
|
qubits = cirq.LineQubit.range(2)
|
|
37
38
|
moment = cirq.Moment([cirq.X(qubits[0]), cirq.Y(qubits[1])])
|
|
38
39
|
noisy_mom = noise_model.noisy_moment(moment, system_qubits=qubits)
|
|
40
|
+
assert isinstance(noisy_mom, list)
|
|
39
41
|
assert len(noisy_mom) == 2
|
|
40
42
|
assert noisy_mom[1] == moment
|
|
41
43
|
for g in noisy_mom[0]:
|
|
@@ -99,6 +101,7 @@ def test_readout_noise_no_prepend() -> None:
|
|
|
99
101
|
qubits = cirq.LineQubit.range(2)
|
|
100
102
|
moment = cirq.Moment([cirq.measure(*qubits, key="meas")])
|
|
101
103
|
noisy_mom = noise_model.noisy_moment(moment, system_qubits=qubits)
|
|
104
|
+
assert isinstance(noisy_mom, list)
|
|
102
105
|
assert len(noisy_mom) == 2
|
|
103
106
|
assert noisy_mom[0] == moment
|
|
104
107
|
for g in noisy_mom[1]:
|
|
@@ -167,6 +170,7 @@ def test_damped_readout_noise_no_prepend() -> None:
|
|
|
167
170
|
qubits = cirq.LineQubit.range(2)
|
|
168
171
|
moment = cirq.Moment([cirq.measure(*qubits, key="meas")])
|
|
169
172
|
noisy_mom = noise_model.noisy_moment(moment, system_qubits=qubits)
|
|
173
|
+
assert isinstance(noisy_mom, list)
|
|
170
174
|
assert len(noisy_mom) == 2
|
|
171
175
|
assert noisy_mom[0] == moment
|
|
172
176
|
for g in noisy_mom[1]:
|
|
@@ -18,7 +18,8 @@ import cirq
|
|
|
18
18
|
import cirq.contrib.qcircuit as ccq
|
|
19
19
|
|
|
20
20
|
|
|
21
|
-
def test_get_qcircuit_diagram_info():
|
|
21
|
+
def test_get_qcircuit_diagram_info() -> None:
|
|
22
|
+
qubit_map: dict[cirq.LabelEntity, int]
|
|
22
23
|
qubits = cirq.NamedQubit('x'), cirq.NamedQubit('y')
|
|
23
24
|
|
|
24
25
|
gate = cirq.SwapPowGate(exponent=0.5)
|
|
@@ -18,7 +18,7 @@ class FakeDevice(cirq.Device):
|
|
|
18
18
|
qubits = cirq.GridQubit.rect(5, 5)
|
|
19
19
|
|
|
20
20
|
|
|
21
|
-
def test_generate_model_circuit():
|
|
21
|
+
def test_generate_model_circuit() -> None:
|
|
22
22
|
"""Test that a model circuit is randomly generated."""
|
|
23
23
|
model_circuit = cirq.contrib.quantum_volume.generate_model_circuit(
|
|
24
24
|
3, 3, random_state=np.random.RandomState(1)
|
|
@@ -29,7 +29,7 @@ def test_generate_model_circuit():
|
|
|
29
29
|
assert list(model_circuit.findall_operations_with_gate_type(cirq.MeasurementGate)) == []
|
|
30
30
|
|
|
31
31
|
|
|
32
|
-
def test_generate_model_circuit_without_seed():
|
|
32
|
+
def test_generate_model_circuit_without_seed() -> None:
|
|
33
33
|
"""Test that a model circuit is randomly generated without a seed."""
|
|
34
34
|
model_circuit = cirq.contrib.quantum_volume.generate_model_circuit(3, 3)
|
|
35
35
|
|
|
@@ -38,7 +38,7 @@ def test_generate_model_circuit_without_seed():
|
|
|
38
38
|
assert list(model_circuit.findall_operations_with_gate_type(cirq.MeasurementGate)) == []
|
|
39
39
|
|
|
40
40
|
|
|
41
|
-
def test_generate_model_circuit_seed():
|
|
41
|
+
def test_generate_model_circuit_seed() -> None:
|
|
42
42
|
"""Test that a model circuit is determined by its seed ."""
|
|
43
43
|
model_circuit_1 = cirq.contrib.quantum_volume.generate_model_circuit(
|
|
44
44
|
3, 3, random_state=np.random.RandomState(1)
|
|
@@ -54,7 +54,7 @@ def test_generate_model_circuit_seed():
|
|
|
54
54
|
assert model_circuit_2 != model_circuit_3
|
|
55
55
|
|
|
56
56
|
|
|
57
|
-
def test_compute_heavy_set():
|
|
57
|
+
def test_compute_heavy_set() -> None:
|
|
58
58
|
"""Test that the heavy set can be computed from a given circuit."""
|
|
59
59
|
a, b, c = cirq.LineQubit.range(3)
|
|
60
60
|
model_circuit = cirq.Circuit(
|
|
@@ -69,7 +69,7 @@ def test_compute_heavy_set():
|
|
|
69
69
|
assert cirq.contrib.quantum_volume.compute_heavy_set(model_circuit) == [5, 7]
|
|
70
70
|
|
|
71
71
|
|
|
72
|
-
def test_sample_heavy_set():
|
|
72
|
+
def test_sample_heavy_set() -> None:
|
|
73
73
|
"""Test that we correctly sample a circuit's heavy set"""
|
|
74
74
|
|
|
75
75
|
sampler = Mock(spec=cirq.Simulator)
|
|
@@ -89,7 +89,7 @@ def test_sample_heavy_set():
|
|
|
89
89
|
assert probability == 0.75
|
|
90
90
|
|
|
91
91
|
|
|
92
|
-
def test_sample_heavy_set_with_parity():
|
|
92
|
+
def test_sample_heavy_set_with_parity() -> None:
|
|
93
93
|
"""Test that we correctly sample a circuit's heavy set with a parity map"""
|
|
94
94
|
|
|
95
95
|
sampler = Mock(spec=cirq.Simulator)
|
|
@@ -122,7 +122,7 @@ def test_sample_heavy_set_with_parity():
|
|
|
122
122
|
assert probability == 0.5
|
|
123
123
|
|
|
124
124
|
|
|
125
|
-
def test_compile_circuit_router():
|
|
125
|
+
def test_compile_circuit_router() -> None:
|
|
126
126
|
"""Tests that the given router is used."""
|
|
127
127
|
router_mock = MagicMock()
|
|
128
128
|
cirq.contrib.quantum_volume.compile_circuit(
|
|
@@ -134,7 +134,7 @@ def test_compile_circuit_router():
|
|
|
134
134
|
router_mock.assert_called()
|
|
135
135
|
|
|
136
136
|
|
|
137
|
-
def test_compile_circuit():
|
|
137
|
+
def test_compile_circuit() -> None:
|
|
138
138
|
"""Tests that we are able to compile a model circuit."""
|
|
139
139
|
compiler_mock = MagicMock(side_effect=lambda circuit: circuit)
|
|
140
140
|
a, b, c = cirq.LineQubit.range(3)
|
|
@@ -154,7 +154,7 @@ def test_compile_circuit():
|
|
|
154
154
|
compiler_mock.assert_called_with(compilation_result.circuit)
|
|
155
155
|
|
|
156
156
|
|
|
157
|
-
def test_compile_circuit_replaces_swaps():
|
|
157
|
+
def test_compile_circuit_replaces_swaps() -> None:
|
|
158
158
|
"""Tests that the compiler never sees the SwapPermutationGates from the
|
|
159
159
|
router."""
|
|
160
160
|
compiler_mock = MagicMock(side_effect=lambda circuit: circuit)
|
|
@@ -195,7 +195,7 @@ def test_compile_circuit_replaces_swaps():
|
|
|
195
195
|
)
|
|
196
196
|
|
|
197
197
|
|
|
198
|
-
def test_compile_circuit_with_readout_correction():
|
|
198
|
+
def test_compile_circuit_with_readout_correction() -> None:
|
|
199
199
|
"""Tests that we are able to compile a model circuit with readout error
|
|
200
200
|
correction."""
|
|
201
201
|
compiler_mock = MagicMock(side_effect=lambda circuit: circuit)
|
|
@@ -222,9 +222,10 @@ def test_compile_circuit_with_readout_correction():
|
|
|
222
222
|
)
|
|
223
223
|
|
|
224
224
|
|
|
225
|
-
def test_compile_circuit_multiple_routing_attempts():
|
|
225
|
+
def test_compile_circuit_multiple_routing_attempts() -> None:
|
|
226
226
|
"""Tests that we make multiple attempts at routing and keep the best one."""
|
|
227
227
|
qubits = cirq.LineQubit.range(3)
|
|
228
|
+
initial_mapping: dict[cirq.Qid, cirq.Qid]
|
|
228
229
|
initial_mapping = dict(zip(qubits, qubits))
|
|
229
230
|
more_operations = cirq.Circuit([cirq.X.on_each(qubits), cirq.Y.on_each(qubits)])
|
|
230
231
|
more_qubits = cirq.Circuit([cirq.X.on_each(cirq.LineQubit.range(4))])
|
|
@@ -252,7 +253,7 @@ def test_compile_circuit_multiple_routing_attempts():
|
|
|
252
253
|
compiler_mock.assert_called_with(well_routed)
|
|
253
254
|
|
|
254
255
|
|
|
255
|
-
def test_compile_circuit_no_routing_attempts():
|
|
256
|
+
def test_compile_circuit_no_routing_attempts() -> None:
|
|
256
257
|
"""Tests that setting no routing attempts throws an error."""
|
|
257
258
|
a, b, c = cirq.LineQubit.range(3)
|
|
258
259
|
model_circuit = cirq.Circuit([cirq.Moment([cirq.X(a), cirq.Y(b), cirq.Z(c)])])
|
|
@@ -266,7 +267,7 @@ def test_compile_circuit_no_routing_attempts():
|
|
|
266
267
|
assert e.match('Unable to get routing for circuit')
|
|
267
268
|
|
|
268
269
|
|
|
269
|
-
def test_calculate_quantum_volume_result():
|
|
270
|
+
def test_calculate_quantum_volume_result() -> None:
|
|
270
271
|
"""Test that running the main loop returns the desired result"""
|
|
271
272
|
results = cirq.contrib.quantum_volume.calculate_quantum_volume(
|
|
272
273
|
num_qubits=3,
|
|
@@ -287,7 +288,7 @@ def test_calculate_quantum_volume_result():
|
|
|
287
288
|
cirq.to_json(results, buffer)
|
|
288
289
|
|
|
289
290
|
|
|
290
|
-
def test_calculate_quantum_volume_result_with_device_graph():
|
|
291
|
+
def test_calculate_quantum_volume_result_with_device_graph() -> None:
|
|
291
292
|
"""Test that running the main loop routes the circuit onto the given device
|
|
292
293
|
graph"""
|
|
293
294
|
device_qubits = [cirq.GridQubit(i, j) for i in range(2) for j in range(3)]
|
|
@@ -308,7 +309,7 @@ def test_calculate_quantum_volume_result_with_device_graph():
|
|
|
308
309
|
)
|
|
309
310
|
|
|
310
311
|
|
|
311
|
-
def test_calculate_quantum_volume_loop():
|
|
312
|
+
def test_calculate_quantum_volume_loop() -> None:
|
|
312
313
|
"""Test that calculate_quantum_volume is able to run without erring."""
|
|
313
314
|
# Keep test from taking a long time by lowering circuits and routing
|
|
314
315
|
# attempts.
|
|
@@ -323,7 +324,7 @@ def test_calculate_quantum_volume_loop():
|
|
|
323
324
|
)
|
|
324
325
|
|
|
325
326
|
|
|
326
|
-
def test_calculate_quantum_volume_loop_with_readout_correction():
|
|
327
|
+
def test_calculate_quantum_volume_loop_with_readout_correction() -> None:
|
|
327
328
|
"""Test that calculate_quantum_volume is able to run without erring with
|
|
328
329
|
readout error correction."""
|
|
329
330
|
# Keep test from taking a long time by lowering circuits and routing
|
|
@@ -197,7 +197,7 @@ def circuit_to_density_matrix_tensors(
|
|
|
197
197
|
|
|
198
198
|
|
|
199
199
|
def tensor_density_matrix(
|
|
200
|
-
circuit: cirq.Circuit, qubits:
|
|
200
|
+
circuit: cirq.Circuit, qubits: Sequence[cirq.Qid] | None = None
|
|
201
201
|
) -> np.ndarray:
|
|
202
202
|
"""Given a circuit with mixtures or channels, contract a tensor network
|
|
203
203
|
representing the resultant density matrix.
|
|
@@ -8,7 +8,7 @@ import cirq
|
|
|
8
8
|
import cirq.contrib.quimb as ccq
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
def test_tensor_density_matrix_1():
|
|
11
|
+
def test_tensor_density_matrix_1() -> None:
|
|
12
12
|
q = cirq.LineQubit.range(2)
|
|
13
13
|
c = cirq.Circuit(cirq.YPowGate(exponent=0.25).on(q[0]))
|
|
14
14
|
|
|
@@ -17,7 +17,7 @@ def test_tensor_density_matrix_1():
|
|
|
17
17
|
np.testing.assert_allclose(rho1, rho2, atol=1e-15)
|
|
18
18
|
|
|
19
19
|
|
|
20
|
-
def test_tensor_density_matrix_optional_qubits():
|
|
20
|
+
def test_tensor_density_matrix_optional_qubits() -> None:
|
|
21
21
|
q = cirq.LineQubit.range(2)
|
|
22
22
|
c = cirq.Circuit(cirq.YPowGate(exponent=0.25).on(q[0]))
|
|
23
23
|
|
|
@@ -26,7 +26,7 @@ def test_tensor_density_matrix_optional_qubits():
|
|
|
26
26
|
np.testing.assert_allclose(rho1, rho2, atol=1e-15)
|
|
27
27
|
|
|
28
28
|
|
|
29
|
-
def test_tensor_density_matrix_noise_1():
|
|
29
|
+
def test_tensor_density_matrix_noise_1() -> None:
|
|
30
30
|
q = cirq.LineQubit.range(2)
|
|
31
31
|
c = cirq.Circuit(
|
|
32
32
|
cirq.YPowGate(exponent=0.25).on(q[0]),
|
|
@@ -39,7 +39,7 @@ def test_tensor_density_matrix_noise_1():
|
|
|
39
39
|
np.testing.assert_allclose(rho1, rho2, atol=1e-15)
|
|
40
40
|
|
|
41
41
|
|
|
42
|
-
def test_tensor_density_matrix_2():
|
|
42
|
+
def test_tensor_density_matrix_2() -> None:
|
|
43
43
|
q = cirq.LineQubit.range(2)
|
|
44
44
|
rs = np.random.RandomState(52)
|
|
45
45
|
for _ in range(10):
|
|
@@ -50,7 +50,7 @@ def test_tensor_density_matrix_2():
|
|
|
50
50
|
np.testing.assert_allclose(rho1, rho2, atol=1e-8)
|
|
51
51
|
|
|
52
52
|
|
|
53
|
-
def test_tensor_density_matrix_3():
|
|
53
|
+
def test_tensor_density_matrix_3() -> None:
|
|
54
54
|
qubits = cirq.LineQubit.range(10)
|
|
55
55
|
circuit = cirq.testing.random_circuit(qubits=qubits, n_moments=10, op_density=0.8)
|
|
56
56
|
rho1 = cirq.final_density_matrix(circuit, dtype=np.complex128)
|
|
@@ -58,7 +58,7 @@ def test_tensor_density_matrix_3():
|
|
|
58
58
|
np.testing.assert_allclose(rho1, rho2, atol=1e-8)
|
|
59
59
|
|
|
60
60
|
|
|
61
|
-
def test_tensor_density_matrix_4():
|
|
61
|
+
def test_tensor_density_matrix_4() -> None:
|
|
62
62
|
qubits = cirq.LineQubit.range(4)
|
|
63
63
|
circuit = cirq.testing.random_circuit(qubits=qubits, n_moments=100, op_density=0.8)
|
|
64
64
|
circuit = cirq.drop_empty_moments(circuit)
|
|
@@ -69,7 +69,7 @@ def test_tensor_density_matrix_4():
|
|
|
69
69
|
np.testing.assert_allclose(rho1, rho2, atol=1e-8)
|
|
70
70
|
|
|
71
71
|
|
|
72
|
-
def test_tensor_density_matrix_gridqubit():
|
|
72
|
+
def test_tensor_density_matrix_gridqubit() -> None:
|
|
73
73
|
qubits = cirq.GridQubit.rect(2, 2)
|
|
74
74
|
circuit = cirq.testing.random_circuit(qubits=qubits, n_moments=10, op_density=0.8)
|
|
75
75
|
circuit = cirq.drop_empty_moments(circuit)
|