This is an automated email from the ASF dual-hosted git repository.
viiccwen pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/mahout.git
The following commit(s) were added to refs/heads/main by this push:
new c17655984 [QDP] Normalize QuantumDataLoader encoding names to
canonical case (#1475)
c17655984 is described below
commit c176559841e99ec547142ae3f43238d8d0956129
Author: Vladimir Babin <[email protected]>
AuthorDate: Thu Sep 10 17:04:11 2026 +0800
[QDP] Normalize QuantumDataLoader encoding names to canonical case (#1475)
* [QDP] Normalize QuantumDataLoader encoding names to canonical case
The builder validated encoding names case-insensitively but stored the
original spelling, so a mixed-case name such as "Amplitude" passed
builder validation and only failed later when iteration forwarded it to
the case-sensitive backend. Normalize the accepted name to its canonical
lowercase form at both set sites, mirroring the existing .dtype()
handling.
Signed-off-by: Vladimir Babin <[email protected]>
* [QDP] Make encoding-case tests table-driven; rename to
test_loader_encoding.py
Address review feedback:
- Convert case-normalization and rejection tests to @pytest.mark.parametrize
- Rename test_loader_encoding_case.py -> test_loader_encoding.py
* Fix ruff PT006: pass parametrize argnames as a tuple
---------
Signed-off-by: Vladimir Babin <[email protected]>
---
qdp/qdp-python/qumat_qdp/loader.py | 14 +++---
qdp/qdp-python/tests/test_loader_encoding.py | 65 ++++++++++++++++++++++++++++
2 files changed, 72 insertions(+), 7 deletions(-)
diff --git a/qdp/qdp-python/qumat_qdp/loader.py
b/qdp/qdp-python/qumat_qdp/loader.py
index bf225ad1e..e1b4cfd65 100644
--- a/qdp/qdp-python/qumat_qdp/loader.py
+++ b/qdp/qdp-python/qumat_qdp/loader.py
@@ -300,7 +300,7 @@ class QuantumDataLoader:
self._num_qubits = num_qubits
self._batch_size = batch_size
self._total_batches = total_batches
- self._encoding_method = encoding_method
+ self._encoding_method = encoding_method.lower()
self._seed = seed
self._file_path: str | None = None
self._streaming_requested = (
@@ -333,11 +333,11 @@ class QuantumDataLoader:
"""Set the quantum feature encoding method.
Valid values are ``"amplitude"``, ``"angle"``, ``"basis"``,
- ``"iqp"``, ``"iqp-z"``, and ``"phase"``. Use these canonical
- lowercase names because the selected backend receives the string
exactly
- as supplied. The PyTorch reference backend supports the same methods
as
- ``qumat_qdp.torch_ref``; use the native backend for methods that are
- not available in the reference path.
+ ``"iqp"``, ``"iqp-z"``, and ``"phase"``. Names are matched
+ case-insensitively and normalized to the canonical lowercase spelling
+ before they reach the selected backend. The PyTorch reference backend
+ supports the same methods as ``qumat_qdp.torch_ref``; use the native
+ backend for methods that are not available in the reference path.
:param method: Encoding method name.
:returns: ``self`` for fluent builder chaining.
@@ -353,7 +353,7 @@ class QuantumDataLoader:
f"Unknown encoding {method!r}. "
f"Valid options: {sorted(_VALID_ENCODINGS)}"
)
- self._encoding_method = method
+ self._encoding_method = method.lower()
return self
def batches(self, total: int, size: int = 64) -> QuantumDataLoader:
diff --git a/qdp/qdp-python/tests/test_loader_encoding.py
b/qdp/qdp-python/tests/test_loader_encoding.py
new file mode 100644
index 000000000..b391d092b
--- /dev/null
+++ b/qdp/qdp-python/tests/test_loader_encoding.py
@@ -0,0 +1,65 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import pytest
+from qumat_qdp import QuantumDataLoader
+
+
[email protected](
+ ("given", "expected"),
+ [
+ ("Amplitude", "amplitude"),
+ ("ANGLE", "angle"),
+ ("IQP-Z", "iqp-z"),
+ ("Basis", "basis"),
+ ],
+)
+def test_encoding_setter_normalizes_case(given: str, expected: str) -> None:
+ loader = QuantumDataLoader().encoding(given)
+ assert loader._encoding_method == expected
+
+
[email protected](
+ ("given", "expected"),
+ [
+ ("Amplitude", "amplitude"),
+ ("ANGLE", "angle"),
+ ("IQP-Z", "iqp-z"),
+ ("Basis", "basis"),
+ ],
+)
+def test_constructor_normalizes_encoding_case(given: str, expected: str) ->
None:
+ loader = QuantumDataLoader(encoding_method=given)
+ assert loader._encoding_method == expected
+
+
+def test_mixed_case_encoding_iterates_without_error() -> None:
+ pytest.importorskip("torch")
+ loader = (
+ QuantumDataLoader()
+ .backend("pytorch")
+ .qubits(2)
+ .encoding("Amplitude")
+ .batches(1, size=1)
+ )
+ batches = list(loader)
+ assert len(batches) == 1
+
+
[email protected]("bad", ["not-an-encoding", "amplitud", "ANGL"])
+def test_unknown_encoding_still_rejected(bad: str) -> None:
+ with pytest.raises(ValueError):
+ QuantumDataLoader().encoding(bad)