Skip to content

Optimise QubitPermutationGate decomposition #6588

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

Merged
Changes from all 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
34 changes: 16 additions & 18 deletions cirq-core/cirq/ops/permutation_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Dict, Iterable, Sequence, Tuple, TYPE_CHECKING
from typing import Any, Dict, Sequence, Tuple, TYPE_CHECKING

from cirq import protocols, value
from cirq.ops import raw_types, swap_gates
Expand Down Expand Up @@ -74,23 +74,21 @@ def _has_unitary_(self):
return True

def _decompose_(self, qubits: Sequence['cirq.Qid']) -> 'cirq.OP_TREE':
n = len(qubits)
qubit_ids = [*range(n)]
is_sorted = False

def _swap_if_out_of_order(idx: int) -> Iterable['cirq.Operation']:
nonlocal is_sorted
if self._permutation[qubit_ids[idx]] > self._permutation[qubit_ids[idx + 1]]:
yield swap_gates.SWAP(qubits[idx], qubits[idx + 1])
qubit_ids[idx + 1], qubit_ids[idx] = qubit_ids[idx], qubit_ids[idx + 1]
is_sorted = False

while not is_sorted:
is_sorted = True
for i in range(0, n - 1, 2):
yield from _swap_if_out_of_order(i)
for i in range(1, n - 1, 2):
yield from _swap_if_out_of_order(i)
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

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