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

Method for copying a subgraph respecting nonlocal edges #1833

Open
acl-cqc opened this issue Jan 3, 2025 · 2 comments
Open

Method for copying a subgraph respecting nonlocal edges #1833

acl-cqc opened this issue Jan 3, 2025 · 2 comments
Assignees

Comments

@acl-cqc
Copy link
Contributor

acl-cqc commented Jan 3, 2025

Specifically, preserving edges incoming to the copied subgraph from outside it (adding new edges from the same sources). This is hard to achieve at present:

  • HugrMut::insert_from_view cannot be used with a view of the same Hugr, because the latter would borrow the Hugr immutably, preventing a mutable borrow to call insert_from_view.
    • Even if this worked, it would still suffer from the same problem as the next
  • One can construct a view of the subgraph and then call extract_hugr followed by insert_hugr, but extract_hugr loses the incoming intergraph edges (as it must!)
  • There is also insert_subgraph but again, this cannot do what is required, because again the borrow-checker allows only to insert a subgraph of another Hugr. (I have not investigated whether the desired behaviour could be achieved by inserting a subgraph of a clone of the same Hugr - besides the cost of cloning, this seems an odd interface, what happens if you insert a subgraph of a different Hugr where the subgraph has incoming nonlocal edges?)

Hence, I think the required method would have to have a signature

fn copy_subgraph(&mut self, root: Node, new_parent: Node) ....

The bulk of the implementation might (possibly) belong in the portgraph crate.

@acl-cqc
Copy link
Contributor Author

acl-cqc commented Jan 13, 2025

@aborgna-q my thought here is to do this is (Multi)Portgraph via a method that takes a set of nodes to copy (edges between them also copied, edges into the set copied but from the original source - hence the MultiPortgraph, edges out of the set....probably an error). Then we pass in set of descendants.

Hierarchy update also needs to know the root of the set, and we could verify all the nodes in the set are hierarchy-reachable from that root (perhaps even that the set includes all nodes hierarchy-reachable from the root).

@aborgna-q
Copy link
Collaborator

That seems reasonable. I'll open a portgraph issue.

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

No branches or pull requests

2 participants