Skip to content

Commit

Permalink
Deprecate device.decompose_operation in cirq-core. (quantumlib#4922)
Browse files Browse the repository at this point in the history
Yet another deprecation for quantumlib#4744 . Vendor deprecations of this fxn to follow.
  • Loading branch information
MichaelBroughton authored and rht committed May 1, 2023
1 parent fe38693 commit 41ddc5c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
39 changes: 29 additions & 10 deletions cirq-core/cirq/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions cirq-core/cirq/circuits/circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
4 changes: 4 additions & 0 deletions cirq-core/cirq/devices/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions cirq-core/cirq/devices/device_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion cirq-pasqal/cirq_pasqal/pasqal_device_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))


Expand Down

0 comments on commit 41ddc5c

Please sign in to comment.