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

Add reverse argument to topological sorters #1108

Merged
merged 3 commits into from
Mar 1, 2024

Conversation

jakelishman
Copy link
Member

This allows lexicographical_topological_sort and TopologicalSorter to produce a topological sort that would have been found if the edges in the graph had reversed directions, without constructing the edge-flipped graph.

This is useful for graphs where we might want to do topological analysis from both ends of the data flow, for example in Qiskit where we occasionally want to find the "final" operations topologically.

Close #1105

This allows `lexicographical_topological_sort` and `TopologicalSorter`
to produce a topological sort that would have been found if the edges in
the graph had reversed directions, without constructing the edge-flipped
graph.

This is useful for graphs where we might want to do topological analysis
from both ends of the data flow, for example in Qiskit where we
occasionally want to find the "final" operations topologically.
@coveralls
Copy link

coveralls commented Feb 21, 2024

Pull Request Test Coverage Report for Build 8116939754

Details

  • 18 of 18 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.03%) to 96.517%

Totals Coverage Status
Change from base Build 8105396148: 0.03%
Covered Lines: 16820
Relevant Lines: 17427

💛 - Coveralls

@mtreinish mtreinish added this to the 0.15.0 milestone Feb 21, 2024
Copy link
Collaborator

@IvanIsCoding IvanIsCoding left a comment

Choose a reason for hiding this comment

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

LGTM, the implementation is very clever and I like the small dif.

My only comment is for encapsulating the logic of the directions to be overprotective. Especially given we revisit topological sorting once in a while

Comment on lines 377 to 381
let (in_dir, out_dir) = if reverse {
(petgraph::Direction::Outgoing, petgraph::Direction::Incoming)
} else {
(petgraph::Direction::Incoming, petgraph::Direction::Outgoing)
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you create a public function in dag_algo to encapsulate this logic and reuse it in the other files? It is a nitpick but just to avoid the kind of bugs where we flip it

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in 12a91ec.

@jakelishman
Copy link
Member Author

Sorry for disappearing for a while there. I've added the helper in 12a91ec.

@IvanIsCoding IvanIsCoding added the automerge Queue a approved PR for merging label Mar 1, 2024
@mergify mergify bot merged commit efb296e into Qiskit:main Mar 1, 2024
30 checks passed
@jakelishman jakelishman deleted the reverse-topo branch March 1, 2024 21:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Queue a approved PR for merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add reverse topological ordering
4 participants