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_const

Add a constant node to the HUGR.

add_link

Add a link (edge) between two nodes to the HUGR,

add_node

Add a node to the HUGR.

children

The child nodes of a given node.

delete_link

Delete a link (edge) between two nodes from the HUGR.

delete_node

Delete a node from the HUGR.

get

has_link

Check if there is a link between two ports.

incoming_links

Iterator over incoming links to a given node.

incoming_order_links

Iterator over nodes connected by an incoming state order link to a given node.

insert_hugr

Insert a HUGR into this HUGR.

items

keys

linked_ports

Return an iterable of In(Out)Ports linked to given Out(In)Port.

links

Iterator over all the links in the HUGR.

load_json

Deserialize a JSON string into a HUGR.

nodes

Iterator over nodes of the hugr and their data.

num_in_ports

The number of incoming ports of a node.

num_incoming

The number of incoming links to a node.

num_nodes

The number of nodes in the HUGR.

num_out_ports

The number of outgoing ports of a node.

num_outgoing

The number of outgoing links from a node.

num_ports

The number of ports of a node in a given direction.

outgoing_links

Iterator over outgoing links from a given node.

outgoing_order_links

Iterator over nodes connected by an outgoing state order link from a given node.

port_kind

The kind of a port.

port_type

The type of a port, if the kind is ValueKind, else None.

render_dot

Render the HUGR to a graphviz Digraph.

resolve_extensions

Resolve extension types and operations in the HUGR by matching them to extensions in the registry.

root_op

The operation of the root node.

store_dot

Render the HUGR to a graphviz dot file.

to_json

Serialize the HUGR to a JSON string.

values

Attributes

root

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 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 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.#

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

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)])]

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)]

Iterator over all the links in the HUGR.

Returns:

Iterator of pairs of outgoing port and the incoming ports.

classmethod load_json(json_str: str) Hugr[source]#

Deserialize a JSON string into a HUGR.

nodes() Iterable[tuple[Node, NodeData]][source]#

Iterator over nodes of the hugr and their data.

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

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)])]

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: Node#

Root node of the HUGR.

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.

to_json() str[source]#

Serialize the HUGR to a JSON string.

values() an object providing a view on D's values#