Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion qdp/qdp-python/src/pytorch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,18 @@ pub fn validate_cuda_tensor_for_encoding(
)));
}
}
"iqp" | "iqp-z" => {
if !dtype_str_lower.contains("float64") {
return Err(PyRuntimeError::new_err(format!(
"CUDA tensor must have dtype float64 for {} encoding, got {}. \
Use tensor.to(torch.float64)",
method, dtype_str
)));
}
}
Comment on lines +185 to +193
Copy link
Contributor

Choose a reason for hiding this comment

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

It's duplicated with angle method block, could be merged together.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah , agreed it can be merged with the angle path. Since this PR is already green and scoped to the behavior fix, I’m okay to keep this minimal and do that small refactor in a follow-up (unless you prefer I include it here). Also sounds good on edge-case coverage follow-up, thank you.

Copy link
Contributor

Choose a reason for hiding this comment

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

IMO, it should be refactor in this PR.

_ => {
return Err(PyRuntimeError::new_err(format!(
"CUDA tensor encoding currently only supports 'amplitude', 'angle', or 'basis' methods, got '{}'. \
"CUDA tensor encoding currently only supports 'amplitude', 'angle', 'basis', 'iqp', or 'iqp-z' methods, got '{}'. \
Copy link
Member

Choose a reason for hiding this comment

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

+1 with rich. should be dynamic.

Copy link
Member

Choose a reason for hiding this comment

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

opened an issue for it. cc @SuyashParmar

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ryankert01 ok working on it

Use tensor.cpu() to convert to CPU tensor for other encoding methods.",
encoding_method
)));
Expand Down
37 changes: 31 additions & 6 deletions testing/qdp/test_bindings.py
Copy link
Contributor

Choose a reason for hiding this comment

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

It's happy path, I'll take a follow-up PR to cover edge case.

Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,34 @@ def test_encode_cuda_tensor_preserves_input(data_shape, is_batch):

@requires_qdp
@pytest.mark.gpu
@pytest.mark.parametrize("encoding_method", ["iqp"])
def test_encode_cuda_tensor_unsupported_encoding(encoding_method):
"""Test error when using CUDA tensor with an encoding not supported on GPU (only amplitude, angle, basis)."""
@pytest.mark.parametrize(
"encoding_method,data",
[
("iqp-z", [0.1, -0.2]),
("iqp", [0.1, -0.2, 0.3]),
],
)
def test_encode_cuda_tensor_iqp_methods(encoding_method, data):
"""Test CUDA tensor path supports IQP-family encodings."""
pytest.importorskip("torch")
from _qdp import QdpEngine

if not torch.cuda.is_available():
pytest.skip("GPU required for QdpEngine")

engine = QdpEngine(0)
cuda_data = torch.tensor(data, dtype=torch.float64, device="cuda:0")

qtensor = engine.encode(cuda_data, 2, encoding_method)
output = torch.from_dlpack(qtensor)

assert tuple(output.shape) == (1, 4)


@requires_qdp
@pytest.mark.gpu
def test_encode_cuda_tensor_invalid_encoding_method():
"""Test error when using CUDA tensor with an unknown encoding method."""
pytest.importorskip("torch")
from _qdp import QdpEngine

Expand All @@ -433,14 +458,14 @@ def test_encode_cuda_tensor_unsupported_encoding(encoding_method):

engine = QdpEngine(0)

# CUDA path only supports amplitude, angle, basis; iqp/iqp-z should raise unsupported error
# Unknown encoding should fail with supported-method guidance.
data = torch.tensor([1.0, 0.0, 0.0, 0.0], dtype=torch.float64, device="cuda:0")

with pytest.raises(
RuntimeError,
match="only supports .*amplitude.*angle.*basis.*Use tensor.cpu",
match="only supports .*amplitude.*angle.*basis.*iqp.*iqp-z.*Use tensor.cpu",
):
engine.encode(data, 2, encoding_method)
engine.encode(data, 2, "unknown-encoding")


@requires_qdp
Expand Down