You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adds the concept of "entrypoint" to the HUGRs, detaching the "main node
of interest" from the hierarchy root.
Now hugrs will **always** have a `Module` at their root (appropriately
called `HugrView::module_root`), and any manipulation is mostly focused
around the entrypoint instead.
Closes#2029Closes#2010
before:
```mermaid
graph LR
subgraph 0 ["(0) DFG"]
direction LR
1["(1) Input"]
2["(2) Output"]
3["(3) test.quantum.CX"]
4["(4) test.quantum.CX"]
1--"0:0<br>qubit"-->3
1--"1:1<br>qubit"-->3
3--"0:1<br>qubit"-->4
3--"1:0<br>qubit"-->4
3-."2:2".->4
4--"0:0<br>qubit"-->2
4--"1:1<br>qubit"-->2
end
```
after:
```mermaid
graph LR
subgraph 0 ["(0) Module"]
direction LR
subgraph 1 ["(1) FuncDefn: #quot;main#quot;"]
direction LR
2["(2) Input"]
3["(3) Output"]
subgraph 4 ["(4) [**DFG**]"]
direction LR
style 4 stroke:#832561,stroke-width:3px
5["(5) Input"]
6["(6) Output"]
7["(7) test.quantum.CX"]
8["(8) test.quantum.CX"]
5--"0:0<br>qubit"-->7
5--"1:1<br>qubit"-->7
7--"0:1<br>qubit"-->8
7--"1:0<br>qubit"-->8
7-."2:2".->8
8--"0:0<br>qubit"-->6
8--"1:1<br>qubit"-->6
end
2--"0:0<br>qubit"-->4
2--"1:1<br>qubit"-->4
4--"0:0<br>qubit"-->3
4--"1:1<br>qubit"-->3
end
end
```
# How does this affect...
### ...HugrView?
`::root` has been split into `module_root` and `entrypoint`. In general,
you'll want to use the latter (as described in the docs).
`nodes()` is still defined, but now there's also `descendants(node)` and
`entry_descendants()`, which should be preferred.
### ...builders?
The change should be transparent for hugr building operations. When you
start an specific builder or call `Hugr::new(op)` we add additional
nodes as necessary so that the new entrypoint is correctly defined
inside a module;
- If `op` is a module, we just leave it at the top.
- If `op` can be defined in a module, we put it below the root.
- If `op` is a dataflow operation, we define a "main" function with the
same signature and put it there.
- More exotic operations are not allowed, you'll need to build the
container yourself and change the entrypoint afterwards.
### ...SiblingGraph, SiblingMut, and DescendantsGraph?
The structs are no more. Instead, `HugrView` has two new methods:
- `with_entrypoint(&self, Node) -> Rerooted<&Self>`
- `with_entrypoint_mut(&mut self, Node) -> Rerooted<&mut Self>`
The new wrapper implements `HugrMut`, so it can be used transparently.
The main benefit of this is that `Rerooted` still contains all the nodes
that are not descendants from the entrypoint. So external edges are
still well-defined, and rewrite operations may still put look at the
root module and define things there if needed.
### ...serialization?
The hugr json now has an `entrypoint` field. If missing, we assume it's
the root.
For backwards compatibility, if the serialized root is not a module we
will transparently wrap it as described before so older jsons will
continue working without modifications.
hugr-module encoding is left as a TODO
### ...packages and envelopes?
Now every HUGR can be put in a package without modifications!
Ideally we'll now be able to remove the hugr <-> json methods and only
use envelopes. (That's a TODO).
### ...mermaid/dot renders?
The entrypoint node is now highlighted in purple, and its title shown in
bold between brackets. See the example above.
### ...passes?
Things should work as normal. New passes should look at the entrypoint
and its descendants when looking for things to rewrite, instead of the
whole hugr.
# TODOs not in this PR:
- [ ] Python support. #2148
- [ ] `hugr-module` support #2156
- [ ] Deprecate/remove hugr json methods, always use envelopes instead.
#2159
- [ ] Update the spec.
BREAKING CHANGE: `Hugr`s now have an entrypoint node.
BREAKING CHANGE: Removed `SiblingGraph` / `SiblingMut` /
`DescendantsGraph`
---------
Co-authored-by: Seyon Sivarajah <[email protected]>
0 commit comments