MCPcopy Create free account
hub / github.com/NVIDIA/cuda-quantum / pySamplePTSBE

Function pySamplePTSBE

python/runtime/cudaq/algorithms/py_sample_ptsbe.cpp:42–96  ·  view source on GitHub ↗

@brief Run PTSBE sampling from Python. All PTSBE configuration is handled by the Python wrapper (cudaq.ptsbe.sample) and passed here as positional parameters. nanobind 2.x cannot dispatch NB_TYPE_CASTER-based parameters (MlirModule) when nanobind::object appears in the same function signature. Use concrete std::optional types for all nullable parameters instead.

Source from the content-addressed store, hash-verified

40// when nanobind::object appears in the same function signature. Use concrete
41// std::optional types for all nullable parameters instead.
42static ptsbe::sample_result
43pySamplePTSBE(const std::string &shortName, MlirModule module,
44 cudaq::CompiledModule *compiled, std::size_t shots_count,
45 noise_model noiseModel,
46 std::optional<std::size_t> max_trajectories,
47 std::optional<std::shared_ptr<ptsbe::PTSSamplingStrategy>>
48 sampling_strategy,
49 std::optional<ptsbe::ShotAllocationStrategy> shot_allocation,
50 bool return_execution_data, bool include_sequential_data,
51 nanobind::args runtimeArgs) {
52 if (shots_count == 0)
53 return ptsbe::sample_result();
54
55 ptsbe::PTSBEOptions ptsbe_options;
56 ptsbe_options.return_execution_data = return_execution_data;
57 ptsbe_options.include_sequential_data = include_sequential_data;
58 ptsbe_options.max_trajectories = max_trajectories;
59
60 if (sampling_strategy)
61 ptsbe_options.strategy = *sampling_strategy;
62
63 if (shot_allocation)
64 ptsbe_options.shot_allocation = *shot_allocation;
65
66 auto mod = unwrap(module);
67 runtimeArgs = simplifiedValidateInputArguments(runtimeArgs);
68 auto &platform = get_platform();
69
70 platform.set_noise(&noiseModel);
71
72 auto fnOp = getKernelFuncOp(mod, shortName);
73 auto opaques = marshal_arguments_for_module_launch(mod, runtimeArgs, fnOp);
74
75 ptsbe::sample_result result;
76 try {
77 nanobind::gil_scoped_release release;
78 result = ptsbe::detail::runSamplingPTSBE(
79 [&]() mutable {
80 [[maybe_unused]] auto res =
81 clean_launch_module(shortName, mod, opaques, compiled);
82 },
83 platform, shortName, shots_count, ptsbe_options);
84 } catch (const std::exception &e) {
85 platform.reset_noise();
86 throw std::runtime_error(std::string("cudaq.ptsbe.sample() failed: ") +
87 e.what());
88 } catch (...) {
89 platform.reset_noise();
90 throw std::runtime_error(
91 "cudaq.ptsbe.sample() failed with an unknown error.");
92 }
93
94 platform.reset_noise();
95 return result;
96}
97
98namespace {
99/// @brief Async wrapper that holds the future for PTSBE sampling.

Callers

nothing calls this directly

Calls 5

getKernelFuncOpFunction · 0.85
runSamplingPTSBEFunction · 0.85
set_noiseMethod · 0.80
reset_noiseMethod · 0.80
sample_resultClass · 0.50

Tested by

no test coverage detected