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

Optimise QubitPermutationGate decomposition #6588

Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
PR suggestions applied
  • Loading branch information
migueltorrescosta committed May 10, 2024
commit b318848a625334831a1bab9f5ff9ec06342bf5f4
10 changes: 6 additions & 4 deletions cirq-core/cirq/ops/permutation_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,21 @@ def _has_unitary_(self):
return True

def _decompose_(self, qubits: Sequence['cirq.Qid']) -> 'cirq.OP_TREE':
# List is need otherwise permutation[j] = -1 fails to assign to a tuple
permutation = list(deepcopy(self.permutation))
permutation = [p for p in self.permutation]

for i in range(len(permutation)):

if permutation[i] == -1:
continue
cycle = [i]
while permutation[cycle[-1]] != i:
cycle.append(permutation[cycle[-1]])

for j in cycle:
permutation[j] = -1
cycle.reverse()
# yield len(cycle) - 1 swap operations

for idx in cycle[1:]:
yield swap_gates.SWAP(qubits[cycle[0]], qubits[idx])

def _apply_unitary_(self, args: 'cirq.ApplyUnitaryArgs'):
# Compute the permutation index list.
Expand Down