Skip to content

Commit c69ca7a

Browse files
committed
Add explicit validation checks and tests for partial_trace
1 parent 6c09662 commit c69ca7a

1 file changed

Lines changed: 26 additions & 28 deletions

File tree

toqito/matrix_ops/tests/test_partial_trace.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -646,9 +646,9 @@ def test_is_trace_prserving(input_mat, expected_result, sys_arg, dim_arg):
646646
@pytest.mark.parametrize(
647647
"sys_value",
648648
[
649-
2, # int out-of-bounds
650-
[2], # list out-of-bounds
651-
[-1], # negative index
649+
2, # int out-of-bounds
650+
[2], # list out-of-bounds
651+
[-1], # negative index
652652
],
653653
)
654654
def test_sys_out_of_bounds(sys_value):
@@ -661,8 +661,8 @@ def test_sys_out_of_bounds(sys_value):
661661
@pytest.mark.parametrize(
662662
"dim_value",
663663
[
664-
[2], # len == 1 branch
665-
[2, 2], # len > 1 branch
664+
[2], # len == 1 branch
665+
[2, 2], # len > 1 branch
666666
],
667667
)
668668
def test_dim_list_branches(dim_value):
@@ -672,29 +672,27 @@ def test_dim_list_branches(dim_value):
672672
assert result.shape == (2, 2)
673673

674674

675-
def test_partial_trace_non_square_matrix():
676-
"""Test that non-square matrices raise ValueError."""
677-
mat = np.ones((2, 3))
678-
with pytest.raises(ValueError, match="square"):
679-
partial_trace(mat)
680-
681-
682-
def test_partial_trace_dim_product_mismatch():
683-
"""Test that mismatched dim product raises ValueError."""
684-
mat = np.eye(4)
685-
with pytest.raises(ValueError, match="Product of `dim`"):
686-
partial_trace(mat, [0], [2, 3])
687-
675+
@pytest.mark.parametrize(
676+
"input_mat, sys_value, dim_value, error_msg",
677+
[
678+
# Non-square matrix
679+
(np.ones((2, 3)), None, None, "square"),
688680
689-
def test_dim_none_non_perfect_square():
690-
"""Test that non-perfect-square matrices with dim=None raise ValueError."""
691-
mat = np.ones((6, 6)) # sqrt(6) not integer
692-
with pytest.raises(ValueError, match="Cannot infer subsystem dimensions directly"):
693-
partial_trace(mat, [0], None)
681+
# Dim product mismatch
682+
(np.eye(4), [0], [2, 3], "Product of `dim`"),
694683
684+
# dim=None and non-perfect-square
685+
(np.ones((6, 6)), [0], None, "Cannot infer subsystem dimensions directly"),
695686
696-
def test_dim_invalid_type_branch():
697-
"""Test that invalid dim type raises ValueError."""
698-
mat = np.eye(4)
699-
with pytest.raises(ValueError):
700-
partial_trace(mat, [0], (2, 2)) # tuple instead of list
687+
# Invalid dim type
688+
(np.eye(4), [0], (2, 2), None),
689+
],
690+
)
691+
def test_partial_trace_invalid_inputs(input_mat, sys_value, dim_value, error_msg):
692+
"""Test various invalid parameter combinations."""
693+
if error_msg:
694+
with pytest.raises(ValueError, match=error_msg):
695+
partial_trace(input_mat, sys_value, dim_value)
696+
else:
697+
with pytest.raises(ValueError):
698+
partial_trace(input_mat, sys_value, dim_value)

0 commit comments

Comments
 (0)