Skip to content

Commit

Permalink
Use group_interchangeable_qubits for qubit comparison in GateOperation (
Browse files Browse the repository at this point in the history
quantumlib#4941)

Fix for issue quantumlib#4766 
Also, Introduced more idiomatic code for group_interchangeable_qubits implementation.
  • Loading branch information
Ashalynd authored and rht committed May 1, 2023
1 parent 4c313e4 commit 122b192
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 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,19 @@ 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
else:

def make_key(i_q: Tuple[int, 'cirq.Qid']) -> int:
return cast(
gate_features.InterchangeableQubitsGate, self.gate
).qubit_index_to_equivalence_group_key(i_q[0])

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()))
return tuple(
(k, frozenset(g for _, g in kg))
for k, kg in itertools.groupby(enumerate(self.qubits), make_key)
)

def _value_equality_values_(self):
return self.gate, self._group_interchangeable_qubits()
Expand Down Expand Up @@ -349,7 +351,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

0 comments on commit 122b192

Please sign in to comment.