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

Fix handling of conditions in SabreDAG creation #8727

Merged
merged 6 commits into from
Sep 16, 2022

Conversation

mtreinish
Copy link
Member

Summary

In #8388 we moved all of the SabreSwap pass's processing into rust. To facilitate that we had to create a rust DAG data structure to represent the data flow graph solely in Rust to enable analyzing the DAG structure in rust code without having to callback to python. To do this the DAGCircuit (which has a nearly identical representation in python) would export a list of nodes and the bits (both quantum and classical) that they operated on. This information is then used to create the SabreDAG used in the rust code. However, in this process an edge case was missed with classical condtions. If a condition was specified solely as a property of the operation and not in cargs list the SabreDAG would not know about the classical bit dependency between any subsequent operations involving that classical bit. This would cause incorrect output because the ndoes would not get processed as they were in the circuit. This commit fixes this issue by explicitly checking if there is a condition on the operation and there are no cargs and if so adding the carg bits to the SabreDAG directly. This fixes the incorrect representation in SabreDAG.

Details and comments

Fixes #8675

In Qiskit#8388 we moved all of the SabreSwap pass's processing into rust. To
facilitate that we had to create a rust DAG data structure to represent
the data flow graph solely in Rust to enable analyzing the DAG structure
in rust code without having to callback to python. To do this the
DAGCircuit (which has a nearly identical representation in python) would
export a list of nodes and the bits (both quantum and classical) that
they operated on. This information is then used to create the SabreDAG
used in the rust code. However, in this process an edge case was missed
with classical condtions. If a condition was specified solely as a
property of the operation and not in cargs list the SabreDAG would not
know about the classical bit dependency between any subsequent
operations involving that classical bit. This would cause incorrect
output because the ndoes would not get processed as they were in the
circuit. This commit fixes this issue by explicitly checking if there is
a condition on the operation and there are no cargs and if so adding the
carg bits to the SabreDAG directly. This fixes the incorrect
representation in SabreDAG.

Fixes Qiskit#8675
@mtreinish mtreinish added the Changelog: None Do not include in changelog label Sep 12, 2022
@mtreinish mtreinish added this to the 0.22 milestone Sep 12, 2022
@mtreinish mtreinish requested a review from a team as a code owner September 12, 2022 14:01
@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

@coveralls
Copy link

coveralls commented Sep 12, 2022

Pull Request Test Coverage Report for Build 3040977407

  • 4 of 4 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.0009%) to 84.305%

Totals Coverage Status
Change from base Build 3040943610: 0.0009%
Covered Lines: 57831
Relevant Lines: 68597

💛 - Coveralls

This commit fixes the logic for handling instructions that have both
cargs and conditions set. The previous commit fixed the behavior only if
cargs was mutually exclusive with having classical arguments. However,
the same bug this PR is fixing would persist for instructions with clbit
arguments and a condition. This fixes the behavior to correctly
represent the data dependency in SabreDAG for these cases.
This commit improves the efficiency of the condition handling on
SabreDAG creation that was added in the previous commit. Previously, it
was potentially expensive to loop over the list of cargs and condition
bits as for instructions with a large number of both the repeated
duplicate searches would get quite expensive. This commit fixes that by
converting the cargs data structure to be a set to prevent adding
duplicates. Since the order isn't significant for the cargs in sabre as
it's only used to represent data dependency between instructions we can
convert it to internally use a set in python and a HashSet in rust. This
also removes storing of cargs for each node in the SabreDAG as this was
never used and just wasted memory by storing the list and never using
it.
let mut dag: DiGraph<(usize, Vec<usize>, Vec<usize>), ()> =
let mut dag: DiGraph<(usize, Vec<usize>), ()> =
Copy link
Member

Choose a reason for hiding this comment

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

If we don't need to track clbit order within the SabreDAG (which I think you're right - we don't need to), I don't think we need to track qubit order either - it's not important for the Sabre algorithm. That said, it's not really worth bothering about, and if doing it becomes a memory/performance concern in the future we can revisit it.

@mergify mergify bot merged commit bdec5b2 into Qiskit:main Sep 16, 2022
@mtreinish mtreinish deleted the fix-classic-sabre branch September 16, 2022 01:05
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 changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SabreSwap can reorder or throw away operations near conditions
4 participants