Skip to content

Add phase encoding#1199

Open
vvvdwbvvv wants to merge 3 commits intoapache:mainfrom
vvvdwbvvv:add-phase-encoding
Open

Add phase encoding#1199
vvvdwbvvv wants to merge 3 commits intoapache:mainfrom
vvvdwbvvv:add-phase-encoding

Conversation

@vvvdwbvvv
Copy link

@vvvdwbvvv vvvdwbvvv commented Mar 17, 2026

Related Issues

Changes

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

Why

Phase encoding is a standard QML data encoding strategy, which is distinct from angle encoding (real-amplitude product state) and IQP encoding (entangled global-phase state).

How

Add phase.cu kernel impl
Add phase.rs encoder

Test done

uv run pytest testing/qdp/test_bindings.py -k 'test_phase' -rF

============== test session starts ==============
platform linux -- Python 3.10.19, pytest-9.0.2, pluggy-1.6.0 -- /home/vvvdwbvvv/oss/mahout/.venv/bin/python3
cachedir: .pytest_cache
rootdir: /home/vvvdwbvvv/oss/mahout
configfile: pyproject.toml
plugins: cov-7.0.0, xdist-3.8.0
collected 69 items / 61 deselected / 8 selected 

testing/qdp/test_bindings.py::test_phase_encode_basic PASSED [ 12%]
testing/qdp/test_bindings.py::test_phase_encode_single_qubit PASSED [ 25%]
testing/qdp/test_bindings.py::test_phase_encode_pi_phase PASSED [ 37%]
testing/qdp/test_bindings.py::test_phase_encode_normalization PASSED [ 50%]
testing/qdp/test_bindings.py::test_phase_encode_batch PASSED [ 62%]
testing/qdp/test_bindings.py::test_phase_encode_errors PASSED [ 75%]
testing/qdp/test_bindings.py::test_phase_encode_shape[data_shape0-expected_shape0] PASSED [ 87%]
testing/qdp/test_bindings.py::test_phase_encode_shape[data_shape1-expected_shape1] PASSED [100%]

======= 8 passed, 61 deselected in 1.46s ========

Checklist

  • Added or updated unit tests for all changes
  • Added or updated documentation for all changes

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds GPU-backed phase encoding to QDP, complementing existing angle/IQP encoders, and validates behavior via new binding tests.

Changes:

  • Introduces CUDA kernels for single-sample and batched phase encoding.
  • Adds a Rust PhaseEncoder implementation (single + batch, including async pipeline path).
  • Extends encoder registry to expose "phase" and adds pytest coverage for correctness, shape, and error cases.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
testing/qdp/test_bindings.py Adds GPU tests validating phase encoding outputs, shapes, normalization, and errors.
qdp/qdp-kernels/src/phase.cu Implements CUDA kernels + launchers for phase encoding (single + batch).
qdp/qdp-kernels/src/lib.rs Exposes FFI bindings and non-CUDA stubs for phase encode launchers.
qdp/qdp-kernels/build.rs Adds phase.cu to rebuild triggers and NVCC compilation list.
qdp/qdp-core/src/gpu/encodings/phase.rs Adds PhaseEncoder with host input, batch, and async pipeline support.
qdp/qdp-core/src/gpu/encodings/mod.rs Registers "phase" encoder and updates the unknown-encoder error message.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +471 to +486
let chunk_samples = chunk_len / sample_size;
let sample_offset = chunk_offset / sample_size;
let offset_elements = sample_offset.checked_mul(state_len).ok_or_else(|| {
MahoutError::InvalidInput("Phase batch output offset overflow".to_string())
})?;

let state_ptr_offset = unsafe { state_ptr.add(offset_elements) as *mut c_void };
let ret = unsafe {
qdp_kernels::launch_phase_encode_batch(
input_ptr,
state_ptr_offset,
chunk_samples,
state_len,
num_qubits as u32,
stream.stream as *mut c_void,
)
Comment on lines +62 to +66
double norm = phase_norm(num_qubits);
double re, im;
sincos(phi, &im, &re); // re = cos(φ), im = sin(φ)

state[idx] = make_cuDoubleComplex(norm * re, norm * im);
Comment on lines +1313 to +1317
pytest.importorskip("torch")
from _qdp import QdpEngine

if not torch.cuda.is_available():
pytest.skip("GPU required for QdpEngine")
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.

2 participants