Skip to content

Commit

Permalink
extract_subgraph and FunctionBuilder::new take PolyFuncType not Signa…
Browse files Browse the repository at this point in the history
…ture
  • Loading branch information
acl-cqc committed Nov 13, 2023
1 parent 6c0edbb commit 3078259
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
21 changes: 8 additions & 13 deletions src/builder/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::marker::PhantomData;
use crate::hugr::{HugrView, NodeType, ValidationError};
use crate::ops;

use crate::types::{FunctionType, Signature};
use crate::types::{FunctionType, PolyFuncType};

use crate::extension::{ExtensionRegistry, ExtensionSet};
use crate::Node;
Expand Down Expand Up @@ -146,22 +146,17 @@ impl FunctionBuilder<Hugr> {
/// # Errors
///
/// Error in adding DFG child nodes.
pub fn new(name: impl Into<String>, signature: Signature) -> Result<Self, BuildError> {
// ALAN this looks dodgy too
pub fn new(name: impl Into<String>, signature: PolyFuncType) -> Result<Self, BuildError> {
let body = signature.body.clone();
let op = ops::FuncDefn {
signature: signature.signature.clone().into(),
signature,
name: name.into(),
};

let base = Hugr::new(NodeType::new(op, signature.input_extensions.clone()));
let base = Hugr::new(NodeType::new_pure(op));
let root = base.root();

let db = DFGBuilder::create_with_io(
base,
root,
signature.signature,
Some(signature.input_extensions),
)?;
let db = DFGBuilder::create_with_io(base, root, body, Some(ExtensionSet::new()))?;
Ok(Self::from_dfg_builder(db))
}
}
Expand Down Expand Up @@ -341,7 +336,7 @@ pub(crate) mod test {
let builder = || -> Result<Hugr, BuildError> {
let mut f_build = FunctionBuilder::new(
"main",
FunctionType::new(type_row![BIT], type_row![BIT]).pure(),
FunctionType::new(type_row![BIT], type_row![BIT]).into(),
)?;

let [i1] = f_build.input_wires_arr();
Expand All @@ -365,7 +360,7 @@ pub(crate) mod test {
fn error_on_linear_inter_graph_edge() -> Result<(), BuildError> {
let mut f_build = FunctionBuilder::new(
"main",
FunctionType::new(type_row![QB], type_row![QB]).pure(),
FunctionType::new(type_row![QB], type_row![QB]).into(),
)?;

let [i1] = f_build.input_wires_arr();
Expand Down
15 changes: 4 additions & 11 deletions src/hugr/views/sibling_subgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ use portgraph::{view::Subgraph, Direction, PortView};
use thiserror::Error;

use crate::builder::{Container, FunctionBuilder};
use crate::extension::ExtensionSet;
use crate::hugr::{HugrError, HugrMut, HugrView, RootTagged};
use crate::ops::handle::{ContainerHandle, DataflowOpID};
use crate::ops::{OpTag, OpTrait};
use crate::types::Signature;
use crate::types::{FunctionType, Type};
use crate::{Hugr, IncomingPort, Node, OutgoingPort, Port, SimpleReplacement};

Expand Down Expand Up @@ -400,19 +398,14 @@ impl SiblingSubgraph {

/// Create a new Hugr containing only the subgraph.
///
/// The new Hugr will contain a function root wth the same signature as the
/// subgraph and the specified `input_extensions`.
/// The new Hugr will contain a [FuncDefn] root wth the same signature as the
/// subgraph and the specified `name`
pub fn extract_subgraph(
&self,
hugr: &impl HugrView,
name: impl Into<String>,
input_extensions: ExtensionSet,
) -> Result<Hugr, HugrError> {
let signature = Signature {
signature: self.signature(hugr),
input_extensions,
};
let mut builder = FunctionBuilder::new(name, signature).unwrap();
let mut builder = FunctionBuilder::new(name, self.signature(hugr).into()).unwrap();
// Take the unfinished Hugr from the builder, to avoid unnecessary
// validation checks that require connecting the inputs and outputs.
let mut extracted = mem::take(builder.hugr_mut());
Expand Down Expand Up @@ -971,7 +964,7 @@ mod tests {
let func_graph: SiblingGraph<'_, FuncID<true>> =
SiblingGraph::try_new(&hugr, func_root).unwrap();
let subgraph = SiblingSubgraph::try_new_dataflow_subgraph(&func_graph).unwrap();
let extracted = subgraph.extract_subgraph(&hugr, "region", ExtensionSet::new())?;
let extracted = subgraph.extract_subgraph(&hugr, "region")?;

extracted.validate(&PRELUDE_REGISTRY).unwrap();

Expand Down

0 comments on commit 3078259

Please sign in to comment.