From f51a6d4efaf7b747741b6ffa023d51e1a876c7fb Mon Sep 17 00:00:00 2001 From: Purva Thakre Date: Wed, 15 Nov 2023 07:24:48 -0600 Subject: [PATCH 1/8] move kp_norm --- toqito/helper/__init__.py | 1 - toqito/matrix_props/__init__.py | 2 ++ toqito/{helper => matrix_props}/kp_norm.py | 0 toqito/matrix_props/sk_norm.py | 3 +-- 4 files changed, 3 insertions(+), 3 deletions(-) rename toqito/{helper => matrix_props}/kp_norm.py (100%) 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 100% rename from toqito/helper/kp_norm.py rename to toqito/matrix_props/kp_norm.py 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 From 495c6a0f33f6ac783efee60c3f2b66b7ffda9adf Mon Sep 17 00:00:00 2001 From: Purva Thakre Date: Wed, 15 Nov 2023 20:01:57 -0600 Subject: [PATCH 2/8] pylint bug --- tests/test_state_props/test_schmidt_rank.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_state_props/test_schmidt_rank.py b/tests/test_state_props/test_schmidt_rank.py index 307edc14..7fc47635 100644 --- a/tests/test_state_props/test_schmidt_rank.py +++ b/tests/test_state_props/test_schmidt_rank.py @@ -6,7 +6,7 @@ from toqito.states import basis, bell -e_0, e_1 = np.array([[1], [0]]), np.array([[0], [1]]) +bas_0, bas_1 = np.array([[1], [0]]), np.array([[0], [1]]) @pytest.mark.parametrize("rho, dim, expected_result", [ @@ -17,7 +17,7 @@ # Computing the Schmidt rank of Bell state with list as argument for dims. (bell(0), [2, 2], 2), # Computing the Schmidt rank of a separable state should yield a value equal to 1. - (1 / 2 * (np.kron(e_0, e_0) - np.kron(e_0, e_1) - np.kron(e_1, e_0) + np.kron(e_1, e_1)), None, 1), + (1 / 2 * (np.kron(bas_0, bas_0) - np.kron(bas_0, bas_1) - np.kron(bas_1, bas_0) + np.kron(bas_1, bas_1)), None, 1), # Computing Schmidt rank of separable density matrix should be 1. (np.identity(4), None, 1), # Computing Schmidt rank of separable density matrix should be 1. From 75668738fc20b8d77cff6041e3737299ecb83a8d Mon Sep 17 00:00:00 2001 From: Purva Thakre Date: Thu, 16 Nov 2023 10:37:46 -0600 Subject: [PATCH 3/8] add tests --- docs/matrices.rst | 1 + tests/test_matrix_props/test_kp_norm.py | 36 +++++++++++++++++++++++++ toqito/matrix_props/kp_norm.py | 2 +- 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/test_matrix_props/test_kp_norm.py 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..2c53a383 --- /dev/null +++ b/tests/test_matrix_props/test_kp_norm.py @@ -0,0 +1,36 @@ +"""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 numpy as np +import pytest +import re + +from toqito.matrix_props import kp_norm, trace_norm +from toqito.states import bell + + +def test_operator_norm(): + """When (k=1, p= Inf)the kp_norm(vector) is the same as the trace norm.""" + calculated_kp_norm = kp_norm(bell(0), 1, np.inf) + expected_kp_norm = trace_norm(bell(0)) + assert calculated_kp_norm == expected_kp_norm + + +def test_frobenius_norm(): + """When p=2 and k is greater than or equal to one of the input matrice's + dimensions, the value calculated is the frobenius norm.""" + input_mat = np.random.rand(5, 4) + k = min(input_mat.shape) + p = 2 + calculated_value = kp_norm(input_mat, k, p) + expected_value = np.linalg.norm(input_mat, ord="fro") + assert calculated_value == expected_value + + +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)) + diff --git a/toqito/matrix_props/kp_norm.py b/toqito/matrix_props/kp_norm.py index 694eade3..4dad1035 100644 --- a/toqito/matrix_props/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. From 1c001418a9049d0a80d408bbbc2e46d2d71fdb9c Mon Sep 17 00:00:00 2001 From: Purva Thakre Date: Thu, 16 Nov 2023 10:38:27 -0600 Subject: [PATCH 4/8] use black --- tests/test_matrix_props/test_kp_norm.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/test_matrix_props/test_kp_norm.py b/tests/test_matrix_props/test_kp_norm.py index 2c53a383..7c8c6e66 100644 --- a/tests/test_matrix_props/test_kp_norm.py +++ b/tests/test_matrix_props/test_kp_norm.py @@ -30,7 +30,6 @@ def test_frobenius_norm(): 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)) - + TypeError, match=re.escape("kp_norm() missing 2 required positional arguments: 'k' and 'p'") + ): + kp_norm(bell(0)) From 37c7208440c8bb4969f9ef0e9929c2f1adc0fb6d Mon Sep 17 00:00:00 2001 From: Purva Thakre Date: Thu, 16 Nov 2023 10:46:12 -0600 Subject: [PATCH 5/8] pylint kp_norm --- tests/test_matrix_props/test_kp_norm.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_matrix_props/test_kp_norm.py b/tests/test_matrix_props/test_kp_norm.py index 7c8c6e66..59e14d5c 100644 --- a/tests/test_matrix_props/test_kp_norm.py +++ b/tests/test_matrix_props/test_kp_norm.py @@ -1,9 +1,10 @@ """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 -import re from toqito.matrix_props import kp_norm, trace_norm from toqito.states import bell @@ -32,4 +33,4 @@ def test_no_default_kp_values(): with pytest.raises( TypeError, match=re.escape("kp_norm() missing 2 required positional arguments: 'k' and 'p'") ): - kp_norm(bell(0)) + kp_norm(bell(0)) # pylint: disable=no-value-for-parameter From 52d8b82242bc4525b52fd1eaf106d0b76a5b804e Mon Sep 17 00:00:00 2001 From: Purva Thakre Date: Thu, 16 Nov 2023 17:31:00 -0600 Subject: [PATCH 6/8] Revert "pylint bug" This reverts commit 495c6a0f33f6ac783efee60c3f2b66b7ffda9adf. --- tests/test_state_props/test_schmidt_rank.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_state_props/test_schmidt_rank.py b/tests/test_state_props/test_schmidt_rank.py index 7fc47635..307edc14 100644 --- a/tests/test_state_props/test_schmidt_rank.py +++ b/tests/test_state_props/test_schmidt_rank.py @@ -6,7 +6,7 @@ from toqito.states import basis, bell -bas_0, bas_1 = np.array([[1], [0]]), np.array([[0], [1]]) +e_0, e_1 = np.array([[1], [0]]), np.array([[0], [1]]) @pytest.mark.parametrize("rho, dim, expected_result", [ @@ -17,7 +17,7 @@ # Computing the Schmidt rank of Bell state with list as argument for dims. (bell(0), [2, 2], 2), # Computing the Schmidt rank of a separable state should yield a value equal to 1. - (1 / 2 * (np.kron(bas_0, bas_0) - np.kron(bas_0, bas_1) - np.kron(bas_1, bas_0) + np.kron(bas_1, bas_1)), None, 1), + (1 / 2 * (np.kron(e_0, e_0) - np.kron(e_0, e_1) - np.kron(e_1, e_0) + np.kron(e_1, e_1)), None, 1), # Computing Schmidt rank of separable density matrix should be 1. (np.identity(4), None, 1), # Computing Schmidt rank of separable density matrix should be 1. From 61e83fcd205131545111981d9e841e3b832746d0 Mon Sep 17 00:00:00 2001 From: Purva Thakre Date: Thu, 16 Nov 2023 17:38:56 -0600 Subject: [PATCH 7/8] vincent's feedback: pylint schmidt rank --- tests/test_state_props/test_schmidt_rank.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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) From ba7a4473d7cb684c3246225f89cd2445ca7482df Mon Sep 17 00:00:00 2001 From: Purva Thakre Date: Thu, 16 Nov 2023 18:03:17 -0600 Subject: [PATCH 8/8] vincent' feedback: kp_norm pytest parametrize --- tests/test_matrix_props/test_kp_norm.py | 31 +++++++++++-------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/tests/test_matrix_props/test_kp_norm.py b/tests/test_matrix_props/test_kp_norm.py index 59e14d5c..77aa9b9e 100644 --- a/tests/test_matrix_props/test_kp_norm.py +++ b/tests/test_matrix_props/test_kp_norm.py @@ -6,26 +6,23 @@ import numpy as np import pytest -from toqito.matrix_props import kp_norm, trace_norm +from toqito.matrix_props import kp_norm +from toqito.random import random_unitary from toqito.states import bell -def test_operator_norm(): - """When (k=1, p= Inf)the kp_norm(vector) is the same as the trace norm.""" - calculated_kp_norm = kp_norm(bell(0), 1, np.inf) - expected_kp_norm = trace_norm(bell(0)) - assert calculated_kp_norm == expected_kp_norm - - -def test_frobenius_norm(): - """When p=2 and k is greater than or equal to one of the input matrice's - dimensions, the value calculated is the frobenius norm.""" - input_mat = np.random.rand(5, 4) - k = min(input_mat.shape) - p = 2 - calculated_value = kp_norm(input_mat, k, p) - expected_value = np.linalg.norm(input_mat, ord="fro") - assert calculated_value == expected_value +@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():