Skip to content

Commit

Permalink
Remove op validation when inserting into Circuit (quantumlib#4669)
Browse files Browse the repository at this point in the history
Proposal to remove validation on op insertion methods quantumlib#4667
  • Loading branch information
dabacon authored Nov 17, 2021
1 parent 26b6dd3 commit c1a84ae
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 58 deletions.
27 changes: 0 additions & 27 deletions 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 @@ -1705,15 +1683,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 @@ -1968,7 +1944,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 @@ -2012,7 +1987,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 @@ -2207,7 +2181,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/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):
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

0 comments on commit c1a84ae

Please sign in to comment.