diff --git a/docs/matrices.rst b/docs/matrices.rst index 467c3fd6..1c4e9469 100644 --- a/docs/matrices.rst +++ b/docs/matrices.rst @@ -65,6 +65,7 @@ Properties of Matrices and Vectors toqito.matrix_props.is_square toqito.matrix_props.is_symmetric toqito.matrix_props.is_unitary + toqito.matrix_props.kp_norm toqito.matrix_props.majorizes toqito.matrix_props.sk_operator_norm toqito.matrix_props.trace_norm \ No newline at end of file diff --git a/tests/test_matrix_props/test_kp_norm.py b/tests/test_matrix_props/test_kp_norm.py new file mode 100644 index 00000000..77aa9b9e --- /dev/null +++ b/tests/test_matrix_props/test_kp_norm.py @@ -0,0 +1,33 @@ +"""Tests for the S(k)-norm of a matrix.""" +# Tests in this file, follow the discussion of the characteristics of kp_norm at https://qetlab.com/kpNorm + +import re + +import numpy as np +import pytest + +from toqito.matrix_props import kp_norm +from toqito.random import random_unitary +from toqito.states import bell + + +@pytest.mark.parametrize( + "vector, k, p, norm_to_compare", + [ + # When (k=1, p= Inf)the kp_norm(vector) is the same as the trace norm (the 1-norm). + (bell(0), 1, np.inf, 1), + # When p=2 and k is greater than or equal to one of the input matrix dimensions, the value calculated is the frobenius norm. + (random_unitary(5), 5, 2, np.linalg.norm(random_unitary(5), "fro")), + ], +) +def test_kp_norm(vector, k, p, norm_to_compare): + calculated_kp_norm = kp_norm(vector, k, p) + assert calculated_kp_norm == pytest.approx(norm_to_compare) + + +def test_no_default_kp_values(): + """kp_norm does not have any default values for k or p.""" + with pytest.raises( + TypeError, match=re.escape("kp_norm() missing 2 required positional arguments: 'k' and 'p'") + ): + kp_norm(bell(0)) # pylint: disable=no-value-for-parameter diff --git a/tests/test_state_props/test_schmidt_rank.py b/tests/test_state_props/test_schmidt_rank.py index 307edc14..5c792b0e 100644 --- a/tests/test_state_props/test_schmidt_rank.py +++ b/tests/test_state_props/test_schmidt_rank.py @@ -3,7 +3,7 @@ import pytest from toqito.state_props import schmidt_rank -from toqito.states import basis, bell +from toqito.states import bell e_0, e_1 = np.array([[1], [0]]), np.array([[0], [1]]) @@ -41,7 +41,6 @@ def test_schmidt_rank_bell_state(rho, dim, expected_result): def test_schmidt_rank_entangled_state(): """Computing Schmidt rank of entangled state should be > 1.""" - e_0, e_1 = basis(2, 0), basis(2, 1) phi = ( (1 + np.sqrt(6)) / (2 * np.sqrt(6)) * np.kron(e_0, e_0) + (1 - np.sqrt(6)) / (2 * np.sqrt(6)) * np.kron(e_0, e_1) @@ -55,6 +54,5 @@ def test_schmidt_rank_singlet_state(): Computing the Schmidt rank of the entangled singlet state should yield a value greater than 1. """ - e_0, e_1 = basis(2, 0), basis(2, 1) rho = 1 / np.sqrt(2) * (np.kron(e_0, e_1) - np.kron(e_1, e_0)) np.testing.assert_equal(schmidt_rank(rho) > 1, True) diff --git a/toqito/helper/__init__.py b/toqito/helper/__init__.py index c6a8bff1..7e146ccb 100644 --- a/toqito/helper/__init__.py +++ b/toqito/helper/__init__.py @@ -4,5 +4,4 @@ from toqito.helper.update_odometer import update_odometer from toqito.helper.cvx_kron import cvx_kron from toqito.helper.npa_hierarchy import npa_constraints -from toqito.helper.kp_norm import kp_norm from toqito.helper.channel_dim import channel_dim diff --git a/toqito/matrix_props/__init__.py b/toqito/matrix_props/__init__.py index 3334efb5..d856187c 100644 --- a/toqito/matrix_props/__init__.py +++ b/toqito/matrix_props/__init__.py @@ -1,5 +1,6 @@ """Properties of matrices and vectors.""" from toqito.matrix_props.is_square import is_square +from toqito.matrix_props.kp_norm import kp_norm from toqito.matrix_props.is_hermitian import is_hermitian from toqito.matrix_props.is_positive_semidefinite import is_positive_semidefinite from toqito.matrix_props.is_density import is_density @@ -20,3 +21,4 @@ from toqito.matrix_props.is_block_positive import is_block_positive from toqito.matrix_props.trace_norm import trace_norm from toqito.matrix_props.is_diagonally_dominant import is_diagonally_dominant + diff --git a/toqito/helper/kp_norm.py b/toqito/matrix_props/kp_norm.py similarity index 91% rename from toqito/helper/kp_norm.py rename to toqito/matrix_props/kp_norm.py index 694eade3..4dad1035 100644 --- a/toqito/helper/kp_norm.py +++ b/toqito/matrix_props/kp_norm.py @@ -4,7 +4,7 @@ def kp_norm(mat: np.ndarray, k: int, p: int) -> float: """ - Compute the p-norm of the vector of the k-largest singular values of a matrix. + Compute the p-norm of the vector or the k-largest singular values of a matrix. :param mat: 2D numpy ndarray :param k: The number of singular values to take. diff --git a/toqito/matrix_props/sk_norm.py b/toqito/matrix_props/sk_norm.py index c497eee7..923abc77 100644 --- a/toqito/matrix_props/sk_norm.py +++ b/toqito/matrix_props/sk_norm.py @@ -8,8 +8,7 @@ import scipy from toqito.channels import partial_trace, partial_transpose, realignment -from toqito.helper import kp_norm -from toqito.matrix_props import is_hermitian +from toqito.matrix_props import is_hermitian, kp_norm from toqito.perms import swap, symmetric_projection from toqito.state_ops.schmidt_decomposition import schmidt_decomposition from toqito.state_props.schmidt_rank import schmidt_rank