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

Use group_interchangeable_qubits for qubit comparison in GateOperation #4941

Merged
merged 5 commits into from
Feb 3, 2022
Merged
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
23 changes: 12 additions & 11 deletions cirq-core/cirq/ops/gate_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

"""Basic types defining qubits, gates, and operations."""

import itertools
import re
from typing import (
AbstractSet,
Expand All @@ -22,7 +23,6 @@
Collection,
Dict,
FrozenSet,
List,
Optional,
Sequence,
Tuple,
Expand Down Expand Up @@ -146,17 +146,18 @@ def _json_dict_(self) -> Dict[str, Any]:
def _group_interchangeable_qubits(
self,
) -> Tuple[Union['cirq.Qid', Tuple[int, FrozenSet['cirq.Qid']]], ...]:

if not isinstance(self.gate, gate_features.InterchangeableQubitsGate):
return self.qubits

groups: Dict[int, List['cirq.Qid']] = {}
for i, q in enumerate(self.qubits):
k = self.gate.qubit_index_to_equivalence_group_key(i)
if k not in groups:
groups[k] = []
groups[k].append(q)
return tuple(sorted((k, frozenset(v)) for k, v in groups.items()))
else:
return tuple(
(k, frozenset(g for _, g in kg))
for k, kg in itertools.groupby(
enumerate(self.qubits),
lambda i_q: cast(
gate_features.InterchangeableQubitsGate, self.gate
).qubit_index_to_equivalence_group_key(i_q[0]),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we break this up into a few simpler lines ? This is a little hard to read.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

)
)

def _value_equality_values_(self):
return self.gate, self._group_interchangeable_qubits()
Expand Down Expand Up @@ -349,7 +350,7 @@ def _equal_up_to_global_phase_(
) -> Union[NotImplementedType, bool]:
if not isinstance(other, type(self)):
return NotImplemented
if self.qubits != other.qubits:
if self._group_interchangeable_qubits() != other._group_interchangeable_qubits():
return False
return protocols.equal_up_to_global_phase(self.gate, other.gate, atol=atol)

Expand Down