yehfelareborn commented on code in PR #1179:
URL: https://github.com/apache/mahout/pull/1179#discussion_r2941634415


##########
testing/qdp/test_numpy.py:
##########
@@ -147,6 +146,255 @@ def test_encode_numpy_encoding_methods(encoding_method):
     _verify_tensor(tensor, (1, sample_size))
 
 
+# ---------------------------------------------------------------------------
+# Angle encoding tests
+# ---------------------------------------------------------------------------
+
+
+@requires_qdp
[email protected]
[email protected]("num_qubits", [1, 2, 3, 4])
+def test_encode_numpy_angle_encoding_1d(num_qubits):
+    """Angle encoding: 1D array with num_qubits angles (single sample)"""
+    from _qdp import QdpEngine
+
+    pytest.importorskip("torch")
+    if not torch.cuda.is_available():
+        pytest.skip("GPU required for QdpEngine")
+
+    engine = QdpEngine(device_id=0)
+    # Angle encoding expects exactly num_qubits values: one angle per qubit
+    angles = np.random.uniform(0, 2 * np.pi, 
size=num_qubits).astype(np.float64)
+
+    qtensor = engine.encode(angles, num_qubits, encoding_method="angle")
+    tensor = torch.from_dlpack(qtensor)
+
+    _verify_tensor(tensor, (1, 2**num_qubits), check_normalization=True)
+
+
+@requires_qdp
[email protected]
[email protected](("num_samples", "num_qubits"), [(5, 2), (10, 3), (1, 
4)])
+def test_encode_numpy_angle_encoding_2d(num_samples, num_qubits):
+    """Angle encoding: 2D array of shape (num_samples, num_qubits)"""
+    from _qdp import QdpEngine
+
+    pytest.importorskip("torch")
+    if not torch.cuda.is_available():
+        pytest.skip("GPU required for QdpEngine")
+
+    engine = QdpEngine(device_id=0)
+    angles = np.random.uniform(0, 2 * np.pi, size=(num_samples, 
num_qubits)).astype(
+        np.float64
+    )
+
+    qtensor = engine.encode(angles, num_qubits, encoding_method="angle")
+    tensor = torch.from_dlpack(qtensor)
+
+    _verify_tensor(tensor, (num_samples, 2**num_qubits), 
check_normalization=True)
+
+
+@requires_qdp
[email protected]
+def test_encode_numpy_angle_encoding_from_file():
+    """Angle encoding: load angles from .npy file"""
+    from _qdp import QdpEngine
+
+    pytest.importorskip("torch")
+    if not torch.cuda.is_available():
+        pytest.skip("GPU required for QdpEngine")
+
+    engine = QdpEngine(device_id=0)
+    num_qubits = 3
+    num_samples = 8
+    angles = np.random.uniform(0, 2 * np.pi, size=(num_samples, 
num_qubits)).astype(
+        np.float64
+    )
+
+    with tempfile.NamedTemporaryFile(suffix=".npy", delete=False) as f:
+        npy_path = f.name
+    try:
+        np.save(npy_path, angles)
+        qtensor = engine.encode(npy_path, num_qubits, encoding_method="angle")
+        tensor = torch.from_dlpack(qtensor)
+        _verify_tensor(tensor, (num_samples, 2**num_qubits), 
check_normalization=True)
+    finally:
+        if os.path.exists(npy_path):
+            os.remove(npy_path)
+
+
+@requires_qdp
[email protected]
+def test_encode_numpy_angle_encoding_wrong_sample_size():
+    """Angle encoding: wrong sample_size raises an error"""
+    from _qdp import QdpEngine
+
+    pytest.importorskip("torch")
+    if not torch.cuda.is_available():
+        pytest.skip("GPU required for QdpEngine")
+
+    engine = QdpEngine(device_id=0)
+    num_qubits = 3
+    # Pass 2^num_qubits values instead of num_qubits values
+    wrong_data = np.ones(2**num_qubits, dtype=np.float64)
+
+    with pytest.raises((RuntimeError, ValueError)):
+        engine.encode(wrong_data, num_qubits, encoding_method="angle")
+
+
+# ---------------------------------------------------------------------------
+# Basis encoding tests
+# ---------------------------------------------------------------------------
+
+
+@requires_qdp
[email protected]
[email protected]("num_qubits", [1, 2, 3, 4])
+def test_encode_numpy_basis_encoding_1d(num_qubits):
+    """Basis encoding: 1D array with a single index (single sample)"""
+    from _qdp import QdpEngine
+
+    pytest.importorskip("torch")
+    if not torch.cuda.is_available():
+        pytest.skip("GPU required for QdpEngine")
+
+    engine = QdpEngine(device_id=0)
+    # Basis encoding expects sample_size=1: one integer index per sample
+    index = np.array([float(2**num_qubits - 1)], dtype=np.float64)
+
+    qtensor = engine.encode(index, num_qubits, encoding_method="basis")
+    tensor = torch.from_dlpack(qtensor)
+
+    _verify_tensor(tensor, (1, 2**num_qubits), check_normalization=True)
+
+
+@requires_qdp
[email protected]
[email protected](("num_samples", "num_qubits"), [(5, 2), (10, 3), (1, 
4)])
+def test_encode_numpy_basis_encoding_2d(num_samples, num_qubits):
+    """Basis encoding: 2D array of shape (num_samples, 1) with integer 
indices"""
+    from _qdp import QdpEngine
+
+    pytest.importorskip("torch")
+    if not torch.cuda.is_available():
+        pytest.skip("GPU required for QdpEngine")
+
+    engine = QdpEngine(device_id=0)
+    max_index = 2**num_qubits
+    indices = np.random.randint(0, max_index, size=(num_samples, 
1)).astype(np.float64)
+
+    qtensor = engine.encode(indices, num_qubits, encoding_method="basis")
+    tensor = torch.from_dlpack(qtensor)
+
+    _verify_tensor(tensor, (num_samples, 2**num_qubits), 
check_normalization=True)
+
+
+@requires_qdp
[email protected]
+def test_encode_numpy_basis_encoding_from_file():
+    """Basis encoding: load indices from .npy file"""
+    from _qdp import QdpEngine
+
+    pytest.importorskip("torch")
+    if not torch.cuda.is_available():
+        pytest.skip("GPU required for QdpEngine")
+
+    engine = QdpEngine(device_id=0)
+    num_qubits = 3
+    num_samples = 6
+    indices = np.random.randint(0, 2**num_qubits, size=(num_samples, 
1)).astype(
+        np.float64
+    )
+
+    with tempfile.NamedTemporaryFile(suffix=".npy", delete=False) as f:
+        npy_path = f.name
+    try:
+        np.save(npy_path, indices)
+        qtensor = engine.encode(npy_path, num_qubits, encoding_method="basis")
+        tensor = torch.from_dlpack(qtensor)
+        _verify_tensor(tensor, (num_samples, 2**num_qubits), 
check_normalization=True)
+    finally:
+        if os.path.exists(npy_path):
+            os.remove(npy_path)
+
+
+@requires_qdp
[email protected]
[email protected](
+    ("basis_index", "num_qubits"),
+    [
+        (0, 2),  # |00⟩: amplitude 1 at index 0
+        (1, 2),  # |01⟩: amplitude 1 at index 1
+        (3, 2),  # |11⟩: amplitude 1 at index 3
+        (0, 3),  # |000⟩: amplitude 1 at index 0
+        (13, 4),  # |1101⟩: amplitude 1 at index 13
+    ],
+)
+def test_encode_numpy_basis_state_correctness(basis_index, num_qubits):
+    """Basis encoding: verify the encoded state is exactly |basis_index⟩"""
+    from _qdp import QdpEngine
+
+    pytest.importorskip("torch")
+    if not torch.cuda.is_available():
+        pytest.skip("GPU required for QdpEngine")
+
+    engine = QdpEngine(device_id=0, precision="float64")
+    data = np.array([[float(basis_index)]], dtype=np.float64)
+
+    qtensor = engine.encode(data, num_qubits, encoding_method="basis")
+    tensor = torch.from_dlpack(qtensor)
+
+    # The encoded state should be a one-hot complex vector with |amplitude|²=1
+    # at basis_index and 0 elsewhere
+    probs = tensor.abs().pow(2).squeeze(0)  # shape: (2^num_qubits,)
+    expected = torch.zeros(2**num_qubits, dtype=probs.dtype, 
device=probs.device)
+    expected[basis_index] = 1.0
+
+    assert torch.allclose(probs, expected, atol=1e-6), (
+        f"Expected |{basis_index}⟩ but got probabilities 
{probs.cpu().tolist()}"
+    )
+
+
+@requires_qdp
[email protected]
[email protected](
+    ("bad_data", "description"),
+    [
+        (np.array([[-1.0]], dtype=np.float64), "negative index"),
+        (np.array([[0.5]], dtype=np.float64), "non-integer index"),
+    ],
+)
+def test_encode_numpy_basis_encoding_invalid_index(bad_data, description):

Review Comment:
   Hi, i removed the description part and replaced it with pytest.param, thanks



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