-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
High-level-synthesis for permutations #9157
Merged
Merged
Changes from 48 commits
Commits
Show all changes
61 commits
Select commit
Hold shift + click to select a range
8475b92
Adding permutation synthesis algorithm for LNN
alexanderivrii 54c915f
release notes
alexanderivrii 6a99690
Checking that the synthesized permutation adheres to the LNN connecti…
alexanderivrii 7d65299
Adding tests for 15 qubits
alexanderivrii 5665623
Changing Permutation to be a Gate rather than QuantumCircuit
alexanderivrii 3b6bf99
Adding the property pattern to Permutation class
alexanderivrii 7b21ffe
fixing assert
alexanderivrii 5872607
improving description message for _get_ordered_swap
alexanderivrii 3b67f88
Merge branch 'main' into permutations-over-lnn
alexanderivrii fe4c5ac
applying suggestions from code review
alexanderivrii c908692
Merge branch 'permutations-over-lnn' of github.com:alexanderivrii/qis…
alexanderivrii 64b41bd
minor
alexanderivrii 63d4aba
attempt to fix docstring
alexanderivrii b80ec53
Another attempt to fix docsting
alexanderivrii 542d9fb
another attempt to fix docstring
alexanderivrii d0d61f4
temporarily simplifying docstring to see if this passes docs build
alexanderivrii e85dbf2
adding blank line
alexanderivrii 500f093
another attempt
alexanderivrii 40e494c
Restoring docstring
alexanderivrii f9391ee
removing extra line
alexanderivrii ea5f191
Merge branch 'main' into permutations-over-lnn
alexanderivrii 00b2345
Merge branch 'main' into permutations-hls
alexanderivrii 235b9bb
Merge branch 'permutations-over-lnn' into permutations-hls
alexanderivrii 8baa0a8
adding __array__ method for permutation + tests
alexanderivrii 4c63a77
HLS permutation plugin based on the original synthesis algorithm for …
alexanderivrii 15f1228
speeding up _get_ordered_swap based on review comments
alexanderivrii 9e9d796
Merge branch 'permutations-over-lnn' into permutations-hls
alexanderivrii c273097
Adding depth-2 synthesis algorithm for permutations for all-to-all co…
alexanderivrii 9be458f
release notes
alexanderivrii 58148dc
Adding example to release notes
alexanderivrii a609575
Merge branch 'main' into permutations-hls
alexanderivrii 297cc75
Update documentation of Permutation
alexanderivrii a37c98c
add missing import
alexanderivrii 85bdfa5
drawing decomposed circuit with permutations
alexanderivrii 55a0ab5
forgot parenthesis
alexanderivrii 8b80e02
Merge branch 'main' into permutations-hls
alexanderivrii ec71978
restoring qasm for circuits containing Permutations
alexanderivrii 6d6c5f3
Adding permutation method to QuantumCircuit
alexanderivrii 6559510
Adding test for quantum circuit with permutations
alexanderivrii a686c5b
pylint
alexanderivrii e139bf2
adding inverse() method to permutations
alexanderivrii 3d97306
qpy support for permutations
alexanderivrii 2cfa53c
tests for quantum circuits with permutations
alexanderivrii 4584353
Merge branch 'main' into permutations-hls
alexanderivrii bdf5966
Merge branch 'main' into permutations-hls
alexanderivrii 8ff8fb1
Merge branch 'main' into permutations-hls
alexanderivrii 42fdd0a
checking depth bound on the ACG method
alexanderivrii e826085
Adding tests for new Permutation functionality
alexanderivrii 1a02cd2
Merge branch 'main' into permutations-hls
alexanderivrii ced6d6a
black
alexanderivrii d07dd3f
Following review, keeping the old Permutation quantum circuit for bac…
alexanderivrii fc63af6
Merge branch 'main' into permutations-hls
alexanderivrii f2ecb31
additional fixes
alexanderivrii a408fef
updating release notes
alexanderivrii 38743ec
docs fix
alexanderivrii e1e9f05
Removing permutation method from QuantumCircuit
alexanderivrii e577372
Adding QPY test for circuits with permutation gates
alexanderivrii 4eca538
Update qiskit/circuit/quantumcircuit.py
alexanderivrii 79c7a73
Set default qasm name override to None
mtreinish 97c1f78
Merge branch 'main' into permutations-hls
mtreinish 794d31b
Merge branch 'main' into permutations-hls
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2022. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
"""Synthesis algorithm for Permutation gates for full-connectivity.""" | ||
|
||
from qiskit.circuit.quantumcircuit import QuantumCircuit | ||
from .permutation_utils import ( | ||
_get_ordered_swap, | ||
_inverse_pattern, | ||
_pattern_to_cycles, | ||
_decompose_cycles, | ||
) | ||
|
||
|
||
def synth_permutation_basic(pattern): | ||
"""Synthesize a permutation circuit for a fully-connected | ||
architecture using sorting. | ||
More precisely, if the input permutation is a cycle of length ``m``, | ||
then this creates a quantum circuit with ``m-1`` SWAPs (and of depth ``m-1``); | ||
if the input permutation consists of several disjoint cycles, then each cycle | ||
is essentially treated independently. | ||
Args: | ||
pattern (Union[list[int], np.ndarray]): permutation pattern, describing | ||
which qubits occupy the positions 0, 1, 2, etc. after applying the | ||
permutation. That is, ``pattern[k] = m`` when the permutation maps | ||
qubit ``m`` to position ``k``. As an example, the pattern ``[2, 4, 3, 0, 1]`` | ||
means that qubit ``2`` goes to position ``0``, qubit ``4`` goes to | ||
position ``1``, etc. | ||
Returns: | ||
QuantumCircuit: the synthesized quantum circuit. | ||
""" | ||
# This is the very original Qiskit algorithm for synthesizing permutations. | ||
|
||
num_qubits = len(pattern) | ||
qc = QuantumCircuit(num_qubits) | ||
|
||
swaps = _get_ordered_swap(pattern) | ||
|
||
for swap in swaps: | ||
qc.swap(swap[0], swap[1]) | ||
|
||
return qc | ||
|
||
|
||
def synth_permutation_acg(pattern): | ||
"""Synthesize a permutation circuit for a fully-connected | ||
architecture using the Alon, Chung, Graham method. | ||
This produces a quantum circuit of depth 2 (measured in the number of SWAPs). | ||
This implementation is based on the Theorem 2 in the paper | ||
"Routing Permutations on Graphs Via Matchings" (1993), | ||
available at https://www.cs.tau.ac.il/~nogaa/PDFS/r.pdf. | ||
Args: | ||
pattern (Union[list[int], np.ndarray]): permutation pattern, describing | ||
which qubits occupy the positions 0, 1, 2, etc. after applying the | ||
permutation. That is, ``pattern[k] = m`` when the permutation maps | ||
qubit ``m`` to position ``k``. As an example, the pattern ``[2, 4, 3, 0, 1]`` | ||
means that qubit ``2`` goes to position ``0``, qubit ``4`` goes to | ||
position ``1``, etc. | ||
Returns: | ||
QuantumCircuit: the synthesized quantum circuit. | ||
""" | ||
|
||
num_qubits = len(pattern) | ||
qc = QuantumCircuit(num_qubits) | ||
|
||
# invert pattern (Qiskit notation is opposite) | ||
cur_pattern = _inverse_pattern(pattern) | ||
cycles = _pattern_to_cycles(cur_pattern) | ||
swaps = _decompose_cycles(cycles) | ||
|
||
for swap in swaps: | ||
qc.swap(swap[0], swap[1]) | ||
|
||
return qc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit concerned about making this change from a backwards compatibility PoV in a single release. While I agree with this being a
Gate
subclass instead of aQuantumCircuit
is where we want to be I'm worried about any users of this class today that may be using it with a circuit as is. For example the docs here show a good example of this withPermutation(...).draw()
, but there are others I can think of the top of my head would be something like:Permutation(...).compose(sub_circuit)
orPermutation.measure_all()
all of which were previously completely valid which would be broken by this change.I'm thinking we need to figure out a better way to manage this migration which leaves the
QuantumCircuit
basedPermutation
class in place as is for now but establishes a newGate
based class as a standalone thing (likePermutationGate
or something) in this PR and maybe update thisQuantumCircuit
based one to just wrap the newGate
class. Then in the next release we can deprecate theQuantumCircuit
version and tell people to use the gate directly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this is a very good point. I keep relying on qiskit-terra tests to make sure that nothing gets broken, yet I keep forgetting that there is also a lot of other code outside of qiskit-terra. The example with
Permutation.measure_all()
clearly shows why we need to keep the old functionality. It should be quite straightforward for me to keep the old implementation and to call the new implementation differently, in this way this development no longer changes any existing behavior, it only adds new things.I have already started doing changes, but here are a couple of preliminary thoughts. The name
PermutationGate
for the new class is good. For objects of typePermutationGate
we can still haveop.name = "permutation"
, since there is no danger to confuse permutation circuits and permutation gates (and in any case the name assigned to a permutation circuit is never"pemutation"
but rather something like"permutation_[2,4,3,0,1]"
). In particular, we can keep using names likepermutation.kms
inentry_points
to specify permutation synthesis algorithms forPermutationGate
objects.The suggested deprecation flow also allows to avoid passing
num_qubits
andseed
to the initializer of thePermutationGate
, making the code a bit cleaner. There is a functionnp.random.permutation
that returns a random permutation pattern, from which aPermutationGate
can be constructed.