Skip to content
Open
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
22 changes: 18 additions & 4 deletions python/runtime/cudaq/analysis/py_dem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <nanobind/stl/optional.h>
#include <nanobind/stl/string.h>
#include <optional>
#include <stdexcept>
#include <string>

using namespace cudaq;
Expand All @@ -27,10 +28,23 @@ static std::string dem_from_kernel_impl(const std::string &kernelName,
args = simplifiedValidateInputArguments(args);

const cudaq::noise_model *noisePtr = noise ? &(*noise) : nullptr;
return cudaq::detail::runDemFromKernel(kernelName, platform, noisePtr, [&]() {
[[maybe_unused]] auto result =
cudaq::marshal_and_launch_module(kernelName, kernelMod, args);
});
// Re-throw as a module-local RuntimeError; plugin exceptions otherwise
// escape uncaught on macOS and abort instead of raising in Python.
try {
return cudaq::detail::runDemFromKernel(
kernelName, platform, noisePtr, [&]() {
[[maybe_unused]] auto result =
cudaq::marshal_and_launch_module(kernelName, kernelMod, args);
});
} catch (nanobind::python_error &) {
throw; // keep real Python exceptions (e.g. from argument marshalling)
} catch (const std::exception &e) {
throw std::runtime_error(e.what()); // dem_from_kernel reports via RuntimeError
} catch (...) {
throw std::runtime_error(
"cudaq::dem_from_kernel failed: the kernel uses an operation the Stim "
"analysis backend cannot simulate (only Clifford gates are supported).");
}
}

void cudaq::bindDemFromKernel(nanobind::module_ &mod) {
Expand Down
Loading