Skip to content

[QDP] Delegate GPU pointer encoding to QuantumEncoder trait#1006

Open
viiccwen wants to merge 2 commits intoapache:mainfrom
viiccwen:refactor-gpu-ptr-encoding
Open

[QDP] Delegate GPU pointer encoding to QuantumEncoder trait#1006
viiccwen wants to merge 2 commits intoapache:mainfrom
viiccwen:refactor-gpu-ptr-encoding

Conversation

@viiccwen
Copy link
Contributor

@viiccwen viiccwen commented Feb 2, 2026

Purpose of PR

Refactors qdp/qdp-core/src/lib.rs so that GPU-pointer encoding (encode_from_gpu_ptr and encode_batch_from_gpu_ptr) is delegated to the QuantumEncoder trait instead of being implemented in large match blocks inside the engine. This aligns the design with the existing host-side encode() and encode_batch() flow and reduces duplication.

Changes

1. QuantumEncoder trait (gpu/encodings/mod.rs)

  • Added two 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>
  • Encoders that do not implement these (e.g. IQP) get the default and return a clear NotImplemented error.

2. Encoder implementations

  • AmplitudeEncoder (gpu/encodings/amplitude.rs): Implemented encode_from_gpu_ptr and encode_batch_from_gpu_ptr (logic moved from lib.rs: norm kernel, amplitude kernel, sync, validation).
  • AngleEncoder (gpu/encodings/angle.rs): Same; angle validation and launch_angle_encode / launch_angle_encode_batch with stream.
  • BasisEncoder (gpu/encodings/basis.rs): Same; launch_basis_encode_batch for both single (num_samples=1) and batch.

3. QdpEngine (lib.rs)

  • encode_from_gpu_ptr_with_stream: Replaced the large match method.as_str() { "amplitude" => ..., "angle" => ..., "basis" => ..., _ => ... } with:
    • get_encoder(encoding_method)?
    • encoder.encode_from_gpu_ptr(&self.device, input_d, input_len, num_qubits, stream)?
    • state_vector.to_precision(&self.device, self.precision)?
    • Ok(state_vector.to_dlpack())
  • encode_batch_from_gpu_ptr_with_stream: Same pattern using encode_batch_from_gpu_ptr; kept the num_samples == 0 check in the engine before calling the encoder.
  • Removed the inline TODOs that asked for this refactor.

Related Issues or PRs

Closes #1005

Changes Made

  • Bug fix
  • New feature
  • Refactoring
  • Documentation
  • Test
  • CI/CD pipeline
  • Other

Breaking Changes

  • Yes
  • No

Checklist

  • Added or updated unit tests for all changes
  • Added or updated documentation for all changes
  • Successfully built and ran all unit tests or manual tests locally
  • PR title follows "MAHOUT-XXX: Brief Description" format (if related to an issue)
  • Code follows ASF guidelines

@guan404ming guan404ming added this to the Qumat 0.6.0 milestone Feb 4, 2026
Copy link
Member

@guan404ming guan404ming left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks nice!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this computes L2 norms (expensive) just to check finiteness (cheap side effect). Maybe we could introduce a dedicated check_finite_batch kernel would be more direct. I think we could do it in a follow up and please correct me if I'm wrong. Thanks!

cc @rich7420 @ryankert01

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree : )

@viiccwen
Copy link
Contributor Author

viiccwen commented Feb 4, 2026

I'll fix conflict later :)

@viiccwen viiccwen force-pushed the refactor-gpu-ptr-encoding branch from c2f3827 to 84a57bd Compare February 6, 2026 08:55
@viiccwen
Copy link
Contributor Author

viiccwen commented Feb 6, 2026

cc @guan404ming
sorry for late.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[QDP] Delegate GPU pointer encoding to QuantumEncoder trait

2 participants