-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add default decomposition for cirq.QubitPermutationGate in terms of adjacent swaps #5093
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
Changes from 2 commits
1e8f898
54a783e
e3d5f4a
68c8ce9
ed002f4
f4c2154
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
|
||
from cirq import protocols, value | ||
from cirq._compat import deprecated | ||
from cirq.ops import raw_types | ||
from cirq.ops import raw_types, swap_gates | ||
|
||
if TYPE_CHECKING: | ||
import cirq | ||
|
@@ -74,6 +74,15 @@ def num_qubits(self): | |
def _has_unitary_(self): | ||
return True | ||
|
||
def _decompose_(self, qubits: Sequence['cirq.Qid']) -> 'cirq.OP_TREE': | ||
qubit_ids = [*range(len(qubits))] | ||
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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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:
I'd support adding a separate method for converting to linear-swap, but the default should be all-to-all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The class already has a There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opened #5097 to track the optimization. |
||
qubit_ids[j], qubit_ids[j - 1] = qubit_ids[j - 1], qubit_ids[j] | ||
assert self._permutation[qubit_ids[i]] == i | ||
|
||
def _apply_unitary_(self, args: 'cirq.ApplyUnitaryArgs'): | ||
# Compute the permutation index list. | ||
permuted_axes = list(range(len(args.target_tensor.shape))) | ||
|
Uh oh!
There was an error while loading. Please reload this page.