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

Remove Sabre's manual insertion-order iteration and unnecessary sorts #9560

Merged
merged 6 commits into from
Feb 15, 2023

Conversation

jakelishman
Copy link
Member

Summary

The manual insertion-order tracking was initially added in 02a1939 (gh-9012) during a complete rewrite of the scoring algorithms for Sabre to demonstrate that the new algorithm could be made fully RNG compatible. It maintained the identical iteration order to the previous iteration once the front-layer data structure was swapped from being a raw list to hash-based.

The new data structure doesn't require the manual tracking to be reproducible as long as the iteration order is independent of the hash seed. This swaps the relevant places over the IndexMap to be able to remove a lot of the manual tracking. In casual testing, this didn't appear to have much effect on performance, but the resulting code is much simpler.

The sorts (and deliberate canonicalisation of the swaps) was necessary to match RNG compatibility in the pre-relative-score Sabre, but since the swaps are now iterated through in a deterministic order and guaranteed to be generated only once each (the previous version used a hashset to remove duplicates), neither step is necessary.

For a QV circuit of depth 5 at 1081 qubits on heavy hex, this is worth almost a 2x speedup in routing performance.

Details and comments

I've still got some fun ideas for improving the handling of the extended set by a more data-structure-explicit segregation into layers within it. I don't know at what point we should stop calling this "Sabre", because with all the changes we've made to the algorithm, the router only really bears a passing resemblance to the original paper.

The manual insertion-order tracking was initially added in 02a1939
(Qiskitgh-9012) during a complete rewrite of the scoring algorithms for Sabre
to demonstrate that the new algorithm could be made fully RNG
compatible.  It maintained the identical iteration order to the previous
iteration once the front-layer data structure was swapped from being a
raw list to hash-based.

The new data structure doesn't require the manual tracking to be
reproducible as long as the iteration order is independent of the hash
seed.  This swaps the relevant places over the `IndexMap` to be able to
remove a lot of the manual tracking.  In casual testing, this didn't
appear to have much effect on performance, but the resulting code is
much simpler.

The sorts (and deliberate canonicalisation of the swaps) was necessary
to match RNG compatibility in the pre-relative-score Sabre, but since
the swaps are now iterated through in a deterministic order and
guaranteed to be generated only once each (the previous version used a
hashset to remove duplicates), neither step is necessary.

For a QV circuit of depth 5 at 1081 qubits on heavy hex, this is worth
almost a 2x speedup in routing performance.
@jakelishman jakelishman added performance Changelog: New Feature Include in the "Added" section of the changelog Rust This PR or issue is related to Rust code in the repository mod: transpiler Issues and PRs related to Transpiler labels Feb 9, 2023
@jakelishman jakelishman added this to the 0.24.0 milestone Feb 9, 2023
@jakelishman jakelishman requested a review from a team as a code owner February 9, 2023 12:06
@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:

@jakelishman
Copy link
Member Author

Oops, I forgot to sort out the testing changes because of the new RNG patterns. Not sure what's going on with the gate-direction stuff, but I suspect it's related to a swap [x, y] no longer being strictly ordered to have x < y in the output.

@jakelishman
Copy link
Member Author

254fc51 fixes the RNG-related tests. The remaining ones could be fixed by changing the CouplingMaps in those tests to be bi-directional, but I think more properly the fix is #9561, which correctly registers swap as a symmetric gate in the GateDirection transpiler pass.

This PR now depends on #9561.

@coveralls
Copy link

coveralls commented Feb 9, 2023

Pull Request Test Coverage Report for Build 4178888775

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

Files with Coverage Reduction New Missed Lines %
src/sabre_swap/layer.rs 2 97.32%
Totals Coverage Status
Change from base Build 4167578437: -0.005%
Covered Lines: 67229
Relevant Lines: 78838

💛 - Coveralls

@alexanderivrii
Copy link
Contributor

I don't know at what point we should stop calling this "Sabre", because with all the changes we've made to the algorithm, the router only really bears a passing resemblance to the original paper.

Since you've made it so much more efficient, maybe it should be called Lightsabre?

@jakelishman
Copy link
Member Author

jakelishman commented Feb 10, 2023

Test non-determinism is fixed by #9568, but I'll need a new commit after that merges to update the chosen fixed layout in the currently flaky test to whatever that PR causes it to settle on.

It occurring only on the Mac machines is a red herring; it's only related to PYTHONHASHSEED.

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.

Overall this LGTM, the code is pretty straightforward (or this could just be because we discussed it at the time as part of the #9012 review) and the speed boost is a big win (also shaking loose bugs in other parts of the code was great). Just one small inline comment about expanding the release notes, but once that's there I think this should be good to go.

@mtreinish mtreinish added Changelog: API Change Include in the "Changed" section of the changelog automerge labels Feb 14, 2023
@mergify mergify bot merged commit e016f9c into Qiskit:main Feb 15, 2023
@jakelishman jakelishman deleted the sabre/deterministic-iteration branch February 15, 2023 00:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: API Change Include in the "Changed" section of the changelog Changelog: New Feature Include in the "Added" section of the changelog mod: transpiler Issues and PRs related to Transpiler performance Rust This PR or issue is related to Rust code in the repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants