rich7420 commented on code in PR #1389:
URL: https://github.com/apache/mahout/pull/1389#discussion_r3620195770


##########
testing/qdp/test_iqp_tc_path.py:
##########
@@ -0,0 +1,102 @@
+#
+# 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.
+
+"""Smoke and normalization tests for FWT vs Tensor Core IQP paths (GPU vs 
GPU)."""
+
+import pytest
+import torch
+from qumat_qdp import QdpEngine
+
+
+def _iqp_param_count(num_qubits: int) -> int:
+    return num_qubits + num_qubits * (num_qubits - 1) // 2
+
+
[email protected](scope="module")
+def engine():
+    try:
+        eng = QdpEngine(device_id=0, precision="float64")
+    except Exception as exc:
+        pytest.skip(f"Could not initialize QdpEngine: {exc}")
+    if not hasattr(eng, "encode_batch_tc"):
+        pytest.skip("encode_batch_tc not available in this build")
+    if not torch.cuda.is_available():
+        pytest.skip("CUDA not available")
+    return eng
+
+
+def _assert_normalized(state: torch.Tensor, num_qubits: int, label: str) -> 
None:
+    probs = state.abs() ** 2
+    if state.ndim == 2:
+        row_sums = probs.sum(dim=1)
+        assert torch.allclose(row_sums, torch.ones_like(row_sums), atol=1e-6), 
(
+            f"{label}: batch normalization failed at N={num_qubits}"
+        )
+    else:
+        assert torch.allclose(probs.sum(), torch.tensor(1.0), atol=1e-6), (
+            f"{label}: normalization failed at N={num_qubits}"
+        )
+
+
[email protected]("num_qubits", [8, 12])
[email protected]("batch_size", [4, 32])
+def test_fwt_and_tc_paths_normalized(engine, num_qubits, batch_size):
+    """For N<=12 both GPU paths return normalized states."""
+    data_len = _iqp_param_count(num_qubits)
+    data = torch.randn(batch_size, data_len, dtype=torch.float64).numpy()
+    state_len = 1 << num_qubits
+
+    fwt_state = torch.from_dlpack(engine.encode(data, num_qubits, "iqp"))
+    assert fwt_state.shape == (batch_size, state_len)
+    _assert_normalized(fwt_state, num_qubits, "FWT")
+
+    tc_state = torch.from_dlpack(engine.encode_batch_tc(data, num_qubits))
+    assert tc_state.shape == (batch_size, state_len)
+    _assert_normalized(tc_state, num_qubits, "TC")
+
+
[email protected]("num_qubits", [14])
[email protected]("batch_size", [4, 8])
+def test_large_n_tc_path_smoke(engine, num_qubits, batch_size):
+    """Large-N TC Kronecker path runs; FWT remains normalized baseline."""
+    data_len = _iqp_param_count(num_qubits)
+    data = torch.randn(batch_size, data_len, dtype=torch.float64).numpy()
+    state_len = 1 << num_qubits
+
+    fwt_state = torch.from_dlpack(engine.encode(data, num_qubits, "iqp"))
+    assert fwt_state.shape == (batch_size, state_len)
+    _assert_normalized(fwt_state, num_qubits, "FWT")
+
+    tc_state = torch.from_dlpack(engine.encode_batch_tc(data, num_qubits))
+    assert tc_state.shape == (batch_size, state_len)
+    assert torch.isfinite(tc_state).all()
+
+
[email protected]("num_qubits", [14])
+def test_fwt_tc_path_agreement_loose(engine, num_qubits):
+    """Large-N TC scaffold should be within loose tolerance of FWT (structural 
PR)."""
+    batch_size = 8
+    data_len = _iqp_param_count(num_qubits)
+    data = torch.randn(batch_size, data_len, dtype=torch.float64).numpy()
+
+    fwt_state = torch.from_dlpack(engine.encode(data, num_qubits, "iqp"))
+    tc_state = torch.from_dlpack(engine.encode_batch_tc(data, num_qubits))
+
+    max_err = (fwt_state - tc_state).abs().max().item()
+    # Ozaki Kronecker scaffold may diverge until PR6 malloc pooling lands.
+    assert max_err < 0.1, (

Review Comment:
   This test admits the N>12 Kronecker TC path can be off by up to 0.1 in 
absolute error and treats that as an accepted, known limitation for now 
("scaffold may diverge until PR6 malloc pooling lands"). That's fine for a 
test, but the public `encode_batch_tc` API on the Rust/Python side doesn't have 
any equivalent guard — it'll happily take any `num_qubits` and return a state 
vector with no indication to the caller that accuracy degrades past 12 qubits. 
I'd suggest either gating that path behind an explicit opt-in, or at minimum 
surfacing a warning/doc note at the API boundary so people don't trust these 
results for anything precision-sensitive.



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