Skip to content

Commit

Permalink
Add dfg_builder_endo
Browse files Browse the repository at this point in the history
  • Loading branch information
acl-cqc committed Jun 24, 2024
1 parent e029f81 commit d4a3af2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
15 changes: 15 additions & 0 deletions hugr-core/src/builder/build_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,21 @@ pub trait Dataflow: Container {
)
}

/// Return a builder for a [`crate::ops::DFG`] node, i.e. a nested dataflow subgraph,
/// that is endomorphic (the output types are the same as the input types).
/// The `inputs` must be an iterable over pairs of the type of the input and
/// the corresponding wire.
fn dfg_builder_endo(
&mut self,
inputs: impl IntoIterator<Item = (Type, Wire)>,
) -> Result<DFGBuilder<&mut Hugr>, BuildError> {
let (types, input_wires): (Vec<Type>, Vec<Wire>) = inputs.into_iter().unzip();
self.dfg_builder_fn(
FunctionType::new_endo(types).with_extension_delta(TO_BE_INFERRED),
input_wires,
)
}

/// Return a builder for a [`crate::ops::CFG`] node,
/// i.e. a nested controlflow subgraph.
/// The `inputs` must be an iterable over pairs of the type of the input and
Expand Down
4 changes: 2 additions & 2 deletions hugr-core/src/hugr/rewrite/inline_dfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ mod test {
}
let c1 = nonlocal.then(|| make_const(&mut outer));
let inner = {
let mut inner = outer.dfg_builder([(int_ty.clone(), a)], int_ty.clone())?;
let mut inner = outer.dfg_builder_endo([(int_ty.clone(), a)])?;
let [a] = inner.input_wires_arr();
let c1 = c1.unwrap_or_else(|| make_const(&mut inner))?;
let a1 = inner.add_dataflow_op(IntOpDef::iadd.with_log_width(6), [a, c1])?;
Expand Down Expand Up @@ -344,7 +344,7 @@ mod test {
let h_a = outer.add_dataflow_op(test_quantum_extension::h_gate(), [a])?;
let h_b = outer.add_dataflow_op(test_quantum_extension::h_gate(), [b])?;
let [hb_out] = h_b.outputs_arr();
let mut inner = outer.dfg_builder([(QB_T, hb_out)], QB_T)?;
let mut inner = outer.dfg_builder_endo([(QB_T, hb_out)])?;
let [i] = inner.input_wires_arr();
let f = inner.add_load_value(float_types::ConstF64::new(1.0));
inner.add_other_wire(inner.input().node(), f.node());
Expand Down

0 comments on commit d4a3af2

Please sign in to comment.