hugr_core/builder/
dataflow.rs

1//! Builders for dataflow regions.
2
3use itertools::Itertools;
4
5use super::build_traits::{HugrBuilder, SubContainer};
6use super::handle::BuildHandle;
7use super::{BuildError, Container, Dataflow, DfgID, FuncID};
8
9use std::marker::PhantomData;
10
11use crate::hugr::internal::HugrMutInternals;
12use crate::hugr::{HugrView, ValidationError};
13use crate::ops::handle::NodeHandle;
14use crate::ops::{self, DFG, FuncDefn, NamedOp, OpParent, OpType};
15use crate::types::{PolyFuncType, Signature, Type};
16use crate::{Direction, Hugr, IncomingPort, Node, OutgoingPort, Visibility, Wire, hugr::HugrMut};
17
18/// Builder for a [`ops::DFG`] node.
19#[derive(Debug, Clone, PartialEq)]
20pub struct DFGBuilder<T> {
21    pub(crate) base: T,
22    pub(crate) dfg_node: Node,
23    pub(crate) num_in_wires: usize,
24    pub(crate) num_out_wires: usize,
25}
26
27/// Error returned by [`DFGBuilder::add_input`] and [`DFGBuilder::add_output`].
28#[derive(Debug, Clone, PartialEq, derive_more::Display, derive_more::Error)]
29#[non_exhaustive]
30pub enum DFGAddPortError {
31    /// The parent optype is not a [`FuncDefn`], [`DFG`], or dataflow block so we cannot update its signature.
32    #[display("Adding port to an optype {op} is not supported.")]
33    ParentOpNotSupported {
34        /// The name of the unsupported operation.
35        op: String,
36    },
37}
38
39impl<T: AsMut<Hugr> + AsRef<Hugr>> DFGBuilder<T> {
40    /// Returns a new `DFGBuilder` with the given base and parent node.
41    ///
42    /// Sets up the input and output nodes of the region. If `parent` already has
43    /// input and output nodes, use [`DFGBuilder::create`] instead.
44    pub fn create_with_io(
45        mut base: T,
46        parent: Node,
47        signature: Signature,
48    ) -> Result<Self, BuildError> {
49        debug_assert_eq!(base.as_ref().children(parent).count(), 0);
50
51        let num_in_wires = signature.input_count();
52        let num_out_wires = signature.output_count();
53        let input = ops::Input {
54            types: signature.input().clone(),
55        };
56        let output = ops::Output {
57            types: signature.output().clone(),
58        };
59        base.as_mut().add_node_with_parent(parent, input);
60        base.as_mut().add_node_with_parent(parent, output);
61
62        Ok(Self {
63            base,
64            dfg_node: parent,
65            num_in_wires,
66            num_out_wires,
67        })
68    }
69
70    /// Returns a new `DFGBuilder` with the given base and parent node.
71    ///
72    /// The parent node may be any `DataflowParent` node.
73    ///
74    /// If `parent` doesn't have input and output nodes, use
75    /// [`DFGBuilder::create_with_io`] instead.
76    pub fn create(base: T, parent: Node) -> Result<Self, BuildError> {
77        let sig = base
78            .as_ref()
79            .get_optype(parent)
80            .inner_function_type()
81            .expect("DFG parent must have an inner function signature.");
82        let num_in_wires = sig.input_count();
83        let num_out_wires = sig.output_count();
84
85        Ok(Self {
86            base,
87            dfg_node: parent,
88            num_in_wires,
89            num_out_wires,
90        })
91    }
92
93    /// Add a new input to the dataflow being constructed.
94    ///
95    /// Updates the parent's optype to include the new input type.
96    ///
97    /// # Returns
98    ///
99    /// - The new wire from the input node.
100    ///
101    /// # Errors
102    ///
103    /// - [`DFGAddPortError::ParentOpNotSupported`] if the container optype is not a [`FuncDefn`], [`DFG`], or dataflow block so we cannot add an input.
104    ///   In this case, the Hugr will not be updated.
105    pub fn add_input(&mut self, input_type: Type) -> Result<Wire, DFGAddPortError> {
106        let [inp_node, _] = self.io();
107
108        // Update the parent's root type
109        if !self.update_parent_signature(|mut s| {
110            s.input.to_mut().push(input_type.clone());
111            s
112        }) {
113            return Err(DFGAddPortError::ParentOpNotSupported {
114                op: self
115                    .hugr()
116                    .get_optype(self.container_node())
117                    .name()
118                    .to_string(),
119            });
120        }
121
122        // Update the inner input node
123        let OpType::Input(inp) = self.hugr_mut().optype_mut(inp_node) else {
124            panic!("Input node {inp_node} is not an Input");
125        };
126        inp.types.to_mut().push(input_type);
127
128        let mut new_port = self.hugr_mut().add_ports(inp_node, Direction::Outgoing, 1);
129        let new_port = new_port.next().unwrap();
130
131        // The last port in an input/output node is an order edge port, so we must shift any connections to it.
132        let new_value_port: OutgoingPort = (new_port - 1).into();
133        let new_order_port: OutgoingPort = new_port.into();
134        let order_edge_targets = self
135            .hugr()
136            .linked_inputs(inp_node, new_value_port)
137            .collect_vec();
138        self.hugr_mut().disconnect(inp_node, new_value_port);
139        for (tgt_node, tgt_port) in order_edge_targets {
140            self.hugr_mut()
141                .connect(inp_node, new_order_port, tgt_node, tgt_port);
142        }
143
144        // Update the builder metadata
145        self.num_in_wires += 1;
146
147        Ok(self.input_wires().next_back().unwrap())
148    }
149
150    /// Add a new output to the function being constructed.
151    ///
152    /// Updates the parent's optype to include the new input type, if possible.
153    /// If the container optype is not a [`FuncDefn`], [`DFG`], or dataflow
154    /// block, the optype will not be updated.
155    ///
156    /// # Errors
157    ///
158    /// - [`DFGAddPortError::ParentOpNotSupported`] if the container optype is not a [`FuncDefn`], [`DFG`], or dataflow block so we cannot add an input.
159    ///   In this case, the Hugr will not be updated.
160    pub fn add_output(&mut self, output_type: Type) -> Result<(), DFGAddPortError> {
161        let [_, out_node] = self.io();
162
163        // Update the parent's root type
164        if !self.update_parent_signature(|mut s| {
165            s.output.to_mut().push(output_type.clone());
166            s
167        }) {
168            return Err(DFGAddPortError::ParentOpNotSupported {
169                op: self
170                    .hugr()
171                    .get_optype(self.container_node())
172                    .name()
173                    .to_string(),
174            });
175        }
176
177        // Update the inner input node
178        let OpType::Output(out) = self.hugr_mut().optype_mut(out_node) else {
179            panic!("Output node {out_node} is not an Output");
180        };
181        out.types.to_mut().push(output_type);
182
183        let mut new_port = self.hugr_mut().add_ports(out_node, Direction::Incoming, 1);
184        let new_port = new_port.next().unwrap();
185
186        // The last port in an input/output node is an order edge port, so we must shift any connections to it.
187        let new_value_port: IncomingPort = (new_port - 1).into();
188        let new_order_port: IncomingPort = new_port.into();
189        let order_edge_sources = self
190            .hugr()
191            .linked_outputs(out_node, new_value_port)
192            .collect_vec();
193        self.hugr_mut().disconnect(out_node, new_value_port);
194        for (src_node, src_port) in order_edge_sources {
195            self.hugr_mut()
196                .connect(src_node, src_port, out_node, new_order_port);
197        }
198
199        // Update the builder metadata
200        self.num_out_wires += 1;
201
202        Ok(())
203    }
204
205    /// Update the container's parent signature, if it is a function definition, DFG region, or dataflow block.
206    ///
207    /// Internal function used in [`add_input`] and [`add_output`].
208    ///
209    /// Does not update the input and output nodes.
210    ///
211    /// # Returns
212    ///
213    /// - `true` if the parent signature was updated.
214    /// - `false` if the parent optype is not a [`FuncDefn`], [`DFG`], or dataflow block so we cannot update the signature.
215    fn update_parent_signature(&mut self, f: impl FnOnce(Signature) -> Signature) -> bool {
216        let parent = self.container_node();
217
218        match self.hugr_mut().optype_mut(parent) {
219            ops::OpType::FuncDefn(fd) => {
220                let mut sig = std::mem::take(fd.signature_mut());
221                let body = std::mem::take(sig.body_mut());
222                *sig.body_mut() = f(body);
223                *fd.signature_mut() = sig;
224            }
225            ops::OpType::DFG(dfg) => {
226                let sig = std::mem::take(&mut dfg.signature);
227                dfg.signature = f(sig);
228            }
229            ops::OpType::DataflowBlock(dfb) => {
230                let inp = std::mem::take(&mut dfb.inputs);
231                let other_outputs = std::mem::take(&mut dfb.other_outputs);
232                let sig = f(Signature::new(inp, other_outputs));
233                dfb.inputs = sig.input;
234                dfb.other_outputs = sig.output;
235            }
236            _ => return false,
237        }
238        true
239    }
240}
241
242impl DFGBuilder<Hugr> {
243    /// Begin building a new DFG-rooted HUGR given its inputs, outputs,
244    /// and extension delta.
245    ///
246    /// # Errors
247    ///
248    /// Error in adding DFG child nodes.
249    pub fn new(signature: Signature) -> Result<DFGBuilder<Hugr>, BuildError> {
250        let dfg_op: DFG = ops::DFG {
251            signature: signature.clone(),
252        };
253        let base = Hugr::new_with_entrypoint(dfg_op).expect("DFG entrypoint should be valid");
254        let root = base.entrypoint();
255        DFGBuilder::create_with_io(base, root, signature)
256    }
257}
258
259impl HugrBuilder for DFGBuilder<Hugr> {
260    fn finish_hugr(self) -> Result<Hugr, ValidationError<Node>> {
261        self.base.validate()?;
262        Ok(self.base)
263    }
264}
265
266impl<T: AsMut<Hugr> + AsRef<Hugr>> Container for DFGBuilder<T> {
267    #[inline]
268    fn container_node(&self) -> Node {
269        self.dfg_node
270    }
271
272    #[inline]
273    fn hugr_mut(&mut self) -> &mut Hugr {
274        self.base.as_mut()
275    }
276
277    #[inline]
278    fn hugr(&self) -> &Hugr {
279        self.base.as_ref()
280    }
281}
282
283impl<T: AsMut<Hugr> + AsRef<Hugr>> SubContainer for DFGBuilder<T> {
284    type ContainerHandle = BuildHandle<DfgID>;
285    #[inline]
286    fn finish_sub_container(self) -> Result<Self::ContainerHandle, BuildError> {
287        Ok((self.dfg_node, self.num_out_wires).into())
288    }
289}
290
291impl<T: AsMut<Hugr> + AsRef<Hugr>> Dataflow for DFGBuilder<T> {
292    #[inline]
293    fn num_inputs(&self) -> usize {
294        self.num_in_wires
295    }
296}
297
298/// Wrapper around [`DFGBuilder`] used to build other dataflow regions.
299// Stores option of DFGBuilder so it can be taken out without moving.
300#[derive(Debug, Clone, PartialEq)]
301pub struct DFGWrapper<B, T>(DFGBuilder<B>, PhantomData<T>);
302
303impl<B, T: NodeHandle> DFGWrapper<B, T> {
304    /// Wrap a [`DFGBuilder`] into a [`DFGWrapper`], with the given handle type.
305    ///
306    /// The caller must ensure that the DFGBuilder's parent node has the correct
307    /// handle type given by `T::TAG`.
308    pub fn from_dfg_builder(db: DFGBuilder<B>) -> Self {
309        Self(db, PhantomData)
310    }
311
312    /// Unwrap the [`DFGWrapper`] into a [`DFGBuilder`].
313    pub fn into_dfg_builder(self) -> DFGBuilder<B> {
314        self.0
315    }
316}
317
318impl<B: AsMut<Hugr> + AsRef<Hugr>, T> DFGWrapper<B, T> {
319    /// Add a new input to the dataflow being constructed.
320    ///
321    /// Updates the parent's optype to include the new input type, if possible.
322    /// If the container optype is not a [`FuncDefn`] or a [`DFG`], the optype
323    /// will not be updated.
324    ///
325    /// # Returns
326    ///
327    /// - The new wire from the region's input node.
328    ///
329    /// # Panics
330    ///
331    /// Panics if the parent optype cannot be updated to include the new input type.
332    /// See [DFGAddPortError::ParentOpNotSupported] for more details.
333    ///
334    // TODO(breaking): Change return type to Result<Wire, DFGAddPortError>
335    pub fn add_input(&mut self, input_type: Type) -> Wire {
336        self.0
337            .add_input(input_type)
338            .unwrap_or_else(|e| panic!("{e}"))
339    }
340
341    /// Add a new output to the dataflow being constructed.
342    ///
343    /// Updates the parent's optype to include the new output type, if possible.
344    /// If the container optype is not a [`FuncDefn`] or a [`DFG`], the optype
345    /// will not be updated.
346    ///
347    /// # Panics
348    ///
349    /// Panics if the parent optype cannot be updated to include the new input type.
350    /// See [DFGAddPortError::ParentOpNotSupported] for more details
351    ///
352    // TODO(breaking): Change return type to Result<Wire, DFGAddPortError>
353    pub fn add_output(&mut self, output_type: Type) {
354        self.0
355            .add_output(output_type)
356            .unwrap_or_else(|e| panic!("{e}"));
357    }
358}
359
360/// Builder for a [`ops::FuncDefn`] node
361pub type FunctionBuilder<B> = DFGWrapper<B, BuildHandle<FuncID<true>>>;
362
363impl FunctionBuilder<Hugr> {
364    /// Initialize a builder for a [`FuncDefn`]-rooted HUGR; the function will
365    /// be private. (See also [Self::new_vis].)
366    ///
367    /// # Errors
368    ///
369    /// Error in adding DFG child nodes.
370    pub fn new(
371        name: impl Into<String>,
372        signature: impl Into<PolyFuncType>,
373    ) -> Result<Self, BuildError> {
374        Self::new_with_op(FuncDefn::new(name, signature))
375    }
376
377    /// Initialize a builder for a FuncDefn-rooted HUGR, with the specified
378    /// [Visibility].
379    ///
380    /// # Errors
381    ///
382    /// Error in adding DFG child nodes.
383    pub fn new_vis(
384        name: impl Into<String>,
385        signature: impl Into<PolyFuncType>,
386        visibility: Visibility,
387    ) -> Result<Self, BuildError> {
388        Self::new_with_op(FuncDefn::new_vis(name, signature, visibility))
389    }
390
391    fn new_with_op(op: FuncDefn) -> Result<Self, BuildError> {
392        let body = op.signature().body().clone();
393
394        let base = Hugr::new_with_entrypoint(op).expect("FuncDefn entrypoint should be valid");
395        let root = base.entrypoint();
396
397        let db = DFGBuilder::create_with_io(base, root, body)?;
398        Ok(Self::from_dfg_builder(db))
399    }
400}
401
402impl<B: AsMut<Hugr> + AsRef<Hugr>> FunctionBuilder<B> {
403    /// Initialize a new function definition on the root module of an existing HUGR.
404    ///
405    /// The HUGR's entrypoint will **not** be modified.
406    ///
407    /// # Errors
408    ///
409    /// Error in adding DFG child nodes.
410    pub fn with_hugr(
411        mut hugr: B,
412        name: impl Into<String>,
413        signature: impl Into<PolyFuncType>,
414    ) -> Result<Self, BuildError> {
415        let signature: PolyFuncType = signature.into();
416        let body = signature.body().clone();
417        let op = ops::FuncDefn::new(name, signature);
418
419        let module = hugr.as_ref().module_root();
420        let func = hugr.as_mut().add_node_with_parent(module, op);
421
422        let db = DFGBuilder::create_with_io(hugr, func, body)?;
423        Ok(Self::from_dfg_builder(db))
424    }
425}
426
427impl<B: AsMut<Hugr> + AsRef<Hugr>, T> Container for DFGWrapper<B, T> {
428    #[inline]
429    fn container_node(&self) -> Node {
430        self.0.container_node()
431    }
432
433    #[inline]
434    fn hugr_mut(&mut self) -> &mut Hugr {
435        self.0.hugr_mut()
436    }
437
438    #[inline]
439    fn hugr(&self) -> &Hugr {
440        self.0.hugr()
441    }
442}
443
444impl<B: AsMut<Hugr> + AsRef<Hugr>, T> Dataflow for DFGWrapper<B, T> {
445    #[inline]
446    fn num_inputs(&self) -> usize {
447        self.0.num_inputs()
448    }
449}
450
451impl<B: AsMut<Hugr> + AsRef<Hugr>, T: From<BuildHandle<DfgID>>> SubContainer for DFGWrapper<B, T> {
452    type ContainerHandle = T;
453
454    #[inline]
455    fn finish_sub_container(self) -> Result<Self::ContainerHandle, BuildError> {
456        self.0.finish_sub_container().map(Into::into)
457    }
458}
459
460impl<T> HugrBuilder for DFGWrapper<Hugr, T> {
461    fn finish_hugr(self) -> Result<Hugr, ValidationError<Node>> {
462        self.0.finish_hugr()
463    }
464}
465
466#[cfg(test)]
467pub(crate) mod test {
468    use cool_asserts::assert_matches;
469    use ops::OpParent;
470    use rstest::rstest;
471    use serde_json::json;
472
473    use crate::builder::build_traits::DataflowHugr;
474    use crate::builder::{
475        BuilderWiringError, CFGBuilder, DataflowSubContainer, ModuleBuilder, TailLoopBuilder,
476        endo_sig, inout_sig,
477    };
478    use crate::extension::SignatureError;
479    use crate::extension::prelude::Noop;
480    use crate::extension::prelude::{bool_t, qb_t, usize_t};
481    use crate::hugr::validate::InterGraphEdgeError;
482    use crate::ops::{OpTag, handle::NodeHandle};
483    use crate::ops::{OpTrait, Value};
484
485    use crate::std_extensions::logic::test::and_op;
486    use crate::types::type_param::TypeParam;
487    use crate::types::{EdgeKind, FuncValueType, RowVariable, Signature, Type, TypeBound, TypeRV};
488    use crate::utils::test_quantum_extension::h_gate;
489    use crate::{Wire, builder::test::n_identity, type_row};
490
491    use super::super::test::simple_dfg_hugr;
492    use super::*;
493    #[test]
494    fn nested_identity() -> Result<(), BuildError> {
495        let build_result = {
496            let mut outer_builder = DFGBuilder::new(endo_sig(vec![usize_t(), qb_t()]))?;
497
498            let [int, qb] = outer_builder.input_wires_arr();
499
500            let q_out = outer_builder.add_dataflow_op(h_gate(), vec![qb])?;
501
502            let inner_builder = outer_builder.dfg_builder_endo([(usize_t(), int)])?;
503            let inner_id = n_identity(inner_builder)?;
504
505            outer_builder.finish_hugr_with_outputs(inner_id.outputs().chain(q_out.outputs()))
506        };
507
508        assert_eq!(build_result.err(), None);
509
510        Ok(())
511    }
512
513    // Scaffolding for copy insertion tests
514    fn copy_scaffold<F>(f: F, msg: &'static str) -> Result<(), BuildError>
515    where
516        F: FnOnce(&mut DFGBuilder<Hugr>) -> Result<(), BuildError>,
517    {
518        let build_result = {
519            let mut builder = DFGBuilder::new(inout_sig(bool_t(), vec![bool_t(), bool_t()]))?;
520
521            f(&mut builder)?;
522
523            builder.finish_hugr()
524        };
525        assert_matches!(build_result, Ok(_), "Failed on example: {}", msg);
526
527        Ok(())
528    }
529    #[test]
530    fn copy_insertion() -> Result<(), BuildError> {
531        copy_scaffold(
532            |f_build| {
533                let [b1] = f_build.input_wires_arr();
534                f_build.set_outputs([b1, b1])
535            },
536            "Copy input and output",
537        )?;
538
539        copy_scaffold(
540            |f_build| {
541                let [b1] = f_build.input_wires_arr();
542                let xor = f_build.add_dataflow_op(and_op(), [b1, b1])?;
543                f_build.set_outputs([xor.out_wire(0), b1])
544            },
545            "Copy input and use with binary function",
546        )?;
547
548        copy_scaffold(
549            |f_build| {
550                let [b1] = f_build.input_wires_arr();
551                let xor1 = f_build.add_dataflow_op(and_op(), [b1, b1])?;
552                let xor2 = f_build.add_dataflow_op(and_op(), [b1, xor1.out_wire(0)])?;
553                f_build.set_outputs([xor2.out_wire(0), b1])
554            },
555            "Copy multiple times",
556        )?;
557
558        Ok(())
559    }
560
561    #[test]
562    fn copy_insertion_qubit() {
563        let builder = || {
564            let mut module_builder = ModuleBuilder::new();
565
566            let f_build = module_builder
567                .define_function("main", Signature::new(vec![qb_t()], vec![qb_t(), qb_t()]))?;
568
569            let [q1] = f_build.input_wires_arr();
570            f_build.finish_with_outputs([q1, q1])?;
571
572            Ok(module_builder.finish_hugr()?)
573        };
574
575        assert_matches!(
576            builder(),
577            Err(BuildError::OutputWiring {
578                error: BuilderWiringError::NoCopyLinear { typ, .. },
579                ..
580            })
581            if *typ == qb_t()
582        );
583    }
584
585    #[test]
586    fn simple_inter_graph_edge() {
587        let builder = || -> Result<Hugr, BuildError> {
588            let mut f_build =
589                FunctionBuilder::new("main", Signature::new(vec![bool_t()], vec![bool_t()]))?;
590
591            let [i1] = f_build.input_wires_arr();
592            let noop = f_build.add_dataflow_op(Noop(bool_t()), [i1])?;
593            let i1 = noop.out_wire(0);
594
595            let mut nested =
596                f_build.dfg_builder(Signature::new(type_row![], vec![bool_t()]), [])?;
597
598            let id = nested.add_dataflow_op(Noop(bool_t()), [i1])?;
599
600            let nested = nested.finish_with_outputs([id.out_wire(0)])?;
601
602            f_build.finish_hugr_with_outputs([nested.out_wire(0)])
603        };
604
605        assert_matches!(builder(), Ok(_));
606    }
607
608    #[rstest]
609    #[case::function(|sig| FunctionBuilder::new("main", sig).unwrap().into_dfg_builder())]
610    #[case::dfg(|sig| DFGBuilder::new(sig).unwrap())]
611    #[case::df_block(|sig: Signature| {
612        let outs = sig.output.clone();
613        let mut h = CFGBuilder::new(sig).unwrap();
614        let entry_node = h.entry_builder([], outs).unwrap().finish_sub_container().unwrap().node();
615        let hugr = std::mem::take(h.hugr_mut());
616        DFGBuilder::create(hugr, entry_node).unwrap()
617    })]
618    fn add_inputs_outputs(#[case] builder: impl FnOnce(Signature) -> DFGBuilder<Hugr>) {
619        let builder = || -> Result<(Hugr, Node, Signature), BuildError> {
620            let mut dfg = builder(Signature::new(vec![bool_t()], vec![bool_t()]));
621            let dfg_node = dfg.container_node();
622
623            // Initial inner signature, before any inputs or outputs are added
624            let initial_sig = dfg
625                .hugr()
626                .get_optype(dfg_node)
627                .inner_function_type()
628                .unwrap()
629                .into_owned();
630
631            let [i0] = dfg.input_wires_arr();
632            let noop0 = dfg.add_dataflow_op(Noop(bool_t()), [i0])?;
633
634            // Some some order edges
635            dfg.set_order(&dfg.io()[0], &noop0.node());
636            dfg.set_order(&noop0.node(), &dfg.io()[1]);
637
638            // Add a new input and output, and connect them with a noop in between
639            dfg.add_output(qb_t()).unwrap();
640            let i1 = dfg.add_input(qb_t()).unwrap();
641            let noop1 = dfg.add_dataflow_op(Noop(qb_t()), [i1])?;
642
643            // Do not validate the final hugr, as it may have disconnected inputs.
644            dfg.set_outputs([noop0.out_wire(0), noop1.out_wire(0)])?;
645            let hugr = std::mem::take(dfg.hugr_mut());
646            Ok((hugr, dfg_node, initial_sig))
647        };
648
649        let (hugr, dfg_node, initial_sig) = builder().unwrap_or_else(|e| panic!("{e}"));
650
651        let container_sig = hugr.get_optype(dfg_node).inner_function_type().unwrap();
652        let mut expected_sig = initial_sig;
653        expected_sig.input.to_mut().push(qb_t());
654        expected_sig.output.to_mut().push(qb_t());
655        assert_eq!(
656            container_sig.io(),
657            (&expected_sig.input, &expected_sig.output),
658            "Got signature: {container_sig}, expected: {expected_sig}",
659        );
660    }
661
662    #[rstest]
663    #[case::tail_loop(|sig: Signature| TailLoopBuilder::new(sig.input, vec![], sig.output).unwrap().into_dfg_builder())]
664    fn add_inputs_outputs_unsupported(#[case] builder: impl FnOnce(Signature) -> DFGBuilder<Hugr>) {
665        let mut dfg = builder(Signature::new(vec![bool_t()], vec![bool_t()]));
666
667        // Add a new input and output, and connect them with a noop in between
668        assert!(dfg.add_output(qb_t()).is_err());
669        assert!(dfg.add_input(qb_t()).is_err());
670    }
671
672    #[test]
673    fn error_on_linear_inter_graph_edge() -> Result<(), BuildError> {
674        let mut f_build = FunctionBuilder::new("main", Signature::new(vec![qb_t()], vec![qb_t()]))?;
675
676        let [i1] = f_build.input_wires_arr();
677        let noop = f_build.add_dataflow_op(Noop(qb_t()), [i1])?;
678        let i1 = noop.out_wire(0);
679
680        let mut nested = f_build.dfg_builder(Signature::new(type_row![], vec![qb_t()]), [])?;
681
682        let id_res = nested.add_dataflow_op(Noop(qb_t()), [i1]);
683
684        // The error would anyway be caught in validation when we finish the Hugr,
685        // but the builder catches it earlier
686        assert_matches!(
687            id_res.map(|bh| bh.handle().node()), // Transform into something that impl's Debug
688            Err(BuildError::OperationWiring {
689                error: BuilderWiringError::NonCopyableIntergraph { .. },
690                ..
691            })
692        );
693
694        Ok(())
695    }
696
697    #[rstest]
698    fn dfg_hugr(simple_dfg_hugr: Hugr) {
699        assert_eq!(simple_dfg_hugr.num_nodes(), 7);
700        assert_eq!(simple_dfg_hugr.entry_descendants().count(), 3);
701        assert_matches!(simple_dfg_hugr.entrypoint_optype().tag(), OpTag::Dfg);
702    }
703
704    #[test]
705    fn insert_hugr() -> Result<(), BuildError> {
706        // Create a simple DFG
707        let mut dfg_builder = DFGBuilder::new(Signature::new(vec![bool_t()], vec![bool_t()]))?;
708        let [i1] = dfg_builder.input_wires_arr();
709        dfg_builder.set_metadata("x", 42);
710        let dfg_hugr = dfg_builder.finish_hugr_with_outputs([i1])?;
711
712        // Create a module, and insert the DFG into it
713        let mut module_builder = ModuleBuilder::new();
714
715        let (dfg_node, f_node) = {
716            let mut f_build =
717                module_builder.define_function("main", Signature::new_endo(bool_t()))?;
718
719            let [i1] = f_build.input_wires_arr();
720            let dfg = f_build.add_hugr_with_wires(dfg_hugr, [i1])?;
721            let f = f_build.finish_with_outputs([dfg.out_wire(0)])?;
722            module_builder.set_child_metadata(f.node(), "x", "hi");
723            (dfg.node(), f.node())
724        };
725
726        let hugr = module_builder.finish_hugr()?;
727        assert_eq!(hugr.entry_descendants().count(), 7);
728
729        assert_eq!(hugr.get_metadata(hugr.entrypoint(), "x"), None);
730        assert_eq!(hugr.get_metadata(dfg_node, "x").cloned(), Some(json!(42)));
731        assert_eq!(hugr.get_metadata(f_node, "x").cloned(), Some(json!("hi")));
732
733        Ok(())
734    }
735
736    #[test]
737    fn barrier_node() -> Result<(), BuildError> {
738        let mut parent = DFGBuilder::new(endo_sig(bool_t()))?;
739
740        let [w] = parent.input_wires_arr();
741
742        let mut dfg_b = parent.dfg_builder(endo_sig(bool_t()), [w])?;
743        let [w] = dfg_b.input_wires_arr();
744
745        let barr0 = dfg_b.add_barrier([w])?;
746        let [w] = barr0.outputs_arr();
747
748        let barr1 = dfg_b.add_barrier([w])?;
749        let [w] = barr1.outputs_arr();
750
751        let dfg = dfg_b.finish_with_outputs([w])?;
752        let [w] = dfg.outputs_arr();
753
754        let mut dfg2_b = parent.dfg_builder(endo_sig(vec![bool_t(), bool_t()]), [w, w])?;
755        let [w1, w2] = dfg2_b.input_wires_arr();
756        let barr2 = dfg2_b.add_barrier([w1, w2])?;
757        let wires: Vec<Wire> = barr2.outputs().collect();
758
759        let dfg2 = dfg2_b.finish_with_outputs(wires)?;
760        let [w, _] = dfg2.outputs_arr();
761        parent.finish_hugr_with_outputs([w])?;
762
763        Ok(())
764    }
765
766    #[test]
767    fn non_cfg_ancestor() -> Result<(), BuildError> {
768        let unit_sig = Signature::new(type_row![Type::UNIT], type_row![Type::UNIT]);
769        let mut b = DFGBuilder::new(unit_sig.clone())?;
770        let b_child = b.dfg_builder(unit_sig.clone(), [b.input().out_wire(0)])?;
771        let b_child_in_wire = b_child.input().out_wire(0);
772        b_child.finish_with_outputs([])?;
773        let b_child_2 = b.dfg_builder(unit_sig.clone(), [])?;
774
775        // DFG block has edge coming a sibling block, which is only valid for
776        // CFGs
777        let b_child_2_handle = b_child_2.finish_with_outputs([b_child_in_wire])?;
778
779        let res = b.finish_hugr_with_outputs([b_child_2_handle.out_wire(0)]);
780
781        assert_matches!(
782            res,
783            Err(BuildError::InvalidHUGR(
784                ValidationError::InterGraphEdgeError(InterGraphEdgeError::NonCFGAncestor { .. })
785            ))
786        );
787        Ok(())
788    }
789
790    #[test]
791    fn no_relation_edge() -> Result<(), BuildError> {
792        let unit_sig = Signature::new(type_row![Type::UNIT], type_row![Type::UNIT]);
793        let mut b = DFGBuilder::new(unit_sig.clone())?;
794        let mut b_child = b.dfg_builder(unit_sig.clone(), [b.input().out_wire(0)])?;
795        let b_child_child = b_child.dfg_builder(unit_sig.clone(), [b_child.input().out_wire(0)])?;
796        let b_child_child_in_wire = b_child_child.input().out_wire(0);
797
798        b_child_child.finish_with_outputs([])?;
799        b_child.finish_with_outputs([])?;
800
801        let mut b_child_2 = b.dfg_builder(unit_sig.clone(), [])?;
802        let b_child_2_child =
803            b_child_2.dfg_builder(unit_sig.clone(), [b_child_2.input().out_wire(0)])?;
804
805        let res = b_child_2_child.finish_with_outputs([b_child_child_in_wire]);
806
807        assert_matches!(
808            res.map(|h| h.handle().node()), // map to something that implements Debug
809            Err(BuildError::OutputWiring {
810                error: BuilderWiringError::NoRelationIntergraph { .. },
811                ..
812            })
813        );
814        Ok(())
815    }
816
817    #[test]
818    fn no_outer_row_variables() -> Result<(), BuildError> {
819        let e = crate::hugr::validate::test::extension_with_eval_parallel();
820        let tv = TypeRV::new_row_var_use(0, TypeBound::Copyable);
821        // Can *declare* a function that takes a function-value of unknown #args
822        FunctionBuilder::new(
823            "bad_eval",
824            PolyFuncType::new(
825                [TypeParam::new_list_type(TypeBound::Copyable)],
826                Signature::new(
827                    Type::new_function(FuncValueType::new(usize_t(), tv.clone())),
828                    vec![],
829                ),
830            ),
831        )?;
832
833        // But cannot eval it...
834        let ev = e.instantiate_extension_op(
835            "eval",
836            [vec![usize_t().into()].into(), vec![tv.into()].into()],
837        );
838        assert_eq!(
839            ev,
840            Err(SignatureError::RowVarWhereTypeExpected {
841                var: RowVariable(0, TypeBound::Copyable)
842            })
843        );
844        Ok(())
845    }
846
847    #[test]
848    fn order_edges() {
849        let (mut hugr, load_constant, call) = {
850            let mut builder = ModuleBuilder::new();
851            let func = builder
852                .declare("func", Signature::new_endo(bool_t()).into())
853                .unwrap();
854            let (load_constant, call) = {
855                let mut builder = builder
856                    .define_function("main", Signature::new(Type::EMPTY_TYPEROW, bool_t()))
857                    .unwrap();
858                let load_constant = builder.add_load_value(Value::true_val());
859                let [r] = builder
860                    .call(&func, &[], [load_constant])
861                    .unwrap()
862                    .outputs_arr();
863                builder.finish_with_outputs([r]).unwrap();
864                (load_constant.node(), r.node())
865            };
866            (builder.finish_hugr().unwrap(), load_constant, call)
867        };
868
869        let lc_optype = hugr.get_optype(load_constant);
870        let call_optype = hugr.get_optype(call);
871        assert_eq!(EdgeKind::StateOrder, lc_optype.other_input().unwrap());
872        assert_eq!(EdgeKind::StateOrder, lc_optype.other_output().unwrap());
873        assert_eq!(EdgeKind::StateOrder, call_optype.other_input().unwrap());
874        assert_eq!(EdgeKind::StateOrder, call_optype.other_output().unwrap());
875
876        hugr.connect(
877            load_constant,
878            lc_optype.other_output_port().unwrap(),
879            call,
880            call_optype.other_input_port().unwrap(),
881        );
882
883        hugr.validate().unwrap();
884    }
885}