Skip to content

Leverage retworkx generators for all cmap constructors#5469

Merged
mergify[bot] merged 3 commits into
Qiskit:masterfrom
mtreinish:leverage-retworkx-for-cmap-constructors
Dec 4, 2020
Merged

Leverage retworkx generators for all cmap constructors#5469
mergify[bot] merged 3 commits into
Qiskit:masterfrom
mtreinish:leverage-retworkx-for-cmap-constructors

Conversation

@mtreinish
Copy link
Copy Markdown
Member

Summary

In #5183 we switched the internal graph datastructures used for the
CouplingMap to be based on retworkx instead of networkx. That PR was
written against the 0.6.0 retworkx release and for the from_grid()
and from_full() constructors python had to be used since retworkx
was missing functions to generate graphs of those types. However, in the
recent 0.7.x retworkx release 2 new generators directed_grid_graph() [1]
and directed_mesh_graph() [2] were added. This commit switches the
implementation of from_grid() and from_full() to use these generators,
which significantly speeds up (and reduces the memory overhead) of the
constructor methods.

The only constructor which still relies on a Python loop is the
from_full() method when bidirection=False. This is because retworkx
doesn't offer a constructor that will build a graph like that. If in
a future release a generator is added we can remove that for loop too.

Details and comments

[1] https://retworkx.readthedocs.io/en/stable/stubs/retworkx.generators.directed_grid_graph.html
[2] https://retworkx.readthedocs.io/en/stable/stubs/retworkx.generators.directed_mesh_graph.html

In Qiskit#5183 we switched the internal graph datastructures used for the
CouplingMap to be based on retworkx instead of networkx. That PR was
written against the 0.6.0 retworkx release and for the from_grid()
and from_full() constructors python had to be used since retworkx
was missing functions to generate graphs of those types. However, in the
recent 0.7.x retworkx release 2 new generators directed_grid_graph() [1]
and directed_mesh_graph() [2] were added. This commit switches the
implementation of from_grid() and from_full() to use these generators,
which significantly speeds up (and reduces the memory overhead) of the
constructor methods.

The only constructor which still relies on a Python loop is the
from_full() method when bidirection=False. This is because retworkx
doesn't offer a constructor that will build a graph like that. If in
a future release a generator is added we can remove that for loop too.

[1] https://retworkx.readthedocs.io/en/stable/stubs/retworkx.generators.directed_grid_graph.html
[2] https://retworkx.readthedocs.io/en/stable/stubs/retworkx.generators.directed_mesh_graph.html
@mtreinish mtreinish requested a review from a team as a code owner December 4, 2020 15:10
@mtreinish
Copy link
Copy Markdown
Member Author

I ran some quick benchmarks on this:

grid
full

(both graphs are iog-log)

It's also worth noting that I ran these on my laptop with only 16GB of ram which wasn't enough for the Python full case, and at around 10k nodes it consumed all the available ram and started swapping pretty heavily which is probably why run time spiked there. This wasn't an issue on the retworkx constructor.

for i in np.logspace(1, 6, 20, dtype=int):
    dim = int(math.sqrt(i))
    start = time.time()
    temp = CouplingMap.from_grid(dim, dim)
    stop = time.time()
    del temp

for i in np.logspace(1, 4.2, 20, dtype=int):
    start = time.time()
    temp = CouplingMap.from_full(i)
    stop = time.time()
    del temp

is what I used to generate the data

@mergify mergify Bot merged commit fe45863 into Qiskit:master Dec 4, 2020
@mtreinish mtreinish deleted the leverage-retworkx-for-cmap-constructors branch December 7, 2020 14:15
@kdk kdk added the Changelog: None Do not include in the GitHub Release changelog. label Mar 31, 2021
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 the GitHub Release changelog. performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants