Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Binary file modified test_files/guppy_optimization/angles/angles.flat.hugr
Binary file not shown.
2 changes: 1 addition & 1 deletion test_files/guppy_optimization/angles/angles.flat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "guppylang >=0.21.3",
# "guppylang >=0.21.6",
# ]
# ///

Expand Down
Binary file modified test_files/guppy_optimization/angles/angles.hugr
Binary file not shown.
Binary file modified test_files/guppy_optimization/angles/angles.opt.hugr
Binary file not shown.
2 changes: 1 addition & 1 deletion test_files/guppy_optimization/angles/angles.opt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "guppylang >=0.21.3",
# "guppylang >=0.21.6",
# ]
# ///

Expand Down
2 changes: 1 addition & 1 deletion test_files/guppy_optimization/angles/angles.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "guppylang >=0.21.3",
# "guppylang >=0.21.6",
# ]
# ///

Expand Down
Binary file modified test_files/guppy_optimization/false_branch/false_branch.hugr
Binary file not shown.
Binary file modified test_files/guppy_optimization/false_branch/false_branch.opt.hugr
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "guppylang >=0.21.3",
# "guppylang >=0.21.6",
# ]
# ///

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "guppylang >=0.21.3",
# "guppylang >=0.21.6",
# ]
# ///

Expand Down
Binary file modified test_files/guppy_optimization/nested/nested.flat.hugr
Binary file not shown.
2 changes: 1 addition & 1 deletion test_files/guppy_optimization/nested/nested.flat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "guppylang >=0.21.3",
# "guppylang >=0.21.6",
# ]
# ///

Expand Down
Binary file modified test_files/guppy_optimization/nested/nested.hugr
Binary file not shown.
Binary file modified test_files/guppy_optimization/nested/nested.opt.hugr
Binary file not shown.
2 changes: 1 addition & 1 deletion test_files/guppy_optimization/nested/nested.opt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "guppylang >=0.21.3",
# "guppylang >=0.21.6",
# ]
# ///

Expand Down
2 changes: 1 addition & 1 deletion test_files/guppy_optimization/nested/nested.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "guppylang >=0.21.3",
# "guppylang >=0.21.6",
# ]
# ///

Expand Down
Binary file modified test_files/guppy_optimization/ranges/ranges.flat.hugr
Binary file not shown.
2 changes: 1 addition & 1 deletion test_files/guppy_optimization/ranges/ranges.flat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "guppylang >=0.21.3",
# "guppylang >=0.21.6",
# ]
# ///

Expand Down
Binary file modified test_files/guppy_optimization/ranges/ranges.hugr
Binary file not shown.
Binary file modified test_files/guppy_optimization/ranges/ranges.opt.hugr
Binary file not shown.
2 changes: 1 addition & 1 deletion test_files/guppy_optimization/ranges/ranges.opt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "guppylang >=0.21.3",
# "guppylang >=0.21.6",
# ]
# ///

Expand Down
2 changes: 1 addition & 1 deletion test_files/guppy_optimization/ranges/ranges.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "guppylang >=0.21.3",
# "guppylang >=0.21.6",
# ]
# ///

Expand Down
Binary file modified test_files/guppy_optimization/simple_cx/simple_cx.hugr
Binary file not shown.
Binary file modified test_files/guppy_optimization/simple_cx/simple_cx.opt.hugr
Binary file not shown.
2 changes: 1 addition & 1 deletion test_files/guppy_optimization/simple_cx/simple_cx.opt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "guppylang >=0.21.3",
# "guppylang >=0.21.6",
# ]
# ///

Expand Down
2 changes: 1 addition & 1 deletion test_files/guppy_optimization/simple_cx/simple_cx.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "guppylang >=0.21.3",
# "guppylang >=0.21.6",
# ]
# ///

Expand Down
42 changes: 36 additions & 6 deletions tket-qsystem/tests/guppy_opt.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//! Tests optimizing Guppy-generated programs.

use smol_str::SmolStr;
use std::collections::HashMap;
use std::fs;
use std::io::BufReader;
use std::path::Path;
use tket::extension::{TKET1_EXTENSION_ID, TKET_EXTENSION_ID};

use hugr::algorithms::ComposablePass;
use hugr::{Hugr, HugrView};
Expand Down Expand Up @@ -44,19 +47,46 @@ fn guppy_simple_cx() -> Hugr {
load_guppy_circuit("simple_cx")
}

fn count_gates(h: &impl HugrView) -> HashMap<SmolStr, usize> {
let mut counts = HashMap::new();
for n in h.nodes() {
if let Some(eop) = h.get_optype(n).as_extension_op() {
if [TKET_EXTENSION_ID, TKET1_EXTENSION_ID].contains(eop.extension_id()) {
*counts.entry(eop.qualified_id()).or_default() += 1;
}
}
}
counts
}

/// Run some simple optimization passes on the guppy-generated HUGRs and validate the result.
///
/// This test is intended to check the current status of the Guppy optimization passes.
///
#[rstest]
#[case::angles(guppy_angles())]
#[case::false_branch(guppy_false_branch())]
#[case::nested(guppy_nested())]
#[case::ranges(guppy_ranges())]
#[case::simple_cx(guppy_simple_cx())]
#[case::angles(guppy_angles(), [
("tket.quantum.H", 2), ("tket.quantum.QAlloc", 1), ("tket.quantum.Rz", 2), ("tket.quantum.MeasureFree", 1)
])]
#[case::false_branch(guppy_false_branch(), [
("tket.quantum.H", 2), ("tket.quantum.QAlloc", 1), ("tket.quantum.MeasureFree", 1)
])]
#[case::nested(guppy_nested(), [
("tket.quantum.CZ", 1), ("tket.quantum.H", 2), ("tket.quantum.QAlloc", 3), ("tket.quantum.MeasureFree", 3)
])]
#[case::ranges(guppy_ranges(), [
("tket.quantum.QAlloc", 4), ("tket.quantum.MeasureFree", 4), ("tket.quantum.H", 2), ("tket.quantum.CX", 2)
])]
#[case::simple_cx(guppy_simple_cx(), [
("tket.quantum.QAlloc", 2), ("tket.quantum.CX", 2), ("tket.quantum.MeasureFree", 2)
])]
#[cfg_attr(miri, ignore)] // Opening files is not supported in (isolated) miri
fn optimise_guppy(#[case] mut hugr: Hugr) {
fn optimise_guppy<'a>(
#[case] mut hugr: Hugr,
#[case] before: impl IntoIterator<Item = (&'a str, usize)>,
) {
NormalizeGuppy::default().run(&mut hugr).unwrap();
let before = before.into_iter().map(|(k, v)| (k.into(), v)).collect();
assert_eq!(count_gates(&hugr), before);

// TODO: Run pytket passes here, and check that the circuit is as optimized as possible at this point.
//
Expand Down
Loading