rich7420 commented on code in PR #649:
URL: https://github.com/apache/mahout/pull/649#discussion_r2572766538


##########
qdp/qdp-python/src/lib.rs:
##########
@@ -0,0 +1,58 @@
+use pyo3::prelude::*;
+use pyo3::exceptions::PyRuntimeError;
+use qdp_core::QdpEngine as CoreEngine;
+
+/// PyO3 wrapper for QdpEngine
+///
+/// Provides Python bindings for GPU-accelerated quantum state encoding.
+#[pyclass]
+struct QdpEngine {
+    engine: CoreEngine,
+}
+
+#[pymethods]
+impl QdpEngine {
+    /// Initialize QDP engine on specified GPU device
+    ///
+    /// Args:
+    ///     device_id: CUDA device ID (typically 0)
+    ///
+    /// Returns:
+    ///     QdpEngine instance
+    ///
+    /// Raises:
+    ///     RuntimeError: If CUDA device initialization fails
+    #[new]
+    fn new(device_id: usize) -> PyResult<Self> {
+        let engine = CoreEngine::new(device_id)
+            .map_err(|e| PyRuntimeError::new_err(format!("Failed to 
initialize: {}", e)))?;
+        Ok(Self { engine })
+    }
+
+    /// Encode classical data into quantum state
+    ///
+    /// Args:
+    ///     data: Input data as list of floats
+    ///     num_qubits: Number of qubits for encoding
+    ///     encoding_method: Encoding strategy ("amplitude", "angle", or 
"basis")
+    ///
+    /// Returns:
+    ///     DLPack tensor pointer (integer) for zero-copy PyTorch integration
+    ///
+    /// Raises:
+    ///     RuntimeError: If encoding fails
+    fn encode(&self, data: Vec<f64>, num_qubits: usize, encoding_method: &str) 
-> PyResult<usize> {

Review Comment:
   Additionally, the current encode accepting Vec<f64> causes a memory copy 
from Python to Rust. I think this is OK for the PoC , but let's open a ticket 
or left a TODO to plan for introducing the numpy crate in the future to enable 
zero-copy reading for inputs.



##########
qdp/qdp-python/src/lib.rs:
##########
@@ -0,0 +1,58 @@
+use pyo3::prelude::*;
+use pyo3::exceptions::PyRuntimeError;
+use qdp_core::QdpEngine as CoreEngine;
+
+/// PyO3 wrapper for QdpEngine
+///
+/// Provides Python bindings for GPU-accelerated quantum state encoding.
+#[pyclass]
+struct QdpEngine {
+    engine: CoreEngine,
+}
+
+#[pymethods]
+impl QdpEngine {
+    /// Initialize QDP engine on specified GPU device
+    ///
+    /// Args:
+    ///     device_id: CUDA device ID (typically 0)
+    ///
+    /// Returns:
+    ///     QdpEngine instance
+    ///
+    /// Raises:
+    ///     RuntimeError: If CUDA device initialization fails
+    #[new]
+    fn new(device_id: usize) -> PyResult<Self> {

Review Comment:
   I think the current encode method returns an integer (usize) representing 
the memory address.
   PyTorch's torch.from_dlpack() does not accept a raw integer pointer. It 
expects a Python object implementing the __dlpack__ protocol or a PyCapsule. 
Returning a raw integer will prevent users from converting it to a Tensor in 
Python.
   I think we need to add a wrapper class (ex, QuantumTensor or something) and 
implement the __dlpack__ method within it. This allows PyTorch to correctly 
recognize and take over the GPU memory.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to