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 PyDiGraph method to make edges symmetric #814

Merged
merged 14 commits into from
May 15, 2023

Conversation

mtreinish
Copy link
Member

@mtreinish mtreinish commented Feb 10, 2023

This commit adds a new method make_symmetric() to PyDiGraph which will modify the graph and add a reverse edge to each edge in the graph if it is not already present.

TODO:

  • Add tests
  • Add release notes

This commit adds a new method make_symmetric() to PyDiGraph which will
modify the graph and add a reverse edge to each edge in the graph if it
is not already present.
@mtreinish mtreinish added this to the 0.13.0 milestone Feb 10, 2023
mtreinish added a commit to mtreinish/qiskit-core that referenced this pull request Feb 10, 2023
Right now the CouplingMap.make_symmetric has a quadratic overhead. It
was iterating over a list of each edgess' endpoints in the graph
and for each edge it was iterating over the same list again to check
whether the reverse edge is present or not. The overhead for this for
large coupling maps can be quite large, for example with a 10497 qubit
heavy hex coupling map the time it took to run this method as part of
SabreLayout's initialization was 10x slower than actually running the
pass (a equally sized Bernstein-Vazirani circuit). Instead of doing an
O(n) contains check inside the loop this commit updates it to use a
set which will be an O(1) lookup. This should address the performance
issue with this method. In the future we should leverage the native
rustworkx method to do this, which is being added in:
Qiskit/rustworkx#814
@coveralls
Copy link

coveralls commented Feb 10, 2023

Pull Request Test Coverage Report for Build 4981712940

  • 20 of 20 (100.0%) changed or added relevant lines in 1 file are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.002%) to 96.935%

Files with Coverage Reduction New Missed Lines %
rustworkx-core/src/connectivity/find_cycle.rs 3 94.34%
Totals Coverage Status
Change from base Build 4974720931: 0.002%
Covered Lines: 14547
Relevant Lines: 15007

💛 - Coveralls

mergify bot pushed a commit to Qiskit/qiskit that referenced this pull request Feb 10, 2023
* Improve efficiency of CouplingMap.make_symmetric

Right now the CouplingMap.make_symmetric has a quadratic overhead. It
was iterating over a list of each edgess' endpoints in the graph
and for each edge it was iterating over the same list again to check
whether the reverse edge is present or not. The overhead for this for
large coupling maps can be quite large, for example with a 10497 qubit
heavy hex coupling map the time it took to run this method as part of
SabreLayout's initialization was 10x slower than actually running the
pass (a equally sized Bernstein-Vazirani circuit). Instead of doing an
O(n) contains check inside the loop this commit updates it to use a
set which will be an O(1) lookup. This should address the performance
issue with this method. In the future we should leverage the native
rustworkx method to do this, which is being added in:
Qiskit/rustworkx#814

* Further reduce the overhead by avoiding CouplingMap.add_edge()

The CouplingMap add_edge() method has similar overhead to check whether
nodes need to be added. However, in the case of make_symmetric we don't
need this checking because both endpoints are already in the graph. This
commit further improves the performance of the method by just adding the
reverse edge directly to the graph via the rustworkx api instead.
src/digraph.rs Outdated Show resolved Hide resolved
nbronn pushed a commit to nbronn/qiskit-terra that referenced this pull request Feb 13, 2023
* Improve efficiency of CouplingMap.make_symmetric

Right now the CouplingMap.make_symmetric has a quadratic overhead. It
was iterating over a list of each edgess' endpoints in the graph
and for each edge it was iterating over the same list again to check
whether the reverse edge is present or not. The overhead for this for
large coupling maps can be quite large, for example with a 10497 qubit
heavy hex coupling map the time it took to run this method as part of
SabreLayout's initialization was 10x slower than actually running the
pass (a equally sized Bernstein-Vazirani circuit). Instead of doing an
O(n) contains check inside the loop this commit updates it to use a
set which will be an O(1) lookup. This should address the performance
issue with this method. In the future we should leverage the native
rustworkx method to do this, which is being added in:
Qiskit/rustworkx#814

* Further reduce the overhead by avoiding CouplingMap.add_edge()

