Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ElidePermutations pass to optimization level 3 (Qiskit#12111)
* Add ElideSwaps transpiler pass This commit adds a new transpiler pass ElideSwaps which is a logical optimization pass designed to run prior to layout or any other physical embedding steps in the transpiler pipeline. It traverses the DAG in topological order and when a swap gate is encountered it removes that gate and instead permutes the logical qubits for any subsequent gates in the DAG. This will eliminate any swaps in a circuit not caused by routing. Additionally, this pass is added to the preset pass manager for optimization level 3, we can consider adding it to other levels too if we think it makes sense (there is little overhead, and almost 0 if there are no swaps). One thing to note is that this pass does not recurse into control flow blocks at all, it treats them as black box operations. So if any control flow blocks contain swap gates those will not be optimized away. This change was made because mapping the permutation outside and inside any control flow block was larger in scope than what the intent for this pass was. That type of work is better suited for the routing passes which already have to reason about this. * Update tests with optimization level 3 * Pass final layout from ElideSwap to output The new ElideSwap pass is causing an output permutation just as a routing pass would. This information needs to be passed through to the output in case there is no layout or routing run. In those cases the information about the output permutation caused by the swap elision will be lost and doing layout aware operations like Operator.from_circuit() will not be able to reason about the permutation. This commit fixes this by inserting the original layout and qubit mapping into the property set along with the final layout. Then the base pass class and pass manager class are updated to use the original layout as the initial layout if one isn't set. In cases where we run layout and routing the output metadata from those passes will superscede these new metadata fields. * Move pass in opt level 3 earlier in stage and skip with explicit layout This commit fixes 2 issues in the execution of the new ElideSwaps pass as part of optimization level 3. First we were running it too late in the process both after high level synthesis (which isn't relavant yet, but will be soon when this is expanded to elide all permutations not just swaps) and also after reset diagonal before measurement. The second issue is that if the user is specifying to run with a manual layout set we should not run this pass, as it will interfere with the user intent. * Doc and copy paste fixes * Expand test coverage * Update permutation tracking There were 2 issues with the permutation tracking done in an earlier commit. First, it conflicted with the final_layout property set via routing (or internally by the routing done in the combined sabre layout). This was breaking conditional checks in the preset pass manager around embedding. To fix this a different property is used and set as the output final layout if no final layout is set. The second issue was the output layout object was not taking into account a set initial layout which will permute the qubits and cause the output to not be up to date. This is fixed by updating apply layout to apply the initial layout to the elision_final_layout in the property set. * Generalize pass to support PermutationGate too This commit generalizes the pass from just considering swap gates to all permutations (via the PermutationGate class). This enables the pass to elide additional circuit permutations, not just the special case of a swap gate. The pass and module are renamed accordingly to ElidePermutations and elide_permutations respectively. * Fix permutation handling This commit fixes the recently added handling of the PermutationGate so that it correctly is mapping the qubits. The previous iteration of this messed up the mapping logic so it wasn't valid. * Fix formatting * Fix final layout handling for no initial layout * Improve documentation and log a warning if run post layout * Fix final layout handling with no ElideSwaps being run * Fix docs build * Fix release note * Fix typo * Add test for routing and elide permutations * Apply suggestions from code review Co-authored-by: Jim Garrison <[email protected]> * Rename test file to test_elide_permutations.py * Apply suggestions from code review Co-authored-by: Kevin Hartman <[email protected]> * Fix test import after rebase * fixing failing test cases this should pass CI after merging Qiskit#12057 * addresses kehas comments - thx * Adding FinalyzeLayouts pass to pull the virtual circuit permutation from ElidePermutations to the final layout * formatting * partial rebase on top of 12057 + tests * also need test_operator for partial rebase * removing from transpiler flow for now; reworking elide tests * also adding permutation gate to the example * also temporarily reverting test_transpiler.py * update to release notes * minor fixes * adding ElidePermutations and FinalizeLayouts to level 3 * fixing tests * release notes * properly merging with main (now that ElidePermutations is merged) * and also deleting finalize_layouts * Update releasenotes/notes/add-elide-permutations-to-pipeline-077dad03bd55ab9c.yaml * updating code comment * Fixing failing test: now that ElidePermutations pass runs with optimization level 3, it does remove the SWAP gate. So we need to consider optimization_level=1 to assert that the extra pass does not remove SWAP gates --------- Co-authored-by: Matthew Treinish <[email protected]> Co-authored-by: Jim Garrison <[email protected]> Co-authored-by: Kevin Hartman <[email protected]> Co-authored-by: Sebastian Brandhofer <[email protected]>
- Loading branch information