@brief Helper to get BufferInfo from a numpy array via Python buffer protocol.
| 262 | /// @brief Helper to get BufferInfo from a numpy array via Python buffer |
| 263 | /// protocol. |
| 264 | static 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 | |
| 296 | static cudaq::state createStateFromPyBuffer(nanobind::object data, |
| 297 | LinkedLibraryHolder &holder) { |
no test coverage detected