Conditional#

class hugr.build.cond_loop.Conditional(sum_ty: Sum, other_inputs: TypeRow)[source]#

Bases: ParentBuilder[Conditional], AbstractContextManager

Build a conditional branching on a sum type.

Parameters:
  • sum_ty – The sum type to branch on.

  • other_inputs – The inputs for the conditional that aren’t included in the

  • cases. (sum variants. These are passed to all)

Examples

>>> cond = Conditional(tys.Bool, [tys.Qubit])
>>> cond.parent_op
Conditional(sum_ty=Bool, other_inputs=[Qubit])

Methods

add_case

Start building a case for the conditional.

inp

Generate an input port for this node.

new_nested

Build a Conditional nested inside an existing HUGR graph.

out

Generate an output port for this node.

out_port

OutPort corresponding to this Wire.

outputs

Returns an iterator over the output ports of this node.

port

Generate a port in direction for this node with offset.

to_node

Convert to a Node.

Attributes

cases

Map from case index to node holding the Case.

metadata

Metadata associated with this node.

parent_op

The parent node's operation.

hugr

The child HUGR.

parent_node

add_case(case_id: int) Case[source]#

Start building a case for the conditional.

Parameters:
  • case_id – The index of the case to build. Input types for the case

  • the (are the corresponding variant of the sum type concatenated with)

  • conditional. (other inputs to the)

Returns:

The new case builder.

Raises:

ConditionalError – If the case index is out of range.

Examples

>>> cond = Conditional(tys.Bool, [tys.Qubit])
>>> with cond.add_case(0) as case:                    case.set_outputs(*case.inputs())
property cases: dict[int, Node | None]#

Map from case index to node holding the Case.

DEPRECATED

hugr: Hugr[OpVar]#

The child HUGR.

inp(offset: int) InPort#

Generate an input port for this node.

Parameters:

offset – port offset.

Returns:

Incoming port for this node.

Examples

>>> Node(0).inp(1)
InPort(Node(0), 1)
property metadata: dict[str, object]#

Metadata associated with this node.

classmethod new_nested(sum_ty: Sum, other_inputs: TypeRow, hugr: Hugr, parent: ToNode | None = None) Conditional[source]#

Build a Conditional nested inside an existing HUGR graph.

Parameters:
  • sum_ty – The sum type to branch on.

  • other_inputs – The inputs for the conditional that aren’t included in the sum variants. These are passed to all cases.

  • hugr – The HUGR instance this Conditional is part of.

  • parent – The parent node for the Conditional: defaults to the root of the HUGR instance.

Returns:

The new Conditional builder.

out(offset: int) OutPort#

Generate an output port for this node.

Parameters:

offset – port offset.

Returns:

Outgoing port for this node.

Examples

>>> Node(0).out(1)
OutPort(Node(0), 1)
out_port() OutPort#

OutPort corresponding to this Wire.

outputs() Iterator[OutPort]#

Returns an iterator over the output ports of this node.

property parent_op: OpVar#

The parent node’s operation.

port(offset: int, direction: Direction) InPort | OutPort#

Generate a port in direction for this node with offset.

Examples

>>> Node(0).port(1, Direction.INCOMING)
InPort(Node(0), 1)
>>> Node(0).port(1, Direction.OUTGOING)
OutPort(Node(0), 1)
to_node() Node#

Convert to a Node.