Hugr#
- class hugr.hugr.base.Hugr(root_op: OpVarCov | None = None)[source]#
Bases:
Mapping
[Node
,NodeData
],Generic
[OpVarCov
]The core HUGR datastructure.
- Parameters:
root_op – The operation for the root node. Defaults to a Module.
Examples
>>> h = Hugr() >>> h.root_op() Module() >>> h[h.root].op Module()
Methods
Add a constant node to the HUGR.
Add a link (edge) between two nodes to the HUGR,
Add a node to the HUGR.
The child nodes of a given node.
Delete a link (edge) between two nodes from the HUGR.
Delete a node from the HUGR.
Check if there is a link between two ports.
Iterator over incoming links to a given node.
Iterator over nodes connected by an incoming state order link to a given node.
Insert a HUGR into this HUGR.
Return an iterable of In(Out)Ports linked to given Out(In)Port.
Iterator over all the links in the HUGR.
Deserialize a JSON string into a HUGR.
Iterator over nodes of the hugr and their data.
The number of incoming ports of a node.
The number of incoming links to a node.
The number of nodes in the HUGR.
The number of outgoing ports of a node.
The number of outgoing links from a node.
The number of ports of a node in a given direction.
Iterator over outgoing links from a given node.
Iterator over nodes connected by an outgoing state order link from a given node.
The kind of a port.
The type of a port, if the kind is
ValueKind
, else None.Render the HUGR to a graphviz Digraph.
Resolve extension types and operations in the HUGR by matching them to extensions in the registry.
The operation of the root node.
Render the HUGR to a graphviz dot file.
Serialize the HUGR to a JSON string.
Attributes
Root node of the HUGR.
- add_const(value: Value, parent: ToNode | None = None, metadata: dict[str, Any] | None = None) Node [source]#
Add a constant node to the HUGR.
- Parameters:
value – Value of the constant.
parent – Parent node of added node. Defaults to HUGR root if None.
metadata – A dictionary of metadata to associate with the node. Defaults to None.
- Returns:
Handle to the added node.
Examples
>>> h = Hugr() >>> n = h.add_const(val.TRUE) >>> h[n].op Const(TRUE)
- add_link(src: OutPort, dst: InPort) None [source]#
- Add a link (edge) between two nodes to the HUGR,
from an outgoing port to an incoming port.
- Parameters:
src – Source port.
dst – Destination port.
Examples
>>> df = dfg.Dfg(tys.Bool) >>> df.hugr.add_link(df.input_node.out(0), df.output_node.inp(0)) >>> list(df.hugr.linked_ports(df.input_node[0])) [InPort(Node(2), 0)]
- add_node(op: Op, parent: ToNode | None = None, num_outs: int | None = None, metadata: dict[str, Any] | None = None) Node [source]#
Add a node to the HUGR.
- Parameters:
op – Operation of the node.
parent – Parent node of added node. Defaults to HUGR root if None.
num_outs – Number of output ports expected for this node. Defaults to None.
metadata – A dictionary of metadata to associate with the node. Defaults to None.
- Returns:
Handle to the added node.
- children(node: ToNode | None = None) list[Node] [source]#
The child nodes of a given node.
- Parameters:
node – Parent node. Defaults to the HUGR root.
- Returns:
List of child nodes.
Examples
>>> h = Hugr() >>> n = h.add_node(ops.Const(val.TRUE)) >>> h.children(h.root) [Node(1)]
- delete_link(src: OutPort, dst: InPort) None [source]#
Delete a link (edge) between two nodes from the HUGR.
- Parameters:
src – Source port.
dst – Destination port.
- delete_node(node: ToNode) NodeData | None [source]#
Delete a node from the HUGR.
- Parameters:
node – Node to delete.
- Returns:
The deleted node data, or None if the node was not found.
Examples
>>> h = Hugr() >>> n = h.add_const(val.TRUE) >>> deleted = h.delete_node(n) >>> deleted.op Const(TRUE) >>> len(h) 1
- get(k[, d]) D[k] if k in D, else d. d defaults to None. #
- has_link(src: OutPort, dst: InPort) bool [source]#
Check if there is a link between two ports.
- Parameters:
src – Source port.
dst – Destination port.
- Returns:
True if there is a link, False otherwise.
Examples
>>> df = dfg.Dfg(tys.Bool) >>> df.hugr.add_link(df.input_node.out(0), df.output_node.inp(0)) >>> df.hugr.has_link(df.input_node.out(0), df.output_node.inp(0)) True
- incoming_links(node: ToNode) Iterable[tuple[InPort, list[OutPort]]] [source]#
Iterator over incoming links to a given node.
- Parameters:
node – Node to query.
- Returns:
Iterator of pairs of incoming port and the outgoing ports connected to that port.
Examples
>>> df = dfg.Dfg() >>> df.hugr.add_link(df.input_node.out(0), df.output_node.inp(0)) >>> df.hugr.add_link(df.input_node.out(0), df.output_node.inp(1)) >>> list(df.hugr.incoming_links(df.output_node)) [(InPort(Node(2), 0), [OutPort(Node(1), 0)]), (InPort(Node(2), 1), [OutPort(Node(1), 0)])]
- incoming_order_links(node: ToNode) Iterable[Node] [source]#
Iterator over nodes connected by an incoming state order link to a given node.
- Parameters:
node – Destination node of state order link.
Examples
>>> df = dfg.Dfg() >>> df.add_state_order(df.input_node, df.output_node) >>> list(df.hugr.incoming_order_links(df.output_node)) [Node(1)]
- insert_hugr(hugr: Hugr, parent: ToNode | None = None) dict[Node, Node] [source]#
Insert a HUGR into this HUGR.
- Parameters:
hugr – HUGR to insert.
parent – Parent for root of inserted HUGR. Defaults to None.
- Returns:
Mapping from node indices in inserted HUGR to their new indices in this HUGR.
Examples
>>> d = dfg.Dfg() >>> h = Hugr() >>> h.insert_hugr(d.hugr) {Node(0): Node(1), Node(1): Node(2), Node(2): Node(3)}
- items() a set-like object providing a view on D's items #
- keys() a set-like object providing a view on D's keys #
- linked_ports(port: OutPort) Iterable[InPort] [source]#
- linked_ports(port: InPort) Iterable[OutPort]
Return an iterable of In(Out)Ports linked to given Out(In)Port.
- Parameters:
port – Given port.
- Returns:
Iterator over linked ports.
Examples
>>> df = dfg.Dfg(tys.Bool) >>> df.set_outputs(df.input_node[0]) >>> list(df.hugr.linked_ports(df.input_node[0])) [InPort(Node(2), 0)]
- links() Iterator[tuple[OutPort, InPort]] [source]#
Iterator over all the links in the HUGR.
- Returns:
Iterator of pairs of outgoing port and the incoming ports.
- num_in_ports(node: ToNode) int [source]#
The number of incoming ports of a node. See
num_ports()
.
- num_incoming(node: Node) int [source]#
The number of incoming links to a node.
Examples
>>> df = dfg.Dfg() >>> df.hugr.add_link(df.input_node.out(0), df.output_node.inp(0)) >>> df.hugr.num_incoming(df.output_node) 1
- num_nodes() int [source]#
The number of nodes in the HUGR.
Examples
>>> h = Hugr() >>> n = h.add_const(val.TRUE) >>> h.num_nodes() 2
- num_out_ports(node: ToNode) int [source]#
The number of outgoing ports of a node. See
num_ports()
.
- num_outgoing(node: ToNode) int [source]#
The number of outgoing links from a node.
Examples
>>> df = dfg.Dfg() >>> df.hugr.add_link(df.input_node.out(0), df.output_node.inp(0)) >>> df.hugr.num_outgoing(df.input_node) 1
- num_ports(node: ToNode, direction: Direction) int [source]#
The number of ports of a node in a given direction. Not necessarily the number of connected ports - if port i is connected, then all ports 0..i are assumed to exist.
- Parameters:
node – Node to query.
direction – Direction of ports to count.
Examples
>>> h = Hugr() >>> n1 = h.add_const(val.TRUE) >>> n2 = h.add_const(val.FALSE) >>> h.add_link(n1.out(0), n2.inp(2)) # not a valid link! >>> h.num_ports(n1, Direction.OUTGOING) 1 >>> h.num_ports(n2, Direction.INCOMING) 3
- outgoing_links(node: ToNode) Iterable[tuple[OutPort, list[InPort]]] [source]#
Iterator over outgoing links from a given node.
- Parameters:
node – Node to query.
- Returns:
Iterator of pairs of outgoing port and the incoming ports connected to that port.
Examples
>>> df = dfg.Dfg() >>> df.hugr.add_link(df.input_node.out(0), df.output_node.inp(0)) >>> df.hugr.add_link(df.input_node.out(0), df.output_node.inp(1)) >>> list(df.hugr.outgoing_links(df.input_node)) [(OutPort(Node(1), 0), [InPort(Node(2), 0), InPort(Node(2), 1)])]
- outgoing_order_links(node: ToNode) Iterable[Node] [source]#
Iterator over nodes connected by an outgoing state order link from a given node.
- Parameters:
node – Source node of state order link.
Examples
>>> df = dfg.Dfg() >>> df.add_state_order(df.input_node, df.output_node) >>> list(df.hugr.outgoing_order_links(df.input_node)) [Node(2)]
- port_kind(port: InPort | OutPort) ValueKind | ConstKind | FunctionKind | CFKind | OrderKind [source]#
The kind of a port.
Examples
>>> df = dfg.Dfg(tys.Bool) >>> df.hugr.port_kind(df.input_node.out(0)) ValueKind(Bool)
- port_type(port: InPort | OutPort) Type | None [source]#
The type of a port, if the kind is
ValueKind
, else None.Examples
>>> df = dfg.Dfg(tys.Bool) >>> df.hugr.port_type(df.input_node.out(0)) Bool
- render_dot(config: RenderConfig | None = None) gv.Digraph [source]#
Render the HUGR to a graphviz Digraph.
- Parameters:
config – Render configuration.
- Returns:
The graphviz Digraph.
- resolve_extensions(registry: ext.ExtensionRegistry) Hugr [source]#
Resolve extension types and operations in the HUGR by matching them to extensions in the registry.
- root_op() OpVarCov [source]#
The operation of the root node.
Examples
>>> h = Hugr() >>> h.root_op() Module()
- store_dot(filename: str, format: str = 'svg', config: RenderConfig | None = None) None [source]#
Render the HUGR to a graphviz dot file.
- Parameters:
filename – The file to render to.
format – The format used for rendering (‘pdf’, ‘png’, etc.). Defaults to SVG.
config – Render configuration.
- values() an object providing a view on D's values #