Skip to content

Commit

Permalink
add order link iterators and fix detected bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Jun 6, 2024
1 parent 5aa92ce commit 273fe7e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion hugr-py/src/hugr/_dfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def _wire_up(self, node: Node, ports: Iterable[Wire]):
node_ancestor = _ancestral_sibling(self.hugr, src.node, node)
if node_ancestor is None:
raise NoSiblingAncestor(src.node.idx, node.idx)
if node_ancestor != src.node:
if node_ancestor != node:
self.add_state_order(src.node, node_ancestor)
self.hugr.add_link(src, node.inp(i))

Expand Down
7 changes: 6 additions & 1 deletion hugr-py/src/hugr/_hugr.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@ def linked_ports(self, port: OutPort | InPort):

# TODO: single linked port

def outgoing_order_links(self, node: Node) -> Iterable[Node]:
return (p.node for p in self.linked_ports(node.out(-1)))

def incoming_order_links(self, node: Node) -> Iterable[Node]:
return (p.node for p in self.linked_ports(node.inp(-1)))

def _node_links(
self, node: Node, links: dict[_SubPort[P], _SubPort[K]]
) -> Iterable[tuple[P, list[K]]]:
Expand All @@ -292,7 +298,6 @@ def _node_links(
return
# iterate over known offsets
for offset in range(self.num_ports(node, direction)):
# TODO should this also look for -1 state order edges?
port = cast(P, node.port(offset, direction))
yield port, list(self._linked_ports(port, links))

Expand Down
6 changes: 5 additions & 1 deletion hugr-py/tests/test_hugr_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,13 @@ def test_build_inter_graph():

h.set_outputs(nested.root, b)

_validate(h.hugr, True)

assert _SubPort(h.input_node.out(-1)) in h.hugr._links
assert h.hugr.num_outgoing(h.input_node) == 2 # doesn't count state order
_validate(h.hugr, True)
assert len(list(h.hugr.outgoing_order_links(h.input_node))) == 1
assert len(list(h.hugr.incoming_order_links(nested.root))) == 1
assert len(list(h.hugr.incoming_order_links(h.output_node))) == 0


def test_ancestral_sibling():
Expand Down

0 comments on commit 273fe7e

Please sign in to comment.