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 to_numpy_array or to_numpy_matrix #342

Closed
t-imamichi opened this issue May 28, 2021 · 2 comments · Fixed by #343
Closed

Add to_numpy_array or to_numpy_matrix #342

t-imamichi opened this issue May 28, 2021 · 2 comments · Fixed by #343

Comments

@t-imamichi
Copy link
Member

What is the expected enhancement?

It would be great if retworkx supports to_numpy_array or to_numpy_matrix.
It is used in a tutorial of qiskit-optimization.
https://github.com/Qiskit/qiskit-optimization/blob/main/docs/tutorials/06_examples_max_cut_and_tsp.ipynb
It is not urgent because we can implement a utility function in Python.

Related to qiskit-community/qiskit-optimization#150

@georgios-ts
Copy link
Collaborator

@t-imamichi
Copy link
Member Author

Thank for your information. It would be great if it is mentioned in https://qiskit.org/documentation/retworkx/networkx.html

mtreinish added a commit to mtreinish/retworkx that referenced this issue May 28, 2021
This commit adds documentation to the retworkx for networkx users guide
on the differences between networkx and retworkx when it comes to matrix
generation. Right now retworkx only has an `adjacency_matrix()` function
which is equivalent to networkx's `to_numpy_array()` and returns a numpy
array of the adjacency matrix. This is different from networkx's
`adjacency_matrix()` function which returns a scipy sparse array (and is
just an alias for networkx's `to_scipy_sparse_matrix()`). Documenting
this gap is important because it's easy to get confused by these API
differences.

This difference is fundamentally because we can effeciently create numpy
arrays in retworkx by just pass a pointer to numpy with a 2d array
that was constructed natively in rust (using rust-numpy) while other
matrix formats (such as scipy sparse matrices) don't expose a C api so
to create those objects we'd have to go through Python. Eventually we
might expand the API to include pure python functions to do additional
conversions (although I worry about increasing the external dependency
list) but numpy arrays will still be the default because of the inherent
performance benefits.

Fixes Qiskit#342
mtreinish added a commit that referenced this issue May 28, 2021
…#343)

This commit adds documentation to the retworkx for networkx users guide
on the differences between networkx and retworkx when it comes to matrix
generation. Right now retworkx only has an `adjacency_matrix()` function
which is equivalent to networkx's `to_numpy_array()` and returns a numpy
array of the adjacency matrix. This is different from networkx's
`adjacency_matrix()` function which returns a scipy sparse array (and is
just an alias for networkx's `to_scipy_sparse_matrix()`). Documenting
this gap is important because it's easy to get confused by these API
differences.

This difference is fundamentally because we can effeciently create numpy
arrays in retworkx by just pass a pointer to numpy with a 2d array
that was constructed natively in rust (using rust-numpy) while other
matrix formats (such as scipy sparse matrices) don't expose a C api so
to create those objects we'd have to go through Python. Eventually we
might expand the API to include pure python functions to do additional
conversions (although I worry about increasing the external dependency
list) but numpy arrays will still be the default because of the inherent
performance benefits.

Fixes #342
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 a pull request may close this issue.

2 participants