Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove op validation when inserting into Circuit #4669

Merged
merged 4 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions cirq-core/cirq/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,28 +848,6 @@ def _has_op_at(self, moment_index: int, qubits: Iterable['cirq.Qid']) -> bool:
qubits
)

def _validate_op_tree_qids(self, op_tree: 'cirq.OP_TREE') -> None:
"""Raises an exception if any operation in `op_tree` has qids that don't
match its qid shape.

Args:
op_tree: The operation to validate.

Raises:
ValueError: The operation had qids that don't match its qid shape.
"""
# Cast from Iterable[Operation, Moment] because preserve_moments is
# False.
for op in cast(Iterable['cirq.Operation'], ops.flatten_op_tree(op_tree)):
if protocols.qid_shape(op) != protocols.qid_shape(op.qubits):
raise ValueError(
'Invalid operation. '
'An operation has qid shape <{!r}> but is on qids with '
'shape <{!r}>. The operation is <{!r}>.'.format(
protocols.qid_shape(op), protocols.qid_shape(op.qubits), op
)
)

def all_qubits(self) -> FrozenSet['cirq.Qid']:
"""Returns the qubits acted upon by Operations in this circuit."""
return frozenset(q for m in self.moments for q in m.qubits)
Expand Down Expand Up @@ -1710,15 +1688,13 @@ def __setitem__(self, key, value):
if not isinstance(value, ops.Moment):
raise TypeError('Can only assign Moments into Circuits.')
self._device.validate_moment(value)
self._validate_op_tree_qids(value)

if isinstance(key, slice):
value = list(value)
if any(not isinstance(v, ops.Moment) for v in value):
raise TypeError('Can only assign Moments into Circuits.')
for moment in value:
self._device.validate_moment(moment)
self._validate_op_tree_qids(moment)

self._moments[key] = value

Expand Down Expand Up @@ -1973,7 +1949,6 @@ def insert(
self._device.validate_moment(cast(ops.Moment, moment_or_op))
else:
self._device.validate_operation(cast(ops.Operation, moment_or_op))
self._validate_op_tree_qids(moment_or_op)

# limit index to 0..len(self._moments), also deal with indices smaller 0
k = max(min(index if index >= 0 else len(self._moments) + index, len(self._moments)), 0)
Expand Down Expand Up @@ -2017,7 +1992,6 @@ def insert_into_range(self, operations: 'cirq.OP_TREE', start: int, end: int) ->
flat_ops = list(ops.flatten_to_ops(operations))
for op in flat_ops:
self._device.validate_operation(op)
self._validate_op_tree_qids(flat_ops)

i = start
op_index = 0
Expand Down Expand Up @@ -2212,7 +2186,6 @@ def batch_insert_into(self, insert_intos: Iterable[Tuple[int, 'cirq.OP_TREE']])
for i, insertions in insert_intos:
copy._moments[i] = copy._moments[i].with_operations(insertions)
self._device.validate_circuit(copy)
self._validate_op_tree_qids(copy)
self._moments = copy._moments

def batch_insert(self, insertions: Iterable[Tuple[int, 'cirq.OP_TREE']]) -> None:
Expand Down
31 changes: 0 additions & 31 deletions cirq-core/cirq/circuits/circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4025,37 +4025,6 @@ def test_moments_property(circuit_cls):
assert c.moments[1] == cirq.Moment([cirq.Y(q)])


@pytest.mark.parametrize('circuit_cls', [cirq.Circuit, cirq.FrozenCircuit])
def test_operation_shape_validation(circuit_cls):
class BadOperation1(cirq.Operation):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do validation in cirq.Operation base class ?

def _qid_shape_(self):
return (1,)

@property
def qubits(self):
return cirq.LineQid.for_qid_shape((1, 2, 3))

def with_qubits(self, *qubits):
raise NotImplementedError

class BadOperation2(cirq.Operation):
def _qid_shape_(self):
return (1, 2, 3, 9)

@property
def qubits(self):
return cirq.LineQid.for_qid_shape((1, 2, 3))

def with_qubits(self, *qubits):
raise NotImplementedError

_ = circuit_cls(cirq.X(cirq.LineQid(0, 2))) # Valid
with pytest.raises(ValueError, match='Invalid operation'):
_ = circuit_cls(BadOperation1())
with pytest.raises(ValueError, match='Invalid operation'):
_ = circuit_cls(BadOperation2())


@pytest.mark.parametrize('circuit_cls', [cirq.Circuit, cirq.FrozenCircuit])
def test_json_dict(circuit_cls):
q0, q1 = cirq.LineQubit.range(2)
Expand Down