cirq-core 1.6.0.dev20250520054601__py3-none-any.whl → 1.6.0.dev20250520183459__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/linear_dict_test.py +2 -2
- 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.dev20250520183459.dist-info}/METADATA +1 -1
- {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520183459.dist-info}/RECORD +290 -290
- {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520183459.dist-info}/WHEEL +1 -1
- {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520183459.dist-info}/licenses/LICENSE +0 -0
- {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520183459.dist-info}/top_level.txt +0 -0
|
@@ -23,21 +23,7 @@ from __future__ import annotations
|
|
|
23
23
|
|
|
24
24
|
import math
|
|
25
25
|
from functools import cached_property
|
|
26
|
-
from typing import
|
|
27
|
-
Any,
|
|
28
|
-
Callable,
|
|
29
|
-
cast,
|
|
30
|
-
Dict,
|
|
31
|
-
FrozenSet,
|
|
32
|
-
Iterator,
|
|
33
|
-
List,
|
|
34
|
-
Mapping,
|
|
35
|
-
Optional,
|
|
36
|
-
Sequence,
|
|
37
|
-
Tuple,
|
|
38
|
-
TYPE_CHECKING,
|
|
39
|
-
Union,
|
|
40
|
-
)
|
|
26
|
+
from typing import Any, Callable, cast, Iterator, Mapping, Sequence, TYPE_CHECKING, TypeAlias
|
|
41
27
|
|
|
42
28
|
import numpy as np
|
|
43
29
|
import sympy
|
|
@@ -50,12 +36,12 @@ if TYPE_CHECKING:
|
|
|
50
36
|
|
|
51
37
|
|
|
52
38
|
INT_CLASSES = (int, np.integer)
|
|
53
|
-
INT_TYPE =
|
|
54
|
-
IntParam =
|
|
39
|
+
INT_TYPE: TypeAlias = int | np.integer
|
|
40
|
+
IntParam: TypeAlias = INT_TYPE | sympy.Expr
|
|
55
41
|
REPETITION_ID_SEPARATOR = '-'
|
|
56
42
|
|
|
57
43
|
|
|
58
|
-
def default_repetition_ids(repetitions: IntParam) ->
|
|
44
|
+
def default_repetition_ids(repetitions: IntParam) -> list[str] | None:
|
|
59
45
|
if isinstance(repetitions, INT_CLASSES) and abs(repetitions) != 1:
|
|
60
46
|
abs_repetitions: int = abs(int(repetitions))
|
|
61
47
|
return [str(i) for i in range(abs_repetitions)]
|
|
@@ -63,8 +49,8 @@ def default_repetition_ids(repetitions: IntParam) -> Optional[List[str]]:
|
|
|
63
49
|
|
|
64
50
|
|
|
65
51
|
def _full_join_string_lists(
|
|
66
|
-
list1:
|
|
67
|
-
) ->
|
|
52
|
+
list1: Sequence[str] | None, list2: Sequence[str] | None
|
|
53
|
+
) -> Sequence[str] | None:
|
|
68
54
|
if list1 is None and list2 is None:
|
|
69
55
|
return None # pragma: no cover
|
|
70
56
|
if list1 is None:
|
|
@@ -86,14 +72,14 @@ class CircuitOperation(ops.Operation):
|
|
|
86
72
|
self,
|
|
87
73
|
circuit: cirq.FrozenCircuit,
|
|
88
74
|
repetitions: INT_TYPE = 1,
|
|
89
|
-
qubit_map:
|
|
90
|
-
measurement_key_map:
|
|
91
|
-
param_resolver:
|
|
92
|
-
repetition_ids:
|
|
93
|
-
parent_path:
|
|
94
|
-
extern_keys:
|
|
95
|
-
use_repetition_ids:
|
|
96
|
-
repeat_until:
|
|
75
|
+
qubit_map: dict[cirq.Qid, cirq.Qid] | None = None,
|
|
76
|
+
measurement_key_map: dict[str, str] | None = None,
|
|
77
|
+
param_resolver: study.ParamResolverOrSimilarType | None = None,
|
|
78
|
+
repetition_ids: Sequence[str] | None = None,
|
|
79
|
+
parent_path: tuple[str, ...] = (),
|
|
80
|
+
extern_keys: frozenset[cirq.MeasurementKey] = frozenset(),
|
|
81
|
+
use_repetition_ids: bool | None = None,
|
|
82
|
+
repeat_until: cirq.Condition | None = None,
|
|
97
83
|
):
|
|
98
84
|
"""Initializes a CircuitOperation.
|
|
99
85
|
|
|
@@ -225,7 +211,7 @@ class CircuitOperation(ops.Operation):
|
|
|
225
211
|
return self._repetitions
|
|
226
212
|
|
|
227
213
|
@property
|
|
228
|
-
def repetition_ids(self) ->
|
|
214
|
+
def repetition_ids(self) -> Sequence[str] | None:
|
|
229
215
|
return self._repetition_ids
|
|
230
216
|
|
|
231
217
|
@property
|
|
@@ -233,7 +219,7 @@ class CircuitOperation(ops.Operation):
|
|
|
233
219
|
return self._use_repetition_ids
|
|
234
220
|
|
|
235
221
|
@property
|
|
236
|
-
def repeat_until(self) ->
|
|
222
|
+
def repeat_until(self) -> cirq.Condition | None:
|
|
237
223
|
return self._repeat_until
|
|
238
224
|
|
|
239
225
|
@property
|
|
@@ -249,7 +235,7 @@ class CircuitOperation(ops.Operation):
|
|
|
249
235
|
return self._param_resolver
|
|
250
236
|
|
|
251
237
|
@property
|
|
252
|
-
def parent_path(self) ->
|
|
238
|
+
def parent_path(self) -> tuple[str, ...]:
|
|
253
239
|
return self._parent_path
|
|
254
240
|
|
|
255
241
|
def base_operation(self) -> cirq.CircuitOperation:
|
|
@@ -296,15 +282,15 @@ class CircuitOperation(ops.Operation):
|
|
|
296
282
|
# Methods for getting post-mapping properties of the contained circuit.
|
|
297
283
|
|
|
298
284
|
@property
|
|
299
|
-
def qubits(self) ->
|
|
285
|
+
def qubits(self) -> tuple[cirq.Qid, ...]:
|
|
300
286
|
"""Returns the qubits operated on by this object."""
|
|
301
287
|
ordered_qubits = ops.QubitOrder.DEFAULT.order_for(self.circuit.all_qubits())
|
|
302
288
|
return tuple(self.qubit_map.get(q, q) for q in ordered_qubits)
|
|
303
289
|
|
|
304
|
-
def _default_repetition_ids(self) ->
|
|
290
|
+
def _default_repetition_ids(self) -> list[str] | None:
|
|
305
291
|
return default_repetition_ids(self.repetitions) if self.use_repetition_ids else None
|
|
306
292
|
|
|
307
|
-
def _qid_shape_(self) ->
|
|
293
|
+
def _qid_shape_(self) -> tuple[int, ...]:
|
|
308
294
|
return tuple(q.dimension for q in self.qubits)
|
|
309
295
|
|
|
310
296
|
def _is_measurement_(self) -> bool:
|
|
@@ -322,7 +308,7 @@ class CircuitOperation(ops.Operation):
|
|
|
322
308
|
raise ValueError('Cannot unroll circuit due to nondeterministic repetitions')
|
|
323
309
|
|
|
324
310
|
@cached_property
|
|
325
|
-
def _measurement_key_objs(self) ->
|
|
311
|
+
def _measurement_key_objs(self) -> frozenset[cirq.MeasurementKey]:
|
|
326
312
|
circuit_keys = protocols.measurement_key_objs(self.circuit)
|
|
327
313
|
if circuit_keys and self.use_repetition_ids:
|
|
328
314
|
self._ensure_deterministic_loop_count()
|
|
@@ -340,14 +326,14 @@ class CircuitOperation(ops.Operation):
|
|
|
340
326
|
for key in circuit_keys
|
|
341
327
|
)
|
|
342
328
|
|
|
343
|
-
def _measurement_key_objs_(self) ->
|
|
329
|
+
def _measurement_key_objs_(self) -> frozenset[cirq.MeasurementKey]:
|
|
344
330
|
return self._measurement_key_objs
|
|
345
331
|
|
|
346
|
-
def _measurement_key_names_(self) ->
|
|
332
|
+
def _measurement_key_names_(self) -> frozenset[str]:
|
|
347
333
|
return frozenset(str(key) for key in self._measurement_key_objs_())
|
|
348
334
|
|
|
349
335
|
@cached_property
|
|
350
|
-
def _control_keys(self) ->
|
|
336
|
+
def _control_keys(self) -> frozenset[cirq.MeasurementKey]:
|
|
351
337
|
keys = (
|
|
352
338
|
frozenset()
|
|
353
339
|
if not protocols.control_keys(self.circuit)
|
|
@@ -358,13 +344,13 @@ class CircuitOperation(ops.Operation):
|
|
|
358
344
|
keys |= frozenset(mapped_repeat_until.keys) - self._measurement_key_objs_()
|
|
359
345
|
return keys
|
|
360
346
|
|
|
361
|
-
def _control_keys_(self) ->
|
|
347
|
+
def _control_keys_(self) -> frozenset[cirq.MeasurementKey]:
|
|
362
348
|
return self._control_keys
|
|
363
349
|
|
|
364
350
|
def _is_parameterized_(self) -> bool:
|
|
365
351
|
return any(self._parameter_names_generator())
|
|
366
352
|
|
|
367
|
-
def _parameter_names_(self) ->
|
|
353
|
+
def _parameter_names_(self) -> frozenset[str]:
|
|
368
354
|
return frozenset(self._parameter_names_generator())
|
|
369
355
|
|
|
370
356
|
def _parameter_names_generator(self) -> Iterator[str]:
|
|
@@ -385,7 +371,7 @@ class CircuitOperation(ops.Operation):
|
|
|
385
371
|
circuit = protocols.resolve_parameters(circuit, self.param_resolver, recursive=False)
|
|
386
372
|
return circuit.unfreeze(copy=False)
|
|
387
373
|
|
|
388
|
-
def _mapped_single_loop(self, repetition_id:
|
|
374
|
+
def _mapped_single_loop(self, repetition_id: str | None = None) -> cirq.Circuit:
|
|
389
375
|
circuit = self._mapped_any_loop
|
|
390
376
|
if repetition_id:
|
|
391
377
|
circuit = protocols.with_rescoped_keys(circuit, (repetition_id,))
|
|
@@ -394,7 +380,7 @@ class CircuitOperation(ops.Operation):
|
|
|
394
380
|
)
|
|
395
381
|
|
|
396
382
|
@cached_property
|
|
397
|
-
def _mapped_repeat_until(self) ->
|
|
383
|
+
def _mapped_repeat_until(self) -> cirq.Condition | None:
|
|
398
384
|
"""Applies measurement_key_map, param_resolver, and current scope to repeat_until."""
|
|
399
385
|
repeat_until = self.repeat_until
|
|
400
386
|
if not repeat_until:
|
|
@@ -536,7 +522,7 @@ class CircuitOperation(ops.Operation):
|
|
|
536
522
|
def __hash__(self) -> int:
|
|
537
523
|
return self._hash
|
|
538
524
|
|
|
539
|
-
def __getstate__(self) ->
|
|
525
|
+
def __getstate__(self) -> dict[str, Any]:
|
|
540
526
|
# clear cached hash value when pickling, see #6674
|
|
541
527
|
state = self.__dict__
|
|
542
528
|
# cached_property stores value in the property-named attribute
|
|
@@ -593,9 +579,9 @@ class CircuitOperation(ops.Operation):
|
|
|
593
579
|
|
|
594
580
|
def repeat(
|
|
595
581
|
self,
|
|
596
|
-
repetitions:
|
|
597
|
-
repetition_ids:
|
|
598
|
-
use_repetition_ids:
|
|
582
|
+
repetitions: IntParam | None = None,
|
|
583
|
+
repetition_ids: Sequence[str] | None = None,
|
|
584
|
+
use_repetition_ids: bool | None = None,
|
|
599
585
|
) -> CircuitOperation:
|
|
600
586
|
"""Returns a copy of this operation repeated 'repetitions' times.
|
|
601
587
|
Each repetition instance will be identified by a single repetition_id.
|
|
@@ -662,14 +648,14 @@ class CircuitOperation(ops.Operation):
|
|
|
662
648
|
def __pow__(self, power: IntParam) -> cirq.CircuitOperation:
|
|
663
649
|
return self.repeat(power)
|
|
664
650
|
|
|
665
|
-
def _with_key_path_(self, path:
|
|
651
|
+
def _with_key_path_(self, path: tuple[str, ...]):
|
|
666
652
|
return self.replace(parent_path=path)
|
|
667
653
|
|
|
668
|
-
def _with_key_path_prefix_(self, prefix:
|
|
654
|
+
def _with_key_path_prefix_(self, prefix: tuple[str, ...]):
|
|
669
655
|
return self.replace(parent_path=prefix + self.parent_path)
|
|
670
656
|
|
|
671
657
|
def _with_rescoped_keys_(
|
|
672
|
-
self, path:
|
|
658
|
+
self, path: tuple[str, ...], bindable_keys: frozenset[cirq.MeasurementKey]
|
|
673
659
|
):
|
|
674
660
|
# The following line prevents binding to measurement keys in previous repeated subcircuits
|
|
675
661
|
# "just because their repetition ids matched". If we eventually decide to change that
|
|
@@ -681,7 +667,7 @@ class CircuitOperation(ops.Operation):
|
|
|
681
667
|
path += self.parent_path
|
|
682
668
|
return self.replace(parent_path=path, extern_keys=bindable_keys)
|
|
683
669
|
|
|
684
|
-
def with_key_path(self, path:
|
|
670
|
+
def with_key_path(self, path: tuple[str, ...]):
|
|
685
671
|
"""Alias for `cirq.with_key_path(self, path)`.
|
|
686
672
|
|
|
687
673
|
Args:
|
|
@@ -693,7 +679,7 @@ class CircuitOperation(ops.Operation):
|
|
|
693
679
|
"""
|
|
694
680
|
return self._with_key_path_(path)
|
|
695
681
|
|
|
696
|
-
def with_repetition_ids(self, repetition_ids:
|
|
682
|
+
def with_repetition_ids(self, repetition_ids: list[str]) -> cirq.CircuitOperation:
|
|
697
683
|
"""Returns a copy of this `CircuitOperation` with the given repetition IDs.
|
|
698
684
|
|
|
699
685
|
Args:
|
|
@@ -706,7 +692,7 @@ class CircuitOperation(ops.Operation):
|
|
|
706
692
|
return self.replace(repetition_ids=repetition_ids)
|
|
707
693
|
|
|
708
694
|
def with_qubit_mapping(
|
|
709
|
-
self, qubit_map:
|
|
695
|
+
self, qubit_map: Mapping[cirq.Qid, cirq.Qid] | Callable[[cirq.Qid], cirq.Qid]
|
|
710
696
|
) -> cirq.CircuitOperation:
|
|
711
697
|
"""Returns a copy of this operation with an updated qubit mapping.
|
|
712
698
|
|
cirq/circuits/circuit_test.py
CHANGED
|
@@ -19,7 +19,7 @@ import os
|
|
|
19
19
|
import time
|
|
20
20
|
from collections import defaultdict
|
|
21
21
|
from random import randint, random, randrange, sample
|
|
22
|
-
from typing import Iterator
|
|
22
|
+
from typing import Iterator
|
|
23
23
|
|
|
24
24
|
import numpy as np
|
|
25
25
|
import pytest
|
|
@@ -894,7 +894,7 @@ def test_insert_at_frontier():
|
|
|
894
894
|
|
|
895
895
|
def optimization_at(
|
|
896
896
|
self, circuit: cirq.Circuit, index: int, op: cirq.Operation
|
|
897
|
-
) ->
|
|
897
|
+
) -> cirq.PointOptimizationSummary | None:
|
|
898
898
|
new_ops = self.replacer(op)
|
|
899
899
|
return cirq.PointOptimizationSummary(
|
|
900
900
|
clear_span=1, clear_qubits=op.qubits, new_operations=new_ops
|
|
@@ -2122,7 +2122,7 @@ M('msg')─M──────M
|
|
|
2122
2122
|
@pytest.mark.parametrize('circuit_cls', [cirq.Circuit, cirq.FrozenCircuit])
|
|
2123
2123
|
def test_to_text_diagram_many_qubits_gate_but_multiple_wire_symbols(circuit_cls):
|
|
2124
2124
|
class BadGate(cirq.testing.ThreeQubitGate):
|
|
2125
|
-
def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) ->
|
|
2125
|
+
def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> tuple[str, str]:
|
|
2126
2126
|
return 'a', 'a'
|
|
2127
2127
|
|
|
2128
2128
|
q1 = cirq.NamedQubit('(0, 0)')
|
|
@@ -2724,7 +2724,7 @@ def test_expanding_gate_symbols(circuit_cls):
|
|
|
2724
2724
|
def num_qubits(self) -> int:
|
|
2725
2725
|
return self._num_qubits
|
|
2726
2726
|
|
|
2727
|
-
def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) ->
|
|
2727
|
+
def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> tuple[str, ...]:
|
|
2728
2728
|
assert args.known_qubit_count is not None
|
|
2729
2729
|
return ('@',) + ('Z',) * (args.known_qubit_count - 1)
|
|
2730
2730
|
|
cirq/circuits/frozen_circuit.py
CHANGED
|
@@ -18,17 +18,7 @@ from __future__ import annotations
|
|
|
18
18
|
|
|
19
19
|
from functools import cached_property
|
|
20
20
|
from types import NotImplementedType
|
|
21
|
-
from typing import
|
|
22
|
-
AbstractSet,
|
|
23
|
-
FrozenSet,
|
|
24
|
-
Hashable,
|
|
25
|
-
Iterable,
|
|
26
|
-
Iterator,
|
|
27
|
-
Sequence,
|
|
28
|
-
Tuple,
|
|
29
|
-
TYPE_CHECKING,
|
|
30
|
-
Union,
|
|
31
|
-
)
|
|
21
|
+
from typing import AbstractSet, Hashable, Iterable, Iterator, Sequence, TYPE_CHECKING
|
|
32
22
|
|
|
33
23
|
from cirq import _compat, protocols
|
|
34
24
|
from cirq.circuits import AbstractCircuit, Alignment, Circuit
|
|
@@ -91,7 +81,7 @@ class FrozenCircuit(AbstractCircuit, protocols.SerializableByKey):
|
|
|
91
81
|
return Circuit._from_moments(self._moments)
|
|
92
82
|
|
|
93
83
|
@property
|
|
94
|
-
def tags(self) ->
|
|
84
|
+
def tags(self) -> tuple[Hashable, ...]:
|
|
95
85
|
"""Returns a tuple of the Circuit's tags."""
|
|
96
86
|
return self._tags
|
|
97
87
|
|
|
@@ -134,7 +124,7 @@ class FrozenCircuit(AbstractCircuit, protocols.SerializableByKey):
|
|
|
134
124
|
return len(self.all_qubits())
|
|
135
125
|
|
|
136
126
|
@_compat.cached_method
|
|
137
|
-
def _qid_shape_(self) ->
|
|
127
|
+
def _qid_shape_(self) -> tuple[int, ...]:
|
|
138
128
|
return super()._qid_shape_()
|
|
139
129
|
|
|
140
130
|
@_compat.cached_method
|
|
@@ -142,7 +132,7 @@ class FrozenCircuit(AbstractCircuit, protocols.SerializableByKey):
|
|
|
142
132
|
return super()._has_unitary_()
|
|
143
133
|
|
|
144
134
|
@_compat.cached_method
|
|
145
|
-
def _unitary_(self) ->
|
|
135
|
+
def _unitary_(self) -> np.ndarray | NotImplementedType:
|
|
146
136
|
return super()._unitary_()
|
|
147
137
|
|
|
148
138
|
@_compat.cached_method
|
|
@@ -150,11 +140,11 @@ class FrozenCircuit(AbstractCircuit, protocols.SerializableByKey):
|
|
|
150
140
|
return protocols.is_measurement(self.unfreeze())
|
|
151
141
|
|
|
152
142
|
@_compat.cached_method
|
|
153
|
-
def all_qubits(self) ->
|
|
143
|
+
def all_qubits(self) -> frozenset[cirq.Qid]:
|
|
154
144
|
return super().all_qubits()
|
|
155
145
|
|
|
156
146
|
@cached_property
|
|
157
|
-
def _all_operations(self) ->
|
|
147
|
+
def _all_operations(self) -> tuple[cirq.Operation, ...]:
|
|
158
148
|
return tuple(super().all_operations())
|
|
159
149
|
|
|
160
150
|
def all_operations(self) -> Iterator[cirq.Operation]:
|
|
@@ -164,14 +154,14 @@ class FrozenCircuit(AbstractCircuit, protocols.SerializableByKey):
|
|
|
164
154
|
return self._is_measurement_()
|
|
165
155
|
|
|
166
156
|
@_compat.cached_method
|
|
167
|
-
def all_measurement_key_objs(self) ->
|
|
157
|
+
def all_measurement_key_objs(self) -> frozenset[cirq.MeasurementKey]:
|
|
168
158
|
return super().all_measurement_key_objs()
|
|
169
159
|
|
|
170
|
-
def _measurement_key_objs_(self) ->
|
|
160
|
+
def _measurement_key_objs_(self) -> frozenset[cirq.MeasurementKey]:
|
|
171
161
|
return self.all_measurement_key_objs()
|
|
172
162
|
|
|
173
163
|
@_compat.cached_method
|
|
174
|
-
def _control_keys_(self) ->
|
|
164
|
+
def _control_keys_(self) -> frozenset[cirq.MeasurementKey]:
|
|
175
165
|
return super()._control_keys_()
|
|
176
166
|
|
|
177
167
|
@_compat.cached_method
|
|
@@ -179,7 +169,7 @@ class FrozenCircuit(AbstractCircuit, protocols.SerializableByKey):
|
|
|
179
169
|
return super().are_all_measurements_terminal()
|
|
180
170
|
|
|
181
171
|
@_compat.cached_method
|
|
182
|
-
def all_measurement_key_names(self) ->
|
|
172
|
+
def all_measurement_key_names(self) -> frozenset[str]:
|
|
183
173
|
return frozenset(str(key) for key in self.all_measurement_key_objs())
|
|
184
174
|
|
|
185
175
|
@_compat.cached_method
|
|
@@ -202,7 +192,7 @@ class FrozenCircuit(AbstractCircuit, protocols.SerializableByKey):
|
|
|
202
192
|
]
|
|
203
193
|
return resolved_circuit.with_tags(*resolved_tags)
|
|
204
194
|
|
|
205
|
-
def _measurement_key_names_(self) ->
|
|
195
|
+
def _measurement_key_names_(self) -> frozenset[str]:
|
|
206
196
|
return self.all_measurement_key_names()
|
|
207
197
|
|
|
208
198
|
def __add__(self, other) -> cirq.FrozenCircuit:
|
|
@@ -242,14 +232,14 @@ class FrozenCircuit(AbstractCircuit, protocols.SerializableByKey):
|
|
|
242
232
|
return cls(moments, strategy=InsertStrategy.EARLIEST, tags=tags)
|
|
243
233
|
|
|
244
234
|
def concat_ragged(
|
|
245
|
-
*circuits: cirq.AbstractCircuit, align:
|
|
235
|
+
*circuits: cirq.AbstractCircuit, align: cirq.Alignment | str = Alignment.LEFT
|
|
246
236
|
) -> cirq.FrozenCircuit:
|
|
247
237
|
return AbstractCircuit.concat_ragged(*circuits, align=align).freeze()
|
|
248
238
|
|
|
249
239
|
concat_ragged.__doc__ = AbstractCircuit.concat_ragged.__doc__
|
|
250
240
|
|
|
251
241
|
def zip(
|
|
252
|
-
*circuits: cirq.AbstractCircuit, align:
|
|
242
|
+
*circuits: cirq.AbstractCircuit, align: cirq.Alignment | str = Alignment.LEFT
|
|
253
243
|
) -> cirq.FrozenCircuit:
|
|
254
244
|
return AbstractCircuit.zip(*circuits, align=align).freeze()
|
|
255
245
|
|
cirq/circuits/moment.py
CHANGED
|
@@ -24,18 +24,12 @@ from typing import (
|
|
|
24
24
|
Any,
|
|
25
25
|
Callable,
|
|
26
26
|
cast,
|
|
27
|
-
Dict,
|
|
28
|
-
FrozenSet,
|
|
29
27
|
Iterable,
|
|
30
28
|
Iterator,
|
|
31
|
-
List,
|
|
32
29
|
Mapping,
|
|
33
|
-
Optional,
|
|
34
30
|
overload,
|
|
35
31
|
Sequence,
|
|
36
|
-
Tuple,
|
|
37
32
|
TYPE_CHECKING,
|
|
38
|
-
Union,
|
|
39
33
|
)
|
|
40
34
|
|
|
41
35
|
import numpy as np
|
|
@@ -57,7 +51,7 @@ text_diagram_drawer = LazyLoader(
|
|
|
57
51
|
)
|
|
58
52
|
|
|
59
53
|
|
|
60
|
-
def _default_breakdown(qid: cirq.Qid) ->
|
|
54
|
+
def _default_breakdown(qid: cirq.Qid) -> tuple[Any, Any]:
|
|
61
55
|
# Attempt to convert into a position on the complex plane.
|
|
62
56
|
try:
|
|
63
57
|
plane_pos = complex(qid) # type: ignore
|
|
@@ -102,12 +96,12 @@ class Moment:
|
|
|
102
96
|
self._operations = (
|
|
103
97
|
tuple(op_tree.flatten_to_ops(contents))
|
|
104
98
|
if _flatten_contents
|
|
105
|
-
else cast(
|
|
99
|
+
else cast(tuple['cirq.Operation'], contents)
|
|
106
100
|
)
|
|
107
|
-
self._sorted_operations:
|
|
101
|
+
self._sorted_operations: tuple[cirq.Operation, ...] | None = None
|
|
108
102
|
|
|
109
103
|
# An internal dictionary to support efficient operation access by qubit.
|
|
110
|
-
self._qubit_to_op:
|
|
104
|
+
self._qubit_to_op: dict[cirq.Qid, cirq.Operation] = {}
|
|
111
105
|
for op in self.operations:
|
|
112
106
|
for q in op.qubits:
|
|
113
107
|
# Check that operations don't overlap.
|
|
@@ -115,8 +109,8 @@ class Moment:
|
|
|
115
109
|
raise ValueError(f'Overlapping operations: {self.operations}')
|
|
116
110
|
self._qubit_to_op[q] = op
|
|
117
111
|
|
|
118
|
-
self._measurement_key_objs:
|
|
119
|
-
self._control_keys:
|
|
112
|
+
self._measurement_key_objs: frozenset[cirq.MeasurementKey] | None = None
|
|
113
|
+
self._control_keys: frozenset[cirq.MeasurementKey] | None = None
|
|
120
114
|
|
|
121
115
|
@classmethod
|
|
122
116
|
def from_ops(cls, *ops: cirq.Operation) -> cirq.Moment:
|
|
@@ -133,11 +127,11 @@ class Moment:
|
|
|
133
127
|
return cls(*ops, _flatten_contents=False)
|
|
134
128
|
|
|
135
129
|
@property
|
|
136
|
-
def operations(self) ->
|
|
130
|
+
def operations(self) -> tuple[cirq.Operation, ...]:
|
|
137
131
|
return self._operations
|
|
138
132
|
|
|
139
133
|
@cached_property
|
|
140
|
-
def qubits(self) ->
|
|
134
|
+
def qubits(self) -> frozenset[cirq.Qid]:
|
|
141
135
|
return frozenset(self._qubit_to_op)
|
|
142
136
|
|
|
143
137
|
def operates_on_single_qubit(self, qubit: cirq.Qid) -> bool:
|
|
@@ -160,7 +154,7 @@ class Moment:
|
|
|
160
154
|
"""
|
|
161
155
|
return not self._qubit_to_op.keys().isdisjoint(qubits)
|
|
162
156
|
|
|
163
|
-
def operation_at(self, qubit: raw_types.Qid) ->
|
|
157
|
+
def operation_at(self, qubit: raw_types.Qid) -> cirq.Operation | None:
|
|
164
158
|
"""Returns the operation on a certain qubit for the moment.
|
|
165
159
|
|
|
166
160
|
Args:
|
|
@@ -267,7 +261,7 @@ class Moment:
|
|
|
267
261
|
|
|
268
262
|
def _resolve_parameters_(self, resolver: cirq.ParamResolver, recursive: bool) -> cirq.Moment:
|
|
269
263
|
changed = False
|
|
270
|
-
resolved_ops:
|
|
264
|
+
resolved_ops: list[cirq.Operation] = []
|
|
271
265
|
for op in self:
|
|
272
266
|
resolved_op = protocols.resolve_parameters(op, resolver, recursive)
|
|
273
267
|
changed = (
|
|
@@ -291,35 +285,35 @@ class Moment:
|
|
|
291
285
|
)
|
|
292
286
|
|
|
293
287
|
@_compat.cached_method()
|
|
294
|
-
def _measurement_key_names_(self) ->
|
|
288
|
+
def _measurement_key_names_(self) -> frozenset[str]:
|
|
295
289
|
return frozenset(str(key) for key in self._measurement_key_objs_())
|
|
296
290
|
|
|
297
|
-
def _measurement_key_objs_(self) ->
|
|
291
|
+
def _measurement_key_objs_(self) -> frozenset[cirq.MeasurementKey]:
|
|
298
292
|
if self._measurement_key_objs is None:
|
|
299
293
|
self._measurement_key_objs = frozenset(
|
|
300
294
|
key for op in self.operations for key in protocols.measurement_key_objs(op)
|
|
301
295
|
)
|
|
302
296
|
return self._measurement_key_objs
|
|
303
297
|
|
|
304
|
-
def _control_keys_(self) ->
|
|
298
|
+
def _control_keys_(self) -> frozenset[cirq.MeasurementKey]:
|
|
305
299
|
if self._control_keys is None:
|
|
306
300
|
self._control_keys = frozenset(
|
|
307
301
|
k for op in self.operations for k in protocols.control_keys(op)
|
|
308
302
|
)
|
|
309
303
|
return self._control_keys
|
|
310
304
|
|
|
311
|
-
def _sorted_operations_(self) ->
|
|
305
|
+
def _sorted_operations_(self) -> tuple[cirq.Operation, ...]:
|
|
312
306
|
if self._sorted_operations is None:
|
|
313
307
|
self._sorted_operations = tuple(sorted(self._operations, key=lambda op: op.qubits))
|
|
314
308
|
return self._sorted_operations
|
|
315
309
|
|
|
316
|
-
def _with_key_path_(self, path:
|
|
310
|
+
def _with_key_path_(self, path: tuple[str, ...]):
|
|
317
311
|
return Moment(
|
|
318
312
|
protocols.with_key_path(op, path) if protocols.is_measurement(op) else op
|
|
319
313
|
for op in self.operations
|
|
320
314
|
)
|
|
321
315
|
|
|
322
|
-
def _with_key_path_prefix_(self, prefix:
|
|
316
|
+
def _with_key_path_prefix_(self, prefix: tuple[str, ...]):
|
|
323
317
|
return Moment(
|
|
324
318
|
(
|
|
325
319
|
protocols.with_key_path_prefix(op, prefix)
|
|
@@ -330,7 +324,7 @@ class Moment:
|
|
|
330
324
|
)
|
|
331
325
|
|
|
332
326
|
def _with_rescoped_keys_(
|
|
333
|
-
self, path:
|
|
327
|
+
self, path: tuple[str, ...], bindable_keys: frozenset[cirq.MeasurementKey]
|
|
334
328
|
):
|
|
335
329
|
return Moment(
|
|
336
330
|
protocols.with_rescoped_keys(op, path, bindable_keys) for op in self.operations
|
|
@@ -364,7 +358,7 @@ class Moment:
|
|
|
364
358
|
def __hash__(self):
|
|
365
359
|
return hash((Moment, self._sorted_operations_()))
|
|
366
360
|
|
|
367
|
-
def __getstate__(self) ->
|
|
361
|
+
def __getstate__(self) -> dict[str, Any]:
|
|
368
362
|
# clear cached hash value when pickling, see #6674
|
|
369
363
|
state = self.__dict__
|
|
370
364
|
hash_attr = _compat._method_cache_name(self.__hash__)
|
|
@@ -407,7 +401,7 @@ class Moment:
|
|
|
407
401
|
return self._operations
|
|
408
402
|
|
|
409
403
|
def transform_qubits(
|
|
410
|
-
self, qubit_map:
|
|
404
|
+
self, qubit_map: dict[cirq.Qid, cirq.Qid] | Callable[[cirq.Qid], cirq.Qid]
|
|
411
405
|
) -> Self:
|
|
412
406
|
"""Returns the same moment, but with different qubits.
|
|
413
407
|
|
|
@@ -516,7 +510,7 @@ class Moment:
|
|
|
516
510
|
return NotImplemented
|
|
517
511
|
return qis.kraus_to_superoperator(self._kraus_())
|
|
518
512
|
|
|
519
|
-
def _json_dict_(self) ->
|
|
513
|
+
def _json_dict_(self) -> dict[str, Any]:
|
|
520
514
|
return protocols.obj_to_dict_helper(self, ['operations'])
|
|
521
515
|
|
|
522
516
|
@classmethod
|
|
@@ -569,10 +563,10 @@ class Moment:
|
|
|
569
563
|
def to_text_diagram(
|
|
570
564
|
self: cirq.Moment,
|
|
571
565
|
*,
|
|
572
|
-
xy_breakdown_func: Callable[[cirq.Qid],
|
|
566
|
+
xy_breakdown_func: Callable[[cirq.Qid], tuple[Any, Any]] = _default_breakdown,
|
|
573
567
|
extra_qubits: Iterable[cirq.Qid] = (),
|
|
574
568
|
use_unicode_characters: bool = True,
|
|
575
|
-
precision:
|
|
569
|
+
precision: int | None = None,
|
|
576
570
|
include_tags: bool = True,
|
|
577
571
|
) -> str:
|
|
578
572
|
"""Create a text diagram for the moment.
|
|
@@ -657,7 +651,7 @@ class Moment:
|
|
|
657
651
|
|
|
658
652
|
return diagram.render()
|
|
659
653
|
|
|
660
|
-
def _commutes_(self, other: Any, *, atol: float = 1e-8) ->
|
|
654
|
+
def _commutes_(self, other: Any, *, atol: float = 1e-8) -> bool | NotImplementedType:
|
|
661
655
|
"""Determines whether Moment commutes with the other Moment or Operation.
|
|
662
656
|
|
|
663
657
|
Args:
|
|
@@ -18,7 +18,7 @@ from __future__ import annotations
|
|
|
18
18
|
|
|
19
19
|
import abc
|
|
20
20
|
from collections import defaultdict
|
|
21
|
-
from typing import Callable, cast,
|
|
21
|
+
from typing import Callable, cast, Iterable, Sequence, TYPE_CHECKING
|
|
22
22
|
|
|
23
23
|
from cirq import ops
|
|
24
24
|
|
|
@@ -106,7 +106,7 @@ class PointOptimizer:
|
|
|
106
106
|
@abc.abstractmethod
|
|
107
107
|
def optimization_at(
|
|
108
108
|
self, circuit: cirq.Circuit, index: int, op: cirq.Operation
|
|
109
|
-
) ->
|
|
109
|
+
) -> cirq.PointOptimizationSummary | None:
|
|
110
110
|
"""Describes how to change operations near the given location.
|
|
111
111
|
|
|
112
112
|
For example, this method could realize that the given operation is an
|
|
@@ -127,7 +127,7 @@ class PointOptimizer:
|
|
|
127
127
|
"""
|
|
128
128
|
|
|
129
129
|
def optimize_circuit(self, circuit: cirq.Circuit):
|
|
130
|
-
frontier:
|
|
130
|
+
frontier: dict[cirq.Qid, int] = defaultdict(lambda: 0)
|
|
131
131
|
i = 0
|
|
132
132
|
while i < len(circuit): # Note: circuit may mutate as we go.
|
|
133
133
|
for op in circuit[i].operations:
|
|
@@ -150,7 +150,7 @@ class PointOptimizer:
|
|
|
150
150
|
circuit.clear_operations_touching(
|
|
151
151
|
opt.clear_qubits, [e for e in range(i, i + opt.clear_span)]
|
|
152
152
|
)
|
|
153
|
-
new_operations = self.post_clean_up(cast(
|
|
153
|
+
new_operations = self.post_clean_up(cast(tuple[ops.Operation], opt.new_operations))
|
|
154
154
|
|
|
155
155
|
flat_new_operations = tuple(ops.flatten_to_ops(new_operations))
|
|
156
156
|
|
|
@@ -14,8 +14,6 @@
|
|
|
14
14
|
|
|
15
15
|
from __future__ import annotations
|
|
16
16
|
|
|
17
|
-
from typing import List, Optional, Set
|
|
18
|
-
|
|
19
17
|
import pytest
|
|
20
18
|
|
|
21
19
|
import cirq
|
|
@@ -64,7 +62,7 @@ class ReplaceWithXGates(PointOptimizer):
|
|
|
64
62
|
|
|
65
63
|
def optimization_at(
|
|
66
64
|
self, circuit: cirq.Circuit, index: int, op: cirq.Operation
|
|
67
|
-
) ->
|
|
65
|
+
) -> cirq.PointOptimizationSummary | None:
|
|
68
66
|
end = index + 1
|
|
69
67
|
new_ops = [cirq.X(q) for q in op.qubits]
|
|
70
68
|
done = False
|
|
@@ -72,8 +70,8 @@ class ReplaceWithXGates(PointOptimizer):
|
|
|
72
70
|
n = circuit.next_moment_operating_on(op.qubits, end)
|
|
73
71
|
if n is None:
|
|
74
72
|
break
|
|
75
|
-
next_ops:
|
|
76
|
-
next_ops_list:
|
|
73
|
+
next_ops: set[Operation | None] = {circuit.operation_at(q, n) for q in op.qubits}
|
|
74
|
+
next_ops_list: list[Operation] = [e for e in next_ops if e]
|
|
77
75
|
next_ops_sorted = sorted(next_ops_list, key=lambda e: str(e.qubits))
|
|
78
76
|
for next_op in next_ops_sorted:
|
|
79
77
|
if next_op:
|
|
@@ -158,7 +156,7 @@ def test_point_optimizer_raises_on_gates_changing_qubits() -> None:
|
|
|
158
156
|
|
|
159
157
|
def optimization_at(
|
|
160
158
|
self, circuit: cirq.Circuit, index: int, op: cirq.Operation
|
|
161
|
-
) ->
|
|
159
|
+
) -> cirq.PointOptimizationSummary | None:
|
|
162
160
|
new_op = op
|
|
163
161
|
if len(op.qubits) == 1 and isinstance(op, cirq.GateOperation):
|
|
164
162
|
new_op = op.gate(cirq.LineQubit(42))
|