Skip to content

Commit

Permalink
Inconsistent ordering of tags (#6123)
Browse files Browse the repository at this point in the history
* added sorted(*) to fix issue of identical sets being labeled unequal and added tests

* only comparing description when given

* Update cirq-core/cirq/ops/gateset_test.py

Co-authored-by: Tanuj Khattar <tanujkhattar@google.com>

* ran formatter

---------

Co-authored-by: Tanuj Khattar <tanujkhattar@google.com>
  • Loading branch information
skushnir123 and tanujkhattar committed Jun 7, 2023
1 parent cb89f3a commit 3ac3c30
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cirq-core/cirq/ops/gateset.py
Expand Up @@ -257,11 +257,12 @@ def __repr__(self) -> str:

def _value_equality_values_(self) -> Any:
# `isinstance` is used to ensure the a gate type and gate instance is not compared.
description = self.description if self.description != self._default_description() else None
return (
isinstance(self.gate, raw_types.Gate),
self.gate,
self.name,
self.description,
description,
self._ignore_global_phase,
self._tags_to_accept,
self._tags_to_ignore,
Expand Down
21 changes: 21 additions & 0 deletions cirq-core/cirq/ops/gateset_test.py
Expand Up @@ -81,6 +81,27 @@ def test_gate_family_default_name_and_description(gate, tags_to_accept, tags_to_
assert (ignored_match is None) == (tags_to_ignore == [])


@pytest.mark.parametrize(
'tags_to_accept_fam1, tags_to_ignore_fam1, tags_to_accept_fam2, tags_to_ignore_fam2',
[
(tuple("ab"), tuple("cd"), tuple("ba"), tuple("dc")),
(tuple("ab"), [], tuple("ba"), []),
([], tuple("ab"), [], tuple("ba")),
],
)
def test_gate_family_equality_with_tags(
tags_to_accept_fam1, tags_to_ignore_fam1, tags_to_accept_fam2, tags_to_ignore_fam2
):
gate_fam1 = cirq.GateFamily(
cirq.X, tags_to_accept=tags_to_accept_fam1, tags_to_ignore=tags_to_ignore_fam1
)
gate_fam2 = cirq.GateFamily(
cirq.X, tags_to_accept=tags_to_accept_fam2, tags_to_ignore=tags_to_ignore_fam2
)

assert gate_fam1 == gate_fam2


def test_invalid_gate_family():
with pytest.raises(ValueError, match='instance or subclass of `cirq.Gate`'):
_ = cirq.GateFamily(gate=cirq.Operation)
Expand Down

0 comments on commit 3ac3c30

Please sign in to comment.