From 3ac3c30d0db92152a25ef2cf528454f561f4bf16 Mon Sep 17 00:00:00 2001 From: skushnir123 Date: Wed, 7 Jun 2023 13:32:09 -0400 Subject: [PATCH] Inconsistent ordering of tags (#6123) * 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 * ran formatter --------- Co-authored-by: Tanuj Khattar --- cirq-core/cirq/ops/gateset.py | 3 ++- cirq-core/cirq/ops/gateset_test.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/cirq-core/cirq/ops/gateset.py b/cirq-core/cirq/ops/gateset.py index d7b72d11aa7..f58c0c55cf0 100644 --- a/cirq-core/cirq/ops/gateset.py +++ b/cirq-core/cirq/ops/gateset.py @@ -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, diff --git a/cirq-core/cirq/ops/gateset_test.py b/cirq-core/cirq/ops/gateset_test.py index 011a53cad3c..e09be035149 100644 --- a/cirq-core/cirq/ops/gateset_test.py +++ b/cirq-core/cirq/ops/gateset_test.py @@ -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)