From add6d3612ef6508dd3c7e91fe63674c113da84f3 Mon Sep 17 00:00:00 2001 From: Ben Howe <141149032+bmhowe23@users.noreply.github.com> Date: Tue, 12 Sep 2023 12:29:10 -0500 Subject: [PATCH] Unset noise after noise tests (#635) This helps fix issue #609 by unsetting noise models at the end of noise tests. Without this change, the prior test's noise model could be used in subsequent tests, which is undesirable. --- .github/workflows/config/spelling_allowlist.txt | 3 +++ docs/sphinx/examples/cpp/basics/noise_modeling.cpp | 4 ++++ docs/sphinx/using/advanced/_noise.rst | 5 ++++- runtime/cudaq.h | 3 ++- unittests/integration/noise_tester.cpp | 9 +++++++++ 5 files changed, 22 insertions(+), 2 deletions(-) diff --git a/.github/workflows/config/spelling_allowlist.txt b/.github/workflows/config/spelling_allowlist.txt index 54c92d0581..e9d42e48c1 100644 --- a/.github/workflows/config/spelling_allowlist.txt +++ b/.github/workflows/config/spelling_allowlist.txt @@ -5,6 +5,7 @@ BFGS CLA CMake COBYLA +CPTP CPU CPUs CUDA @@ -25,6 +26,7 @@ JIT JSON Kraus LLVM +LSB MLIR MPI Max-Cut @@ -107,6 +109,7 @@ instantiations namespace namespaces natively +nullary optimizer optimizers parallelization diff --git a/docs/sphinx/examples/cpp/basics/noise_modeling.cpp b/docs/sphinx/examples/cpp/basics/noise_modeling.cpp index 42ab1e6bea..3d11a47827 100644 --- a/docs/sphinx/examples/cpp/basics/noise_modeling.cpp +++ b/docs/sphinx/examples/cpp/basics/noise_modeling.cpp @@ -49,4 +49,8 @@ int main() { // Run the noisy simulation counts = cudaq::sample(xgate); counts.dump(); + + // Unset the noise model when done. This is not necessary in this case but is + // good practice in order to not interfere with future simulations. + cudaq::unset_noise(); } diff --git a/docs/sphinx/using/advanced/_noise.rst b/docs/sphinx/using/advanced/_noise.rst index 2ac9c7061c..7cda1d7a21 100644 --- a/docs/sphinx/using/advanced/_noise.rst +++ b/docs/sphinx/using/advanced/_noise.rst @@ -47,7 +47,10 @@ apply to the simulated state via a :code:`noise_model::get_channel(...)` call. Noise models can be constructed via the :code:`cudaq::noise_model` and specified for execution via a public :code:`cudaq::set_noise(cudaq::noise_model&)` function. This function should forward the :code:`noise_model` to the current :code:`quantum_platform` which can attach it -to the current :code:`ExecutionContext` and retrieved by backend simulators. +to the current :code:`ExecutionContext` and retrieved by backend simulators. The +:code:`noise_model` must stay in scope in order to be successfully used by the +backend simulators, and you must call :code:`cudaq::unset_noise()` when you are +done with the noise model. The :code:`kraus_op` matrix data assumes a LSB qubit ordering. diff --git a/runtime/cudaq.h b/runtime/cudaq.h index e1566d8f02..877ba3c561 100644 --- a/runtime/cudaq.h +++ b/runtime/cudaq.h @@ -182,7 +182,8 @@ void set_target_backend(const char *backend); /// @brief Utility function for setting the shots on the platform void set_shots(const std::size_t nShots); -/// @brief Set a custom noise model for simulation +/// @brief Set a custom noise model for simulation. The caller must also call +/// `cudaq::unset_noise` before `model` gets deallocated or goes out of scope. void set_noise(cudaq::noise_model &model); /// @brief Remove an existing noise model from simulation. diff --git a/unittests/integration/noise_tester.cpp b/unittests/integration/noise_tester.cpp index bf4ee3dd15..cecc764efc 100644 --- a/unittests/integration/noise_tester.cpp +++ b/unittests/integration/noise_tester.cpp @@ -78,6 +78,7 @@ CUDAQ_TEST(NoiseTest, checkAmplitudeDamping) { EXPECT_NEAR(counts.probability("0"), .25, .1); EXPECT_NEAR(counts.probability("1"), .75, .1); + cudaq::unset_noise(); // clear for subsequent tests } CUDAQ_TEST(NoiseTest, checkCNOT) { @@ -154,6 +155,7 @@ CUDAQ_TEST(NoiseTest, checkCNOT) { auto counts = cudaq::sample(10000, bell{}); counts.dump(); EXPECT_TRUE(counts.size() > 2); + cudaq::unset_noise(); // clear for subsequent tests } CUDAQ_TEST(NoiseTest, checkExceptions) { @@ -174,6 +176,7 @@ CUDAQ_TEST(NoiseTest, checkDepolType) { auto counts = cudaq::sample(xOp{}); counts.dump(); EXPECT_EQ(2, counts.size()); + cudaq::unset_noise(); // clear for subsequent tests } CUDAQ_TEST(NoiseTest, checkDepolTypeSimple) { @@ -187,6 +190,7 @@ CUDAQ_TEST(NoiseTest, checkDepolTypeSimple) { EXPECT_EQ(2, counts.size()); EXPECT_NEAR(counts.probability("0"), .50, .2); EXPECT_NEAR(counts.probability("1"), .50, .2); + cudaq::unset_noise(); // clear for subsequent tests } CUDAQ_TEST(NoiseTest, checkAmpDampType) { @@ -199,6 +203,7 @@ CUDAQ_TEST(NoiseTest, checkAmpDampType) { EXPECT_EQ(2, counts.size()); EXPECT_NEAR(counts.probability("0"), .25, .1); EXPECT_NEAR(counts.probability("1"), .75, .1); + cudaq::unset_noise(); // clear for subsequent tests } CUDAQ_TEST(NoiseTest, checkAmpDampTypeSimple) { @@ -210,6 +215,7 @@ CUDAQ_TEST(NoiseTest, checkAmpDampTypeSimple) { counts.dump(); EXPECT_EQ(1, counts.size()); EXPECT_NEAR(counts.probability("0"), 1., .1); + cudaq::unset_noise(); // clear for subsequent tests } CUDAQ_TEST(NoiseTest, checkBitFlipType) { @@ -222,6 +228,7 @@ CUDAQ_TEST(NoiseTest, checkBitFlipType) { EXPECT_EQ(2, counts.size()); EXPECT_NEAR(counts.probability("0"), .1, .1); EXPECT_NEAR(counts.probability("1"), .9, .1); + cudaq::unset_noise(); // clear for subsequent tests } CUDAQ_TEST(NoiseTest, checkBitFlipTypeSimple) { @@ -233,6 +240,7 @@ CUDAQ_TEST(NoiseTest, checkBitFlipTypeSimple) { counts.dump(); EXPECT_EQ(1, counts.size()); EXPECT_NEAR(counts.probability("0"), 1., .1); + cudaq::unset_noise(); // clear for subsequent tests } CUDAQ_TEST(NoiseTest, checkPhaseFlipType) { @@ -253,6 +261,7 @@ CUDAQ_TEST(NoiseTest, checkPhaseFlipType) { counts.dump(); EXPECT_EQ(1, counts.size()); EXPECT_NEAR(counts.probability("0"), 1., .1); + cudaq::unset_noise(); // clear for subsequent tests } #endif