cirq-core 1.6.0.dev20250520054601__py3-none-any.whl → 1.6.0.dev20250520181654__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/_compat.py +15 -17
- cirq/_compat_test.py +6 -9
- cirq/_doc.py +2 -2
- cirq/_import.py +6 -6
- cirq/_version.py +1 -1
- cirq/_version_test.py +1 -1
- cirq/circuits/_block_diagram_drawer.py +9 -10
- cirq/circuits/_box_drawing_character_data.py +6 -8
- cirq/circuits/_bucket_priority_queue.py +7 -7
- cirq/circuits/circuit.py +118 -125
- cirq/circuits/circuit_operation.py +38 -52
- cirq/circuits/circuit_test.py +4 -4
- cirq/circuits/frozen_circuit.py +13 -23
- cirq/circuits/moment.py +23 -29
- cirq/circuits/optimization_pass.py +4 -4
- cirq/circuits/optimization_pass_test.py +4 -6
- cirq/circuits/qasm_output.py +11 -11
- cirq/circuits/text_diagram_drawer.py +21 -36
- cirq/contrib/acquaintance/bipartite.py +5 -8
- cirq/contrib/acquaintance/executor.py +5 -5
- cirq/contrib/acquaintance/executor_test.py +3 -3
- cirq/contrib/acquaintance/gates.py +16 -26
- cirq/contrib/acquaintance/gates_test.py +3 -3
- cirq/contrib/acquaintance/mutation_utils.py +4 -4
- cirq/contrib/acquaintance/optimizers.py +4 -4
- cirq/contrib/acquaintance/permutation.py +15 -27
- cirq/contrib/acquaintance/shift.py +3 -3
- cirq/contrib/acquaintance/shift_swap_network.py +4 -4
- cirq/contrib/acquaintance/strategies/cubic.py +2 -2
- cirq/contrib/acquaintance/strategies/quartic_paired.py +6 -6
- cirq/contrib/bayesian_network/bayesian_network_gate.py +9 -10
- cirq/contrib/circuitdag/circuit_dag.py +2 -2
- cirq/contrib/custom_simulators/custom_state_simulator.py +3 -3
- cirq/contrib/custom_simulators/custom_state_simulator_test.py +4 -4
- cirq/contrib/graph_device/graph_device.py +5 -5
- cirq/contrib/graph_device/hypergraph.py +12 -12
- cirq/contrib/graph_device/uniform_graph_device.py +4 -4
- cirq/contrib/paulistring/clifford_optimize.py +2 -2
- cirq/contrib/paulistring/clifford_target_gateset.py +7 -7
- cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation.py +31 -31
- cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation_test.py +23 -23
- cirq/contrib/paulistring/recombine.py +3 -3
- cirq/contrib/paulistring/separate.py +2 -2
- cirq/contrib/qasm_import/_parser.py +20 -32
- cirq/contrib/qcircuit/qcircuit_diagram_info.py +3 -5
- cirq/contrib/quantum_volume/quantum_volume.py +24 -24
- cirq/contrib/quimb/density_matrix.py +12 -14
- cirq/contrib/quimb/mps_simulator.py +20 -20
- cirq/contrib/quimb/state_vector.py +6 -10
- cirq/contrib/quirk/export_to_quirk.py +3 -3
- cirq/contrib/quirk/quirk_gate.py +15 -15
- cirq/contrib/routing/device.py +3 -3
- cirq/contrib/routing/greedy.py +10 -21
- cirq/contrib/routing/initialization.py +2 -2
- cirq/contrib/routing/swap_network.py +3 -3
- cirq/contrib/routing/utils.py +2 -2
- cirq/contrib/shuffle_circuits/shuffle_circuits_with_readout_benchmarking.py +8 -8
- cirq/contrib/svg/svg.py +3 -3
- cirq/devices/grid_device_metadata.py +10 -10
- cirq/devices/grid_qubit.py +20 -20
- cirq/devices/insertion_noise_model.py +5 -5
- cirq/devices/line_qubit.py +13 -13
- cirq/devices/named_topologies.py +18 -29
- cirq/devices/noise_model.py +3 -3
- cirq/devices/noise_properties.py +2 -2
- cirq/devices/noise_properties_test.py +1 -3
- cirq/devices/noise_utils.py +7 -7
- cirq/devices/superconducting_qubits_noise_properties.py +21 -21
- cirq/devices/superconducting_qubits_noise_properties_test.py +5 -7
- cirq/devices/thermal_noise_model.py +14 -14
- cirq/devices/unconstrained_device.py +2 -2
- cirq/experiments/benchmarking/parallel_xeb.py +29 -31
- cirq/experiments/n_qubit_tomography.py +5 -7
- cirq/experiments/qubit_characterizations.py +29 -40
- cirq/experiments/qubit_characterizations_test.py +1 -1
- cirq/experiments/random_quantum_circuit_generation.py +19 -33
- cirq/experiments/random_quantum_circuit_generation_test.py +6 -6
- cirq/experiments/readout_confusion_matrix.py +14 -14
- cirq/experiments/single_qubit_readout_calibration.py +12 -12
- cirq/experiments/t2_decay_experiment.py +7 -7
- cirq/experiments/two_qubit_xeb.py +32 -32
- cirq/experiments/two_qubit_xeb_test.py +5 -5
- cirq/experiments/xeb_fitting.py +25 -25
- cirq/experiments/xeb_sampling.py +22 -33
- cirq/experiments/xeb_simulation.py +5 -5
- cirq/experiments/xeb_simulation_test.py +3 -3
- cirq/experiments/z_phase_calibration.py +19 -19
- cirq/interop/quirk/cells/arithmetic_cells.py +23 -36
- cirq/interop/quirk/cells/cell.py +9 -21
- cirq/interop/quirk/cells/composite_cell.py +7 -22
- cirq/interop/quirk/cells/control_cells.py +8 -8
- cirq/interop/quirk/cells/input_cells.py +4 -4
- cirq/interop/quirk/cells/input_rotation_cells.py +5 -5
- cirq/interop/quirk/cells/parse.py +20 -23
- cirq/interop/quirk/cells/qubit_permutation_cells.py +3 -3
- cirq/interop/quirk/cells/swap_cell.py +3 -3
- cirq/interop/quirk/cells/testing.py +5 -7
- cirq/interop/quirk/url_to_circuit.py +17 -33
- cirq/json_resolver_cache.py +6 -6
- cirq/linalg/decompositions.py +20 -31
- cirq/linalg/diagonalize.py +4 -4
- cirq/linalg/diagonalize_test.py +3 -4
- cirq/linalg/operator_spaces.py +5 -5
- cirq/linalg/predicates.py +7 -7
- cirq/linalg/transformations.py +20 -20
- cirq/ops/arithmetic_operation.py +13 -15
- cirq/ops/boolean_hamiltonian.py +17 -17
- cirq/ops/classically_controlled_operation.py +13 -25
- cirq/ops/clifford_gate.py +31 -35
- cirq/ops/clifford_gate_test.py +2 -3
- cirq/ops/common_channels.py +30 -32
- cirq/ops/common_gates.py +64 -74
- cirq/ops/control_values.py +12 -12
- cirq/ops/controlled_gate.py +15 -30
- cirq/ops/controlled_gate_test.py +5 -5
- cirq/ops/controlled_operation.py +12 -25
- cirq/ops/controlled_operation_test.py +5 -5
- cirq/ops/dense_pauli_string.py +23 -34
- cirq/ops/dense_pauli_string_test.py +1 -2
- cirq/ops/diagonal_gate.py +9 -20
- cirq/ops/diagonal_gate_test.py +1 -3
- cirq/ops/eigen_gate.py +11 -23
- cirq/ops/eigen_gate_test.py +6 -8
- cirq/ops/fourier_transform.py +5 -5
- cirq/ops/fsim_gate.py +14 -14
- cirq/ops/gate_operation.py +23 -44
- cirq/ops/gateset.py +23 -37
- cirq/ops/gateset_test.py +2 -2
- cirq/ops/global_phase_op.py +8 -10
- cirq/ops/greedy_qubit_manager.py +6 -6
- cirq/ops/identity.py +9 -9
- cirq/ops/kraus_channel.py +7 -7
- cirq/ops/linear_combinations.py +29 -48
- cirq/ops/matrix_gates.py +8 -8
- cirq/ops/measure_util.py +13 -14
- cirq/ops/measurement_gate.py +18 -29
- cirq/ops/mixed_unitary_channel.py +8 -8
- cirq/ops/named_qubit.py +10 -10
- cirq/ops/op_tree.py +7 -7
- cirq/ops/parallel_gate.py +5 -5
- cirq/ops/parity_gates.py +14 -14
- cirq/ops/pauli_gates.py +8 -10
- cirq/ops/pauli_interaction_gate.py +6 -6
- cirq/ops/pauli_measurement_gate.py +11 -23
- cirq/ops/pauli_string.py +35 -52
- cirq/ops/pauli_string_phasor.py +4 -14
- cirq/ops/pauli_string_raw_types.py +3 -3
- cirq/ops/pauli_sum_exponential.py +2 -2
- cirq/ops/permutation_gate.py +4 -4
- cirq/ops/phased_iswap_gate.py +9 -9
- cirq/ops/phased_x_gate.py +10 -10
- cirq/ops/phased_x_z_gate.py +11 -11
- cirq/ops/projector.py +6 -6
- cirq/ops/qubit_manager.py +6 -6
- cirq/ops/qubit_order.py +3 -3
- cirq/ops/random_gate_channel.py +4 -4
- cirq/ops/raw_types.py +48 -70
- cirq/ops/state_preparation_channel.py +3 -3
- cirq/ops/swap_gates.py +9 -9
- cirq/ops/tags.py +2 -4
- cirq/ops/three_qubit_gates.py +20 -38
- cirq/ops/two_qubit_diagonal_gate.py +5 -5
- cirq/ops/uniform_superposition_gate.py +2 -2
- cirq/ops/wait_gate.py +5 -5
- cirq/protocols/act_on_protocol_test.py +3 -3
- cirq/protocols/apply_channel_protocol.py +8 -14
- cirq/protocols/apply_mixture_protocol.py +14 -16
- cirq/protocols/apply_mixture_protocol_test.py +5 -6
- cirq/protocols/apply_unitary_protocol.py +17 -19
- cirq/protocols/circuit_diagram_info_protocol.py +19 -30
- cirq/protocols/decompose_protocol.py +30 -34
- cirq/protocols/inverse_protocol.py +7 -7
- cirq/protocols/json_serialization.py +32 -51
- cirq/protocols/json_serialization_test.py +9 -10
- cirq/protocols/kraus_protocol.py +4 -4
- cirq/protocols/kraus_protocol_test.py +3 -3
- cirq/protocols/measurement_key_protocol.py +11 -13
- cirq/protocols/mixture_protocol.py +4 -4
- cirq/protocols/qasm.py +11 -13
- cirq/protocols/qid_shape_protocol.py +6 -8
- cirq/qis/clifford_tableau.py +12 -12
- cirq/qis/measures.py +7 -7
- cirq/qis/quantum_state_representation.py +3 -3
- cirq/qis/states.py +51 -51
- cirq/sim/classical_simulator.py +10 -10
- cirq/sim/clifford/clifford_simulator.py +6 -6
- cirq/sim/clifford/clifford_tableau_simulation_state_test.py +1 -3
- cirq/sim/clifford/stabilizer_sampler.py +4 -4
- cirq/sim/clifford/stabilizer_state_ch_form.py +3 -3
- cirq/sim/density_matrix_simulation_state.py +15 -15
- cirq/sim/density_matrix_simulator.py +11 -11
- cirq/sim/density_matrix_utils.py +9 -9
- cirq/sim/mux.py +9 -9
- cirq/sim/simulation_product_state.py +9 -9
- cirq/sim/simulation_product_state_test.py +2 -2
- cirq/sim/simulation_state.py +14 -27
- cirq/sim/simulation_state_base.py +8 -24
- cirq/sim/simulation_utils.py +3 -4
- cirq/sim/simulator.py +28 -43
- cirq/sim/simulator_base.py +12 -25
- cirq/sim/simulator_base_test.py +6 -6
- cirq/sim/simulator_test.py +7 -7
- cirq/sim/sparse_simulator.py +8 -8
- cirq/sim/state_vector.py +8 -8
- cirq/sim/state_vector_simulation_state.py +17 -17
- cirq/sim/state_vector_simulator.py +4 -4
- cirq/study/flatten_expressions.py +12 -14
- cirq/study/resolver.py +9 -11
- cirq/study/result.py +11 -24
- cirq/study/sweepable.py +5 -5
- cirq/study/sweeps.py +27 -40
- cirq/testing/circuit_compare.py +5 -5
- cirq/testing/consistent_controlled_gate_op_test.py +7 -11
- cirq/testing/consistent_protocols.py +10 -10
- cirq/testing/consistent_protocols_test.py +7 -7
- cirq/testing/consistent_qasm.py +4 -4
- cirq/testing/consistent_qasm_test.py +2 -3
- cirq/testing/devices.py +4 -5
- cirq/testing/equals_tester.py +2 -2
- cirq/testing/equivalent_basis_map.py +4 -4
- cirq/testing/equivalent_repr_eval.py +3 -3
- cirq/testing/json.py +14 -14
- cirq/testing/logs.py +3 -3
- cirq/testing/no_identifier_qubit.py +2 -3
- cirq/testing/random_circuit.py +7 -7
- cirq/testing/random_circuit_test.py +3 -3
- cirq/transformers/analytical_decompositions/clifford_decomposition.py +16 -16
- cirq/transformers/analytical_decompositions/controlled_gate_decomposition.py +13 -13
- cirq/transformers/analytical_decompositions/cphase_to_fsim.py +5 -5
- cirq/transformers/analytical_decompositions/cphase_to_fsim_test.py +3 -3
- cirq/transformers/analytical_decompositions/pauli_string_decomposition.py +3 -3
- cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py +4 -4
- cirq/transformers/analytical_decompositions/single_qubit_decompositions.py +6 -7
- cirq/transformers/analytical_decompositions/single_to_two_qubit_isometry.py +2 -2
- cirq/transformers/analytical_decompositions/three_qubit_decomposition.py +7 -7
- cirq/transformers/analytical_decompositions/two_qubit_state_preparation.py +4 -4
- cirq/transformers/analytical_decompositions/two_qubit_to_cz.py +7 -7
- cirq/transformers/analytical_decompositions/two_qubit_to_fsim.py +11 -11
- cirq/transformers/analytical_decompositions/two_qubit_to_ms.py +5 -5
- cirq/transformers/analytical_decompositions/two_qubit_to_sqrt_iswap.py +14 -14
- cirq/transformers/dynamical_decoupling.py +13 -13
- cirq/transformers/dynamical_decoupling_test.py +4 -4
- cirq/transformers/eject_phased_paulis.py +16 -16
- cirq/transformers/eject_z.py +5 -7
- cirq/transformers/gauge_compiling/gauge_compiling.py +38 -38
- cirq/transformers/gauge_compiling/sqrt_cz_gauge.py +2 -2
- cirq/transformers/heuristic_decompositions/two_qubit_gate_tabulation.py +8 -8
- cirq/transformers/insertion_sort.py +5 -5
- cirq/transformers/measurement_transformers.py +14 -14
- cirq/transformers/merge_k_qubit_gates_test.py +1 -3
- cirq/transformers/merge_single_qubit_gates_test.py +1 -3
- cirq/transformers/qubit_management_transformers.py +5 -5
- cirq/transformers/routing/initial_mapper.py +4 -4
- cirq/transformers/routing/line_initial_mapper.py +9 -9
- cirq/transformers/routing/mapping_manager.py +7 -7
- cirq/transformers/routing/route_circuit_cqc.py +27 -27
- cirq/transformers/routing/visualize_routed_circuit.py +4 -4
- cirq/transformers/stratify.py +8 -8
- cirq/transformers/synchronize_terminal_measurements.py +6 -6
- cirq/transformers/target_gatesets/compilation_target_gateset.py +8 -8
- cirq/transformers/target_gatesets/compilation_target_gateset_test.py +2 -2
- cirq/transformers/target_gatesets/cz_gateset.py +4 -4
- cirq/transformers/target_gatesets/sqrt_iswap_gateset.py +5 -5
- cirq/transformers/transformer_api.py +11 -26
- cirq/transformers/transformer_primitives.py +24 -36
- cirq/transformers/transformer_primitives_test.py +3 -3
- cirq/value/classical_data.py +18 -18
- cirq/value/condition.py +8 -8
- cirq/value/digits.py +7 -7
- cirq/value/duration.py +12 -12
- cirq/value/linear_dict.py +8 -12
- cirq/value/measurement_key.py +8 -8
- cirq/value/product_state.py +9 -9
- cirq/value/value_equality_attr.py +4 -4
- cirq/vis/heatmap.py +23 -35
- cirq/work/collector.py +9 -17
- cirq/work/observable_grouping.py +4 -7
- cirq/work/observable_measurement.py +29 -41
- cirq/work/observable_measurement_data.py +14 -14
- cirq/work/observable_measurement_test.py +2 -2
- cirq/work/observable_settings.py +9 -10
- cirq/work/pauli_sum_collector.py +5 -5
- cirq/work/sampler.py +17 -17
- cirq/work/zeros_sampler.py +3 -3
- {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520181654.dist-info}/METADATA +1 -1
- {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520181654.dist-info}/RECORD +289 -289
- {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520181654.dist-info}/WHEEL +1 -1
- {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520181654.dist-info}/licenses/LICENSE +0 -0
- {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520181654.dist-info}/top_level.txt +0 -0
|
@@ -16,18 +16,7 @@ from __future__ import annotations
|
|
|
16
16
|
|
|
17
17
|
import re
|
|
18
18
|
from fractions import Fraction
|
|
19
|
-
from typing import
|
|
20
|
-
Any,
|
|
21
|
-
Dict,
|
|
22
|
-
Iterable,
|
|
23
|
-
List,
|
|
24
|
-
Optional,
|
|
25
|
-
overload,
|
|
26
|
-
Sequence,
|
|
27
|
-
TYPE_CHECKING,
|
|
28
|
-
TypeVar,
|
|
29
|
-
Union,
|
|
30
|
-
)
|
|
19
|
+
from typing import Any, Iterable, overload, Sequence, TYPE_CHECKING, TypeVar, Union
|
|
31
20
|
|
|
32
21
|
import numpy as np
|
|
33
22
|
import sympy
|
|
@@ -52,7 +41,7 @@ class CircuitDiagramInfo:
|
|
|
52
41
|
wire_symbols: Iterable[str],
|
|
53
42
|
exponent: Any = 1,
|
|
54
43
|
connected: bool = True,
|
|
55
|
-
exponent_qubit_index:
|
|
44
|
+
exponent_qubit_index: int | None = None,
|
|
56
45
|
auto_exponent_parens: bool = True,
|
|
57
46
|
) -> None:
|
|
58
47
|
"""Inits CircuitDiagramInfo.
|
|
@@ -106,8 +95,8 @@ class CircuitDiagramInfo:
|
|
|
106
95
|
)
|
|
107
96
|
|
|
108
97
|
def _wire_symbols_including_formatted_exponent(
|
|
109
|
-
self, args: cirq.CircuitDiagramInfoArgs, *, preferred_exponent_index:
|
|
110
|
-
) ->
|
|
98
|
+
self, args: cirq.CircuitDiagramInfoArgs, *, preferred_exponent_index: int | None = None
|
|
99
|
+
) -> list[str]:
|
|
111
100
|
result = list(self.wire_symbols)
|
|
112
101
|
exponent = self._formatted_exponent(args)
|
|
113
102
|
if exponent is not None:
|
|
@@ -124,7 +113,7 @@ class CircuitDiagramInfo:
|
|
|
124
113
|
result[k] += f"^{exponent}"
|
|
125
114
|
return result
|
|
126
115
|
|
|
127
|
-
def _formatted_exponent(self, args: cirq.CircuitDiagramInfoArgs) ->
|
|
116
|
+
def _formatted_exponent(self, args: cirq.CircuitDiagramInfoArgs) -> str | None:
|
|
128
117
|
if protocols.is_parameterized(self.exponent):
|
|
129
118
|
name = str(self.exponent)
|
|
130
119
|
return f'({name})' if _is_exposed_formula(name) else name
|
|
@@ -201,11 +190,11 @@ class CircuitDiagramInfoArgs:
|
|
|
201
190
|
|
|
202
191
|
def __init__(
|
|
203
192
|
self,
|
|
204
|
-
known_qubits:
|
|
205
|
-
known_qubit_count:
|
|
193
|
+
known_qubits: Iterable[cirq.Qid] | None,
|
|
194
|
+
known_qubit_count: int | None,
|
|
206
195
|
use_unicode_characters: bool,
|
|
207
|
-
precision:
|
|
208
|
-
label_map:
|
|
196
|
+
precision: int | None,
|
|
197
|
+
label_map: dict[cirq.LabelEntity, int] | None,
|
|
209
198
|
include_tags: bool = True,
|
|
210
199
|
transpose: bool = False,
|
|
211
200
|
) -> None:
|
|
@@ -244,7 +233,7 @@ class CircuitDiagramInfoArgs:
|
|
|
244
233
|
f'transpose={self.transpose!r})'
|
|
245
234
|
)
|
|
246
235
|
|
|
247
|
-
def format_real(self, val:
|
|
236
|
+
def format_real(self, val: sympy.Basic | int | float) -> str:
|
|
248
237
|
if isinstance(val, sympy.Basic):
|
|
249
238
|
return str(val)
|
|
250
239
|
if val == int(val):
|
|
@@ -253,7 +242,7 @@ class CircuitDiagramInfoArgs:
|
|
|
253
242
|
return str(val)
|
|
254
243
|
return f'{float(val):.{self.precision}}'
|
|
255
244
|
|
|
256
|
-
def format_complex(self, val:
|
|
245
|
+
def format_complex(self, val: sympy.Basic | int | float | cirq.TParamValComplex) -> str:
|
|
257
246
|
if isinstance(val, sympy.Basic):
|
|
258
247
|
return str(val)
|
|
259
248
|
c = complex(val)
|
|
@@ -265,7 +254,7 @@ class CircuitDiagramInfoArgs:
|
|
|
265
254
|
imag_str = '' if abs_imag == 1 else self.format_real(abs_imag)
|
|
266
255
|
return f'{self.format_real(c.real)}{joiner}{imag_str}i'
|
|
267
256
|
|
|
268
|
-
def format_radians(self, radians:
|
|
257
|
+
def format_radians(self, radians: sympy.Basic | int | float) -> str:
|
|
269
258
|
"""Returns angle in radians as a human-readable string."""
|
|
270
259
|
if protocols.is_parameterized(radians):
|
|
271
260
|
return str(radians)
|
|
@@ -316,7 +305,7 @@ class SupportsCircuitDiagramInfo(Protocol):
|
|
|
316
305
|
@doc_private
|
|
317
306
|
def _circuit_diagram_info_(
|
|
318
307
|
self, args: CircuitDiagramInfoArgs
|
|
319
|
-
) ->
|
|
308
|
+
) -> str | Iterable[str] | CircuitDiagramInfo:
|
|
320
309
|
"""Describes how to draw an operation in a circuit diagram.
|
|
321
310
|
|
|
322
311
|
This method is used by the global `cirq.diagram_info` method. If this
|
|
@@ -341,7 +330,7 @@ def _op_info_with_fallback(
|
|
|
341
330
|
op: cirq.Operation, args: cirq.CircuitDiagramInfoArgs
|
|
342
331
|
) -> cirq.CircuitDiagramInfo:
|
|
343
332
|
info = protocols.circuit_diagram_info(op, args, None)
|
|
344
|
-
rows:
|
|
333
|
+
rows: list[LabelEntity] = list(op.qubits)
|
|
345
334
|
if args.label_map is not None:
|
|
346
335
|
rows += protocols.measurement_keys_touched(op) & args.label_map.keys()
|
|
347
336
|
if info is not None and info.wire_symbols:
|
|
@@ -371,25 +360,25 @@ def _op_info_with_fallback(
|
|
|
371
360
|
# pylint: disable=function-redefined
|
|
372
361
|
@overload
|
|
373
362
|
def circuit_diagram_info(
|
|
374
|
-
val: Any, args:
|
|
363
|
+
val: Any, args: CircuitDiagramInfoArgs | None = None
|
|
375
364
|
) -> CircuitDiagramInfo:
|
|
376
365
|
pass
|
|
377
366
|
|
|
378
367
|
|
|
379
368
|
@overload
|
|
380
369
|
def circuit_diagram_info(
|
|
381
|
-
val: Any, args:
|
|
382
|
-
) ->
|
|
370
|
+
val: Any, args: CircuitDiagramInfoArgs | None, default: TDefault
|
|
371
|
+
) -> CircuitDiagramInfo | TDefault:
|
|
383
372
|
pass
|
|
384
373
|
|
|
385
374
|
|
|
386
375
|
@overload
|
|
387
|
-
def circuit_diagram_info(val: Any, *, default: TDefault) ->
|
|
376
|
+
def circuit_diagram_info(val: Any, *, default: TDefault) -> CircuitDiagramInfo | TDefault:
|
|
388
377
|
pass
|
|
389
378
|
|
|
390
379
|
|
|
391
380
|
def circuit_diagram_info(
|
|
392
|
-
val: Any, args:
|
|
381
|
+
val: Any, args: CircuitDiagramInfoArgs | None = None, default=RaiseTypeErrorIfNotProvided
|
|
393
382
|
):
|
|
394
383
|
"""Requests information on drawing an operation in a circuit diagram.
|
|
395
384
|
|
|
@@ -22,14 +22,10 @@ from types import NotImplementedType
|
|
|
22
22
|
from typing import (
|
|
23
23
|
Any,
|
|
24
24
|
Callable,
|
|
25
|
-
Dict,
|
|
26
25
|
Iterable,
|
|
27
26
|
Iterator,
|
|
28
|
-
List,
|
|
29
|
-
Optional,
|
|
30
27
|
overload,
|
|
31
28
|
Sequence,
|
|
32
|
-
Tuple,
|
|
33
29
|
TYPE_CHECKING,
|
|
34
30
|
TypeVar,
|
|
35
31
|
Union,
|
|
@@ -58,11 +54,11 @@ _CONTEXT_COUNTER = itertools.count() # Use _reset_context_counter() to reset th
|
|
|
58
54
|
@runtime_checkable
|
|
59
55
|
class OpDecomposerWithContext(Protocol):
|
|
60
56
|
def __call__(
|
|
61
|
-
self, __op: cirq.Operation, *, context:
|
|
57
|
+
self, __op: cirq.Operation, *, context: cirq.DecompositionContext | None = None
|
|
62
58
|
) -> DecomposeResult: ...
|
|
63
59
|
|
|
64
60
|
|
|
65
|
-
OpDecomposer =
|
|
61
|
+
OpDecomposer = Callable[['cirq.Operation'], DecomposeResult] | OpDecomposerWithContext
|
|
66
62
|
|
|
67
63
|
DECOMPOSE_TARGET_GATESET = ops.Gateset(
|
|
68
64
|
ops.XPowGate,
|
|
@@ -130,7 +126,7 @@ class SupportsDecompose(Protocol):
|
|
|
130
126
|
pass
|
|
131
127
|
|
|
132
128
|
def _decompose_with_context_(
|
|
133
|
-
self, *, context:
|
|
129
|
+
self, *, context: DecompositionContext | None = None
|
|
134
130
|
) -> DecomposeResult:
|
|
135
131
|
pass
|
|
136
132
|
|
|
@@ -154,17 +150,17 @@ class SupportsDecomposeWithQubits(Protocol):
|
|
|
154
150
|
implements `SupportsDecomposeWithQubits`.
|
|
155
151
|
"""
|
|
156
152
|
|
|
157
|
-
def _decompose_(self, qubits:
|
|
153
|
+
def _decompose_(self, qubits: tuple[cirq.Qid, ...]) -> DecomposeResult:
|
|
158
154
|
pass
|
|
159
155
|
|
|
160
156
|
def _decompose_with_context_(
|
|
161
|
-
self, qubits:
|
|
157
|
+
self, qubits: tuple[cirq.Qid, ...], *, context: DecompositionContext | None = None
|
|
162
158
|
) -> DecomposeResult:
|
|
163
159
|
pass
|
|
164
160
|
|
|
165
161
|
|
|
166
162
|
def _try_op_decomposer(
|
|
167
|
-
val: Any, decomposer:
|
|
163
|
+
val: Any, decomposer: OpDecomposer | None, *, context: DecompositionContext | None = None
|
|
168
164
|
) -> DecomposeResult:
|
|
169
165
|
if decomposer is None or not isinstance(val, ops.Operation):
|
|
170
166
|
return None
|
|
@@ -177,11 +173,11 @@ def _try_op_decomposer(
|
|
|
177
173
|
|
|
178
174
|
@dataclasses.dataclass(frozen=True)
|
|
179
175
|
class _DecomposeArgs:
|
|
180
|
-
context:
|
|
181
|
-
intercepting_decomposer:
|
|
182
|
-
fallback_decomposer:
|
|
183
|
-
keep:
|
|
184
|
-
on_stuck_raise:
|
|
176
|
+
context: DecompositionContext | None
|
|
177
|
+
intercepting_decomposer: OpDecomposer | None
|
|
178
|
+
fallback_decomposer: OpDecomposer | None
|
|
179
|
+
keep: Callable[[cirq.Operation], bool] | None
|
|
180
|
+
on_stuck_raise: None | Exception | Callable[[cirq.Operation], Exception | None]
|
|
185
181
|
preserve_structure: bool
|
|
186
182
|
|
|
187
183
|
|
|
@@ -227,15 +223,15 @@ def _decompose_dfs(item: Any, args: _DecomposeArgs) -> Iterator[cirq.Operation]:
|
|
|
227
223
|
def decompose(
|
|
228
224
|
val: Any,
|
|
229
225
|
*,
|
|
230
|
-
intercepting_decomposer:
|
|
231
|
-
fallback_decomposer:
|
|
232
|
-
keep:
|
|
233
|
-
on_stuck_raise:
|
|
234
|
-
None
|
|
235
|
-
|
|
226
|
+
intercepting_decomposer: OpDecomposer | None = None,
|
|
227
|
+
fallback_decomposer: OpDecomposer | None = None,
|
|
228
|
+
keep: Callable[[cirq.Operation], bool] | None = None,
|
|
229
|
+
on_stuck_raise: (
|
|
230
|
+
None | Exception | Callable[[cirq.Operation], Exception | None]
|
|
231
|
+
) = _value_error_describing_bad_operation,
|
|
236
232
|
preserve_structure: bool = False,
|
|
237
|
-
context:
|
|
238
|
-
) ->
|
|
233
|
+
context: DecompositionContext | None = None,
|
|
234
|
+
) -> list[cirq.Operation]:
|
|
239
235
|
"""Recursively decomposes a value into `cirq.Operation`s meeting a criteria.
|
|
240
236
|
|
|
241
237
|
Args:
|
|
@@ -316,14 +312,14 @@ def decompose(
|
|
|
316
312
|
|
|
317
313
|
|
|
318
314
|
@overload
|
|
319
|
-
def decompose_once(val: Any, **kwargs) ->
|
|
315
|
+
def decompose_once(val: Any, **kwargs) -> list[cirq.Operation]:
|
|
320
316
|
pass
|
|
321
317
|
|
|
322
318
|
|
|
323
319
|
@overload
|
|
324
320
|
def decompose_once(
|
|
325
321
|
val: Any, default: TDefault, *args, flatten: bool = True, **kwargs
|
|
326
|
-
) ->
|
|
322
|
+
) -> TDefault | list[cirq.Operation]:
|
|
327
323
|
pass
|
|
328
324
|
|
|
329
325
|
|
|
@@ -332,7 +328,7 @@ def decompose_once(
|
|
|
332
328
|
default=RaiseTypeErrorIfNotProvided,
|
|
333
329
|
*args,
|
|
334
330
|
flatten: bool = True,
|
|
335
|
-
context:
|
|
331
|
+
context: DecompositionContext | None = None,
|
|
336
332
|
**kwargs,
|
|
337
333
|
):
|
|
338
334
|
"""Decomposes a value into operations, if possible.
|
|
@@ -400,8 +396,8 @@ def decompose_once_with_qubits(
|
|
|
400
396
|
qubits: Iterable[cirq.Qid],
|
|
401
397
|
*,
|
|
402
398
|
flatten: bool = True,
|
|
403
|
-
context:
|
|
404
|
-
) ->
|
|
399
|
+
context: DecompositionContext | None = None,
|
|
400
|
+
) -> list[cirq.Operation]:
|
|
405
401
|
pass
|
|
406
402
|
|
|
407
403
|
|
|
@@ -409,11 +405,11 @@ def decompose_once_with_qubits(
|
|
|
409
405
|
def decompose_once_with_qubits(
|
|
410
406
|
val: Any,
|
|
411
407
|
qubits: Iterable[cirq.Qid],
|
|
412
|
-
default:
|
|
408
|
+
default: TDefault | None,
|
|
413
409
|
*,
|
|
414
410
|
flatten: bool = True,
|
|
415
|
-
context:
|
|
416
|
-
) ->
|
|
411
|
+
context: DecompositionContext | None = None,
|
|
412
|
+
) -> TDefault | list[cirq.Operation]:
|
|
417
413
|
pass
|
|
418
414
|
|
|
419
415
|
|
|
@@ -422,7 +418,7 @@ def decompose_once_with_qubits(
|
|
|
422
418
|
qubits: Iterable[cirq.Qid],
|
|
423
419
|
default=RaiseTypeErrorIfNotProvided,
|
|
424
420
|
flatten: bool = True,
|
|
425
|
-
context:
|
|
421
|
+
context: DecompositionContext | None = None,
|
|
426
422
|
):
|
|
427
423
|
"""Decomposes a value into operations on the given qubits.
|
|
428
424
|
|
|
@@ -461,7 +457,7 @@ def decompose_once_with_qubits(
|
|
|
461
457
|
|
|
462
458
|
def _try_decompose_into_operations_and_qubits(
|
|
463
459
|
val: Any,
|
|
464
|
-
) ->
|
|
460
|
+
) -> tuple[list[cirq.Operation] | None, Sequence[cirq.Qid], tuple[int, ...]]:
|
|
465
461
|
"""Returns the value's decomposition (if any) and the qubits it applies to."""
|
|
466
462
|
|
|
467
463
|
if isinstance(val, ops.Gate):
|
|
@@ -477,7 +473,7 @@ def _try_decompose_into_operations_and_qubits(
|
|
|
477
473
|
result = decompose_once(val, None)
|
|
478
474
|
if result is not None:
|
|
479
475
|
qubit_set = set()
|
|
480
|
-
qid_shape_dict:
|
|
476
|
+
qid_shape_dict: dict[cirq.Qid, int] = defaultdict(lambda: 1)
|
|
481
477
|
for op in result:
|
|
482
478
|
for level, q in zip(qid_shape_protocol.qid_shape(op), op.qubits):
|
|
483
479
|
qubit_set.add(q)
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
from __future__ import annotations
|
|
16
16
|
|
|
17
|
-
from typing import Any, Iterable,
|
|
17
|
+
from typing import Any, Iterable, overload, TYPE_CHECKING, TypeVar
|
|
18
18
|
|
|
19
19
|
from cirq import ops
|
|
20
20
|
|
|
@@ -23,7 +23,7 @@ if TYPE_CHECKING:
|
|
|
23
23
|
|
|
24
24
|
# This is a special indicator value used by the inverse method to determine
|
|
25
25
|
# whether or not the caller provided a 'default' argument.
|
|
26
|
-
RaiseTypeErrorIfNotProvided:
|
|
26
|
+
RaiseTypeErrorIfNotProvided: tuple[list[Any]] = ([],)
|
|
27
27
|
|
|
28
28
|
TDefault = TypeVar('TDefault')
|
|
29
29
|
|
|
@@ -50,22 +50,22 @@ def inverse(val: cirq.OP_TREE) -> cirq.OP_TREE:
|
|
|
50
50
|
|
|
51
51
|
|
|
52
52
|
@overload
|
|
53
|
-
def inverse(val: cirq.Gate, default: TDefault) ->
|
|
53
|
+
def inverse(val: cirq.Gate, default: TDefault) -> TDefault | cirq.Gate:
|
|
54
54
|
pass
|
|
55
55
|
|
|
56
56
|
|
|
57
57
|
@overload
|
|
58
|
-
def inverse(val: cirq.Operation, default: TDefault) ->
|
|
58
|
+
def inverse(val: cirq.Operation, default: TDefault) -> TDefault | cirq.Operation:
|
|
59
59
|
pass
|
|
60
60
|
|
|
61
61
|
|
|
62
62
|
@overload
|
|
63
|
-
def inverse(val: cirq.Circuit, default: TDefault) ->
|
|
63
|
+
def inverse(val: cirq.Circuit, default: TDefault) -> TDefault | cirq.Circuit:
|
|
64
64
|
pass
|
|
65
65
|
|
|
66
66
|
|
|
67
67
|
@overload
|
|
68
|
-
def inverse(val: cirq.OP_TREE, default: TDefault) ->
|
|
68
|
+
def inverse(val: cirq.OP_TREE, default: TDefault) -> TDefault | cirq.OP_TREE:
|
|
69
69
|
pass
|
|
70
70
|
|
|
71
71
|
|
|
@@ -108,7 +108,7 @@ def inverse(val: Any, default: Any = RaiseTypeErrorIfNotProvided) -> Any:
|
|
|
108
108
|
# Maybe it's an iterable of invertible items?
|
|
109
109
|
# Note: we avoid str because 'a'[0] == 'a', which creates an infinite loop.
|
|
110
110
|
if isinstance(val, Iterable) and not isinstance(val, (str, ops.Operation)):
|
|
111
|
-
unique_indicator:
|
|
111
|
+
unique_indicator: list[Any] = []
|
|
112
112
|
results = tuple(inverse(e, unique_indicator) for e in val)
|
|
113
113
|
if all(e is not unique_indicator for e in results):
|
|
114
114
|
return results[::-1]
|
|
@@ -21,21 +21,7 @@ import json
|
|
|
21
21
|
import numbers
|
|
22
22
|
import pathlib
|
|
23
23
|
from types import NotImplementedType
|
|
24
|
-
from typing import
|
|
25
|
-
Any,
|
|
26
|
-
Callable,
|
|
27
|
-
cast,
|
|
28
|
-
Dict,
|
|
29
|
-
IO,
|
|
30
|
-
Iterable,
|
|
31
|
-
List,
|
|
32
|
-
Optional,
|
|
33
|
-
overload,
|
|
34
|
-
Sequence,
|
|
35
|
-
Tuple,
|
|
36
|
-
Type,
|
|
37
|
-
Union,
|
|
38
|
-
)
|
|
24
|
+
from typing import Any, Callable, cast, IO, Iterable, overload, Sequence
|
|
39
25
|
|
|
40
26
|
import attrs
|
|
41
27
|
import numpy as np
|
|
@@ -45,16 +31,16 @@ from typing_extensions import Protocol
|
|
|
45
31
|
|
|
46
32
|
from cirq._doc import doc_private
|
|
47
33
|
|
|
48
|
-
ObjectFactory =
|
|
34
|
+
ObjectFactory = type | Callable[..., Any]
|
|
49
35
|
|
|
50
36
|
|
|
51
37
|
class JsonResolver(Protocol):
|
|
52
38
|
"""Protocol for json resolver functions passed to read_json."""
|
|
53
39
|
|
|
54
|
-
def __call__(self, cirq_type: str) ->
|
|
40
|
+
def __call__(self, cirq_type: str) -> ObjectFactory | None: ...
|
|
55
41
|
|
|
56
42
|
|
|
57
|
-
def _lazy_resolver(dict_factory: Callable[[],
|
|
43
|
+
def _lazy_resolver(dict_factory: Callable[[], dict[str, ObjectFactory]]) -> JsonResolver:
|
|
58
44
|
"""A lazy JsonResolver based on a dict_factory.
|
|
59
45
|
|
|
60
46
|
It only calls dict_factory when the first key is accessed.
|
|
@@ -64,13 +50,13 @@ def _lazy_resolver(dict_factory: Callable[[], Dict[str, ObjectFactory]]) -> Json
|
|
|
64
50
|
class resolution map - it is assumed to be cached
|
|
65
51
|
"""
|
|
66
52
|
|
|
67
|
-
def json_resolver(cirq_type: str) ->
|
|
53
|
+
def json_resolver(cirq_type: str) -> ObjectFactory | None:
|
|
68
54
|
return dict_factory().get(cirq_type, None)
|
|
69
55
|
|
|
70
56
|
return json_resolver
|
|
71
57
|
|
|
72
58
|
|
|
73
|
-
DEFAULT_RESOLVERS:
|
|
59
|
+
DEFAULT_RESOLVERS: list[JsonResolver] = []
|
|
74
60
|
"""A default list of 'JsonResolver' functions for use in read_json.
|
|
75
61
|
|
|
76
62
|
For more information about cirq_type resolution during deserialization
|
|
@@ -90,7 +76,7 @@ prepended to this list:
|
|
|
90
76
|
"""
|
|
91
77
|
|
|
92
78
|
|
|
93
|
-
def _register_resolver(dict_factory: Callable[[],
|
|
79
|
+
def _register_resolver(dict_factory: Callable[[], dict[str, ObjectFactory]]) -> None:
|
|
94
80
|
"""Register a resolver based on a dict factory for lazy initialization.
|
|
95
81
|
|
|
96
82
|
Cirq modules are the ones referred in cirq/__init__.py. If a Cirq module
|
|
@@ -127,7 +113,7 @@ class SupportsJSON(Protocol):
|
|
|
127
113
|
"""
|
|
128
114
|
|
|
129
115
|
@doc_private
|
|
130
|
-
def _json_dict_(self) ->
|
|
116
|
+
def _json_dict_(self) -> None | NotImplementedType | dict[Any, Any]:
|
|
131
117
|
pass
|
|
132
118
|
|
|
133
119
|
|
|
@@ -148,7 +134,7 @@ class HasJSONNamespace(Protocol):
|
|
|
148
134
|
pass
|
|
149
135
|
|
|
150
136
|
|
|
151
|
-
def obj_to_dict_helper(obj: Any, attribute_names: Iterable[str]) ->
|
|
137
|
+
def obj_to_dict_helper(obj: Any, attribute_names: Iterable[str]) -> dict[str, Any]:
|
|
152
138
|
"""Construct a dictionary containing attributes from obj
|
|
153
139
|
|
|
154
140
|
This is useful as a helper function in objects implementing the
|
|
@@ -170,7 +156,7 @@ def obj_to_dict_helper(obj: Any, attribute_names: Iterable[str]) -> Dict[str, An
|
|
|
170
156
|
|
|
171
157
|
|
|
172
158
|
# pylint: enable=redefined-builtin
|
|
173
|
-
def dataclass_json_dict(obj: Any) ->
|
|
159
|
+
def dataclass_json_dict(obj: Any) -> dict[str, Any]:
|
|
174
160
|
"""Return a dictionary suitable for `_json_dict_` from a dataclass.
|
|
175
161
|
|
|
176
162
|
Dataclasses keep track of their relevant fields, so we can automatically generate these.
|
|
@@ -186,7 +172,7 @@ def dataclass_json_dict(obj: Any) -> Dict[str, Any]:
|
|
|
186
172
|
return obj_to_dict_helper(obj, attribute_names)
|
|
187
173
|
|
|
188
174
|
|
|
189
|
-
def attrs_json_dict(obj: Any) ->
|
|
175
|
+
def attrs_json_dict(obj: Any) -> dict[str, Any]:
|
|
190
176
|
"""Return a dictionary suitable for `_json_dict_` from an attrs dataclass."""
|
|
191
177
|
attribute_names = [f.name for f in attrs.fields(type(obj))]
|
|
192
178
|
return obj_to_dict_helper(obj, attribute_names)
|
|
@@ -339,8 +325,8 @@ class ObjectHook:
|
|
|
339
325
|
|
|
340
326
|
def __init__(self, resolvers: Sequence[JsonResolver]) -> None:
|
|
341
327
|
self.resolvers = resolvers
|
|
342
|
-
self.memo:
|
|
343
|
-
self.context_map:
|
|
328
|
+
self.memo: dict[int, SerializableByKey] = {}
|
|
329
|
+
self.context_map: dict[int, SerializableByKey] = {}
|
|
344
330
|
|
|
345
331
|
def __call__(self, d):
|
|
346
332
|
cirq_type = d.get('cirq_type')
|
|
@@ -383,7 +369,7 @@ class SerializableByKey(SupportsJSON):
|
|
|
383
369
|
"""
|
|
384
370
|
|
|
385
371
|
|
|
386
|
-
def json_namespace(type_obj:
|
|
372
|
+
def json_namespace(type_obj: type) -> str:
|
|
387
373
|
"""Returns a namespace for JSON serialization of `type_obj`.
|
|
388
374
|
|
|
389
375
|
Types can provide custom namespaces with `_json_namespace_`; otherwise, a
|
|
@@ -407,7 +393,7 @@ def json_namespace(type_obj: Type) -> str:
|
|
|
407
393
|
raise ValueError(f'{type_obj} is not a Cirq type, and does not define _json_namespace_.')
|
|
408
394
|
|
|
409
395
|
|
|
410
|
-
def json_cirq_type(type_obj:
|
|
396
|
+
def json_cirq_type(type_obj: type) -> str:
|
|
411
397
|
"""Returns a string type for JSON serialization of `type_obj`.
|
|
412
398
|
|
|
413
399
|
This method is not part of the base serialization path. Together with
|
|
@@ -421,7 +407,7 @@ def json_cirq_type(type_obj: Type) -> str:
|
|
|
421
407
|
|
|
422
408
|
|
|
423
409
|
def factory_from_json(
|
|
424
|
-
type_str: str, resolvers:
|
|
410
|
+
type_str: str, resolvers: Sequence[JsonResolver] | None = None
|
|
425
411
|
) -> ObjectFactory:
|
|
426
412
|
"""Returns a factory for constructing objects of type `type_str`.
|
|
427
413
|
|
|
@@ -447,7 +433,7 @@ def factory_from_json(
|
|
|
447
433
|
raise ValueError(f"Could not resolve type '{type_str}' during deserialization")
|
|
448
434
|
|
|
449
435
|
|
|
450
|
-
def cirq_type_from_json(type_str: str, resolvers:
|
|
436
|
+
def cirq_type_from_json(type_str: str, resolvers: Sequence[JsonResolver] | None = None) -> type:
|
|
451
437
|
"""Returns a type object for JSON deserialization of `type_str`.
|
|
452
438
|
|
|
453
439
|
This method is not part of the base deserialization path. Together with
|
|
@@ -477,12 +463,7 @@ def cirq_type_from_json(type_str: str, resolvers: Optional[Sequence[JsonResolver
|
|
|
477
463
|
# pylint: disable=function-redefined
|
|
478
464
|
@overload
|
|
479
465
|
def to_json(
|
|
480
|
-
obj: Any,
|
|
481
|
-
file_or_fn: Union[IO, pathlib.Path, str],
|
|
482
|
-
*,
|
|
483
|
-
indent=2,
|
|
484
|
-
separators=None,
|
|
485
|
-
cls=CirqEncoder,
|
|
466
|
+
obj: Any, file_or_fn: IO | pathlib.Path | str, *, indent=2, separators=None, cls=CirqEncoder
|
|
486
467
|
) -> None:
|
|
487
468
|
pass
|
|
488
469
|
|
|
@@ -496,12 +477,12 @@ def to_json(
|
|
|
496
477
|
|
|
497
478
|
def to_json(
|
|
498
479
|
obj: Any,
|
|
499
|
-
file_or_fn:
|
|
480
|
+
file_or_fn: None | IO | pathlib.Path | str = None,
|
|
500
481
|
*,
|
|
501
|
-
indent:
|
|
502
|
-
separators:
|
|
503
|
-
cls:
|
|
504
|
-
) ->
|
|
482
|
+
indent: int | None = 2,
|
|
483
|
+
separators: tuple[str, str] | None = None,
|
|
484
|
+
cls: type[json.JSONEncoder] = CirqEncoder,
|
|
485
|
+
) -> str | None:
|
|
505
486
|
"""Write a JSON file containing a representation of obj.
|
|
506
487
|
|
|
507
488
|
The object may be a cirq object or have data members that are cirq
|
|
@@ -538,10 +519,10 @@ def to_json(
|
|
|
538
519
|
|
|
539
520
|
# pylint: enable=function-redefined
|
|
540
521
|
def read_json(
|
|
541
|
-
file_or_fn:
|
|
522
|
+
file_or_fn: None | IO | pathlib.Path | str = None,
|
|
542
523
|
*,
|
|
543
|
-
json_text:
|
|
544
|
-
resolvers:
|
|
524
|
+
json_text: str | None = None,
|
|
525
|
+
resolvers: Sequence[JsonResolver] | None = None,
|
|
545
526
|
):
|
|
546
527
|
"""Read a JSON file that optionally contains cirq objects.
|
|
547
528
|
|
|
@@ -585,11 +566,11 @@ def read_json(
|
|
|
585
566
|
|
|
586
567
|
def to_json_gzip(
|
|
587
568
|
obj: Any,
|
|
588
|
-
file_or_fn:
|
|
569
|
+
file_or_fn: None | IO | pathlib.Path | str = None,
|
|
589
570
|
*,
|
|
590
571
|
indent: int = 2,
|
|
591
|
-
cls:
|
|
592
|
-
) ->
|
|
572
|
+
cls: type[json.JSONEncoder] = CirqEncoder,
|
|
573
|
+
) -> bytes | None:
|
|
593
574
|
"""Write a gzipped JSON file containing a representation of obj.
|
|
594
575
|
|
|
595
576
|
The object may be a cirq object or have data members that are cirq
|
|
@@ -624,10 +605,10 @@ def to_json_gzip(
|
|
|
624
605
|
|
|
625
606
|
|
|
626
607
|
def read_json_gzip(
|
|
627
|
-
file_or_fn:
|
|
608
|
+
file_or_fn: None | IO | pathlib.Path | str = None,
|
|
628
609
|
*,
|
|
629
|
-
gzip_raw:
|
|
630
|
-
resolvers:
|
|
610
|
+
gzip_raw: bytes | None = None,
|
|
611
|
+
resolvers: Sequence[JsonResolver] | None = None,
|
|
631
612
|
):
|
|
632
613
|
"""Read a gzipped JSON file that optionally contains cirq objects.
|
|
633
614
|
|
|
@@ -23,7 +23,6 @@ import json
|
|
|
23
23
|
import os
|
|
24
24
|
import pathlib
|
|
25
25
|
import warnings
|
|
26
|
-
from typing import Dict, List, Optional, Tuple, Type
|
|
27
26
|
from unittest import mock
|
|
28
27
|
|
|
29
28
|
import attrs
|
|
@@ -48,7 +47,7 @@ class _ModuleDeprecation:
|
|
|
48
47
|
|
|
49
48
|
|
|
50
49
|
# tested modules and their deprecation settings
|
|
51
|
-
TESTED_MODULES:
|
|
50
|
+
TESTED_MODULES: dict[str, _ModuleDeprecation | None] = {
|
|
52
51
|
'cirq_aqt': None,
|
|
53
52
|
'cirq_ionq': None,
|
|
54
53
|
'cirq_google': None,
|
|
@@ -66,7 +65,7 @@ if np.__version__.startswith("2."): # pragma: no cover
|
|
|
66
65
|
del TESTED_MODULES["cirq_rigetti"]
|
|
67
66
|
|
|
68
67
|
|
|
69
|
-
def _get_testspecs_for_modules() ->
|
|
68
|
+
def _get_testspecs_for_modules() -> list[ModuleJsonTestSpec]:
|
|
70
69
|
modules = []
|
|
71
70
|
for m in TESTED_MODULES.keys():
|
|
72
71
|
try:
|
|
@@ -359,9 +358,9 @@ class SBKImpl(cirq.SerializableByKey):
|
|
|
359
358
|
def __init__(
|
|
360
359
|
self,
|
|
361
360
|
name: str,
|
|
362
|
-
data_list:
|
|
363
|
-
data_tuple:
|
|
364
|
-
data_dict:
|
|
361
|
+
data_list: list | None = None,
|
|
362
|
+
data_tuple: tuple | None = None,
|
|
363
|
+
data_dict: dict | None = None,
|
|
365
364
|
):
|
|
366
365
|
self.name = name
|
|
367
366
|
self.data_list = data_list or []
|
|
@@ -526,7 +525,7 @@ def test_json_test_data_coverage(mod_spec: ModuleJsonTestSpec, cirq_obj_name: st
|
|
|
526
525
|
|
|
527
526
|
@dataclasses.dataclass
|
|
528
527
|
class SerializableTypeObject:
|
|
529
|
-
test_type:
|
|
528
|
+
test_type: type
|
|
530
529
|
|
|
531
530
|
def _json_dict_(self):
|
|
532
531
|
return {'test_type': json_serialization.json_cirq_type(self.test_type)}
|
|
@@ -607,9 +606,9 @@ def test_to_from_json_gzip():
|
|
|
607
606
|
_ = cirq.read_json_gzip()
|
|
608
607
|
|
|
609
608
|
|
|
610
|
-
def _eval_repr_data_file(path: pathlib.Path, deprecation_deadline:
|
|
609
|
+
def _eval_repr_data_file(path: pathlib.Path, deprecation_deadline: str | None):
|
|
611
610
|
content = path.read_text()
|
|
612
|
-
ctx_managers:
|
|
611
|
+
ctx_managers: list[contextlib.AbstractContextManager] = [contextlib.suppress()]
|
|
613
612
|
if deprecation_deadline: # pragma: no cover
|
|
614
613
|
# we ignore coverage here, because sometimes there are no deprecations at all in any of the
|
|
615
614
|
# modules
|
|
@@ -639,7 +638,7 @@ def assert_repr_and_json_test_data_agree(
|
|
|
639
638
|
repr_path: pathlib.Path,
|
|
640
639
|
json_path: pathlib.Path,
|
|
641
640
|
inward_only: bool,
|
|
642
|
-
deprecation_deadline:
|
|
641
|
+
deprecation_deadline: str | None,
|
|
643
642
|
):
|
|
644
643
|
if not repr_path.exists() and not json_path.exists():
|
|
645
644
|
return
|