From 874f8f5fb5f601b0299ff15c9219f6824980b19a Mon Sep 17 00:00:00 2001 From: "Stefan J. Wernli" Date: Tue, 2 Jun 2026 11:38:24 -0700 Subject: [PATCH 1/2] Ising samples should be part of automatic checks in unit tests Fixes #3284 --- source/samples_test/build.rs | 14 +++++++------ source/samples_test/src/tests.rs | 7 +++++-- .../src/tests/algorithms_Ising.rs | 20 +++++++++++++++++++ 3 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 source/samples_test/src/tests/algorithms_Ising.rs diff --git a/source/samples_test/build.rs b/source/samples_test/build.rs index 6a58da872b..f97a760578 100644 --- a/source/samples_test/build.rs +++ b/source/samples_test/build.rs @@ -10,6 +10,7 @@ use std::{ fn main() { create_tests_for_files("algorithms"); + create_tests_for_files("algorithms/Ising"); create_tests_for_files("getting_started"); create_tests_for_files("language"); create_tests_for_files_compile_only("estimation"); @@ -22,8 +23,9 @@ fn create_tests_for_files(folder: &str) { // Iterate through the folder and create a test for each qs file let mut paths = read_dir(format!("../../samples/{folder}")).expect("folder should exist and be readable"); + let folder_name = folder.replace('/', "_"); let out_dir = "./src/tests"; - let dest_path = Path::new(&out_dir).join(format!("{folder}_generated.rs")); + let dest_path = Path::new(&out_dir).join(format!("{folder_name}_generated.rs")); let mut f = File::create(dest_path).expect("files should be creatable in ./src/tests"); writeln!( @@ -35,7 +37,7 @@ fn create_tests_for_files(folder: &str) { //! This build-generated module contains tests for the samples in the `/samples/{folder}` folder. //! DO NOT MANUALLY EDIT THIS FILE. To regenerate this file, run `cargo check` or `cargo test` in the `samples_test` directory. -use super::{folder}::*; +use super::{folder_name}::*; use super::{{compile_and_run, compile_and_run_debug, circuit, qirgen}}; use qsc::SourceMap;"#, ) @@ -77,7 +79,7 @@ fn {file_stem}_src() -> SourceMap {{ #[test] fn run_{file_stem}() {{ let output = compile_and_run({file_stem}_src()); - // This constant must be defined in `samples_test/src/tests/{folder}.rs` and + // This constant must be defined in `samples_test/src/tests/{folder_name}.rs` and // must contain the output of the sample {file_name} {file_stem_upper}_EXPECT.assert_eq(&output); }} @@ -86,7 +88,7 @@ fn run_{file_stem}() {{ #[test] fn debug_{file_stem}() {{ let output = compile_and_run_debug({file_stem}_src()); - // This constant must be defined in `samples_test/src/tests/{folder}.rs` and + // This constant must be defined in `samples_test/src/tests/{folder_name}.rs` and // must contain the output of the sample {file_name} {file_stem_upper}_EXPECT_DEBUG.assert_eq(&output); }} @@ -95,7 +97,7 @@ fn debug_{file_stem}() {{ #[test] fn circuit_{file_stem}() {{ let circuit = circuit({file_stem}_src()); - // This constant must be defined in `samples_test/src/tests/{folder}.rs` and + // This constant must be defined in `samples_test/src/tests/{folder_name}.rs` and // must contain the circuit for the sample {file_name} {file_stem_upper}_EXPECT_CIRCUIT.assert_eq(&circuit); }} @@ -104,7 +106,7 @@ fn circuit_{file_stem}() {{ #[test] fn qirgen_{file_stem}() {{ let qir = qirgen({file_stem}_src()); - // This constant must be defined in `samples_test/src/tests/{folder}.rs` and + // This constant must be defined in `samples_test/src/tests/{folder_name}.rs` and // must contain the QIR for the sample {file_name} {file_stem_upper}_EXPECT_QIR.assert_eq(&qir); }}"# diff --git a/source/samples_test/src/tests.rs b/source/samples_test/src/tests.rs index a35cc64da6..1feaf1a978 100644 --- a/source/samples_test/src/tests.rs +++ b/source/samples_test/src/tests.rs @@ -1,9 +1,14 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#![allow(non_snake_case)] + mod algorithms; #[rustfmt::skip] mod algorithms_generated; +mod algorithms_Ising; +#[rustfmt::skip] +mod algorithms_Ising_generated; mod getting_started; #[rustfmt::skip] mod getting_started_generated; @@ -14,9 +19,7 @@ mod language; mod language_generated; #[rustfmt::skip] mod project_generated; -#[allow(non_snake_case)] mod OpenQASM; -#[allow(non_snake_case)] #[rustfmt::skip] mod OpenQASM_generated; diff --git a/source/samples_test/src/tests/algorithms_Ising.rs b/source/samples_test/src/tests/algorithms_Ising.rs new file mode 100644 index 0000000000..cc2efe9d5c --- /dev/null +++ b/source/samples_test/src/tests/algorithms_Ising.rs @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +use expect_test::{Expect, expect}; + +// Each file in the samples/algorithms/Ising folder is compiled and run as two tests and should +// have matching expect strings in this file. If new samples are added, this file will +// fail to compile until the new expect strings are added. +pub const SIMPLE2DISINGORDER2_EXPECT: Expect = expect!["[Zero, Zero, Zero, One, One, Zero, One, Zero, One]"]; +pub const SIMPLE2DISINGORDER2_EXPECT_DEBUG: Expect = expect!["[Zero, Zero, Zero, One, One, Zero, One, Zero, One]"]; +pub const SIMPLE2DISINGORDER2_EXPECT_CIRCUIT: Expect = expect!["generated circuit of length 24940"]; +pub const SIMPLE2DISINGORDER2_EXPECT_QIR: Expect = expect!["generated QIR of length 20849"]; +pub const SIMPLE1DISINGORDER1_EXPECT: Expect = expect!["[Zero, Zero, Zero, One, One, Zero, Zero, Zero, Zero]"]; +pub const SIMPLE1DISINGORDER1_EXPECT_DEBUG: Expect = expect!["[Zero, Zero, Zero, One, One, Zero, Zero, Zero, Zero]"]; +pub const SIMPLE1DISINGORDER1_EXPECT_CIRCUIT: Expect = expect!["generated circuit of length 12317"]; +pub const SIMPLE1DISINGORDER1_EXPECT_QIR: Expect = expect!["generated QIR of length 18408"]; +pub const SIMPLE2DISINGORDER1_EXPECT: Expect = expect!["[Zero, Zero, Zero, One, One, Zero, One, One, Zero]"]; +pub const SIMPLE2DISINGORDER1_EXPECT_DEBUG: Expect = expect!["[Zero, Zero, Zero, One, One, Zero, One, One, Zero]"]; +pub const SIMPLE2DISINGORDER1_EXPECT_CIRCUIT: Expect = expect!["generated circuit of length 24085"]; +pub const SIMPLE2DISINGORDER1_EXPECT_QIR: Expect = expect!["generated QIR of length 16214"]; From 3882bbc5cbeb8733d909fe5026dfb5f8ab5e071d Mon Sep 17 00:00:00 2001 From: "Stefan J. Wernli" Date: Tue, 2 Jun 2026 11:40:33 -0700 Subject: [PATCH 2/2] fix formatting --- .../samples_test/src/tests/algorithms_Ising.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/source/samples_test/src/tests/algorithms_Ising.rs b/source/samples_test/src/tests/algorithms_Ising.rs index cc2efe9d5c..9b78f7dede 100644 --- a/source/samples_test/src/tests/algorithms_Ising.rs +++ b/source/samples_test/src/tests/algorithms_Ising.rs @@ -6,15 +6,21 @@ use expect_test::{Expect, expect}; // Each file in the samples/algorithms/Ising folder is compiled and run as two tests and should // have matching expect strings in this file. If new samples are added, this file will // fail to compile until the new expect strings are added. -pub const SIMPLE2DISINGORDER2_EXPECT: Expect = expect!["[Zero, Zero, Zero, One, One, Zero, One, Zero, One]"]; -pub const SIMPLE2DISINGORDER2_EXPECT_DEBUG: Expect = expect!["[Zero, Zero, Zero, One, One, Zero, One, Zero, One]"]; +pub const SIMPLE2DISINGORDER2_EXPECT: Expect = + expect!["[Zero, Zero, Zero, One, One, Zero, One, Zero, One]"]; +pub const SIMPLE2DISINGORDER2_EXPECT_DEBUG: Expect = + expect!["[Zero, Zero, Zero, One, One, Zero, One, Zero, One]"]; pub const SIMPLE2DISINGORDER2_EXPECT_CIRCUIT: Expect = expect!["generated circuit of length 24940"]; pub const SIMPLE2DISINGORDER2_EXPECT_QIR: Expect = expect!["generated QIR of length 20849"]; -pub const SIMPLE1DISINGORDER1_EXPECT: Expect = expect!["[Zero, Zero, Zero, One, One, Zero, Zero, Zero, Zero]"]; -pub const SIMPLE1DISINGORDER1_EXPECT_DEBUG: Expect = expect!["[Zero, Zero, Zero, One, One, Zero, Zero, Zero, Zero]"]; +pub const SIMPLE1DISINGORDER1_EXPECT: Expect = + expect!["[Zero, Zero, Zero, One, One, Zero, Zero, Zero, Zero]"]; +pub const SIMPLE1DISINGORDER1_EXPECT_DEBUG: Expect = + expect!["[Zero, Zero, Zero, One, One, Zero, Zero, Zero, Zero]"]; pub const SIMPLE1DISINGORDER1_EXPECT_CIRCUIT: Expect = expect!["generated circuit of length 12317"]; pub const SIMPLE1DISINGORDER1_EXPECT_QIR: Expect = expect!["generated QIR of length 18408"]; -pub const SIMPLE2DISINGORDER1_EXPECT: Expect = expect!["[Zero, Zero, Zero, One, One, Zero, One, One, Zero]"]; -pub const SIMPLE2DISINGORDER1_EXPECT_DEBUG: Expect = expect!["[Zero, Zero, Zero, One, One, Zero, One, One, Zero]"]; +pub const SIMPLE2DISINGORDER1_EXPECT: Expect = + expect!["[Zero, Zero, Zero, One, One, Zero, One, One, Zero]"]; +pub const SIMPLE2DISINGORDER1_EXPECT_DEBUG: Expect = + expect!["[Zero, Zero, Zero, One, One, Zero, One, One, Zero]"]; pub const SIMPLE2DISINGORDER1_EXPECT_CIRCUIT: Expect = expect!["generated circuit of length 24085"]; pub const SIMPLE2DISINGORDER1_EXPECT_QIR: Expect = expect!["generated QIR of length 16214"];