Skip to content

Commit

Permalink
test: Failing test for the simple_replace bug
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Jun 12, 2024
1 parent ea5213d commit 6dbdbb0
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions hugr-core/src/hugr/rewrite/simple_replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,11 @@ pub(in crate::hugr::rewrite) mod test {
use crate::hugr::views::{HugrView, SiblingSubgraph};
use crate::hugr::{Hugr, HugrMut, Rewrite};
use crate::ops::dataflow::DataflowOpTrait;
use crate::ops::handle::NodeHandle;
use crate::ops::OpTag;
use crate::ops::OpTrait;
use crate::std_extensions::logic::test::and_op;
use crate::std_extensions::logic::NotOp;
use crate::type_row;
use crate::types::{FunctionType, Type};
use crate::utils::test_quantum_extension::{cx_gate, h_gate};
Expand Down Expand Up @@ -309,6 +311,43 @@ pub(in crate::hugr::rewrite) mod test {
make_dfg_hugr2().unwrap()
}

/// A hugr with a DFG root mapping BOOL_T to (BOOL_T, BOOL_T)
/// ┌─────────┐
/// ┌────┤ (1) NOT ├──
/// ┌─────────┐ │ └─────────┘
/// ─┤ (0) NOT ├───┤
/// └─────────┘ │ ┌─────────┐
/// └────┤ (2) NOT ├──
/// └─────────┘
/// This can be replaced with an empty hugr coping the input to both outputs.
///
/// Returns the hugr and the nodes of the NOT gates, in order.
#[fixture]
pub(in crate::hugr::rewrite) fn dfg_hugr_copy_bools() -> (Hugr, Vec<Node>) {
fn build() -> Result<(Hugr, Vec<Node>), BuildError> {
let mut dfg_builder = DFGBuilder::new(FunctionType::new(
type_row![BOOL_T],
type_row![BOOL_T, BOOL_T],
))?;
let [b] = dfg_builder.input_wires_arr();

let not_inp = dfg_builder.add_dataflow_op(NotOp, vec![b])?;
let [b] = not_inp.outputs_arr();

let not_0 = dfg_builder.add_dataflow_op(NotOp, vec![b])?;
let [b0] = not_0.outputs_arr();
let not_1 = dfg_builder.add_dataflow_op(NotOp, vec![b])?;
let [b1] = not_1.outputs_arr();

Ok((
dfg_builder.finish_prelude_hugr_with_outputs([b0, b1])?,
vec![not_inp.node(), not_0.node(), not_1.node()],
))
}

build().unwrap()
}

#[rstest]
/// Replace the
/// ┌───┐
Expand Down Expand Up @@ -572,6 +611,66 @@ pub(in crate::hugr::rewrite) mod test {
assert_eq!(h.node_count(), orig.node_count());
}

#[rstest]
fn test_copy_inputs(
dfg_hugr_copy_bools: (Hugr, Vec<Node>),
) -> Result<(), Box<dyn std::error::Error>> {
let (mut hugr, nodes) = dfg_hugr_copy_bools;
let (input_not, output_not_0, output_not_1) = nodes.into_iter().collect_tuple().unwrap();

println!("{}", hugr.mermaid_string());

let [_input, output] = hugr.get_io(hugr.root()).unwrap();

let replacement = {
let b = DFGBuilder::new(FunctionType::new(
type_row![BOOL_T],
type_row![BOOL_T, BOOL_T],
))?;
let [w] = b.input_wires_arr();
b.finish_prelude_hugr_with_outputs([w, w])?
};
let [_repl_input, repl_output] = replacement.get_io(replacement.root()).unwrap();

let subgraph =
SiblingSubgraph::try_from_nodes(vec![input_not, output_not_0, output_not_1], &hugr)?;
// A map from (target ports of edges from the Input node of `replacement`) to (target ports of
// edges from nodes not in `removal` to nodes in `removal`).
let nu_inp = [
(
(repl_output, IncomingPort::from(0)),
(input_not, IncomingPort::from(0)),
),
(
(repl_output, IncomingPort::from(1)),
(input_not, IncomingPort::from(1)),
),
]
.into_iter()
.collect();
// A map from (target ports of edges from nodes in `removal` to nodes not in `removal`) to
// (input ports of the Output node of `replacement`).
let nu_out = [
((output, IncomingPort::from(0)), IncomingPort::from(0)),
((output, IncomingPort::from(1)), IncomingPort::from(1)),
]
.into_iter()
.collect();

let rewrite = SimpleReplacement {
subgraph,
replacement,
nu_inp,
nu_out,
};
rewrite.apply(&mut hugr).unwrap_or_else(|e| panic!("{e}"));

assert_eq!(hugr.update_validate(&PRELUDE_REGISTRY), Ok(()));
assert_eq!(hugr.node_count(), 3);

Ok(())
}

use crate::hugr::rewrite::replace::Replacement;
fn to_replace(h: &impl HugrView, s: SimpleReplacement) -> Replacement {
use crate::hugr::rewrite::replace::{NewEdgeKind, NewEdgeSpec};
Expand Down

0 comments on commit 6dbdbb0

Please sign in to comment.