From accb4fa7a62c8df9d1e00b8586a7d5970bf662d5 Mon Sep 17 00:00:00 2001 From: Ben Howe Date: Mon, 9 Sep 2024 14:52:58 +0000 Subject: [PATCH 1/3] Add enumerated noise_model_type to noise interface --- runtime/common/NoiseModel.cpp | 6 +++++- runtime/common/NoiseModel.h | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/runtime/common/NoiseModel.cpp b/runtime/common/NoiseModel.cpp index 5cb37cfaf8..f04664e8ba 100644 --- a/runtime/common/NoiseModel.cpp +++ b/runtime/common/NoiseModel.cpp @@ -82,7 +82,9 @@ kraus_channel::kraus_channel(std::vector &_ops) : ops(_ops) { validateCompleteness(); } -kraus_channel::kraus_channel(const kraus_channel &other) : ops(other.ops) {} +kraus_channel::kraus_channel(const kraus_channel &other) + : ops(other.ops), noise_type(other.noise_type), + probability(other.probability) {} std::size_t kraus_channel::size() const { return ops.size(); } @@ -94,6 +96,8 @@ kraus_op &kraus_channel::operator[](const std::size_t idx) { return ops[idx]; } kraus_channel &kraus_channel::operator=(const kraus_channel &other) { ops = other.ops; + noise_type = other.noise_type; + probability = other.probability; return *this; } diff --git a/runtime/common/NoiseModel.h b/runtime/common/NoiseModel.h index 4823134faf..be28b1b26c 100644 --- a/runtime/common/NoiseModel.h +++ b/runtime/common/NoiseModel.h @@ -18,6 +18,17 @@ namespace cudaq { +/// @brief Noise model enumerated type that allows downstream simulators of +/// `kraus_channel` objects to apply simulator-specific logic for well-known +/// noise models. +enum class noise_model_type { + unknown, + depolarization_channel, + amplitude_damping_channel, + bit_flip_channel, + phase_flip_channel +}; + /// @brief A kraus_op represents a single Kraus operation, /// described as a complex matrix of specific size. The matrix /// is represented here as a 1d array (specifically a std::vector). @@ -106,6 +117,12 @@ class kraus_channel { } public: + /// @brief Noise type enumeration + noise_model_type noise_type = noise_model_type::unknown; + + /// @brief Noise probability + real probability = 0.0; + ~kraus_channel() = default; /// @brief The nullary constructor @@ -252,6 +269,8 @@ class depolarization_channel : public kraus_channel { k3v{std::sqrt(probability / three), 0, 0, negOne * std::sqrt(probability / three)}; ops = {k0v, k1v, k2v, k3v}; + this->probability = probability; + noise_type = noise_model_type::depolarization_channel; validateCompleteness(); } }; @@ -265,6 +284,8 @@ class amplitude_damping_channel : public kraus_channel { std::vector k0v{1, 0, 0, std::sqrt(1 - probability)}, k1v{0, std::sqrt(probability), 0, 0}; ops = {k0v, k1v}; + this->probability = probability; + noise_type = noise_model_type::amplitude_damping_channel; validateCompleteness(); } }; @@ -279,6 +300,8 @@ class bit_flip_channel : public kraus_channel { std::sqrt(1 - probability)}, k1v{0, std::sqrt(probability), std::sqrt(probability), 0}; ops = {k0v, k1v}; + this->probability = probability; + noise_type = noise_model_type::bit_flip_channel; validateCompleteness(); } }; @@ -294,6 +317,8 @@ class phase_flip_channel : public kraus_channel { std::sqrt(1 - probability)}, k1v{std::sqrt(probability), 0, 0, negOne * std::sqrt(probability)}; ops = {k0v, k1v}; + this->probability = probability; + noise_type = noise_model_type::phase_flip_channel; validateCompleteness(); } }; From 5e9c91f2d4c680dc0997708474187ff2680b1eb8 Mon Sep 17 00:00:00 2001 From: Ben Howe Date: Mon, 7 Oct 2024 15:27:38 +0000 Subject: [PATCH 2/3] Fix docs generation issue --- docs/sphinx/api/languages/cpp_api.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/sphinx/api/languages/cpp_api.rst b/docs/sphinx/api/languages/cpp_api.rst index 8504c493da..84cc081f93 100644 --- a/docs/sphinx/api/languages/cpp_api.rst +++ b/docs/sphinx/api/languages/cpp_api.rst @@ -115,6 +115,8 @@ Noise Modeling .. doxygenclass:: cudaq::noise_model :members: +.. doxygenenum:: cudaq::noise_model_type + Kernel Builder =============== From aa7982853436848f389697dd18412810a5758076 Mon Sep 17 00:00:00 2001 From: Ben Howe Date: Mon, 7 Oct 2024 15:35:29 +0000 Subject: [PATCH 3/3] Address PR comments --- runtime/common/NoiseModel.cpp | 4 ++-- runtime/common/NoiseModel.h | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/runtime/common/NoiseModel.cpp b/runtime/common/NoiseModel.cpp index f04664e8ba..4f75011484 100644 --- a/runtime/common/NoiseModel.cpp +++ b/runtime/common/NoiseModel.cpp @@ -84,7 +84,7 @@ kraus_channel::kraus_channel(std::vector &_ops) : ops(_ops) { kraus_channel::kraus_channel(const kraus_channel &other) : ops(other.ops), noise_type(other.noise_type), - probability(other.probability) {} + parameters(other.parameters) {} std::size_t kraus_channel::size() const { return ops.size(); } @@ -97,7 +97,7 @@ kraus_op &kraus_channel::operator[](const std::size_t idx) { return ops[idx]; } kraus_channel &kraus_channel::operator=(const kraus_channel &other) { ops = other.ops; noise_type = other.noise_type; - probability = other.probability; + parameters = other.parameters; return *this; } diff --git a/runtime/common/NoiseModel.h b/runtime/common/NoiseModel.h index be28b1b26c..4f2c74013e 100644 --- a/runtime/common/NoiseModel.h +++ b/runtime/common/NoiseModel.h @@ -120,8 +120,8 @@ class kraus_channel { /// @brief Noise type enumeration noise_model_type noise_type = noise_model_type::unknown; - /// @brief Noise probability - real probability = 0.0; + /// @brief Noise parameter values + std::vector parameters; ~kraus_channel() = default; @@ -269,7 +269,7 @@ class depolarization_channel : public kraus_channel { k3v{std::sqrt(probability / three), 0, 0, negOne * std::sqrt(probability / three)}; ops = {k0v, k1v, k2v, k3v}; - this->probability = probability; + this->parameters.push_back(probability); noise_type = noise_model_type::depolarization_channel; validateCompleteness(); } @@ -284,7 +284,7 @@ class amplitude_damping_channel : public kraus_channel { std::vector k0v{1, 0, 0, std::sqrt(1 - probability)}, k1v{0, std::sqrt(probability), 0, 0}; ops = {k0v, k1v}; - this->probability = probability; + this->parameters.push_back(probability); noise_type = noise_model_type::amplitude_damping_channel; validateCompleteness(); } @@ -300,7 +300,7 @@ class bit_flip_channel : public kraus_channel { std::sqrt(1 - probability)}, k1v{0, std::sqrt(probability), std::sqrt(probability), 0}; ops = {k0v, k1v}; - this->probability = probability; + this->parameters.push_back(probability); noise_type = noise_model_type::bit_flip_channel; validateCompleteness(); } @@ -317,7 +317,7 @@ class phase_flip_channel : public kraus_channel { std::sqrt(1 - probability)}, k1v{std::sqrt(probability), 0, 0, negOne * std::sqrt(probability)}; ops = {k0v, k1v}; - this->probability = probability; + this->parameters.push_back(probability); noise_type = noise_model_type::phase_flip_channel; validateCompleteness(); }