Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pytest Coverage: kp_norm #246

Merged
merged 8 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/matrices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 33 additions & 0 deletions tests/test_matrix_props/test_kp_norm.py
Original file line number Diff line number Diff line change
@@ -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


purva-thakre marked this conversation as resolved.
Show resolved Hide resolved
@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
4 changes: 1 addition & 3 deletions tests/test_state_props/test_schmidt_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]])
Expand Down Expand Up @@ -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)
Expand All @@ -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)
1 change: 0 additions & 1 deletion toqito/helper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions toqito/matrix_props/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions toqito/matrix_props/sk_norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down