Qiskit / rustworkx

A high performance Python graph library implemented in Rust.
https://www.rustworkx.org
Apache License 2.0
1.03k stars 145 forks source link

Allow passing numpy array to `PyDiGraph::extend_from_edge_list` #1125

Open biswash47 opened 6 months ago

biswash47 commented 6 months ago

What is the expected enhancement?

Per current api, one way to extend an existing graph is to use the PyDiGraph::extend_from_edge_list. This method expects a list of tuples. The enhancement would be extend this method so that it accepts a numpy array. This could possibly improve performance if the underlying memory array is directly accessed.

This issue came up in a discussion about the performance of qiskit.transpiler.CouplingMap object initialization: https://github.com/Qiskit/qiskit/issues/11919

IvanIsCoding commented 6 months ago

I think this should be an OK addition, but it will need to be a separate method.

We had issues with conversions from ndarray -> Rust types before and that is why we have from_complex_adjacency_matrix and from_adjacency_matrix. So let's try to avoid those and just create a new method. Upstream users will need to adapt.

jakelishman commented 6 months ago

Ivan: an alternative is to accept a Py<PyAny> in the slot and implement the conversion logic using the PyO3 traits manually (with a little bit of type checking). That's more complex from the implementation side, but potentially more convenient for users?

IvanIsCoding commented 6 months ago

Ivan: an alternative is to accept a Py<PyAny> in the slot and implement the conversion logic using the PyO3 traits manually (with a little bit of type checking). That's more complex from the implementation side, but potentially more convenient for users?

I think it is possible but I would like to keep the method straightforward. Nominally, I don't want to deal with two things:

So I'd rather implement a separate function and document well the specific ndarray format and type we want