Skip to content

Conversation

mtreinish
Copy link
Member

This commit adds 2 new methods to the PyDiGraph class,
insert_node_between() and insert_node_between_multiple. These methods
are for inserting edges between a node and another node that has edges.
You specify a node and which node to insert it between and it will loop
over either the incoming or outgoing edges and add edges between the
existing edges.

This commit adds 2 new methods to the PyDiGraph class,
insert_node_between() and insert_node_between_multiple. These methods
are for inserting edges between a node and another node that has edges.
You specify a node and which node to insert it between and it will loop
over either the incoming or outgoing edges and add edges between the
existing edges.
@mtreinish mtreinish requested a review from kdk October 20, 2020 22:01
@mtreinish
Copy link
Member Author

Marking as on hold because I need to add more unittests to improve the coverage, and also add more than the simple cases.

mtreinish added a commit to mtreinish/qiskit-core that referenced this pull request Oct 20, 2020
This commit migrates the inner loop for apply_operation_back and
apply_operation_front to leverage retworkx's
insert_node_between_multiple PyDiGraph method. This retworkx method
performs essentially the same loop internally but executes faster.

Depends on Qiskit/rustworkx#181 being included in
a release.
@coveralls
Copy link

coveralls commented Oct 20, 2020

Pull Request Test Coverage Report for Build 355266041

  • 47 of 47 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.1%) to 94.229%

Totals Coverage Status
Change from base Build 354704726: 0.1%
Covered Lines: 2596
Relevant Lines: 2755

💛 - Coveralls

@mtreinish mtreinish removed the on hold label Oct 21, 2020
@mtreinish mtreinish added this to the 0.6.0 milestone Oct 27, 2020
Copy link
Collaborator

@itoko itoko left a comment

Choose a reason for hiding this comment

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

Similar to #147, I think it would be better to have insert_node_on_in_edges(node, node_ref) and insert_node_on_out_edges rather than unified insert_node_between(node, node_ref, direction). What do you think?

@mtreinish
Copy link
Member Author

Similar to #147, I think it would be better to have insert_node_on_in_edges(node, node_ref) and insert_node_on_out_edges rather than unified insert_node_between(node, node_ref, direction). What do you think?

Yeah that's a good idea. I'll update it shortly to split the functions

This commit splits the new methods into separate and better named ones
per @itoko's excellent suggestion. The insert_node_between method
becomes insert_node_on_in_edges and insert_node_on_out_edges, and
insert_node_between_multiple becomes insert_node_on_in_edges_multiple
and insert_node_on_out_edges_multiple. The direction kwarg is also
removed from the Python API.
@mtreinish mtreinish changed the title Add insert_node_between and insert_node_between_multiple Add insert_node_on_in_edges and insert_node_on_out_edges PyDiGraph methods Nov 6, 2020
@mtreinish mtreinish changed the title Add insert_node_on_in_edges and insert_node_on_out_edges PyDiGraph methods Add insert_node_on_in_edges and insert_node_on_out_edges methods Nov 6, 2020
@itoko
Copy link
Collaborator

itoko commented Nov 9, 2020

@mtreinish Thank you. I have one more comment on docstrings.

I think we can describe the spec more clearly without parent/child notation:

/// Insert a node between a reference node and all its predecessor nodes
///
/// This essentially iterates over all edges into the reference node specified
/// in the ``ref_node`` parameter removes those edges and then adds 2 edges,
/// one from the predecessor of ``ref_node`` to ``node`` and the other from
/// ``node`` to ``ref_node``. The edge payloads for the newly created edges
/// are copied by reference from the original edge that gets removed.
///
/// :param int node: The node index to insert between
/// :param int ref_node: The reference node index to insert ``node`` between
pub fn insert_node_on_in_edges(..., node, ref_node)

/// Insert a node between a reference node and all its successor nodes
///
/// This essentially iterates over all edges out of the reference node specified
/// in the ``ref_node`` parameter removes those edges and then adds 2 edges,
/// one from ``ref_node`` to ``node`` and the other from ``node`` to the
/// successor of ``ref_node``. The edge payloads for the newly created edges
/// are copied by reference from the original edge that gets removed.
pub fn insert_node_on_out_edges(..., node, ref_node)

(I like the term reference node (ref_node) while I'm afraid someone might confuse with "reference of node object" (node_ref). Anyway, I think we could have better name than node_between. Any good idea?)

BTW, I personally prefer start -> end notation to parent -> child or tail -> head notation.
Honestly, I'm always confused with which is the "head" of parent and child...
(I know the importance of consistency, so if we have already used parent/child notations in many places, I think we should follow the convention.)

@mtreinish
Copy link
Member Author

@itoko thank you for the suggestions. I was actually hoping you'd leave a comment like that. I thought the names were awkward in this function, but couldn't come up with anything better so just left it as is. I'll update it now per your suggestions.

mtreinish and others added 2 commits November 9, 2020 08:01
Co-authored-by: Toshinari Itoko <itoko@jp.ibm.com>
@mtreinish mtreinish requested a review from itoko November 9, 2020 14:29
@mtreinish mtreinish merged commit 009c625 into Qiskit:master Nov 10, 2020
@mtreinish mtreinish deleted the insert_node_between branch November 10, 2020 12:22
mergify bot added a commit to Qiskit/qiskit that referenced this pull request Nov 25, 2020
* Leverage retworkx for apply_operation* inner loop

This commit migrates the inner loop for apply_operation_back and
apply_operation_front to leverage retworkx's
insert_node_between_multiple PyDiGraph method. This retworkx method
performs essentially the same loop internally but executes faster.

Depends on Qiskit/rustworkx#181 being included in
a release.

* DNM: Test with retworkx branch

* Update for retworkx 0.6.0 release

* Remove stray kwarg from earlier revision

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
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.

3 participants