Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unset noise after noise tests #635

Merged
merged 2 commits into from
Sep 12, 2023
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
3 changes: 3 additions & 0 deletions .github/workflows/config/spelling_allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ BFGS
CLA
CMake
COBYLA
CPTP
CPU
CPUs
CUDA
Expand All @@ -25,6 +26,7 @@ JIT
JSON
Kraus
LLVM
LSB
MLIR
MPI
Max-Cut
Expand Down Expand Up @@ -107,6 +109,7 @@ instantiations
namespace
namespaces
natively
nullary
optimizer
optimizers
parallelization
Expand Down
4 changes: 4 additions & 0 deletions docs/sphinx/examples/cpp/basics/noise_modeling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
5 changes: 4 additions & 1 deletion docs/sphinx/using/advanced/_noise.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 2 additions & 1 deletion runtime/cudaq.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions unittests/integration/noise_tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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