From 6f41b898f3f2d0d3cbc818db76337d4bfa12b194 Mon Sep 17 00:00:00 2001 From: Thien Nguyen Date: Fri, 28 Feb 2025 06:28:57 +0000 Subject: [PATCH] Fixes NoiseModel for fp32 targets Signed-off-by: Thien Nguyen --- runtime/common/NoiseModel.h | 51 +++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/runtime/common/NoiseModel.h b/runtime/common/NoiseModel.h index 066bebea2a..ba1dafb22a 100644 --- a/runtime/common/NoiseModel.h +++ b/runtime/common/NoiseModel.h @@ -425,7 +425,11 @@ class noise_model { {KrausChannelT::get_key(), [](const std::vector ¶ms) -> kraus_channel { KrausChannelT userChannel(params); - userChannel.parameters = params; + if constexpr (std::is_same_v) + userChannel.parameters = params; + else + userChannel.parameters = + std::vector(params.begin(), params.end()); return userChannel; }}); } @@ -447,8 +451,27 @@ class noise_model { "noise_model. skipping channel application."); return kraus_channel(); } - return std::get &)>>( - iter->second)(params); + + if (std::holds_alternative< + std::function &)>>( + iter->second)) { + return std::get &)>>( + iter->second)(params); + } else { + // Type mismatch, e.g., the model is defined for float but params is + // supplied as double. + if constexpr (std::is_same_v) { + // Params was double, casted to float + return std::get< + std::function &)>>( + iter->second)(std::vector(params.begin(), params.end())); + } else { + // Params was float, casted to double + return std::get< + std::function &)>>( + iter->second)(std::vector(params.begin(), params.end())); + } + } } // de-mangled name (with namespaces) for NVQIR C API @@ -459,8 +482,26 @@ class noise_model { if (iter == registeredChannels.end()) throw std::runtime_error( "kraus channel not registered with this noise_model."); - return std::get &)>>( - iter->second)(params); + if (std::holds_alternative< + std::function &)>>( + iter->second)) { + return std::get &)>>( + iter->second)(params); + } else { + // Type mismatch, e.g., the model is defined for float but params is + // supplied as double. + if constexpr (std::is_same_v) { + // Params was double, casted to float + return std::get< + std::function &)>>( + iter->second)(std::vector(params.begin(), params.end())); + } else { + // Params was float, casted to double + return std::get< + std::function &)>>( + iter->second)(std::vector(params.begin(), params.end())); + } + } } /// @brief Add the Kraus channel that applies to a quantum operation on any