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
|
@@ -21,20 +21,7 @@ import enum
|
|
|
21
21
|
import functools
|
|
22
22
|
import inspect
|
|
23
23
|
import textwrap
|
|
24
|
-
from typing import
|
|
25
|
-
Any,
|
|
26
|
-
Callable,
|
|
27
|
-
cast,
|
|
28
|
-
Hashable,
|
|
29
|
-
List,
|
|
30
|
-
Optional,
|
|
31
|
-
overload,
|
|
32
|
-
Tuple,
|
|
33
|
-
Type,
|
|
34
|
-
TYPE_CHECKING,
|
|
35
|
-
TypeVar,
|
|
36
|
-
Union,
|
|
37
|
-
)
|
|
24
|
+
from typing import Any, Callable, cast, Hashable, overload, TYPE_CHECKING, TypeVar
|
|
38
25
|
|
|
39
26
|
from typing_extensions import Protocol
|
|
40
27
|
|
|
@@ -87,8 +74,8 @@ class _LoggerNode:
|
|
|
87
74
|
transformer_name: str
|
|
88
75
|
initial_circuit: cirq.AbstractCircuit
|
|
89
76
|
final_circuit: cirq.AbstractCircuit
|
|
90
|
-
logs:
|
|
91
|
-
nested_loggers:
|
|
77
|
+
logs: list[tuple[LogLevel, tuple[str, ...]]] = dataclasses.field(default_factory=list)
|
|
78
|
+
nested_loggers: list[int] = dataclasses.field(default_factory=list)
|
|
92
79
|
|
|
93
80
|
|
|
94
81
|
class TransformerLogger:
|
|
@@ -115,8 +102,8 @@ class TransformerLogger:
|
|
|
115
102
|
def __init__(self) -> None:
|
|
116
103
|
"""Initializes TransformerLogger."""
|
|
117
104
|
self._curr_id: int = 0
|
|
118
|
-
self._logs:
|
|
119
|
-
self._stack:
|
|
105
|
+
self._logs: list[_LoggerNode] = []
|
|
106
|
+
self._stack: list[int] = []
|
|
120
107
|
|
|
121
108
|
def register_initial(self, circuit: cirq.AbstractCircuit, transformer_name: str) -> None:
|
|
122
109
|
"""Register the beginning of a new transformer stage.
|
|
@@ -227,7 +214,7 @@ class TransformerContext:
|
|
|
227
214
|
"""
|
|
228
215
|
|
|
229
216
|
logger: TransformerLogger = NoOpTransformerLogger()
|
|
230
|
-
tags_to_ignore:
|
|
217
|
+
tags_to_ignore: tuple[Hashable, ...] = ()
|
|
231
218
|
deep: bool = False
|
|
232
219
|
|
|
233
220
|
|
|
@@ -264,15 +251,13 @@ class TRANSFORMER(Protocol):
|
|
|
264
251
|
"""
|
|
265
252
|
|
|
266
253
|
def __call__(
|
|
267
|
-
self, circuit: cirq.AbstractCircuit, *, context:
|
|
254
|
+
self, circuit: cirq.AbstractCircuit, *, context: TransformerContext | None = None
|
|
268
255
|
) -> cirq.AbstractCircuit: ...
|
|
269
256
|
|
|
270
257
|
|
|
271
258
|
_TRANSFORMER_T = TypeVar('_TRANSFORMER_T', bound=TRANSFORMER)
|
|
272
|
-
_TRANSFORMER_CLS_T = TypeVar('_TRANSFORMER_CLS_T', bound=
|
|
273
|
-
_TRANSFORMER_OR_CLS_T = TypeVar(
|
|
274
|
-
'_TRANSFORMER_OR_CLS_T', bound=Union[TRANSFORMER, Type[TRANSFORMER]]
|
|
275
|
-
)
|
|
259
|
+
_TRANSFORMER_CLS_T = TypeVar('_TRANSFORMER_CLS_T', bound=type[TRANSFORMER])
|
|
260
|
+
_TRANSFORMER_OR_CLS_T = TypeVar('_TRANSFORMER_OR_CLS_T', bound=TRANSFORMER | type[TRANSFORMER])
|
|
276
261
|
|
|
277
262
|
|
|
278
263
|
@overload
|
|
@@ -404,7 +389,7 @@ def _run_transformer_on_circuit(
|
|
|
404
389
|
add_deep_support: bool,
|
|
405
390
|
func: TRANSFORMER,
|
|
406
391
|
circuit: cirq.AbstractCircuit,
|
|
407
|
-
extracted_context:
|
|
392
|
+
extracted_context: TransformerContext | None,
|
|
408
393
|
**kwargs,
|
|
409
394
|
) -> cirq.AbstractCircuit:
|
|
410
395
|
mutable_circuit = None
|
|
@@ -432,7 +417,7 @@ def _transform_and_log(
|
|
|
432
417
|
func: TRANSFORMER,
|
|
433
418
|
transformer_name: str,
|
|
434
419
|
circuit: cirq.AbstractCircuit,
|
|
435
|
-
extracted_context:
|
|
420
|
+
extracted_context: TransformerContext | None,
|
|
436
421
|
**kwargs,
|
|
437
422
|
) -> cirq.AbstractCircuit:
|
|
438
423
|
"""Helper to log initial and final circuits before and after calling the transformer."""
|
|
@@ -19,19 +19,7 @@ from __future__ import annotations
|
|
|
19
19
|
import bisect
|
|
20
20
|
import dataclasses
|
|
21
21
|
from collections import defaultdict
|
|
22
|
-
from typing import
|
|
23
|
-
Callable,
|
|
24
|
-
cast,
|
|
25
|
-
Dict,
|
|
26
|
-
Hashable,
|
|
27
|
-
List,
|
|
28
|
-
Optional,
|
|
29
|
-
Sequence,
|
|
30
|
-
Set,
|
|
31
|
-
Tuple,
|
|
32
|
-
TYPE_CHECKING,
|
|
33
|
-
Union,
|
|
34
|
-
)
|
|
22
|
+
from typing import Callable, cast, Hashable, Sequence, TYPE_CHECKING
|
|
35
23
|
|
|
36
24
|
from cirq import circuits, ops, protocols
|
|
37
25
|
from cirq.circuits.circuit import CIRCUIT_TYPE
|
|
@@ -63,7 +51,7 @@ def _create_target_circuit_type(ops: ops.OP_TREE, target_circuit: CIRCUIT_TYPE)
|
|
|
63
51
|
|
|
64
52
|
def map_moments(
|
|
65
53
|
circuit: CIRCUIT_TYPE,
|
|
66
|
-
map_func: Callable[[circuits.Moment, int],
|
|
54
|
+
map_func: Callable[[circuits.Moment, int], circuits.Moment | Sequence[circuits.Moment]],
|
|
67
55
|
*,
|
|
68
56
|
tags_to_ignore: Sequence[Hashable] = (),
|
|
69
57
|
deep: bool = False,
|
|
@@ -159,7 +147,7 @@ def _map_operations_impl(
|
|
|
159
147
|
"""
|
|
160
148
|
tags_to_ignore_set = set(tags_to_ignore)
|
|
161
149
|
|
|
162
|
-
def apply_map_func(op: cirq.Operation, idx: int) ->
|
|
150
|
+
def apply_map_func(op: cirq.Operation, idx: int) -> list[cirq.Operation]:
|
|
163
151
|
if tags_to_ignore_set.intersection(op.tags):
|
|
164
152
|
return [op]
|
|
165
153
|
if deep and isinstance(op.untagged, circuits.CircuitOperation):
|
|
@@ -175,7 +163,7 @@ def _map_operations_impl(
|
|
|
175
163
|
).with_tags(*op.tags)
|
|
176
164
|
mapped_ops = [*ops.flatten_to_ops(map_func(op, idx))]
|
|
177
165
|
op_qubits = set(op.qubits)
|
|
178
|
-
mapped_ops_qubits:
|
|
166
|
+
mapped_ops_qubits: set[cirq.Qid] = set()
|
|
179
167
|
has_overlapping_ops = False
|
|
180
168
|
for mapped_op in mapped_ops:
|
|
181
169
|
if raise_if_add_qubits and not op_qubits.issuperset(mapped_op.qubits):
|
|
@@ -196,9 +184,9 @@ def _map_operations_impl(
|
|
|
196
184
|
]
|
|
197
185
|
return mapped_ops
|
|
198
186
|
|
|
199
|
-
new_moments:
|
|
187
|
+
new_moments: list[list[cirq.Operation]] = []
|
|
200
188
|
for idx, moment in enumerate(circuit):
|
|
201
|
-
curr_moments:
|
|
189
|
+
curr_moments: list[list[cirq.Operation]] = [[]] if wrap_in_circuit_op else []
|
|
202
190
|
placement_cache = circuits.circuit._PlacementCache()
|
|
203
191
|
for op in moment:
|
|
204
192
|
mapped_ops = apply_map_func(op, idx)
|
|
@@ -307,16 +295,16 @@ class _MergedCircuit:
|
|
|
307
295
|
of a set to store operations to preserve insertion order.
|
|
308
296
|
"""
|
|
309
297
|
|
|
310
|
-
qubit_indexes:
|
|
298
|
+
qubit_indexes: dict[cirq.Qid, list[int]] = dataclasses.field(
|
|
311
299
|
default_factory=lambda: defaultdict(lambda: [-1])
|
|
312
300
|
)
|
|
313
|
-
mkey_indexes:
|
|
301
|
+
mkey_indexes: dict[cirq.MeasurementKey, list[int]] = dataclasses.field(
|
|
314
302
|
default_factory=lambda: defaultdict(lambda: [-1])
|
|
315
303
|
)
|
|
316
|
-
ckey_indexes:
|
|
304
|
+
ckey_indexes: dict[cirq.MeasurementKey, list[int]] = dataclasses.field(
|
|
317
305
|
default_factory=lambda: defaultdict(lambda: [-1])
|
|
318
306
|
)
|
|
319
|
-
ops_by_index:
|
|
307
|
+
ops_by_index: list[dict[cirq.Operation, int]] = dataclasses.field(default_factory=list)
|
|
320
308
|
|
|
321
309
|
def append_empty_moment(self) -> None:
|
|
322
310
|
self.ops_by_index.append({})
|
|
@@ -346,8 +334,8 @@ class _MergedCircuit:
|
|
|
346
334
|
self.ckey_indexes[ckey].remove(moment_index)
|
|
347
335
|
|
|
348
336
|
def get_mergeable_ops(
|
|
349
|
-
self, op: cirq.Operation, op_qs:
|
|
350
|
-
) ->
|
|
337
|
+
self, op: cirq.Operation, op_qs: set[cirq.Qid]
|
|
338
|
+
) -> tuple[int, list[cirq.Operation]]:
|
|
351
339
|
# Find the index of previous moment which can be merged with `op`.
|
|
352
340
|
idx = max([self.qubit_indexes[q][-1] for q in op_qs], default=-1)
|
|
353
341
|
idx = max([idx] + [self.mkey_indexes[ckey][-1] for ckey in protocols.control_keys(op)])
|
|
@@ -368,7 +356,7 @@ class _MergedCircuit:
|
|
|
368
356
|
|
|
369
357
|
def merge_operations(
|
|
370
358
|
circuit: CIRCUIT_TYPE,
|
|
371
|
-
merge_func: Callable[[ops.Operation, ops.Operation],
|
|
359
|
+
merge_func: Callable[[ops.Operation, ops.Operation], ops.Operation | None],
|
|
372
360
|
*,
|
|
373
361
|
tags_to_ignore: Sequence[Hashable] = (),
|
|
374
362
|
deep: bool = False,
|
|
@@ -422,7 +410,7 @@ def merge_operations(
|
|
|
422
410
|
_circuit_op_tag = "_internal_tag_to_mark_circuit_ops_in_circuit"
|
|
423
411
|
tags_to_ignore_set = set(tags_to_ignore) | {_circuit_op_tag}
|
|
424
412
|
|
|
425
|
-
def apply_merge_func(op1: ops.Operation, op2: ops.Operation) ->
|
|
413
|
+
def apply_merge_func(op1: ops.Operation, op2: ops.Operation) -> ops.Operation | None:
|
|
426
414
|
if not all(tags_to_ignore_set.isdisjoint(op.tags) for op in [op1, op2]):
|
|
427
415
|
return None
|
|
428
416
|
new_op = merge_func(op1, op2)
|
|
@@ -435,7 +423,7 @@ def merge_operations(
|
|
|
435
423
|
return new_op
|
|
436
424
|
|
|
437
425
|
merged_circuit = _MergedCircuit()
|
|
438
|
-
for moment_idx, current_moment in enumerate(cast(
|
|
426
|
+
for moment_idx, current_moment in enumerate(cast(list['cirq.Moment'], circuit)):
|
|
439
427
|
merged_circuit.append_empty_moment()
|
|
440
428
|
for op in sorted(current_moment.operations, key=lambda op: op.qubits):
|
|
441
429
|
if (
|
|
@@ -526,7 +514,7 @@ def merge_operations_to_circuit_op(
|
|
|
526
514
|
Copy of input circuit with valid connected components wrapped in tagged circuit operations.
|
|
527
515
|
"""
|
|
528
516
|
|
|
529
|
-
def merge_func(op1: cirq.Operation, op2: cirq.Operation) ->
|
|
517
|
+
def merge_func(op1: cirq.Operation, op2: cirq.Operation) -> cirq.Operation | None:
|
|
530
518
|
def get_ops(op: cirq.Operation):
|
|
531
519
|
op_untagged = op.untagged
|
|
532
520
|
return (
|
|
@@ -551,7 +539,7 @@ def merge_k_qubit_unitaries_to_circuit_op(
|
|
|
551
539
|
k: int,
|
|
552
540
|
*,
|
|
553
541
|
tags_to_ignore: Sequence[Hashable] = (),
|
|
554
|
-
merged_circuit_op_tag:
|
|
542
|
+
merged_circuit_op_tag: str | None = None,
|
|
555
543
|
deep: bool = False,
|
|
556
544
|
) -> CIRCUIT_TYPE:
|
|
557
545
|
"""Merges connected components of operations, acting on <= k qubits, into circuit operations.
|
|
@@ -593,7 +581,7 @@ def merge_k_qubit_unitaries_to_circuit_op(
|
|
|
593
581
|
|
|
594
582
|
def merge_moments(
|
|
595
583
|
circuit: CIRCUIT_TYPE,
|
|
596
|
-
merge_func: Callable[[circuits.Moment, circuits.Moment],
|
|
584
|
+
merge_func: Callable[[circuits.Moment, circuits.Moment], circuits.Moment | None],
|
|
597
585
|
*,
|
|
598
586
|
tags_to_ignore: Sequence[Hashable] = (),
|
|
599
587
|
deep: bool = False,
|
|
@@ -628,7 +616,7 @@ def merge_moments(
|
|
|
628
616
|
),
|
|
629
617
|
tags_to_ignore=tags_to_ignore,
|
|
630
618
|
)
|
|
631
|
-
merged_moments:
|
|
619
|
+
merged_moments: list[circuits.Moment] = [circuit[0]]
|
|
632
620
|
for current_moment in circuit[1:]:
|
|
633
621
|
merged_moment = merge_func(merged_moments[-1], current_moment)
|
|
634
622
|
if merged_moment is None:
|
|
@@ -642,7 +630,7 @@ def unroll_circuit_op(
|
|
|
642
630
|
circuit: CIRCUIT_TYPE,
|
|
643
631
|
*,
|
|
644
632
|
deep: bool = False,
|
|
645
|
-
tags_to_check:
|
|
633
|
+
tags_to_check: Sequence[Hashable] | None = (MAPPED_CIRCUIT_OP_TAG,),
|
|
646
634
|
) -> CIRCUIT_TYPE:
|
|
647
635
|
"""Unrolls (tagged) `cirq.CircuitOperation`s while preserving the moment structure.
|
|
648
636
|
|
|
@@ -661,7 +649,7 @@ def unroll_circuit_op(
|
|
|
661
649
|
"""
|
|
662
650
|
|
|
663
651
|
def map_func(m: circuits.Moment, _: int):
|
|
664
|
-
to_zip:
|
|
652
|
+
to_zip: list[cirq.AbstractCircuit] = []
|
|
665
653
|
for op in m:
|
|
666
654
|
op_untagged = op.untagged
|
|
667
655
|
if isinstance(op_untagged, circuits.CircuitOperation):
|
|
@@ -687,7 +675,7 @@ def unroll_circuit_op_greedy_earliest(
|
|
|
687
675
|
circuit: CIRCUIT_TYPE,
|
|
688
676
|
*,
|
|
689
677
|
deep: bool = False,
|
|
690
|
-
tags_to_check:
|
|
678
|
+
tags_to_check: Sequence[Hashable] | None = (MAPPED_CIRCUIT_OP_TAG,),
|
|
691
679
|
) -> CIRCUIT_TYPE:
|
|
692
680
|
"""Unrolls (tagged) `cirq.CircuitOperation`s by inserting operations using EARLIEST strategy.
|
|
693
681
|
|
|
@@ -734,7 +722,7 @@ def unroll_circuit_op_greedy_frontier(
|
|
|
734
722
|
circuit: CIRCUIT_TYPE,
|
|
735
723
|
*,
|
|
736
724
|
deep: bool = False,
|
|
737
|
-
tags_to_check:
|
|
725
|
+
tags_to_check: Sequence[Hashable] | None = (MAPPED_CIRCUIT_OP_TAG,),
|
|
738
726
|
) -> CIRCUIT_TYPE:
|
|
739
727
|
"""Unrolls (tagged) `cirq.CircuitOperation`s by inserting operations inline at qubit frontier.
|
|
740
728
|
|
|
@@ -753,7 +741,7 @@ def unroll_circuit_op_greedy_frontier(
|
|
|
753
741
|
Copy of input circuit with (Tagged) CircuitOperation's expanded inline at qubit frontier.
|
|
754
742
|
"""
|
|
755
743
|
unrolled_circuit = circuit.unfreeze(copy=True)
|
|
756
|
-
frontier:
|
|
744
|
+
frontier: dict[cirq.Qid, int] = defaultdict(lambda: 0)
|
|
757
745
|
idx = 0
|
|
758
746
|
while idx < len(unrolled_circuit):
|
|
759
747
|
for op in unrolled_circuit[idx].operations:
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
from __future__ import annotations
|
|
16
16
|
|
|
17
|
-
from typing import Iterator
|
|
17
|
+
from typing import Iterator
|
|
18
18
|
|
|
19
19
|
import pytest
|
|
20
20
|
|
|
@@ -503,7 +503,7 @@ def test_map_moments_drop_empty_moments_deep():
|
|
|
503
503
|
cirq.testing.assert_same_circuits(c_mapped, c_expected)
|
|
504
504
|
|
|
505
505
|
|
|
506
|
-
def _merge_z_moments_func(m1: cirq.Moment, m2: cirq.Moment) ->
|
|
506
|
+
def _merge_z_moments_func(m1: cirq.Moment, m2: cirq.Moment) -> cirq.Moment | None:
|
|
507
507
|
if any(op.gate != cirq.Z for m in [m1, m2] for op in m):
|
|
508
508
|
return None
|
|
509
509
|
return cirq.Moment(
|
|
@@ -723,7 +723,7 @@ def test_merge_operations_to_circuit_op_merges_connected_component():
|
|
|
723
723
|
''',
|
|
724
724
|
)
|
|
725
725
|
|
|
726
|
-
def can_merge(ops1:
|
|
726
|
+
def can_merge(ops1: list[cirq.Operation], ops2: list[cirq.Operation]) -> bool:
|
|
727
727
|
"""Artificial example where a CZ will absorb any merge-able operation."""
|
|
728
728
|
return any(o.gate == cirq.CZ for op_list in [ops1, ops2] for o in op_list)
|
|
729
729
|
|
cirq/value/classical_data.py
CHANGED
|
@@ -16,7 +16,7 @@ from __future__ import annotations
|
|
|
16
16
|
|
|
17
17
|
import abc
|
|
18
18
|
import enum
|
|
19
|
-
from typing import
|
|
19
|
+
from typing import Mapping, Sequence, TYPE_CHECKING
|
|
20
20
|
|
|
21
21
|
from typing_extensions import Self
|
|
22
22
|
|
|
@@ -47,17 +47,17 @@ class MeasurementType(enum.IntEnum):
|
|
|
47
47
|
|
|
48
48
|
class ClassicalDataStoreReader(abc.ABC):
|
|
49
49
|
@abc.abstractmethod
|
|
50
|
-
def keys(self) ->
|
|
50
|
+
def keys(self) -> tuple[cirq.MeasurementKey, ...]:
|
|
51
51
|
"""Gets the measurement keys in the order they were stored."""
|
|
52
52
|
|
|
53
53
|
@property
|
|
54
54
|
@abc.abstractmethod
|
|
55
|
-
def records(self) -> Mapping[cirq.MeasurementKey,
|
|
55
|
+
def records(self) -> Mapping[cirq.MeasurementKey, list[tuple[int, ...]]]:
|
|
56
56
|
"""Gets the a mapping from measurement key to measurement records."""
|
|
57
57
|
|
|
58
58
|
@property
|
|
59
59
|
@abc.abstractmethod
|
|
60
|
-
def channel_records(self) -> Mapping[cirq.MeasurementKey,
|
|
60
|
+
def channel_records(self) -> Mapping[cirq.MeasurementKey, list[int]]:
|
|
61
61
|
"""Gets the a mapping from measurement key to channel measurement records."""
|
|
62
62
|
|
|
63
63
|
@abc.abstractmethod
|
|
@@ -83,7 +83,7 @@ class ClassicalDataStoreReader(abc.ABC):
|
|
|
83
83
|
"""
|
|
84
84
|
|
|
85
85
|
@abc.abstractmethod
|
|
86
|
-
def get_digits(self, key: cirq.MeasurementKey, index=-1) ->
|
|
86
|
+
def get_digits(self, key: cirq.MeasurementKey, index=-1) -> tuple[int, ...]:
|
|
87
87
|
"""Gets the values of the qubits that were measured into this key.
|
|
88
88
|
|
|
89
89
|
For example, if the measurement of qubits [q0, q1] produces [0, 1],
|
|
@@ -143,10 +143,10 @@ class ClassicalDataDictionaryStore(ClassicalDataStore):
|
|
|
143
143
|
def __init__(
|
|
144
144
|
self,
|
|
145
145
|
*,
|
|
146
|
-
_records:
|
|
147
|
-
_measured_qubits:
|
|
148
|
-
_channel_records:
|
|
149
|
-
_measurement_types:
|
|
146
|
+
_records: dict[cirq.MeasurementKey, list[tuple[int, ...]]] | None = None,
|
|
147
|
+
_measured_qubits: dict[cirq.MeasurementKey, list[tuple[cirq.Qid, ...]]] | None = None,
|
|
148
|
+
_channel_records: dict[cirq.MeasurementKey, list[int]] | None = None,
|
|
149
|
+
_measurement_types: dict[cirq.MeasurementKey, cirq.MeasurementType] | None = None,
|
|
150
150
|
):
|
|
151
151
|
"""Initializes a `ClassicalDataDictionaryStore` object."""
|
|
152
152
|
if not _measurement_types:
|
|
@@ -165,27 +165,27 @@ class ClassicalDataDictionaryStore(ClassicalDataStore):
|
|
|
165
165
|
_measured_qubits = {}
|
|
166
166
|
if _channel_records is None:
|
|
167
167
|
_channel_records = {}
|
|
168
|
-
self._records:
|
|
169
|
-
self._measured_qubits:
|
|
168
|
+
self._records: dict[cirq.MeasurementKey, list[tuple[int, ...]]] = _records
|
|
169
|
+
self._measured_qubits: dict[cirq.MeasurementKey, list[tuple[cirq.Qid, ...]]] = (
|
|
170
170
|
_measured_qubits
|
|
171
171
|
)
|
|
172
|
-
self._channel_records:
|
|
173
|
-
self._measurement_types:
|
|
172
|
+
self._channel_records: dict[cirq.MeasurementKey, list[int]] = _channel_records
|
|
173
|
+
self._measurement_types: dict[cirq.MeasurementKey, cirq.MeasurementType] = (
|
|
174
174
|
_measurement_types
|
|
175
175
|
)
|
|
176
176
|
|
|
177
177
|
@property
|
|
178
|
-
def records(self) -> Mapping[cirq.MeasurementKey,
|
|
178
|
+
def records(self) -> Mapping[cirq.MeasurementKey, list[tuple[int, ...]]]:
|
|
179
179
|
"""Gets the a mapping from measurement key to measurement records."""
|
|
180
180
|
return self._records
|
|
181
181
|
|
|
182
182
|
@property
|
|
183
|
-
def channel_records(self) -> Mapping[cirq.MeasurementKey,
|
|
183
|
+
def channel_records(self) -> Mapping[cirq.MeasurementKey, list[int]]:
|
|
184
184
|
"""Gets the a mapping from measurement key to channel measurement records."""
|
|
185
185
|
return self._channel_records
|
|
186
186
|
|
|
187
187
|
@property
|
|
188
|
-
def measured_qubits(self) -> Mapping[cirq.MeasurementKey,
|
|
188
|
+
def measured_qubits(self) -> Mapping[cirq.MeasurementKey, list[tuple[cirq.Qid, ...]]]:
|
|
189
189
|
"""Gets the a mapping from measurement key to the qubits measured."""
|
|
190
190
|
return self._measured_qubits
|
|
191
191
|
|
|
@@ -194,7 +194,7 @@ class ClassicalDataDictionaryStore(ClassicalDataStore):
|
|
|
194
194
|
"""Gets the a mapping from measurement key to the measurement type."""
|
|
195
195
|
return self._measurement_types
|
|
196
196
|
|
|
197
|
-
def keys(self) ->
|
|
197
|
+
def keys(self) -> tuple[cirq.MeasurementKey, ...]:
|
|
198
198
|
return tuple(self._measurement_types.keys())
|
|
199
199
|
|
|
200
200
|
def record_measurement(
|
|
@@ -225,7 +225,7 @@ class ClassicalDataDictionaryStore(ClassicalDataStore):
|
|
|
225
225
|
raise ValueError(f"Measurement already logged to key {key}")
|
|
226
226
|
self._channel_records[key].append(measurement)
|
|
227
227
|
|
|
228
|
-
def get_digits(self, key: cirq.MeasurementKey, index=-1) ->
|
|
228
|
+
def get_digits(self, key: cirq.MeasurementKey, index=-1) -> tuple[int, ...]:
|
|
229
229
|
return (
|
|
230
230
|
self._records[key][index]
|
|
231
231
|
if self._measurement_types[key] == MeasurementType.MEASUREMENT
|
cirq/value/condition.py
CHANGED
|
@@ -16,7 +16,7 @@ from __future__ import annotations
|
|
|
16
16
|
|
|
17
17
|
import abc
|
|
18
18
|
import dataclasses
|
|
19
|
-
from typing import Any,
|
|
19
|
+
from typing import Any, Mapping, TYPE_CHECKING
|
|
20
20
|
|
|
21
21
|
import attrs
|
|
22
22
|
import sympy
|
|
@@ -34,7 +34,7 @@ class Condition(abc.ABC):
|
|
|
34
34
|
|
|
35
35
|
@property
|
|
36
36
|
@abc.abstractmethod
|
|
37
|
-
def keys(self) ->
|
|
37
|
+
def keys(self) -> tuple[cirq.MeasurementKey, ...]:
|
|
38
38
|
"""Gets the control keys."""
|
|
39
39
|
|
|
40
40
|
@abc.abstractmethod
|
|
@@ -50,7 +50,7 @@ class Condition(abc.ABC):
|
|
|
50
50
|
def qasm(self):
|
|
51
51
|
"""Returns the qasm of this condition."""
|
|
52
52
|
|
|
53
|
-
def _qasm_(self, args: cirq.QasmArgs, **kwargs) ->
|
|
53
|
+
def _qasm_(self, args: cirq.QasmArgs, **kwargs) -> str | None:
|
|
54
54
|
return self.qasm
|
|
55
55
|
|
|
56
56
|
def _with_measurement_key_mapping_(self, key_map: Mapping[str, str]) -> cirq.Condition:
|
|
@@ -59,14 +59,14 @@ class Condition(abc.ABC):
|
|
|
59
59
|
condition = condition.replace_key(k, mkp.with_measurement_key_mapping(k, key_map))
|
|
60
60
|
return condition
|
|
61
61
|
|
|
62
|
-
def _with_key_path_prefix_(self, path:
|
|
62
|
+
def _with_key_path_prefix_(self, path: tuple[str, ...]) -> cirq.Condition:
|
|
63
63
|
condition = self
|
|
64
64
|
for k in self.keys:
|
|
65
65
|
condition = condition.replace_key(k, mkp.with_key_path_prefix(k, path))
|
|
66
66
|
return condition
|
|
67
67
|
|
|
68
68
|
def _with_rescoped_keys_(
|
|
69
|
-
self, path:
|
|
69
|
+
self, path: tuple[str, ...], bindable_keys: frozenset[cirq.MeasurementKey]
|
|
70
70
|
) -> cirq.Condition:
|
|
71
71
|
condition = self
|
|
72
72
|
for key in self.keys:
|
|
@@ -121,7 +121,7 @@ class KeyCondition(Condition):
|
|
|
121
121
|
def qasm(self):
|
|
122
122
|
raise ValueError('QASM is defined only for SympyConditions of type key == constant.')
|
|
123
123
|
|
|
124
|
-
def _qasm_(self, args: cirq.QasmArgs, **kwargs) ->
|
|
124
|
+
def _qasm_(self, args: cirq.QasmArgs, **kwargs) -> str | None:
|
|
125
125
|
args.validate_version('2.0', '3.0')
|
|
126
126
|
key_str = str(self.key)
|
|
127
127
|
if key_str not in args.meas_key_id_map:
|
|
@@ -174,7 +174,7 @@ class BitMaskKeyCondition(Condition):
|
|
|
174
174
|
index: int = -1
|
|
175
175
|
target_value: int = 0
|
|
176
176
|
equal_target: bool = False
|
|
177
|
-
bitmask:
|
|
177
|
+
bitmask: int | None = None
|
|
178
178
|
|
|
179
179
|
@property
|
|
180
180
|
def keys(self):
|
|
@@ -285,7 +285,7 @@ class SympyCondition(Condition):
|
|
|
285
285
|
if missing:
|
|
286
286
|
raise ValueError(f'Measurement keys {missing} missing when testing classical control')
|
|
287
287
|
|
|
288
|
-
replacements:
|
|
288
|
+
replacements: dict[str, Any] = {}
|
|
289
289
|
for symbol in self.expr.free_symbols:
|
|
290
290
|
if isinstance(symbol, sympy.Symbol):
|
|
291
291
|
name = symbol.name
|
cirq/value/digits.py
CHANGED
|
@@ -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
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
def big_endian_bits_to_int(bits: Iterable[Any]) -> int:
|
|
@@ -48,7 +48,7 @@ def big_endian_bits_to_int(bits: Iterable[Any]) -> int:
|
|
|
48
48
|
return result
|
|
49
49
|
|
|
50
50
|
|
|
51
|
-
def big_endian_int_to_bits(val: int, *, bit_count: int) ->
|
|
51
|
+
def big_endian_int_to_bits(val: int, *, bit_count: int) -> list[int]:
|
|
52
52
|
"""Returns the big-endian bits of an integer.
|
|
53
53
|
|
|
54
54
|
Args:
|
|
@@ -74,7 +74,7 @@ def big_endian_int_to_bits(val: int, *, bit_count: int) -> List[int]:
|
|
|
74
74
|
return [(val >> i) & 1 for i in range(bit_count)[::-1]]
|
|
75
75
|
|
|
76
76
|
|
|
77
|
-
def big_endian_digits_to_int(digits: Iterable[int], *, base:
|
|
77
|
+
def big_endian_digits_to_int(digits: Iterable[int], *, base: int | Iterable[int]) -> int:
|
|
78
78
|
"""Returns the big-endian integer specified by the given digits and base.
|
|
79
79
|
|
|
80
80
|
Args:
|
|
@@ -122,18 +122,18 @@ def big_endian_digits_to_int(digits: Iterable[int], *, base: Union[int, Iterable
|
|
|
122
122
|
|
|
123
123
|
# pylint: disable=function-redefined
|
|
124
124
|
@overload
|
|
125
|
-
def big_endian_int_to_digits(val: int, *, digit_count: int, base: int) ->
|
|
125
|
+
def big_endian_int_to_digits(val: int, *, digit_count: int, base: int) -> list[int]:
|
|
126
126
|
pass
|
|
127
127
|
|
|
128
128
|
|
|
129
129
|
@overload
|
|
130
|
-
def big_endian_int_to_digits(val: int, *, base: Iterable[int]) ->
|
|
130
|
+
def big_endian_int_to_digits(val: int, *, base: Iterable[int]) -> list[int]:
|
|
131
131
|
pass
|
|
132
132
|
|
|
133
133
|
|
|
134
134
|
def big_endian_int_to_digits(
|
|
135
|
-
val: int, *, digit_count:
|
|
136
|
-
) ->
|
|
135
|
+
val: int, *, digit_count: int | None = None, base: int | Iterable[int]
|
|
136
|
+
) -> list[int]:
|
|
137
137
|
"""Separates an integer into big-endian digits.
|
|
138
138
|
|
|
139
139
|
Args:
|
cirq/value/duration.py
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
from __future__ import annotations
|
|
18
18
|
|
|
19
19
|
import datetime
|
|
20
|
-
from typing import AbstractSet, Any,
|
|
20
|
+
from typing import AbstractSet, Any, TYPE_CHECKING, TypeAlias, Union
|
|
21
21
|
|
|
22
22
|
import numpy as np
|
|
23
23
|
import sympy
|
|
@@ -45,8 +45,8 @@ document(
|
|
|
45
45
|
)
|
|
46
46
|
|
|
47
47
|
|
|
48
|
-
_NUMERIC_INPUT_TYPE =
|
|
49
|
-
_NUMERIC_OUTPUT_TYPE =
|
|
48
|
+
_NUMERIC_INPUT_TYPE: TypeAlias = int | float | sympy.Expr | np.number
|
|
49
|
+
_NUMERIC_OUTPUT_TYPE: TypeAlias = int | float | sympy.Expr
|
|
50
50
|
|
|
51
51
|
|
|
52
52
|
class Duration:
|
|
@@ -82,7 +82,7 @@ class Duration:
|
|
|
82
82
|
>>> print(cirq.Duration(micros=1.5 * sympy.Symbol('t')))
|
|
83
83
|
(1500.0*t) ns
|
|
84
84
|
"""
|
|
85
|
-
self._time_vals:
|
|
85
|
+
self._time_vals: list[_NUMERIC_INPUT_TYPE] = [0, 0, 0, 0]
|
|
86
86
|
self._multipliers = [1, 1000, 1000_000, 1000_000_000]
|
|
87
87
|
if value is not None and value != 0:
|
|
88
88
|
if isinstance(value, datetime.timedelta):
|
|
@@ -159,7 +159,7 @@ class Duration:
|
|
|
159
159
|
def __rmul__(self, other) -> Duration:
|
|
160
160
|
return self.__mul__(other)
|
|
161
161
|
|
|
162
|
-
def __truediv__(self, other) ->
|
|
162
|
+
def __truediv__(self, other) -> Duration | float:
|
|
163
163
|
if isinstance(other, (int, float, sympy.Expr)):
|
|
164
164
|
new_time_vals = [x / other for x in self._time_vals]
|
|
165
165
|
return _duration_from_time_vals(new_time_vals)
|
|
@@ -214,7 +214,7 @@ class Duration:
|
|
|
214
214
|
return hash(datetime.timedelta(microseconds=self.total_picos() / 1000000))
|
|
215
215
|
return hash((Duration, self.total_picos()))
|
|
216
216
|
|
|
217
|
-
def _decompose_into_amount_unit_suffix(self) ->
|
|
217
|
+
def _decompose_into_amount_unit_suffix(self) -> tuple[int, str, str]:
|
|
218
218
|
picos = self.total_picos()
|
|
219
219
|
if (
|
|
220
220
|
isinstance(picos, sympy.Mul)
|
|
@@ -261,11 +261,11 @@ class Duration:
|
|
|
261
261
|
amount, unit, _ = self._decompose_into_amount_unit_suffix()
|
|
262
262
|
return f'cirq.Duration({unit}={proper_repr(amount)})'
|
|
263
263
|
|
|
264
|
-
def _json_dict_(self) ->
|
|
264
|
+
def _json_dict_(self) -> dict[str, Any]:
|
|
265
265
|
return {'picos': self.total_picos()}
|
|
266
266
|
|
|
267
267
|
|
|
268
|
-
def _attempt_duration_like_to_duration(value: Any) ->
|
|
268
|
+
def _attempt_duration_like_to_duration(value: Any) -> Duration | None:
|
|
269
269
|
if isinstance(value, Duration):
|
|
270
270
|
return value
|
|
271
271
|
if isinstance(value, datetime.timedelta):
|
|
@@ -276,9 +276,9 @@ def _attempt_duration_like_to_duration(value: Any) -> Optional[Duration]:
|
|
|
276
276
|
|
|
277
277
|
|
|
278
278
|
def _add_time_vals(
|
|
279
|
-
val1:
|
|
280
|
-
) ->
|
|
281
|
-
ret:
|
|
279
|
+
val1: list[_NUMERIC_INPUT_TYPE], val2: list[_NUMERIC_INPUT_TYPE]
|
|
280
|
+
) -> list[_NUMERIC_INPUT_TYPE]:
|
|
281
|
+
ret: list[_NUMERIC_INPUT_TYPE] = []
|
|
282
282
|
for i in range(4):
|
|
283
283
|
if val1[i] and val2[i]:
|
|
284
284
|
ret.append(val1[i] + val2[i])
|
|
@@ -287,7 +287,7 @@ def _add_time_vals(
|
|
|
287
287
|
return ret
|
|
288
288
|
|
|
289
289
|
|
|
290
|
-
def _duration_from_time_vals(time_vals:
|
|
290
|
+
def _duration_from_time_vals(time_vals: list[_NUMERIC_INPUT_TYPE]):
|
|
291
291
|
ret = Duration()
|
|
292
292
|
ret._time_vals = time_vals
|
|
293
293
|
return ret
|