Fix control-flow routing in StochasticSwap#8880
Conversation
The `StochasticSwap` pass has some fairly complex hand-offs between different parts of its API, including in recursive calls and the regular hand-off between Python and Rust. In the course of adding the control-flow support, some of these became muddled, and the mapping between different virtual/physical/integer representations got mixed up, resulting in invalid swaps being output in the final circuit. This commit simplifies much of the internal mapping, removing many superfluous `DAGCircuit` creations and compositions. This also removes instances where two layouts were "chained"; this was not well typed (the output of a "virtual -> physical" mapping can't be the input for another "virtual -> physical" mapping), and in general was being used to "undo" some of compositions that were about to be applied. This fixes a tacit assumption in the original code that the initial layout was a trivial layout in the hand-off between Rust and Python. This worked until the recursive call added the `initial_layout` option, making this assumption invalid. Previously, virtual qubit bit instances were converted to integers (to allow them to be passed to Rust) using their indices into the original DAG, but the integer outputs were then converted _back_ using the `initial_layout`. In the old form, this worked anyway, but wasn't logically correct and consequently broke when the assumptions about `initial_layout` changed. For the recursive calls, we now ensure that the inner passes are essentially created with the same internal structure as the outer pass; the passed in `DAGCircuit` uses the same bit instances and same meaning of the virtual qubits as the outer circuit, and the `initial_layout` ensures that the inner passes start with at the same layout as the outer pass. This makes the inner passes more like a logical continuation of the current operation, rather than a completely separate entity that needs to have its virtual qubits remapped. The changes to the tests are twofold: - move the `CheckMap` calls earlier and apply them directly to the `StochasticSwap` output rather than the expected circuit, to improve the quality of failure error messages - use the same physical qubits inside the expected control-flow blocks; the new simpler form of doing the circuit rewriting internally in the pass ensures that the same bit objects are used all the way through the control-flow stack now, rather than creating new instances.
This commit adds full path transpile() tests for running with stochastic swap that validates a full path transpilation outputs a valid physical circuit. These tests are purposefully high level to provide some validation that stochastic swap is not creating invalid output by inserting incorrect swaps. It's not meant as a test of valid unitary equivalent output of the full transpilation. Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
|
Thank you for opening a new pull request. Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient. While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone. One or more of the the following people are requested to review this:
|
mtreinish
left a comment
There was a problem hiding this comment.
Overall this LGTM! It's a lot easier to follow the layout and transforms being performed now. Also the updated tests give us a bit more confidence the output is valid. The only thing I think is missing from our test path is that we're generating an equivalent output, but we can save this for the randomized testing I think (by adding control flow to the tests there), since I tried to do that in the new test class here and it was prohibitively expensive for the unit tests.
| best_depth (int): depth returned from _layer_permutation | ||
| best_circuit (DAGCircuit): swap circuit returned from _layer_permutation | ||
| """ | ||
| layout = best_layout |
|
(not tagging as automerge until this is rebased on #8879 which is needed for the tests to pass) |
|
Thanks for tracking this bug and simplifying the code further! |
Pull Request Test Coverage Report for Build 3237543802
💛 - Coveralls |
* Fix control-flow routing in StochasticSwap The `StochasticSwap` pass has some fairly complex hand-offs between different parts of its API, including in recursive calls and the regular hand-off between Python and Rust. In the course of adding the control-flow support, some of these became muddled, and the mapping between different virtual/physical/integer representations got mixed up, resulting in invalid swaps being output in the final circuit. This commit simplifies much of the internal mapping, removing many superfluous `DAGCircuit` creations and compositions. This also removes instances where two layouts were "chained"; this was not well typed (the output of a "virtual -> physical" mapping can't be the input for another "virtual -> physical" mapping), and in general was being used to "undo" some of compositions that were about to be applied. This fixes a tacit assumption in the original code that the initial layout was a trivial layout in the hand-off between Rust and Python. This worked until the recursive call added the `initial_layout` option, making this assumption invalid. Previously, virtual qubit bit instances were converted to integers (to allow them to be passed to Rust) using their indices into the original DAG, but the integer outputs were then converted _back_ using the `initial_layout`. In the old form, this worked anyway, but wasn't logically correct and consequently broke when the assumptions about `initial_layout` changed. For the recursive calls, we now ensure that the inner passes are essentially created with the same internal structure as the outer pass; the passed in `DAGCircuit` uses the same bit instances and same meaning of the virtual qubits as the outer circuit, and the `initial_layout` ensures that the inner passes start with at the same layout as the outer pass. This makes the inner passes more like a logical continuation of the current operation, rather than a completely separate entity that needs to have its virtual qubits remapped. The changes to the tests are twofold: - move the `CheckMap` calls earlier and apply them directly to the `StochasticSwap` output rather than the expected circuit, to improve the quality of failure error messages - use the same physical qubits inside the expected control-flow blocks; the new simpler form of doing the circuit rewriting internally in the pass ensures that the same bit objects are used all the way through the control-flow stack now, rather than creating new instances. * Add tests for stochastic swap valid output This commit adds full path transpile() tests for running with stochastic swap that validates a full path transpilation outputs a valid physical circuit. These tests are purposefully high level to provide some validation that stochastic swap is not creating invalid output by inserting incorrect swaps. It's not meant as a test of valid unitary equivalent output of the full transpilation. Co-authored-by: Jake Lishman <jake.lishman@ibm.com> Co-authored-by: Matthew Treinish <mtreinish@kortar.org> (cherry picked from commit b3cf64f)
* Fix control-flow routing in StochasticSwap The `StochasticSwap` pass has some fairly complex hand-offs between different parts of its API, including in recursive calls and the regular hand-off between Python and Rust. In the course of adding the control-flow support, some of these became muddled, and the mapping between different virtual/physical/integer representations got mixed up, resulting in invalid swaps being output in the final circuit. This commit simplifies much of the internal mapping, removing many superfluous `DAGCircuit` creations and compositions. This also removes instances where two layouts were "chained"; this was not well typed (the output of a "virtual -> physical" mapping can't be the input for another "virtual -> physical" mapping), and in general was being used to "undo" some of compositions that were about to be applied. This fixes a tacit assumption in the original code that the initial layout was a trivial layout in the hand-off between Rust and Python. This worked until the recursive call added the `initial_layout` option, making this assumption invalid. Previously, virtual qubit bit instances were converted to integers (to allow them to be passed to Rust) using their indices into the original DAG, but the integer outputs were then converted _back_ using the `initial_layout`. In the old form, this worked anyway, but wasn't logically correct and consequently broke when the assumptions about `initial_layout` changed. For the recursive calls, we now ensure that the inner passes are essentially created with the same internal structure as the outer pass; the passed in `DAGCircuit` uses the same bit instances and same meaning of the virtual qubits as the outer circuit, and the `initial_layout` ensures that the inner passes start with at the same layout as the outer pass. This makes the inner passes more like a logical continuation of the current operation, rather than a completely separate entity that needs to have its virtual qubits remapped. The changes to the tests are twofold: - move the `CheckMap` calls earlier and apply them directly to the `StochasticSwap` output rather than the expected circuit, to improve the quality of failure error messages - use the same physical qubits inside the expected control-flow blocks; the new simpler form of doing the circuit rewriting internally in the pass ensures that the same bit objects are used all the way through the control-flow stack now, rather than creating new instances. * Add tests for stochastic swap valid output This commit adds full path transpile() tests for running with stochastic swap that validates a full path transpilation outputs a valid physical circuit. These tests are purposefully high level to provide some validation that stochastic swap is not creating invalid output by inserting incorrect swaps. It's not meant as a test of valid unitary equivalent output of the full transpilation. Co-authored-by: Jake Lishman <jake.lishman@ibm.com> Co-authored-by: Matthew Treinish <mtreinish@kortar.org> (cherry picked from commit b3cf64f) Co-authored-by: Jake Lishman <jake.lishman@ibm.com> Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Summary
The
StochasticSwappass has some fairly complex hand-offs betweendifferent parts of its API, including in recursive calls and the regular
hand-off between Python and Rust. In the course of adding the
control-flow support, some of these became muddled, and the mapping
between different virtual/physical/integer representations got mixed up,
resulting in invalid swaps being output in the final circuit.
This commit simplifies much of the internal mapping, removing many
superfluous
DAGCircuitcreations and compositions. This also removesinstances where two layouts were "chained"; this was not well typed (the
output of a "virtual -> physical" mapping can't be the input for another
"virtual -> physical" mapping), and in general was being used to "undo"
some of compositions that were about to be applied.
This fixes a tacit assumption in the original code that the initial
layout was a trivial layout in the hand-off between Rust and Python.
This worked until the recursive call added the
initial_layoutoption,making this assumption invalid. Previously, virtual qubit bit instances
were converted to integers (to allow them to be passed to Rust) using
their indices into the original DAG, but the integer outputs were then
converted back using the
initial_layout. In the old form, thisworked anyway, but wasn't logically correct and consequently broke when
the assumptions about
initial_layoutchanged.For the recursive calls, we now ensure that the inner passes are
essentially created with the same internal structure as the outer pass;
the passed in
DAGCircuituses the same bit instances and same meaningof the virtual qubits as the outer circuit, and the
initial_layoutensures that the inner passes start with at the same layout as the outer
pass. This makes the inner passes more like a logical continuation of
the current operation, rather than a completely separate entity that
needs to have its virtual qubits remapped.
The changes to the tests are twofold:
move the
CheckMapcalls earlier and apply them directly to theStochasticSwapoutput rather than the expected circuit, to improvethe quality of failure error messages
use the same physical qubits inside the expected control-flow blocks;
the new simpler form of doing the circuit rewriting internally in the
pass ensures that the same bit objects are used all the way through
the control-flow stack now, rather than creating new instances.
Details and comments
Also has a patch from Matthew to add a bunch more random-circuit tests. Tests need #8879 to merge before they'll pass due to a bug in
Unroll3qOrMore.