Skip to content

Fix control-flow routing in StochasticSwap#8880

Merged
mergify[bot] merged 3 commits into
Qiskit:mainfrom
jakelishman:fix-stochasticswap-control-flow
Oct 12, 2022
Merged

Fix control-flow routing in StochasticSwap#8880
mergify[bot] merged 3 commits into
Qiskit:mainfrom
jakelishman:fix-stochasticswap-control-flow

Conversation

@jakelishman
Copy link
Copy Markdown
Member

Summary

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.

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.

jakelishman and others added 2 commits October 12, 2022 15:21
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>
@jakelishman jakelishman added priority: high Changelog: None Do not include in the GitHub Release changelog. labels Oct 12, 2022
@jakelishman jakelishman added this to the 0.22 milestone Oct 12, 2022
@jakelishman jakelishman requested a review from a team as a code owner October 12, 2022 14:53
@qiskit-bot
Copy link
Copy Markdown
Collaborator

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:

  • @Qiskit/terra-core

@mtreinish mtreinish added the stable backport potential Make Mergify open a backport PR to the most recent stable branch on merge. label Oct 12, 2022
Copy link
Copy Markdown
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, this was a useful

@mtreinish
Copy link
Copy Markdown
Member

(not tagging as automerge until this is rebased on #8879 which is needed for the tests to pass)

@ewinston
Copy link
Copy Markdown
Contributor

Thanks for tracking this bug and simplifying the code further!

@coveralls
Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 3237543802

  • 32 of 34 (94.12%) changed or added relevant lines in 2 files are covered.
  • 3 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.008%) to 84.755%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/transpiler/passes/routing/stochastic_swap.py 28 30 93.33%
Files with Coverage Reduction New Missed Lines %
qiskit/transpiler/passes/routing/utils.py 1 84.62%
qiskit/extensions/quantum_initializer/squ.py 2 79.78%
Totals Coverage Status
Change from base Build 3237541004: -0.008%
Covered Lines: 61884
Relevant Lines: 73015

💛 - Coveralls

@mergify mergify Bot merged commit b3cf64f into Qiskit:main Oct 12, 2022
mergify Bot pushed a commit that referenced this pull request Oct 12, 2022
* 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)
mergify Bot added a commit that referenced this pull request Oct 13, 2022
* 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>
@jakelishman jakelishman deleted the fix-stochasticswap-control-flow branch October 13, 2022 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: None Do not include in the GitHub Release changelog. stable backport potential Make Mergify open a backport PR to the most recent stable branch on merge.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants