diff --git a/cirq-core/cirq/circuits/circuit.py b/cirq-core/cirq/circuits/circuit.py index c2c2e2d1257..7589253ed55 100644 --- a/cirq-core/cirq/circuits/circuit.py +++ b/cirq-core/cirq/circuits/circuit.py @@ -1717,7 +1717,8 @@ def __init__( """ self._moments: List['cirq.Moment'] = [] self._device = device - self.append(contents, strategy=strategy) + with _compat.block_overlapping_deprecation('.*'): + self.append(contents, strategy=strategy) @property # type: ignore @_compat.deprecated( @@ -2045,15 +2046,32 @@ def insert( Raises: ValueError: Bad insertion strategy. """ - moments_and_operations = list( - ops.flatten_to_ops_or_moments( - ops.transform_op_tree( - moment_or_operation_tree, - self._device.decompose_operation, - preserve_moments=True, - ), + if self._device == devices.UNCONSTRAINED_DEVICE: + moments_and_operations = list( + ops.flatten_to_ops_or_moments( + ops.transform_op_tree( + moment_or_operation_tree, + preserve_moments=True, + ), + ) ) - ) + else: + _compat._warn_or_error( + 'circuit.insert behavior relies on circuit.device.\n' + 'The ability to construct a circuit with a device\n' + 'will be removed in cirq v0.15. please update this use of\n' + 'insert.' + ) + with _compat.block_overlapping_deprecation('decompose'): + moments_and_operations = list( + ops.flatten_to_ops_or_moments( + ops.transform_op_tree( + moment_or_operation_tree, + self._device.decompose_operation, + preserve_moments=True, + ), + ) + ) for moment_or_op in moments_and_operations: if isinstance(moment_or_op, ops.Moment): @@ -2114,13 +2132,14 @@ def insert_into_range(self, operations: 'cirq.OP_TREE', start: int, end: int) -> ) cannot_add_lambda = lambda a, b: not self._device.can_add_operation_into_moment(a, b) - with _compat.block_overlapping_deprecation('can_add_operation_into_moment'): + with _compat.block_overlapping_deprecation('(can_add_operation_into_moment|insert)'): while op_index < len(flat_ops): op = flat_ops[op_index] while i < end and cannot_add_lambda(op, self._moments[i]): i += 1 if i >= end: break + self._moments[i] = self._moments[i].with_operation(op) op_index += 1 diff --git a/cirq-core/cirq/circuits/circuit_test.py b/cirq-core/cirq/circuits/circuit_test.py index 132027b7347..f91d054e0b1 100644 --- a/cirq-core/cirq/circuits/circuit_test.py +++ b/cirq-core/cirq/circuits/circuit_test.py @@ -107,10 +107,12 @@ def test_insert_moment_types_deprecated(): circuit = cirq.Circuit(device=moment_and_op_type_validating_device) moment_or_operation_tree = [cirq.X(x), cirq.Moment([cirq.Y(x)])] - circuit.insert(0, moment_or_operation_tree) + with cirq.testing.assert_deprecated('insert', deadline='v0.15'): + circuit.insert(0, moment_or_operation_tree) moment_or_operation_tree = [[cirq.Moment([cirq.X(x)])]] - circuit.insert(0, moment_or_operation_tree) + with cirq.testing.assert_deprecated('insert', deadline='v0.15'): + circuit.insert(0, moment_or_operation_tree) def test_setitem(): diff --git a/cirq-core/cirq/devices/device.py b/cirq-core/cirq/devices/device.py index 953ec546bdb..f12081660a0 100644 --- a/cirq-core/cirq/devices/device.py +++ b/cirq-core/cirq/devices/device.py @@ -100,6 +100,10 @@ def qid_pairs(self) -> Optional[FrozenSet['cirq.SymmetricalQidPair']]: ) return frozenset([SymmetricalQidPair(q, q2) for q in qs for q2 in qs if q < q2]) + @_compat.deprecated( + deadline='v0.15', + fix='Devices will no longer decompose operations.', + ) def decompose_operation(self, operation: 'cirq.Operation') -> 'cirq.OP_TREE': """Returns a device-valid decomposition for the given operation. diff --git a/cirq-core/cirq/devices/device_test.py b/cirq-core/cirq/devices/device_test.py index 44c2ede3719..b88fea459ab 100644 --- a/cirq-core/cirq/devices/device_test.py +++ b/cirq-core/cirq/devices/device_test.py @@ -53,6 +53,16 @@ def __init__(self, qubits): assert len(QubitFieldDevice([cirq.NamedQubit(str(s)) for s in range(10)]).qid_pairs()) == 45 +def test_decompose_operation_deprecated(): + q0 = cirq.GridQubit(0, 0) + + class RawDevice(cirq.Device): + pass + + with cirq.testing.assert_deprecated('decompose', deadline='v0.15'): + RawDevice().decompose_operation(cirq.H(q0)) + + def test_qid_pair_deprecated(): q0, q1, q2, q3 = cirq.LineQubit.range(4) with cirq.testing.assert_deprecated('device.metadata', deadline='v0.15', count=3): diff --git a/cirq-pasqal/cirq_pasqal/pasqal_device_test.py b/cirq-pasqal/cirq_pasqal/pasqal_device_test.py index b92fa951907..ff5b9e7f620 100644 --- a/cirq-pasqal/cirq_pasqal/pasqal_device_test.py +++ b/cirq-pasqal/cirq_pasqal/pasqal_device_test.py @@ -191,7 +191,7 @@ def test_validate_operation_errors_deprecated(): with pytest.raises( NotImplementedError, match="Measurements on Pasqal devices don't support invert_mask." ): - with cirq.testing.assert_deprecated('decompose', deadline='v0.15'): + with cirq.testing.assert_deprecated('insert', deadline='v0.15'): circuit.append(cirq.measure(*d.qubits, invert_mask=(True, False, False)))