-
Notifications
You must be signed in to change notification settings - Fork 193
Add EdgeList and WeightedEdgeList return types #204
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
Conversation
This commit adds 2 new return types EdgeList and WeightedEdgeList that are used as the return type for functions that return Vec<(usize, usize)> and Vec<(usize, usize, PyObject)> respectively. This custom type implements the Python sequence protocol and can be used as the list which was previously returned except for where explicit type checking was used. Just as in Qiskit#185 and Qiskit#198 the advantage here is that for large edge lists the conversion from Rust to a Python type becomes a large bottleneck. This avoids that conversion and only does it per element on access.
Pull Request Test Coverage Report for Build 396070856
💛 - Coveralls |
In Qiskit/rustworkx#204 the return type of the edge_list() method will be returned as a custom sequence type the defer the type conversion from rust to python. So casting to a list no longer will be a no-op after that point so this commit removes the cast. At the same time in the sabre swap pass one of the bottlenecks at large qubit counts is traversing that edge list looking for edges, this updates that to use the has_edge() method which should be faster than a full list traversal every iteration.
@@ -62,7 +62,7 @@ pub fn digraph_union( | |||
node_map.insert(node.index(), node_index); | |||
} | |||
|
|||
for edge in b.weighted_edge_list(py) { | |||
for edge in b.weighted_edge_list(py).edges { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this the only place where .edges
is needed? Is this the only place where weighted_edge_list()
was used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this is the only place weighted_edge_list()
is called from inside rust. Normally it's only used from python but this function uses it instead of calling b.graph..edge_references()
. We probably should update this to just use the petgraph api directly, but it's not a big deal and an easy followup thing we can change later.
Co-authored-by: Lauren Capelluto <laurencapelluto@gmail.com>
LGTM. Just out of curiosity, I found |
Oh I missed that, I just searched for the exact matches on the return and did a find replace on them. The difference is really just between where reference counting increments happen. I'll update that when I fix the remaining docstring issues @lcapelluto pointed out. That function retuning |
I've updated |
} | ||
|
||
for edge in edges { | ||
for edge in self.graph.edges_directed(NodeIndex::new(u), *dir) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
edges_to_add.push(( | ||
v, | ||
d.index(), | ||
edge.weight().clone_ref(py), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why clone_ref
? Also, why does index()
have to be added here but not before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, is it just because in the previous commits this hadn't been added yet, and so the type was just changed? I'd have thought some tests would fail in that case, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was working before because it was using clone()
instead of clone_ref()
Before this PR in_edges()
and out_edges()
were returning a Vec(usize, usize, &PyObject)
and edges_to_add
was a Vec(usize, usize, &PyObject)
(it's just being used as a temp storage Vec before its used later to add edges). So prior to the changing in_edges()
and out_edges()
this worked by the 2 usizes implement copy so can efficiently be used in place without moving, but to store &PyObject
as a PyObject
(which is neededt for creating a new edge in the graph) it needs to take ownership of the object being borrowed. We can't do *edge.weight()
since it would move the weight out of the graph (and would fail) so we need to clone/copy it which for PyObject isn't a big deal because it just increments the ref counter.
The change I made here (besides switching to petgraph method edges_directed
instead of using in_edges and out_edges) is to switch just switch from clone()
to clone_ref(py)
. There isn't a functional difference between them as they both increment the python reference counter for the object, except that you give a gil handle to clone_ref if you have it: https://github.com/PyO3/pyo3/blob/v0.12.4/src/instance.rs#L196-L200 while clone()
will queue it for when there is one it doesn't already have it: https://github.com/PyO3/pyo3/blob/v0.12.4/src/instance.rs#L517-L524. In the context of this method I don't think there is any performance difference since it has a gil reference, I just prefer it to be explicit and use clone_ref when we already have py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I missed the second half of the question. The index()
calls are needed because the edges_to_add
vec is defined as Vec<(usize, usize, PyObject)>
and self.graph.edges_directed()
returns an iterator yielding petgraph EdgeReference
objects. Those objects return NodeIndex
objects for source()
and target()
instead of usize ints. So to get the usize from the NodeIndex
I had to call index() on it which returns a usize (since NodeIndex is just a wrapper around a usize anyay). This wasn't necessary before because in_edges() and out_edges returned a Vec<usize, usize, &PyObject)>
and had already done the conversion from NodeIndex -> usize for us.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it!:)
* WIP: Use retworkx for CouplingMap This commit migrates Terra's CouplingMap class to us retworkx internally instead of networkx providing >10x speed improvement for CouplingMap operations. Requires: Qiskit/rustworkx#157 Qiskit/rustworkx#156 Qiskit/rustworkx#144 Qiskit/rustworkx#143 Qiskit/rustworkx#147 Qiskit/rustworkx#158 Qiskit/rustworkx#162 Qiskit/rustworkx#161 all be applied to the retworkx version installed. * DNM: Install retworkx from source * DNM: Add retworkx custom branch to travis * Fix draw() copy paste error * Fix typo * DNK: Add source retworkx to docs build * Fix lint * Use graph_distance_matrix instead of floyd warshall * DNM also add retworkx from source for image test job * Use Gnm random function from retworkx in token swapper tests * Remove nx import from token swapper tests * Fix lint * Remove install from git from CI config since retworkx 0.6.0 is released * Fix api call * Use retworkx generators where possible for constructors * Remvoe source install from travis config * Bump version in setup.py too * Use extend_from_edge_list for from_full * Update qiskit/transpiler/coupling.py * Use edge_list() return and has_edge() in sabre In Qiskit/rustworkx#204 the return type of the edge_list() method will be returned as a custom sequence type the defer the type conversion from rust to python. So casting to a list no longer will be a no-op after that point so this commit removes the cast. At the same time in the sabre swap pass one of the bottlenecks at large qubit counts is traversing that edge list looking for edges, this updates that to use the has_edge() method which should be faster than a full list traversal every iteration. * Fix issue in layout_transformation pass In #5281 the layout transformation pass was updated to handle the case where the coupling map was not defined. In those cases for the purposes of the layout transformation it treats the coupling map as being fully connected. So it creates a new full coupling map to use for the token swapper. However, it neglects that the token swapper expects an undirected graph and was passing in a directed graph. This didn't matter too much for the networkx based coupling map object because networkx can handle directed or undirected in the same function. But, for retworkx directed graphs and undirected graphs are different types an can't be used interchangeably. This commit fixes this issue in that pass. * Update qiskit/transpiler/passes/routing/algorithms/token_swapper.py Co-authored-by: Julien Gacon <gaconju@gmail.com> * Fix Lint Co-authored-by: Julien Gacon <gaconju@gmail.com> Co-authored-by: Kevin Krsulich <kevin.krsulich@ibm.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit adds 2 new return types
EdgeList
andWeightedEdgeList
that are used as the return type for functions that return
Vec<(usize, usize)>
andVec<(usize, usize, PyObject)>
respectively.This custom type implements the Python sequence protocol and can be
used as the list which was previously returned except for where
explicit type checking was used. Just as in #185 and #198 the
advantage here is that for large edge lists the conversion from
Rust to a Python type becomes a large bottleneck. This avoids that
conversion and only does it per element on access.