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

Function getNumpyBufferInfo

python/runtime/cudaq/algorithms/py_state.cpp:264–294  ·  view source on GitHub ↗

@brief Helper to get BufferInfo from a numpy array via Python buffer protocol.

Source from the content-addressed store, hash-verified

262/// @brief Helper to get BufferInfo from a numpy array via Python buffer
263/// protocol.
264static BufferInfo getNumpyBufferInfo(nanobind::object numpy_array) {
265 auto dtype = numpy_array.attr("dtype");
266 std::string dtypeStr = nanobind::cast<std::string>(dtype.attr("name"));
267
268 BufferInfo info;
269 if (dtypeStr == "complex64") {
270 info.itemsize = sizeof(std::complex<float>);
271 info.format = "Zf";
272 } else if (dtypeStr == "complex128") {
273 info.itemsize = sizeof(std::complex<double>);
274 info.format = "Zd";
275 } else {
276 info.format = dtypeStr;
277 info.itemsize = nanobind::cast<std::size_t>(dtype.attr("itemsize"));
278 }
279 auto shapeTuple = nanobind::cast<nanobind::tuple>(numpy_array.attr("shape"));
280 info.size = 1;
281 for (std::size_t i = 0; i < shapeTuple.size(); i++) {
282 auto ext = nanobind::cast<std::size_t>(shapeTuple[i]);
283 info.shape.push_back(ext);
284 info.size *= ext;
285 }
286 auto stridesTuple =
287 nanobind::cast<nanobind::tuple>(numpy_array.attr("strides"));
288 for (std::size_t i = 0; i < stridesTuple.size(); i++)
289 info.strides.push_back(nanobind::cast<ssize_t>(stridesTuple[i]));
290 info.ptr = reinterpret_cast<void *>(
291 nanobind::cast<intptr_t>(numpy_array.attr("ctypes").attr("data")));
292 info.readonly = false;
293 return info;
294}
295
296static cudaq::state createStateFromPyBuffer(nanobind::object data,
297 LinkedLibraryHolder &holder) {

Callers 2

createStateFromPyBufferFunction · 0.85
bindPyStateMethod · 0.85

Calls 2

sizeMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected