Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Cleaner error on wiring errors while building #873

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions quantinuum-hugr/src/algorithm/const_fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn const_graph(consts: Vec<Const>, reg: &ExtensionRegistry) -> Hugr {

let outputs = consts
.into_iter()
.map(|c| b.add_load_const(c).unwrap())
.map(|c| b.add_load_const(c))
.collect_vec();

b.finish_hugr_with_outputs(outputs, reg).unwrap()
Expand Down Expand Up @@ -265,9 +265,7 @@ mod test {
let mut build =
DFGBuilder::new(FunctionType::new(type_row![], vec![sum_type.clone()])).unwrap();

let tup = build
.add_load_const(Const::new_tuple([f2c(5.6), f2c(3.2)]))
.unwrap();
let tup = build.add_load_const(Const::new_tuple([f2c(5.6), f2c(3.2)]));

let unpack = build
.add_dataflow_op(
Expand Down Expand Up @@ -320,7 +318,7 @@ mod test {
) -> Result<(), Box<dyn std::error::Error>> {
let mut build = DFGBuilder::new(FunctionType::new(type_row![], vec![BOOL_T])).unwrap();

let ins = ins.map(|b| build.add_load_const(Const::from_bool(b)).unwrap());
let ins = ins.map(|b| build.add_load_const(Const::from_bool(b)));
let logic_op = build.add_dataflow_op(op.with_n_inputs(ins.len() as u64), ins)?;

let reg =
Expand Down
14 changes: 7 additions & 7 deletions quantinuum-hugr/src/algorithm/nest_cfgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,8 @@ pub(crate) mod test {
// \-> right -/ \-<--<-/
let mut cfg_builder = CFGBuilder::new(FunctionType::new(type_row![NAT], type_row![NAT]))?;

let pred_const = cfg_builder.add_constant(Const::unit_sum(0, 2))?; // Nothing here cares which
let const_unit = cfg_builder.add_constant(Const::unary_unit_sum())?;
let pred_const = cfg_builder.add_constant(Const::unit_sum(0, 2)); // Nothing here cares which
let const_unit = cfg_builder.add_constant(Const::unary_unit_sum());

let entry = n_identity(
cfg_builder.simple_entry_builder(type_row![NAT], 1, ExtensionSet::new())?,
Expand Down Expand Up @@ -813,7 +813,7 @@ pub(crate) mod test {
pred_const: &ConstID,
) -> Result<T::ContainerHandle, BuildError> {
let w = dataflow_builder.input_wires();
let u = dataflow_builder.load_const(pred_const)?;
let u = dataflow_builder.load_const(pred_const);
dataflow_builder.finish_with_outputs([u].into_iter().chain(w))
}

Expand Down Expand Up @@ -887,8 +887,8 @@ pub(crate) mod test {
separate: bool,
) -> Result<(Hugr, BasicBlockID, BasicBlockID), BuildError> {
let mut cfg_builder = CFGBuilder::new(FunctionType::new(type_row![NAT], type_row![NAT]))?;
let pred_const = cfg_builder.add_constant(Const::unit_sum(0, 2))?; // Nothing here cares which
let const_unit = cfg_builder.add_constant(Const::unary_unit_sum())?;
let pred_const = cfg_builder.add_constant(Const::unit_sum(0, 2)); // Nothing here cares which
let const_unit = cfg_builder.add_constant(Const::unary_unit_sum());

let entry = n_identity(
cfg_builder.simple_entry_builder(type_row![NAT], 2, ExtensionSet::new())?,
Expand Down Expand Up @@ -929,8 +929,8 @@ pub(crate) mod test {
cfg_builder: &mut CFGBuilder<T>,
separate_headers: bool,
) -> Result<(BasicBlockID, BasicBlockID), BuildError> {
let pred_const = cfg_builder.add_constant(Const::unit_sum(0, 2))?; // Nothing here cares which
let const_unit = cfg_builder.add_constant(Const::unary_unit_sum())?;
let pred_const = cfg_builder.add_constant(Const::unit_sum(0, 2)); // Nothing here cares which
let const_unit = cfg_builder.add_constant(Const::unary_unit_sum());

let entry = n_identity(
cfg_builder.simple_entry_builder(type_row![NAT], 1, ExtensionSet::new())?,
Expand Down
57 changes: 52 additions & 5 deletions quantinuum-hugr/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ use thiserror::Error;
use crate::extension::SignatureError;
use crate::hugr::ValidationError;
use crate::ops::handle::{BasicBlockID, CfgID, ConditionalID, DfgID, FuncID, TailLoopID};
use crate::ops::{OpName, OpType};
use crate::types::ConstTypeError;
use crate::types::Type;
use crate::{Node, Wire};
use crate::{Node, Port, Wire};

pub mod handle;
pub use handle::BuildHandle;
Expand Down Expand Up @@ -122,6 +123,7 @@ mod circuit;
pub use circuit::{CircuitBuildError, CircuitBuilder};

#[derive(Debug, Clone, PartialEq, Error)]
#[non_exhaustive]
/// Error while building the HUGR.
pub enum BuildError {
/// The constructed HUGR is invalid.
Expand Down Expand Up @@ -155,13 +157,58 @@ pub enum BuildError {
#[error("Wire not found in Hugr: {0:?}.")]
WireNotFound(Wire),

/// Can't copy a linear type
#[error("Can't copy linear type: {0:?}.")]
NoCopyLinear(Type),

/// Error in CircuitBuilder
#[error("Error in CircuitBuilder: {0}.")]
CircuitError(#[from] circuit::CircuitBuildError),

/// Invalid wires when setting outputs
#[error("Found an error while setting the outputs of a {} container, {container_node}. {error}", .container_op.name())]
OutputWiring {
container_op: OpType,
container_node: Node,
#[source]
error: BuilderWiringError,
},

/// Invalid input wires to a new operation
///
/// The internal error message already contains the node index.
#[error("Got an input wire while adding a {} to the circuit. {error}", .op.name())]
OperationWiring {
op: OpType,
#[source]
error: BuilderWiringError,
},
}

#[derive(Debug, Clone, PartialEq, Error)]
#[non_exhaustive]
/// Error raised when wiring up a node during the build process.
pub enum BuilderWiringError {
/// Tried to copy a linear type.
#[error("Cannot copy linear type {typ} from output {src_offset} of node {src}")]
NoCopyLinear {
typ: Type,
src: Node,
src_offset: Port,
},
/// The ancestors of an inter-graph edge are not related.
#[error("Cannot connect an inter-graph edge between unrelated nodes. Tried connecting {src} ({src_offset}) with {dst} ({dst_offset}).")]
NoRelationIntergraph {
src: Node,
src_offset: Port,
dst: Node,
dst_offset: Port,
},
/// Inter-Graph edges can only carry copyable data.
#[error("Inter-graph edges cannot carry non-copyable data {typ}. Tried connecting {src} ({src_offset}) with {dst} ({dst_offset}).")]
NonCopyableIntergraph {
src: Node,
src_offset: Port,
dst: Node,
dst_offset: Port,
typ: Type,
},
}

#[cfg(test)]
Expand Down
Loading