-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
General routing #3762
General routing #3762
Conversation
This implements the Token Swapping approximation algorithm
There is a |
This pass just calls the ApproximateTokenSwapper code to compute which swaps needs to be performed and appends that to the circuit.
@ajavadia Do you want to algorithm (currently in I implemented the code as a pass as well but I haven't tested it yet. |
For me it is clear that everything in However, if this pass is the only one using these algorithms, why not having them together? |
I think the issue is that the functionality is useful outside of a pass as well. Therefore it may be odd to put it inside a pass only. I leave it up to you where to put it :) |
It is useful for transpiler or in general? |
It's useful in multiple transpiler passes.
It doesn't matter that much honestly. Just leave it as is. |
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 tried running the pass. I think the DAGCircuit.extend_back()
method is buggy, which is preventing the pass from working at the moment. Is this what you find too?
It has this line which just seems wrong as it adds an entry with always the same key/value.
edge_map.update([(qbit, qbit) for qbit in qreg if qbit not in edge_map])
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.
Thank you for preparing this PR, @eddieschoute !
I think from_layout
/to_layout
(or start_layout
/goal_layout
?) is less confusing than initial_layout
/final_layout
that they are reserved as layout at the beginning/end of the circuit.
Since LayoutTransformation pass appends swaps at the end of circuit, the starting layout is often the "final_layout". To avoid confusion, I recommend to use the other terms.
I also think "from_layout" and "to_layout" can be taken in run time i.e. within run()
so that this pass can cooperate with the other passes. In many cases, the "from_layout" (in this pass, the layout at the end of the circuit) is given after running a routing pass. I think we should be able to get the "from_layout" (or "to_layout") from property_set
in run()
. (I know this is impossible for now.)
To this make possible, I think all of the routing passes should set property_set["final_layout"]
. It would be useful for other passes (I have one concrete example in my mind). Thoughts? @ajavadia , @1ucian0 , @kdk
(+1 to create a directory for transpiler.algorithms
)
Co-Authored-By: Ali Javadi-Abhari <[email protected]>
While you work on #3769, is there an alternative way of adding the Swaps to the end of the DAG? I think |
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 tested this on top of PR #3772 and it seems to work (Suggested a couple more bug fixes inline)
Sorry, I confused. Is input/output dagcircuit of the LayoutTransformation pass virtual or physical? Both are virtual? Agnostic? Excuse me for my unclear message, I meant something like
(Because it's not allowed to take any arguments other than dag in |
Co-Authored-By: Ali Javadi-Abhari <[email protected]>
Also add init imports
I'm not quite sure what you mean but I think the situation that makes most sense is when you have a DagCircuit that somehow respects the physical constraints and you wish to move the virtual qubits to different physical qubits (the |
They were only used in comments
I'm running into this linter error
but I don't see how that is cyclic. I only import types from the same module (through the init). Any suggestions? |
Co-Authored-By: Kevin Krsulich <[email protected]>
Running into a cyclic import again it seems... |
@eddieschoute I succeeded to reproduce cyclic import in my environment and found a workaround, it can be resolved by moving transpiler/routing directory to transpiler/passes/routing/algorithms. I know we need further discussion on the right place for algorithms used in more than routing. |
That's fine with me. The util functions I factored out of my previous PR #2860 but within this PR do not serve a purpose that is shared across multiple files. Just note that it is in #2860 so would likely need to be factored back out for that PR. So I'm not sure it's worth combining them into one file now. |
OK. Regarding file changes, I had second thought, to make my tweak minimal, I'll leave the two files |
@eddieschoute I've done with my API tweaks. Can you check if they make sense to you? |
I think they make sense. One nitpick I have is that we call the file token_swapper. I think for anyone outside the project they would not know what it does without reading the docs. It's why I chose the routing folder structure. Should we choose to rename the class to GeneralRouting or something similar instead? |
Good. It's better to make the names more user-friendly. I don't stick the name |
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.
Looks good. Please remove the WIP if it is ready.
Co-Authored-By: Ali Javadi-Abhari <[email protected]>
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.
Ok thanks I think this is good to go
* Add files for implementing general routing This implements the Token Swapping approximation algorithm * Implement LayoutTransform pass This pass just calls the ApproximateTokenSwapper code to compute which swaps needs to be performed and appends that to the circuit. * Restructure case+break (lint) * Change indentation (lint) * Fix some bugs suggested by Ali Co-Authored-By: Ali Javadi-Abhari <[email protected]> * Bugfixes in the LayoutTransformation pass Co-Authored-By: Ali Javadi-Abhari <[email protected]> * Moving LayoutTransformation to passes folder Also add init imports * Removed unused imports They were only used in comments * Resolve circular import (lint) Co-Authored-By: Kevin Krsulich <[email protected]> * Use relative import Co-Authored-By: Kevin Krsulich <[email protected]> * Fix references to Swap with relative import * Add seed to ApproximateTokenSwapper and its pass * Fix bug assuming dag.qubits() order is given Added basic test for pass Some types added * Reformat imports for test * Keep things DRY ;) * Add doc for test * move directory to avoid cyclic import * rename general.py to token_swapper.py * decrease graph size to make the test faster * expose PermutationCircuit * rename to from/to_layout * remove unused function * allow string layouts * allow string layouts * fix test * lint * Doc grammar Co-Authored-By: Ali Javadi-Abhari <[email protected]> Co-authored-by: Ali Javadi-Abhari <[email protected]> Co-authored-by: Kevin Krsulich <[email protected]> Co-authored-by: itoko <[email protected]> Co-authored-by: Matthew Treinish <[email protected]>
Summary
From #2860 the question arose if I could submit a PR for the Token Swapping algorithm separately and this pull request does that.
The algorithm allows for a partial permutation to be implemented through a Swap circuit that attempts to minimize circuit size.
Details and comments