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
Start building a case for the conditional.
Generate an input port for this node.
Build a Conditional nested inside an existing HUGR graph.
Generate an output port for this node.
OutPort corresponding to this
Wire
.Returns an iterator over the output ports of this node.
Generate a port in direction for this node with offset.
Convert to a
Node
.Attributes
Map from case index to node holding the
Case
.Metadata associated with this node.
The parent node's operation.
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())
- 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)
- 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)
- property parent_op: OpVar#
The parent node’s operation.