Skip to content

Commit

Permalink
Remove static_input from AbstractSignature (#429)
Browse files Browse the repository at this point in the history
Closes #419
Need to add some logic to report the port for the static input if it
exists.

Matches existing templated logic for "other_input" etc.

Boilerplate methods on Signature that ignore static_input can therefore
be removed.

---------

Co-authored-by: Agustín Borgna <[email protected]>
  • Loading branch information
ss2165 and aborgna-q authored Aug 21, 2023
1 parent d5ccbd4 commit 652bba1
Show file tree
Hide file tree
Showing 22 changed files with 161 additions and 245 deletions.
5 changes: 2 additions & 3 deletions specification/hugr.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,8 @@ the node). Incoming ports are associated with exactly one edge. All edges associ
with a port have the same type; thus a port has a well-defined type, matching that
of its adjoining edges. The incoming and outgoing ports of a node are (separately) ordered.

The sequences of incoming and outgoing port types of a node constitute its
_signature_. This signature may include the types of both `Value` and `Static`
edges, with `Static` edges following `Value` edges in the ordering.
The sequences of incoming and outgoing port types (carried on `Value` edges) of a node constitute its
_signature_.

Note that the locality is not fixed or even specified by the signature.

Expand Down
4 changes: 2 additions & 2 deletions src/builder/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> BlockBuilder<B> {
let predicate_type = Type::new_predicate(predicate_variants);
let mut node_outputs = vec![predicate_type];
node_outputs.extend_from_slice(&other_outputs);
let signature = AbstractSignature::new_df(inputs, TypeRow::from(node_outputs));
let signature = AbstractSignature::new(inputs, TypeRow::from(node_outputs));
let db = DFGBuilder::create_with_io(base, block_n, signature, None)?;
Ok(BlockBuilder::from_dfg_builder(db))
}
Expand Down Expand Up @@ -307,7 +307,7 @@ mod test {
let mut module_builder = ModuleBuilder::new();
let mut func_builder = module_builder.define_function(
"main",
AbstractSignature::new_df(vec![NAT], type_row![NAT]).pure(),
AbstractSignature::new(vec![NAT], type_row![NAT]).pure(),
)?;
let _f_id = {
let [int] = func_builder.input_wires_arr();
Expand Down
6 changes: 3 additions & 3 deletions src/builder/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ mod test {
#[test]
fn simple_linear() {
let build_res = build_main(
AbstractSignature::new_df(type_row![QB, QB], type_row![QB, QB]).pure(),
AbstractSignature::new(type_row![QB, QB], type_row![QB, QB]).pure(),
|mut f_build| {
let wires = f_build.input_wires().collect();

Expand Down Expand Up @@ -181,12 +181,12 @@ mod test {
"MyOp",
"unknown op".to_string(),
vec![],
Some(AbstractSignature::new(vec![QB, NAT], vec![QB], vec![])),
Some(AbstractSignature::new(vec![QB, NAT], vec![QB])),
))
.into(),
);
let build_res = build_main(
AbstractSignature::new_df(type_row![QB, QB, NAT], type_row![QB, QB, BOOL_T]).pure(),
AbstractSignature::new(type_row![QB, QB, NAT], type_row![QB, QB, BOOL_T]).pure(),
|mut f_build| {
let [q0, q1, angle]: [Wire; 3] = f_build.input_wires_arr();

Expand Down
8 changes: 4 additions & 4 deletions src/builder/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> ConditionalBuilder<B> {

let outputs = cond.outputs;
let case_op = ops::Case {
signature: AbstractSignature::new_df(inputs.clone(), outputs.clone()),
signature: AbstractSignature::new(inputs.clone(), outputs.clone()),
};
let case_node =
// add case before any existing subsequent cases
Expand All @@ -134,7 +134,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> ConditionalBuilder<B> {
let dfg_builder = DFGBuilder::create_with_io(
self.hugr_mut(),
case_node,
AbstractSignature::new_df(inputs, outputs),
AbstractSignature::new(inputs, outputs),
None,
)?;

Expand Down Expand Up @@ -186,7 +186,7 @@ impl CaseBuilder<Hugr> {
pub fn new(input: impl Into<TypeRow>, output: impl Into<TypeRow>) -> Result<Self, BuildError> {
let input = input.into();
let output = output.into();
let signature = AbstractSignature::new_df(input, output);
let signature = AbstractSignature::new(input, output);
let op = ops::Case {
signature: signature.clone(),
};
Expand Down Expand Up @@ -232,7 +232,7 @@ mod test {
let mut module_builder = ModuleBuilder::new();
let mut fbuild = module_builder.define_function(
"main",
AbstractSignature::new_df(type_row![NAT], type_row![NAT]).pure(),
AbstractSignature::new(type_row![NAT], type_row![NAT]).pure(),
)?;
let tru_const = fbuild.add_constant(Const::true_val())?;
let _fdef = {
Expand Down
34 changes: 15 additions & 19 deletions src/builder/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,15 @@ mod test {
let _f_id = {
let mut func_builder = module_builder.define_function(
"main",
AbstractSignature::new_df(type_row![NAT, QB], type_row![NAT, QB]).pure(),
AbstractSignature::new(type_row![NAT, QB], type_row![NAT, QB]).pure(),
)?;

let [int, qb] = func_builder.input_wires_arr();

let q_out = func_builder.add_dataflow_op(h_gate(), vec![qb])?;

let inner_builder = func_builder.dfg_builder(
AbstractSignature::new_df(type_row![NAT], type_row![NAT]),
AbstractSignature::new(type_row![NAT], type_row![NAT]),
// TODO: This should be None
Some(ExtensionSet::new()),
[int],
Expand All @@ -277,7 +277,7 @@ mod test {

let f_build = module_builder.define_function(
"main",
AbstractSignature::new_df(type_row![BOOL_T], type_row![BOOL_T, BOOL_T]).pure(),
AbstractSignature::new(type_row![BOOL_T], type_row![BOOL_T, BOOL_T]).pure(),
)?;

f(f_build)?;
Expand Down Expand Up @@ -327,7 +327,7 @@ mod test {

let f_build = module_builder.define_function(
"main",
AbstractSignature::new_df(type_row![QB], type_row![QB, QB]).pure(),
AbstractSignature::new(type_row![QB], type_row![QB, QB]).pure(),
)?;

let [q1] = f_build.input_wires_arr();
Expand All @@ -344,15 +344,15 @@ mod test {
let builder = || -> Result<Hugr, BuildError> {
let mut f_build = FunctionBuilder::new(
"main",
AbstractSignature::new_df(type_row![BIT], type_row![BIT]).pure(),
AbstractSignature::new(type_row![BIT], type_row![BIT]).pure(),
)?;

let [i1] = f_build.input_wires_arr();
let noop = f_build.add_dataflow_op(LeafOp::Noop { ty: BIT }, [i1])?;
let i1 = noop.out_wire(0);

let mut nested = f_build.dfg_builder(
AbstractSignature::new_df(type_row![], type_row![BIT]),
AbstractSignature::new(type_row![], type_row![BIT]),
None,
[],
)?;
Expand All @@ -371,18 +371,15 @@ mod test {
fn error_on_linear_inter_graph_edge() -> Result<(), BuildError> {
let mut f_build = FunctionBuilder::new(
"main",
AbstractSignature::new_df(type_row![QB], type_row![QB]).pure(),
AbstractSignature::new(type_row![QB], type_row![QB]).pure(),
)?;

let [i1] = f_build.input_wires_arr();
let noop = f_build.add_dataflow_op(LeafOp::Noop { ty: QB }, [i1])?;
let i1 = noop.out_wire(0);

let mut nested = f_build.dfg_builder(
AbstractSignature::new_df(type_row![], type_row![QB]),
None,
[],
)?;
let mut nested =
f_build.dfg_builder(AbstractSignature::new(type_row![], type_row![QB]), None, [])?;

let id_res = nested.add_dataflow_op(LeafOp::Noop { ty: QB }, [i1]);

Expand All @@ -400,8 +397,7 @@ mod test {

#[test]
fn dfg_hugr() -> Result<(), BuildError> {
let dfg_builder =
DFGBuilder::new(AbstractSignature::new_df(type_row![BIT], type_row![BIT]))?;
let dfg_builder = DFGBuilder::new(AbstractSignature::new(type_row![BIT], type_row![BIT]))?;

let [i1] = dfg_builder.input_wires_arr();
let hugr = dfg_builder.finish_hugr_with_outputs([i1])?;
Expand All @@ -416,7 +412,7 @@ mod test {
fn insert_hugr() -> Result<(), BuildError> {
// Create a simple DFG
let mut dfg_builder =
DFGBuilder::new(AbstractSignature::new_df(type_row![BIT], type_row![BIT]))?;
DFGBuilder::new(AbstractSignature::new(type_row![BIT], type_row![BIT]))?;
let [i1] = dfg_builder.input_wires_arr();
dfg_builder.set_metadata(json!(42));
let dfg_hugr = dfg_builder.finish_hugr_with_outputs([i1])?;
Expand All @@ -427,7 +423,7 @@ mod test {
{
let mut f_build = module_builder.define_function(
"main",
AbstractSignature::new_df(type_row![BIT], type_row![BIT]).pure(),
AbstractSignature::new(type_row![BIT], type_row![BIT]).pure(),
)?;

let [i1] = f_build.input_wires_arr();
Expand All @@ -448,20 +444,20 @@ mod test {
let c_extensions = ExtensionSet::singleton(&"C".into());
let abc_extensions = ab_extensions.clone().union(&c_extensions);

let parent_sig = AbstractSignature::new_df(type_row![BIT], type_row![BIT])
let parent_sig = AbstractSignature::new(type_row![BIT], type_row![BIT])
.with_extension_delta(&abc_extensions);
let mut parent = module_builder.define_function(
"parent",
parent_sig.with_input_extensions(ExtensionSet::new()),
)?;

let add_c_sig = AbstractSignature::new_df(type_row![BIT], type_row![BIT])
let add_c_sig = AbstractSignature::new(type_row![BIT], type_row![BIT])
.with_extension_delta(&c_extensions)
.with_input_extensions(ab_extensions.clone());

let [w] = parent.input_wires_arr();

let add_ab_sig = AbstractSignature::new_df(type_row![BIT], type_row![BIT])
let add_ab_sig = AbstractSignature::new(type_row![BIT], type_row![BIT])
.with_extension_delta(&ab_extensions);

// A box which adds extensions A and B, via child Lift nodes
Expand Down
8 changes: 4 additions & 4 deletions src/builder/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ mod test {

let f_id = module_builder.declare(
"main",
AbstractSignature::new_df(type_row![NAT], type_row![NAT]).pure(),
AbstractSignature::new(type_row![NAT], type_row![NAT]).pure(),
)?;

let mut f_build = module_builder.define_declaration(&f_id)?;
Expand All @@ -211,7 +211,7 @@ mod test {

let f_build = module_builder.define_function(
"main",
AbstractSignature::new_df(
AbstractSignature::new(
vec![qubit_state_type.get_alias_type()],
vec![qubit_state_type.get_alias_type()],
)
Expand All @@ -231,11 +231,11 @@ mod test {

let mut f_build = module_builder.define_function(
"main",
AbstractSignature::new_df(type_row![NAT], type_row![NAT]).pure(),
AbstractSignature::new(type_row![NAT], type_row![NAT]).pure(),
)?;
let local_build = f_build.define_function(
"local",
AbstractSignature::new_df(type_row![NAT], type_row![NAT]).pure(),
AbstractSignature::new(type_row![NAT], type_row![NAT]).pure(),
)?;
let [wire] = local_build.input_wires_arr();
let f_id = local_build.finish_with_outputs([wire])?;
Expand Down
4 changes: 2 additions & 2 deletions src/builder/tail_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> TailLoopBuilder<B> {
tail_loop: &ops::TailLoop,
) -> Result<Self, BuildError> {
let signature =
AbstractSignature::new_df(tail_loop.body_input_row(), tail_loop.body_output_row());
AbstractSignature::new(tail_loop.body_input_row(), tail_loop.body_output_row());
let dfg_build = DFGBuilder::create_with_io(base, loop_node, signature, None)?;

Ok(TailLoopBuilder::from_dfg_builder(dfg_build))
Expand Down Expand Up @@ -127,7 +127,7 @@ mod test {
let mut module_builder = ModuleBuilder::new();
let mut fbuild = module_builder.define_function(
"main",
AbstractSignature::new_df(type_row![BIT], type_row![NAT]).pure(),
AbstractSignature::new(type_row![BIT], type_row![NAT]).pure(),
)?;
let _fdef = {
let [b1] = fbuild.input_wires_arr();
Expand Down
2 changes: 1 addition & 1 deletion src/extension/op_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl OpDef {
// TODO bring this assert back once resource inference is done?
// https://github.com/CQCL-DEV/hugr/issues/425
// assert!(res.contains(self.extension()));
Ok(AbstractSignature::new_df(ins, outs).with_extension_delta(&res))
Ok(AbstractSignature::new(ins, outs).with_extension_delta(&res))
}

/// Optional description of the ports in the signature.
Expand Down
2 changes: 1 addition & 1 deletion src/extension/type_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ mod test {
};
let typ = Type::new_extension(
def.instantiate_concrete(vec![TypeArg::Type(Type::new_graph(
AbstractSignature::new_df(vec![], vec![]),
AbstractSignature::new(vec![], vec![]),
))])
.unwrap(),
);
Expand Down
2 changes: 1 addition & 1 deletion src/hugr/hugrmut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ mod test {
module,
ops::FuncDefn {
name: "main".into(),
signature: AbstractSignature::new_df(type_row![NAT], type_row![NAT, NAT]),
signature: AbstractSignature::new(type_row![NAT], type_row![NAT, NAT]),
},
)
.expect("Failed to add function definition node");
Expand Down
34 changes: 9 additions & 25 deletions src/hugr/rewrite/simple_replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,6 @@ impl Rewrite for SimpleReplacement {
.collect::<Vec<Node>>();
// slice of nodes omitting Input and Output:
let replacement_inner_nodes = &replacement_nodes[2..];
for &node in replacement_inner_nodes {
// Check there are no const inputs.
if !self
.replacement
.get_optype(node)
.signature()
.static_input()
.is_empty()
{
return Err(SimpleReplacementError::InvalidReplacementNode());
}
}
let self_output_node = h.children(self.parent).nth(1).unwrap();
let replacement_output_node = *replacement_nodes.get(1).unwrap();
for &node in replacement_inner_nodes {
Expand Down Expand Up @@ -237,15 +225,15 @@ mod test {
let _f_id = {
let mut func_builder = module_builder.define_function(
"main",
AbstractSignature::new_df(type_row![QB, QB, QB], type_row![QB, QB, QB]).pure(),
AbstractSignature::new(type_row![QB, QB, QB], type_row![QB, QB, QB]).pure(),
)?;

let [qb0, qb1, qb2] = func_builder.input_wires_arr();

let q_out = func_builder.add_dataflow_op(h_gate(), vec![qb2])?;

let mut inner_builder = func_builder.dfg_builder(
AbstractSignature::new_df(type_row![QB, QB], type_row![QB, QB]),
AbstractSignature::new(type_row![QB, QB], type_row![QB, QB]),
None,
[qb0, qb1],
)?;
Expand Down Expand Up @@ -273,10 +261,8 @@ mod test {
/// ┤ H ├┤ X ├
/// └───┘└───┘
fn make_dfg_hugr() -> Result<Hugr, BuildError> {
let mut dfg_builder = DFGBuilder::new(AbstractSignature::new_df(
type_row![QB, QB],
type_row![QB, QB],
))?;
let mut dfg_builder =
DFGBuilder::new(AbstractSignature::new(type_row![QB, QB], type_row![QB, QB]))?;
let [wire0, wire1] = dfg_builder.input_wires_arr();
let wire2 = dfg_builder.add_dataflow_op(h_gate(), vec![wire0])?;
let wire3 = dfg_builder.add_dataflow_op(h_gate(), vec![wire1])?;
Expand All @@ -291,10 +277,8 @@ mod test {
/// ┤ H ├
/// └───┘
fn make_dfg_hugr2() -> Result<Hugr, BuildError> {
let mut dfg_builder = DFGBuilder::new(AbstractSignature::new_df(
type_row![QB, QB],
type_row![QB, QB],
))?;
let mut dfg_builder =
DFGBuilder::new(AbstractSignature::new(type_row![QB, QB], type_row![QB, QB]))?;
let [wire0, wire1] = dfg_builder.input_wires_arr();
let wire2 = dfg_builder.add_dataflow_op(h_gate(), vec![wire1])?;
let wire2out = wire2.outputs().exactly_one().unwrap();
Expand Down Expand Up @@ -469,7 +453,7 @@ mod test {
#[test]
fn test_replace_cx_cross() {
let q_row: Vec<Type> = vec![QB, QB];
let mut builder = DFGBuilder::new(AbstractSignature::new_df(q_row.clone(), q_row)).unwrap();
let mut builder = DFGBuilder::new(AbstractSignature::new(q_row.clone(), q_row)).unwrap();
let mut circ = builder.as_circuit(builder.input_wires().collect());
circ.append(cx_gate(), [0, 1]).unwrap();
circ.append(cx_gate(), [1, 0]).unwrap();
Expand Down Expand Up @@ -516,7 +500,7 @@ mod test {
let two_bit = type_row![BOOL_T, BOOL_T];

let mut builder =
DFGBuilder::new(AbstractSignature::new_df(one_bit.clone(), one_bit.clone())).unwrap();
DFGBuilder::new(AbstractSignature::new(one_bit.clone(), one_bit.clone())).unwrap();
let inw = builder.input_wires().exactly_one().unwrap();
let outw = builder
.add_dataflow_op(and_op(), [inw, inw])
Expand All @@ -525,7 +509,7 @@ mod test {
let [input, _] = builder.io();
let mut h = builder.finish_hugr_with_outputs(outw).unwrap();

let mut builder = DFGBuilder::new(AbstractSignature::new_df(two_bit, one_bit)).unwrap();
let mut builder = DFGBuilder::new(AbstractSignature::new(two_bit, one_bit)).unwrap();
let inw = builder.input_wires();
let outw = builder.add_dataflow_op(and_op(), inw).unwrap().outputs();
let [repl_input, repl_output] = builder.io();
Expand Down
Loading

0 comments on commit 652bba1

Please sign in to comment.