hugr_core

Module builder

source
Expand description

Utilities for building valid HUGRs.

This module includes various tools for building HUGRs.

Depending on the type of HUGR you want to build, you may want to use one of the following builders:

Additionally, the CircuitBuilder provides an alternative to the DFGBuilder when working with circuits, where some inputs of operations directly correspond to some outputs and operations can be directly appended using unit indices.

§Example

The following example shows how to build a simple HUGR module with two dataflow functions, one built using the DFGBuilder and the other using the CircuitBuilder.

use hugr::extension::prelude::BOOL_T;
use hugr::std_extensions::logic::{EXTENSION_ID, LOGIC_REG, LogicOp};
use hugr::types::Signature;

let hugr = {
    let mut module_builder = ModuleBuilder::new();

    // Add a `main` function with signature `bool -> bool`.
    //
    // This block returns a handle to the built function.
    let _dfg_handle = {
        let mut dfg = module_builder.define_function(
            "main",
            Signature::new_endo(BOOL_T).with_extension_delta(EXTENSION_ID),
        )?;

        // Get the wires from the function inputs.
        let [w] = dfg.input_wires_arr();

        // Add an operation connected to the input wire, and get the new dangling wires.
        let [w] = dfg.add_dataflow_op(LogicOp::Not, [w])?.outputs_arr();

        // Finish the function, connecting some wires to the output.
        dfg.finish_with_outputs([w])
    }?;

    // Add a similar function, using the circuit builder interface.
    let _circuit_handle = {
        let mut dfg = module_builder.define_function(
            "circuit",
            Signature::new_endo(vec![BOOL_T, BOOL_T])
                .with_extension_delta(EXTENSION_ID),
        )?;
        let mut circuit = dfg.as_circuit(dfg.input_wires());

        // Add multiple operations, indicating only the wire index.
        circuit.append(LogicOp::Not, [0])?.append(LogicOp::Not, [1])?;

        // Finish the circuit, and return the dataflow graph after connecting its outputs.
        let outputs = circuit.finish();
        dfg.finish_with_outputs(outputs)
    }?;

    // Finish building the HUGR, consuming the builder.
    //
    // Requires a registry with all the extensions used in the module.
    module_builder.finish_hugr(&LOGIC_REG)
}?;

// The built HUGR is always valid.
hugr.validate(&LOGIC_REG).unwrap_or_else(|e| {
    panic!("HUGR validation failed: {e}");
});

Re-exports§

Modules§

  • Handles to nodes in HUGR used during the building phase.

Structs§

Enums§

Traits§

  • Trait for HUGR container builders. Containers are nodes that are parents of sibling graphs. Implementations of this trait allow the child sibling graph to be added to the HUGR.
  • Trait for building dataflow regions of a HUGR.
  • Trait implemented by builders of Dataflow Hugrs
  • Trait implemented by builders of Dataflow container regions of a HUGR
  • Types implementing this trait can be used to build complete HUGRs (with varying root node types)
  • Types implementing this trait build a container graph region by borrowing a HUGR

Functions§

  • Return a FunctionType with the same input and output types (specified) whose extension delta, when used in a non-FuncDefn container, will be inferred.
  • Return a FunctionType with the specified input and output types whose extension delta, when used in a non-FuncDefn container, will be inferred.

Type Aliases§