viiccwen opened a new issue, #1005:
URL: https://github.com/apache/mahout/issues/1005

   ## Summary
   
   `qdp/qdp-core/src/lib.rs` contains a large amount (prettry large 🤡) of 
inline GPU pointer encoding logic in 
`QdpEngine::encode_from_gpu_ptr_with_stream` and 
`QdpEngine::encode_batch_from_gpu_ptr_with_stream`. Each method uses a long 
`match` on `encoding_method` with full implementations for `"amplitude"`, 
`"angle"`, and `"basis"`. This duplicates the design already used for host-side 
`encode()` and `encode_batch()`, which delegate to encoders via the 
`QuantumEncoder` trait and `get_encoder()`.
   
   ## Problem
   
   1. The same encoding strategies are implemented twice—once in the encoder 
modules (for host data) and again inline in `lib.rs` (for GPU pointers). Adding 
a new encoding or changing behavior requires editing both places.
   2. Roughly 400 lines in `lib.rs` are dedicated to GPU-pointer encoding. The 
file is long and the engine type mixes orchestration with encoding details.
   3. The code already states:
      - *"TODO: Refactor to use QuantumEncoder trait (add `encode_from_gpu_ptr` 
to trait) to reduce duplication with AmplitudeEncoder::encode()."*
      - *"TODO: Refactor to use QuantumEncoder trait (see `encode_from_gpu_ptr` 
TODO)."*
   
   ## Proposed solution
   
   1. **Extend `QuantumEncoder`** (in `gpu/encodings/mod.rs`):
      - Add optional trait methods (Linux-only, with default implementations 
that return `NotImplemented`):
        - `encode_from_gpu_ptr(device, input_d, input_len, num_qubits, stream) 
-> Result<GpuStateVector>`
        - `encode_batch_from_gpu_ptr(device, input_batch_d, num_samples, 
sample_size, num_qubits, stream) -> Result<GpuStateVector>`
   
   2. **Implement in encoder modules**:
      - Move the single-sample and batch GPU-pointer logic from `lib.rs` into 
`AmplitudeEncoder`, `AngleEncoder`, and `BasisEncoder` (each in its own file), 
implementing the new trait methods.
   
   3. **Simplify `lib.rs`**:
      - In `encode_from_gpu_ptr_with_stream`: call 
`get_encoder(encoding_method)?`, then `encoder.encode_from_gpu_ptr(...)`, then 
`state_vector.to_precision(...)` and `state_vector.to_dlpack()`.
      - Same pattern for `encode_batch_from_gpu_ptr_with_stream` using 
`encode_batch_from_gpu_ptr`.
      - Remove the large `match` blocks and keep the engine as a thin 
orchestrator.


-- 
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