Skip to content
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

Make SabreSwap account for clbits in predecessors #7952

Merged
merged 7 commits into from
Apr 19, 2022

Conversation

jakelishman
Copy link
Member

@jakelishman jakelishman commented Apr 19, 2022

Summary

Previously, SabreSwap only checked that it was valid to add a gate back
to the DAG by ensuring that all its qubit predecessors had already been
added to the DAG. It did not check for clbit successors. This meant
that measurements and (potentially, unverified) classically conditioned
gates could be re-ordered out from the correct topological ordering.

The most notable effect of this was that measurements writing to the
same bit could be re-ordered, changing the output of the circuit.

See gh-7950 for more detail.

Details and comments

Fix #7950. I also believe this to be the cause of the sporadic randomised test failures (see an example in the linked issue).

Ali (@ajavadia) - this might be something you ought to look at, since I don't understand SabreSwap well.

Previously, SabreSwap only checked that it was valid to add a gate back
to the DAG by ensuring that all its qubit predecessors had already been
added to the DAG.  It did not check for clbit successors.  This meant
that measurements and (potentially, unverified) classically conditioned
gates could be re-ordered out from the correct topological ordering.

The most notable effect of this was that measurements writing to the
same bit could be re-ordered, changing the output of the circuit.

See Qiskitgh-7950 for more detail.
@jakelishman jakelishman requested a review from a team as a code owner April 19, 2022 00:19
@qiskit-bot
Copy link
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

@jakelishman jakelishman added Changelog: Bugfix Include in the "Fixed" section of the changelog randomized test labels Apr 19, 2022
@coveralls
Copy link

coveralls commented Apr 19, 2022

Pull Request Test Coverage Report for Build 2191575209

  • 3 of 3 (100.0%) changed or added relevant lines in 1 file are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.005%) to 83.964%

Files with Coverage Reduction New Missed Lines %
qiskit/pulse/library/waveform.py 3 89.36%
Totals Coverage Status
Change from base Build 2191310058: -0.005%
Covered Lines: 54235
Relevant Lines: 64593

💛 - Coveralls

@kevinhartman
Copy link
Contributor

I approve of this bugfix, though I was sitting next to Jake while he crafted it 😁 (and the same bug actually exists in TOQM too!).

Copy link
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.

This LGTM it makes sense to me that we should be respecting all edges in the dag when looking for successors because there is a ordering dependency there and only looking at the quantum successors is giving us an incomplete picture. Just one inline comment on one small improvement

qiskit/transpiler/passes/routing/sabre_swap.py Outdated Show resolved Hide resolved
@mtreinish mtreinish added the stable backport potential The bug might be minimal and/or import enough to be port to stable label Apr 19, 2022
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
ajavadia
ajavadia previously approved these changes Apr 19, 2022
Copy link
Member

@ajavadia ajavadia left a comment

Choose a reason for hiding this comment

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

looks good. I wonder if this fixes #7707

@mtreinish
Copy link
Member

looks good. I wonder if this fixes #7707

I ran the reproduce script in the issue with this applied locally and it returned and didn't get stuck in an infinite loop.

@mtreinish mtreinish modified the milestone: 0.20.1 Apr 19, 2022
@jakelishman
Copy link
Member Author

The issue with commit b5b79d2 was that DAGCircuit.successors and DAGCircuit.edges have different behaviours when dealing with parallel edges. The version with edges (which does only do the output edges, so SabreSwap._successors is correctly named) will repeat the successor for each parallel edges, whereas successors only shows each one once. Using successors meant that things like measurements or barriers (which may well have the same qubit and clbit going in) were handled differently in the short form I'd written.

The PR now uses edges again, so it all works, but this doesn't fix #7707; Matt had seen improvements with the bust version of this PR because it caused several gates to get dropped, preventing the pass from reaching the loop state.

@mergify mergify bot merged commit 38f3705 into Qiskit:main Apr 19, 2022
mergify bot pushed a commit that referenced this pull request Apr 19, 2022
* Make SabreSwap account for clbits in predecessors

Previously, SabreSwap only checked that it was valid to add a gate back
to the DAG by ensuring that all its qubit predecessors had already been
added to the DAG.  It did not check for clbit successors.  This meant
that measurements and (potentially, unverified) classically conditioned
gates could be re-ordered out from the correct topological ordering.

The most notable effect of this was that measurements writing to the
same bit could be re-ordered, changing the output of the circuit.

See gh-7950 for more detail.

* Add reno

* Fix lint

* Trim fat in SabreSwap._successors

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* Fix _successors method

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
(cherry picked from commit 38f3705)
@jakelishman jakelishman deleted the fix/sabreswap-clbits branch April 19, 2022 20:39
mergify bot added a commit that referenced this pull request Apr 19, 2022
* Make SabreSwap account for clbits in predecessors

Previously, SabreSwap only checked that it was valid to add a gate back
to the DAG by ensuring that all its qubit predecessors had already been
added to the DAG.  It did not check for clbit successors.  This meant
that measurements and (potentially, unverified) classically conditioned
gates could be re-ordered out from the correct topological ordering.

The most notable effect of this was that measurements writing to the
same bit could be re-ordered, changing the output of the circuit.

See gh-7950 for more detail.

* Add reno

* Fix lint

* Trim fat in SabreSwap._successors

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* Fix _successors method

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
(cherry picked from commit 38f3705)

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog randomized test stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SabreSwap can reorder operations that depend on the same classical bit
6 participants