Skip to content

Commit

Permalink
update assert_valid to catch CNOT decomposition
Browse files Browse the repository at this point in the history
  • Loading branch information
lillian542 committed Jul 24, 2024
1 parent a456fce commit b67eea1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pennylane/ops/functions/assert_valid.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def _check_decomposition(op, skip_wire_mapping):

assert isinstance(decomp, list), "decomposition must be a list"
assert isinstance(compute_decomp, list), "decomposition must be a list"
assert op.__class__ not in [
decomp_op.__class__ for decomp_op in decomp
], "an operator should not be included in its own decomposition"

for o1, o2, o3 in zip(decomp, compute_decomp, processed_queue):
assert o1 == o2, "decomposition must match compute_decomposition"
Expand Down
11 changes: 11 additions & 0 deletions tests/ops/functions/test_assert_valid.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ def compute_decomposition(wires):
with pytest.raises(AssertionError, match="If has_decomposition is False"):
assert_valid(BadDecomp(wires=0), skip_pickle=True)

def test_decomposition_must_not_contain_op(self):
"""Test that decomposition op an operator doesn't include the operator itself"""

class BadDecomp(Operator):
@staticmethod
def compute_decomposition(wires):
return [BadDecomp(wires)]

with pytest.raises(AssertionError, match="should not be included in its own decomposition"):
assert_valid(BadDecomp(wires=0), skip_pickle=True)


class TestBadMatrix:
"""Tests involving matrix validation."""
Expand Down

0 comments on commit b67eea1

Please sign in to comment.