Skip to content

Commit 0b4063b

Browse files
Deprecate device.decompose_operation.
1 parent 86c873b commit 0b4063b

File tree

5 files changed

+48
-13
lines changed

5 files changed

+48
-13
lines changed

cirq-core/cirq/circuits/circuit.py

+28-10
Original file line numberDiff line numberDiff line change
@@ -1717,7 +1717,8 @@ def __init__(
17171717
"""
17181718
self._moments: List['cirq.Moment'] = []
17191719
self._device = device
1720-
self.append(contents, strategy=strategy)
1720+
with _compat.block_overlapping_deprecation('.*'):
1721+
self.append(contents, strategy=strategy)
17211722

17221723
@property # type: ignore
17231724
@_compat.deprecated(
@@ -2045,15 +2046,32 @@ def insert(
20452046
Raises:
20462047
ValueError: Bad insertion strategy.
20472048
"""
2048-
moments_and_operations = list(
2049-
ops.flatten_to_ops_or_moments(
2050-
ops.transform_op_tree(
2051-
moment_or_operation_tree,
2052-
self._device.decompose_operation,
2053-
preserve_moments=True,
2054-
),
2049+
if self._device == devices.UNCONSTRAINED_DEVICE:
2050+
moments_and_operations = list(
2051+
ops.flatten_to_ops_or_moments(
2052+
ops.transform_op_tree(
2053+
moment_or_operation_tree,
2054+
preserve_moments=True,
2055+
),
2056+
)
20552057
)
2056-
)
2058+
else:
2059+
_compat._warn_or_error(
2060+
'circuit.insert behavior relies on circuit.device.\n'
2061+
'The ability to construct a circuit with a device\n'
2062+
'will be removed in cirq v0.15. please update this use of\n'
2063+
'insert.'
2064+
)
2065+
with _compat.block_overlapping_deprecation('decompose'):
2066+
moments_and_operations = list(
2067+
ops.flatten_to_ops_or_moments(
2068+
ops.transform_op_tree(
2069+
moment_or_operation_tree,
2070+
self._device.decompose_operation,
2071+
preserve_moments=True,
2072+
),
2073+
)
2074+
)
20572075

20582076
for moment_or_op in moments_and_operations:
20592077
if isinstance(moment_or_op, ops.Moment):
@@ -2114,7 +2132,7 @@ def insert_into_range(self, operations: 'cirq.OP_TREE', start: int, end: int) ->
21142132
)
21152133
cannot_add_lambda = lambda a, b: not self._device.can_add_operation_into_moment(a, b)
21162134

2117-
with _compat.block_overlapping_deprecation('can_add_operation_into_moment'):
2135+
with _compat.block_overlapping_deprecation('(can_add_operation_into_moment|insert)'):
21182136
while op_index < len(flat_ops):
21192137
op = flat_ops[op_index]
21202138
while i < end and cannot_add_lambda(op, self._moments[i]):

cirq-core/cirq/circuits/circuit_test.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,12 @@ def test_insert_moment_types_deprecated():
107107
circuit = cirq.Circuit(device=moment_and_op_type_validating_device)
108108

109109
moment_or_operation_tree = [cirq.X(x), cirq.Moment([cirq.Y(x)])]
110-
circuit.insert(0, moment_or_operation_tree)
110+
with cirq.testing.assert_deprecated('insert', deadline='v0.15'):
111+
circuit.insert(0, moment_or_operation_tree)
111112

112113
moment_or_operation_tree = [[cirq.Moment([cirq.X(x)])]]
113-
circuit.insert(0, moment_or_operation_tree)
114+
with cirq.testing.assert_deprecated('insert', deadline='v0.15'):
115+
circuit.insert(0, moment_or_operation_tree)
114116

115117

116118
def test_setitem():

cirq-core/cirq/devices/device.py

+4
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ def qid_pairs(self) -> Optional[FrozenSet['cirq.SymmetricalQidPair']]:
100100
)
101101
return frozenset([SymmetricalQidPair(q, q2) for q in qs for q2 in qs if q < q2])
102102

103+
@_compat.deprecated(
104+
deadline='v0.15',
105+
fix='Devices will no longer decompose operations.',
106+
)
103107
def decompose_operation(self, operation: 'cirq.Operation') -> 'cirq.OP_TREE':
104108
"""Returns a device-valid decomposition for the given operation.
105109

cirq-core/cirq/devices/device_test.py

+10
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ def __init__(self, qubits):
5353
assert len(QubitFieldDevice([cirq.NamedQubit(str(s)) for s in range(10)]).qid_pairs()) == 45
5454

5555

56+
def test_decompose_operation_deprecated():
57+
q0 = cirq.GridQubit(0, 0)
58+
59+
class RawDevice(cirq.Device):
60+
pass
61+
62+
with cirq.testing.assert_deprecated('decompose', deadline='v0.15'):
63+
RawDevice().decompose_operation(cirq.H(q0))
64+
65+
5666
def test_qid_pair_deprecated():
5767
q0, q1, q2, q3 = cirq.LineQubit.range(4)
5868
with cirq.testing.assert_deprecated('device.metadata', deadline='v0.15', count=3):

cirq-pasqal/cirq_pasqal/pasqal_device_test.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ def test_validate_operation_errors_deprecated():
189189
with pytest.raises(
190190
NotImplementedError, match="Measurements on Pasqal devices don't support invert_mask."
191191
):
192-
circuit.append(cirq.measure(*d.qubits, invert_mask=(True, False, False)))
192+
with cirq.testing.assert_deprecated('insert', deadline='v0.15'):
193+
circuit.append(cirq.measure(*d.qubits, invert_mask=(True, False, False)))
193194

194195

195196
def test_validate_moment():

0 commit comments

Comments
 (0)