The CouplingMap add_edge() method has similar overhead to check whether
nodes need to be added. However, in the case of make_symmetric we don't
need this checking because both endpoints are already in the graph. This
commit further improves the performance of the method by just adding the
reverse edge directly to the graph via the rustworkx api instead.
mergify bot pushed a commit to Qiskit/qiskit that referenced this pull request Feb 17, 2023
* Improve efficiency of CouplingMap.make_symmetric

Right now the CouplingMap.make_symmetric has a quadratic overhead. It
was iterating over a list of each edgess' endpoints in the graph
and for each edge it was iterating over the same list again to check
whether the reverse edge is present or not. The overhead for this for
large coupling maps can be quite large, for example with a 10497 qubit
heavy hex coupling map the time it took to run this method as part of
SabreLayout's initialization was 10x slower than actually running the
pass (a equally sized Bernstein-Vazirani circuit). Instead of doing an
O(n) contains check inside the loop this commit updates it to use a
set which will be an O(1) lookup. This should address the performance
issue with this method. In the future we should leverage the native
rustworkx method to do this, which is being added in:
Qiskit/rustworkx#814

* Further reduce the overhead by avoiding CouplingMap.add_edge()

The CouplingMap add_edge() method has similar overhead to check whether
nodes need to be added. However, in the case of make_symmetric we don't
need this checking because both endpoints are already in the graph. This
commit further improves the performance of the method by just adding the
reverse edge directly to the graph via the rustworkx api instead.

(cherry picked from commit 5afbb0b)
mergify bot added a commit to Qiskit/qiskit that referenced this pull request Feb 17, 2023
* Improve efficiency of CouplingMap.make_symmetric

Right now the CouplingMap.make_symmetric has a quadratic overhead. It
was iterating over a list of each edgess' endpoints in the graph
and for each edge it was iterating over the same list again to check
whether the reverse edge is present or not. The overhead for this for
large coupling maps can be quite large, for example with a 10497 qubit
heavy hex coupling map the time it took to run this method as part of
SabreLayout's initialization was 10x slower than actually running the
pass (a equally sized Bernstein-Vazirani circuit). Instead of doing an
O(n) contains check inside the loop this commit updates it to use a
set which will be an O(1) lookup. This should address the performance
issue with this method. In the future we should leverage the native
rustworkx method to do this, which is being added in:
Qiskit/rustworkx#814

* Further reduce the overhead by avoiding CouplingMap.add_edge()

The CouplingMap add_edge() method has similar overhead to check whether
nodes need to be added. However, in the case of make_symmetric we don't
need this checking because both endpoints are already in the graph. This
commit further improves the performance of the method by just adding the
reverse edge directly to the graph via the rustworkx api instead.

(cherry picked from commit 5afbb0b)

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
@mtreinish mtreinish removed the on hold label May 10, 2023
@mtreinish mtreinish changed the title [WIP] Add PyDiGraph method to make edges symmetric Add PyDiGraph method to make edges symmetric May 10, 2023
@mtreinish mtreinish requested a review from jlapeyre May 10, 2023 19:27
src/digraph.rs Outdated Show resolved Hide resolved
@jlapeyre
Copy link
Collaborator

Other than the comment above, this looks really thorough. LGTM.

mtreinish and others added 3 commits May 12, 2023 09:20
Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>
@jlapeyre jlapeyre self-assigned this May 12, 2023
Copy link
Collaborator

@jlapeyre jlapeyre left a comment

Choose a reason for hiding this comment

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

Good to go.

@enavarro51
Copy link
Contributor

Agreed. LGTM.

@enavarro51 enavarro51 enabled auto-merge (squash) May 15, 2023 14:38
@enavarro51 enavarro51 merged commit ae95d1d into Qiskit:main May 15, 2023
@mtreinish mtreinish deleted the make-symmetric branch May 15, 2023 15:34
IvanIsCoding pushed a commit to IvanIsCoding/rustworkx that referenced this pull request May 26, 2023
* Add PyDiGraph method to make edges symmetric

This commit adds a new method make_symmetric() to PyDiGraph which will
modify the graph and add a reverse edge to each edge in the graph if it
is not already present.

* Simplyify logic

* Add initial tests

* Update docstring wording

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>

* Add release notes

* Expand testing

* Fix tests and cycle checking

* Remove stray debug prints

* Update src/digraph.rs

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>

* Fix lint

---------

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>
Co-authored-by: Edwin Navarro <enavarro@comcast.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants