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

Fixes for NoiseModel when using with fp32 targets #2672

Merged
merged 3 commits into from
Mar 1, 2025
Merged
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
51 changes: 46 additions & 5 deletions runtime/common/NoiseModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,11 @@ class noise_model {
{KrausChannelT::get_key(),
[](const std::vector<cudaq::real> &params) -> kraus_channel {
KrausChannelT userChannel(params);
userChannel.parameters = params;
if constexpr (std::is_same_v<cudaq::real, double>)
userChannel.parameters = params;
else
userChannel.parameters =
std::vector<double>(params.begin(), params.end());
return userChannel;
}});
}
Expand All @@ -447,8 +451,27 @@ class noise_model {
"noise_model. skipping channel application.");
return kraus_channel();
}
return std::get<std::function<kraus_channel(const std::vector<REAL> &)>>(
iter->second)(params);

if (std::holds_alternative<
std::function<kraus_channel(const std::vector<REAL> &)>>(
iter->second)) {
return std::get<std::function<kraus_channel(const std::vector<REAL> &)>>(
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<REAL, double>) {
// Params was double, casted to float
return std::get<
std::function<kraus_channel(const std::vector<float> &)>>(
iter->second)(std::vector<float>(params.begin(), params.end()));
} else {
// Params was float, casted to double
return std::get<
std::function<kraus_channel(const std::vector<double> &)>>(
iter->second)(std::vector<double>(params.begin(), params.end()));
}
}
}

// de-mangled name (with namespaces) for NVQIR C API
Expand All @@ -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<std::function<kraus_channel(const std::vector<REAL> &)>>(
iter->second)(params);
if (std::holds_alternative<
std::function<kraus_channel(const std::vector<REAL> &)>>(
iter->second)) {
return std::get<std::function<kraus_channel(const std::vector<REAL> &)>>(
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<REAL, double>) {
// Params was double, casted to float
return std::get<
std::function<kraus_channel(const std::vector<float> &)>>(
iter->second)(std::vector<float>(params.begin(), params.end()));
} else {
// Params was float, casted to double
return std::get<
std::function<kraus_channel(const std::vector<double> &)>>(
iter->second)(std::vector<double>(params.begin(), params.end()));
}
}
}

/// @brief Add the Kraus channel that applies to a quantum operation on any
Expand Down
Loading