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

Add default decomposition for cirq.QubitPermutationGate in terms of adjacent swaps #5093

Merged
merged 6 commits into from
Mar 18, 2022

Conversation

tanujkhattar
Copy link
Collaborator

@tanujkhattar tanujkhattar commented Mar 17, 2022

Closes #5090

Copy link
Collaborator

@95-martin-orion 95-martin-orion left a comment

Choose a reason for hiding this comment

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

I'm not sure there's a nice way to consistently decompose this, as it depends on the device layout.

cirq-core/cirq/ops/permutation_gate.py Show resolved Hide resolved
for i in range(len(qubits)):
q_i = qubit_ids.index(self._permutation.index(i))
for j in range(q_i, i, -1):
yield swap_gates.SWAP(qubits[j], qubits[j - 1])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Limiting SWAPs to adjacent-index qubits presupposes a linear qubit layout; on a 2D grid or more exotic layout, this will not work:

q = GridQubit.rect(2, 2)
# q[1] and q[2] are not adjacent ((0, 1) and (1, 0))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note that the decomposition here is not intended to be architecture aware. The goal here is to simply write some decomposition in terms of other "simpler" gates which eventually ends up decomposing into the native gateset.

For device specific routing, we would need more sophisticated transformers which take care of device topology, but that is out of scope of this PR. The choice to use adjacent index swaps which can be useful for a linear qubit layout is an arbitrary one, mostly because running circuits on a chain of qubits is a common use case and a low hanging fruit.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If this is meant to be architecture-agnostic, why don't we swap qubits directly (ignoring adjacency)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As I said, it's a choice we need to make. Both the approaches would be valid for the scope of this PR.

If a user is relying on the default decomposition of Cirq for compiling their circuit, it means they have already given up fine grained control. The reason I want to stick to this approach is that it's a low hanging fruit which targets a common use case of running circuits on a chain of qubits. It's not going to be enough for every scenario, but still slightly better than assuming all to all connectivity.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's not going to be enough for every scenario, but still slightly better than assuming all to all connectivity.

I'm not convinced that linear-swap is better than all-to-all, or even equally valid. Compared to linear-swap, all-to-all is:

  • cheaper to run on simulators (which use default decomposition)
  • easier to convert to efficient circuits in device-specific layout
  • cheaper to construct here (low priority, but worth considering)

I'd support adding a separate method for converting to linear-swap, but the default should be all-to-all.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

cheaper to run on simulators (which use default decomposition)

The class already has a _apply_unitary_ defined, which should be used by simulators over the default decomposition. Is that not the case?

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is true for Cirq-internal simulators, but not in general. For example, qsim attempts to decompose gates to a specific subset that it natively supports, falling back on conversion to unitary only if no decomposition is defined (code).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Opened #5097 to track the optimization.

@tanujkhattar
Copy link
Collaborator Author

I'm not sure there's a nice way to consistently decompose this, as it depends on the device layout.

As mentioned above, the goal here is to just decompose into a set of simpler operations which ends up at the default cirq gateset. Topology aware qubit routing should be handled separately by more sophisticated transformers.

Copy link
Collaborator

@95-martin-orion 95-martin-orion left a comment

Choose a reason for hiding this comment

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

Approved to unblock v0.14, conditioned on opening a follow-up issue to convert to all-to-all swaps.

@tanujkhattar tanujkhattar added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Mar 18, 2022
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Mar 18, 2022
@CirqBot CirqBot merged commit 848bfde into quantumlib:master Mar 18, 2022
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Mar 18, 2022
tonybruguier pushed a commit to tonybruguier/Cirq that referenced this pull request Apr 14, 2022
…djacent swaps (quantumlib#5093)

- Adds decomposition to `cirq.QubitPermutationGate` in terms of minimum number of adjacent swap operations on qubits. 
- Part of quantumlib#4858

Closes quantumlib#5090
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
…djacent swaps (quantumlib#5093)

- Adds decomposition to `cirq.QubitPermutationGate` in terms of minimum number of adjacent swap operations on qubits. 
- Part of quantumlib#4858

Closes quantumlib#5090
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
…djacent swaps (quantumlib#5093)

- Adds decomposition to `cirq.QubitPermutationGate` in terms of minimum number of adjacent swap operations on qubits. 
- Part of quantumlib#4858

Closes quantumlib#5090
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: S 10< lines changed <50
Projects
None yet
Development

Successfully merging this pull request may close these issues.

QubitPermutationGate should be json serializable and define a _decompose_
4 participants