From 6a6c8691fd92eeb67e6b85c8ac20bd6e0021404b Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Wed, 16 Oct 2024 14:01:37 +0530 Subject: [PATCH 01/16] adding perturb vectors --- toqito/matrix_ops/perturb_vector.py | 19 +++++++++++++++++++ .../matrix_ops/tests/test_perturb_vector.py | 2 ++ 2 files changed, 21 insertions(+) create mode 100644 toqito/matrix_ops/perturb_vector.py create mode 100644 toqito/matrix_ops/tests/test_perturb_vector.py diff --git a/toqito/matrix_ops/perturb_vector.py b/toqito/matrix_ops/perturb_vector.py new file mode 100644 index 00000000..25deed47 --- /dev/null +++ b/toqito/matrix_ops/perturb_vector.py @@ -0,0 +1,19 @@ +"""Perturb vectors is used to add a small random number/noise to each element of a vector.""" + +import numpy as np + + +def perturb_vectors(vectors: list[np.ndarray], eps: float = 0.1) -> list[np.ndarray]: + """Perturb the vectors by adding a small random number to each element. + + :param vectors: List of vectors to perturb. + :param eps: Amount by which to perturb vectors. + :return: Resulting list of perturbed vectors by a factor of epsilon. + """ + perturbed_vectors: list[np.ndarray] = [] + for i, v in enumerate(vectors): + perturbed_vectors.append(v + np.random.randn(v.shape[0]) * eps) + + # Normalize the vectors after perturbing them. + perturbed_vectors[i] = perturbed_vectors[i] / np.linalg.norm(perturbed_vectors[i]) + return np.array(perturbed_vectors) diff --git a/toqito/matrix_ops/tests/test_perturb_vector.py b/toqito/matrix_ops/tests/test_perturb_vector.py new file mode 100644 index 00000000..05306796 --- /dev/null +++ b/toqito/matrix_ops/tests/test_perturb_vector.py @@ -0,0 +1,2 @@ +"""Test perturb vector.""" + From 996429484fd0a2a36c7a370141f65942e55fc0bb Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Wed, 16 Oct 2024 16:29:51 +0530 Subject: [PATCH 02/16] update perturb vectors --- .../{perturb_vector.py => perturb_vectors.py} | 5 +- .../matrix_ops/tests/test_perturb_vector.py | 67 ++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) rename toqito/matrix_ops/{perturb_vector.py => perturb_vectors.py} (80%) diff --git a/toqito/matrix_ops/perturb_vector.py b/toqito/matrix_ops/perturb_vectors.py similarity index 80% rename from toqito/matrix_ops/perturb_vector.py rename to toqito/matrix_ops/perturb_vectors.py index 25deed47..bad7c118 100644 --- a/toqito/matrix_ops/perturb_vector.py +++ b/toqito/matrix_ops/perturb_vectors.py @@ -1,4 +1,7 @@ -"""Perturb vectors is used to add a small random number/noise to each element of a vector.""" +"""Perturb vectors is used to add a small random number to each element of a vector. + +A random value is added sampled from a normal distribution scaled by `eps`. +""" import numpy as np diff --git a/toqito/matrix_ops/tests/test_perturb_vector.py b/toqito/matrix_ops/tests/test_perturb_vector.py index 05306796..95fb518d 100644 --- a/toqito/matrix_ops/tests/test_perturb_vector.py +++ b/toqito/matrix_ops/tests/test_perturb_vector.py @@ -1,2 +1,67 @@ -"""Test perturb vector.""" +"""Test perturb vectors.""" + +import numpy as np +import pytest + +from toqito.matrix_ops import perturb_vectors + + +@pytest.mark.parametrize( + "vectors, eps, expected_length", + [ + # Test with three vectors along the axes + ([np.array([1, 0, 0]), np.array([0, 1, 0]), np.array([0, 0, 1])], 0.1, 3), + # Test with one vector and eps=0 + ([np.array([1, 1, 0])], 0.0, 1), + # Test with two vectors and a different perturbation value + ([np.array([1, 0, 0]), np.array([0, 1, 0])], 0.2, 2), + ], +) +def test_output_size(vectors, eps, expected_length): + """Test that the function returns the same number of vectors as input.""" + perturbed_vectors = perturb_vectors(vectors, eps) + assert len(perturbed_vectors) == expected_length + + +@pytest.mark.parametrize( + "vectors, eps", + [ + ([np.array([1, 0, 0]), np.array([0, 1, 0]), np.array([0, 0, 1])], 0.1), + ([np.array([1, 1, 1])], 0.1), + ], +) +def test_normalization(vectors, eps): + """Test that each perturbed vector is normalized.""" + perturbed_vectors = perturb_vectors(vectors, eps) + for pv in perturbed_vectors: + norm = np.linalg.norm(pv) + assert np.isclose(norm, 1.0, atol=1e-5) + + +@pytest.mark.parametrize( + "vectors, eps", + [ + ([np.array([1, 0, 0]), np.array([0, 1, 0])], 0.1), + ([np.array([1, 1, 1])], 0.2), + ], +) +def test_perturbation_effect(vectors, eps): + """Test that the perturbed vectors are different from the original vectors.""" + perturbed_vectors = perturb_vectors(vectors, eps) + for i in range(len(vectors)): + assert not np.array_equal(vectors[i], perturbed_vectors[i]) + + +@pytest.mark.parametrize( + "vectors", + [ + ([np.array([1, 0, 0]), np.array([0, 1, 0])]), + ([np.array([1, 1, 1])]), + ], +) +def test_zero_perturbation(vectors): + """Test that if eps = 0, the vectors remain the same.""" + perturbed_vectors = perturb_vectors(vectors, eps=0.0) + for i in range(len(vectors)): + np.testing.assert_array_almost_equal(vectors[i], perturbed_vectors[i]) From fb9d96f2b28c57cab64ec1cd9bc88c5c29345186 Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Wed, 16 Oct 2024 16:47:59 +0530 Subject: [PATCH 03/16] editing module import --- toqito/matrix_ops/tests/test_perturb_vector.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toqito/matrix_ops/tests/test_perturb_vector.py b/toqito/matrix_ops/tests/test_perturb_vector.py index 95fb518d..dd0f31e6 100644 --- a/toqito/matrix_ops/tests/test_perturb_vector.py +++ b/toqito/matrix_ops/tests/test_perturb_vector.py @@ -3,7 +3,7 @@ import numpy as np import pytest -from toqito.matrix_ops import perturb_vectors +from toqito.matrix_ops.perturb_vectors import perturb_vectors @pytest.mark.parametrize( From 6cc582114f6ca27cbbf77007945ed14e8ae49edc Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Wed, 16 Oct 2024 16:51:52 +0530 Subject: [PATCH 04/16] update import modules --- toqito/matrix_ops/__init__.py | 2 ++ toqito/matrix_ops/tests/test_perturb_vector.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/toqito/matrix_ops/__init__.py b/toqito/matrix_ops/__init__.py index d6522b98..5eb79679 100644 --- a/toqito/matrix_ops/__init__.py +++ b/toqito/matrix_ops/__init__.py @@ -9,3 +9,5 @@ from toqito.matrix_ops.vectors_to_gram_matrix import vectors_to_gram_matrix from toqito.matrix_ops.calculate_vector_matrix_dimension import calculate_vector_matrix_dimension from toqito.matrix_ops.to_density_matrix import to_density_matrix +from toqito.matrix_ops.perturb_vectors import perturb_vectors + diff --git a/toqito/matrix_ops/tests/test_perturb_vector.py b/toqito/matrix_ops/tests/test_perturb_vector.py index dd0f31e6..95fb518d 100644 --- a/toqito/matrix_ops/tests/test_perturb_vector.py +++ b/toqito/matrix_ops/tests/test_perturb_vector.py @@ -3,7 +3,7 @@ import numpy as np import pytest -from toqito.matrix_ops.perturb_vectors import perturb_vectors +from toqito.matrix_ops import perturb_vectors @pytest.mark.parametrize( From 1b1ecc71d7f3d987626fa11d5d563371ea2d52a0 Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Wed, 16 Oct 2024 17:03:52 +0530 Subject: [PATCH 05/16] support for no perturbations --- toqito/matrix_ops/perturb_vectors.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/toqito/matrix_ops/perturb_vectors.py b/toqito/matrix_ops/perturb_vectors.py index bad7c118..54fa924e 100644 --- a/toqito/matrix_ops/perturb_vectors.py +++ b/toqito/matrix_ops/perturb_vectors.py @@ -15,8 +15,11 @@ def perturb_vectors(vectors: list[np.ndarray], eps: float = 0.1) -> list[np.ndar """ perturbed_vectors: list[np.ndarray] = [] for i, v in enumerate(vectors): - perturbed_vectors.append(v + np.random.randn(v.shape[0]) * eps) + if eps == 0: + perturbed_vectors.append(v) + else: + perturbed_vectors.append(v + np.random.randn(v.shape[0]) * eps) - # Normalize the vectors after perturbing them. - perturbed_vectors[i] = perturbed_vectors[i] / np.linalg.norm(perturbed_vectors[i]) + # Normalize the vectors after perturbing them. + perturbed_vectors[i] = perturbed_vectors[i] / np.linalg.norm(perturbed_vectors[i]) return np.array(perturbed_vectors) From d60301117f4a5c8eb6ded62b0483a4a4b009d5f0 Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Tue, 22 Oct 2024 22:28:21 +0530 Subject: [PATCH 06/16] adding docstring example --- toqito/matrix_ops/perturb_vectors.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/toqito/matrix_ops/perturb_vectors.py b/toqito/matrix_ops/perturb_vectors.py index 54fa924e..4e21b899 100644 --- a/toqito/matrix_ops/perturb_vectors.py +++ b/toqito/matrix_ops/perturb_vectors.py @@ -12,6 +12,16 @@ def perturb_vectors(vectors: list[np.ndarray], eps: float = 0.1) -> list[np.ndar :param vectors: List of vectors to perturb. :param eps: Amount by which to perturb vectors. :return: Resulting list of perturbed vectors by a factor of epsilon. + + Example: + >>> vectors = [np.array([1.0, 2.0]), np.array([3.0, 4.0])] + >>> perturbed_vectors = perturb_vectors(vectors, eps=0.1) + >>> for v in perturbed_vectors: + ... print(v) + ... + [0.997, 2.001] # The values will be slightly perturbed + [2.999, 4.003] # Actual values will vary due to randomness + """ perturbed_vectors: list[np.ndarray] = [] for i, v in enumerate(vectors): From fb2ec7221a45310b82470d7afef2138cadede904 Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Tue, 22 Oct 2024 22:40:11 +0530 Subject: [PATCH 07/16] adding imports --- toqito/matrix_ops/perturb_vectors.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/toqito/matrix_ops/perturb_vectors.py b/toqito/matrix_ops/perturb_vectors.py index 4e21b899..e0be5045 100644 --- a/toqito/matrix_ops/perturb_vectors.py +++ b/toqito/matrix_ops/perturb_vectors.py @@ -14,6 +14,8 @@ def perturb_vectors(vectors: list[np.ndarray], eps: float = 0.1) -> list[np.ndar :return: Resulting list of perturbed vectors by a factor of epsilon. Example: + >>> import numpy as np + >>> from toqito.matrix_ops import perturb_vectors >>> vectors = [np.array([1.0, 2.0]), np.array([3.0, 4.0])] >>> perturbed_vectors = perturb_vectors(vectors, eps=0.1) >>> for v in perturbed_vectors: From ea71ba866505689319c1bd5db78d8e30b8a5ab91 Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Tue, 22 Oct 2024 23:16:06 +0530 Subject: [PATCH 08/16] update import order --- toqito/matrix_ops/perturb_vectors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toqito/matrix_ops/perturb_vectors.py b/toqito/matrix_ops/perturb_vectors.py index e0be5045..3aa44e9f 100644 --- a/toqito/matrix_ops/perturb_vectors.py +++ b/toqito/matrix_ops/perturb_vectors.py @@ -14,8 +14,8 @@ def perturb_vectors(vectors: list[np.ndarray], eps: float = 0.1) -> list[np.ndar :return: Resulting list of perturbed vectors by a factor of epsilon. Example: - >>> import numpy as np >>> from toqito.matrix_ops import perturb_vectors + >>> import numpy as np >>> vectors = [np.array([1.0, 2.0]), np.array([3.0, 4.0])] >>> perturbed_vectors = perturb_vectors(vectors, eps=0.1) >>> for v in perturbed_vectors: From d54f4f18b03e25943cdc90bf140c674d55c6b1e6 Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Tue, 22 Oct 2024 23:33:11 +0530 Subject: [PATCH 09/16] removing output in example --- toqito/matrix_ops/perturb_vectors.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/toqito/matrix_ops/perturb_vectors.py b/toqito/matrix_ops/perturb_vectors.py index 3aa44e9f..ba265164 100644 --- a/toqito/matrix_ops/perturb_vectors.py +++ b/toqito/matrix_ops/perturb_vectors.py @@ -20,9 +20,6 @@ def perturb_vectors(vectors: list[np.ndarray], eps: float = 0.1) -> list[np.ndar >>> perturbed_vectors = perturb_vectors(vectors, eps=0.1) >>> for v in perturbed_vectors: ... print(v) - ... - [0.997, 2.001] # The values will be slightly perturbed - [2.999, 4.003] # Actual values will vary due to randomness """ perturbed_vectors: list[np.ndarray] = [] From c2a92bc773f51e96bb6a86b1e11428a3c2d7c8c9 Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Tue, 22 Oct 2024 23:38:22 +0530 Subject: [PATCH 10/16] update --- toqito/matrix_ops/perturb_vectors.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/toqito/matrix_ops/perturb_vectors.py b/toqito/matrix_ops/perturb_vectors.py index ba265164..1a5e79ba 100644 --- a/toqito/matrix_ops/perturb_vectors.py +++ b/toqito/matrix_ops/perturb_vectors.py @@ -18,8 +18,10 @@ def perturb_vectors(vectors: list[np.ndarray], eps: float = 0.1) -> list[np.ndar >>> import numpy as np >>> vectors = [np.array([1.0, 2.0]), np.array([3.0, 4.0])] >>> perturbed_vectors = perturb_vectors(vectors, eps=0.1) - >>> for v in perturbed_vectors: - ... print(v) + ... + # `perturbed_vectors` contain the new vectors + # The values will be slightly perturbed + # Actual values will vary due to randomness """ perturbed_vectors: list[np.ndarray] = [] From 2f9db3e53bb382a6f68541d79a47a27e1104c510 Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Tue, 22 Oct 2024 23:41:31 +0530 Subject: [PATCH 11/16] update --- toqito/matrix_ops/perturb_vectors.py | 1 - 1 file changed, 1 deletion(-) diff --git a/toqito/matrix_ops/perturb_vectors.py b/toqito/matrix_ops/perturb_vectors.py index 1a5e79ba..c002db2f 100644 --- a/toqito/matrix_ops/perturb_vectors.py +++ b/toqito/matrix_ops/perturb_vectors.py @@ -18,7 +18,6 @@ def perturb_vectors(vectors: list[np.ndarray], eps: float = 0.1) -> list[np.ndar >>> import numpy as np >>> vectors = [np.array([1.0, 2.0]), np.array([3.0, 4.0])] >>> perturbed_vectors = perturb_vectors(vectors, eps=0.1) - ... # `perturbed_vectors` contain the new vectors # The values will be slightly perturbed # Actual values will vary due to randomness From d14b7dc3dae2a49951b2cd587b02acc69ec81cae Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Tue, 22 Oct 2024 23:44:51 +0530 Subject: [PATCH 12/16] removing comments --- toqito/matrix_ops/perturb_vectors.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/toqito/matrix_ops/perturb_vectors.py b/toqito/matrix_ops/perturb_vectors.py index c002db2f..725c1e0a 100644 --- a/toqito/matrix_ops/perturb_vectors.py +++ b/toqito/matrix_ops/perturb_vectors.py @@ -18,9 +18,6 @@ def perturb_vectors(vectors: list[np.ndarray], eps: float = 0.1) -> list[np.ndar >>> import numpy as np >>> vectors = [np.array([1.0, 2.0]), np.array([3.0, 4.0])] >>> perturbed_vectors = perturb_vectors(vectors, eps=0.1) - # `perturbed_vectors` contain the new vectors - # The values will be slightly perturbed - # Actual values will vary due to randomness """ perturbed_vectors: list[np.ndarray] = [] From e62c5d6f9fcb4ccbfa5dffd85758e9bdc7cce072 Mon Sep 17 00:00:00 2001 From: Shivansh Mittal <88429611+Shivansh20128@users.noreply.github.com> Date: Thu, 24 Oct 2024 10:49:28 +0530 Subject: [PATCH 13/16] Update toqito/matrix_ops/perturb_vectors.py Co-authored-by: Purva Thakre <66048318+purva-thakre@users.noreply.github.com> --- toqito/matrix_ops/perturb_vectors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toqito/matrix_ops/perturb_vectors.py b/toqito/matrix_ops/perturb_vectors.py index 725c1e0a..058edfa5 100644 --- a/toqito/matrix_ops/perturb_vectors.py +++ b/toqito/matrix_ops/perturb_vectors.py @@ -17,7 +17,7 @@ def perturb_vectors(vectors: list[np.ndarray], eps: float = 0.1) -> list[np.ndar >>> from toqito.matrix_ops import perturb_vectors >>> import numpy as np >>> vectors = [np.array([1.0, 2.0]), np.array([3.0, 4.0])] - >>> perturbed_vectors = perturb_vectors(vectors, eps=0.1) + >>> perturb_vectors(vectors, eps=0.1) """ perturbed_vectors: list[np.ndarray] = [] From 4776c9265f4c1fc595e19903f44be6e100495f9a Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Thu, 24 Oct 2024 10:59:55 +0530 Subject: [PATCH 14/16] removing np.testing --- toqito/matrix_ops/tests/test_perturb_vector.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toqito/matrix_ops/tests/test_perturb_vector.py b/toqito/matrix_ops/tests/test_perturb_vector.py index 95fb518d..ee0182ef 100644 --- a/toqito/matrix_ops/tests/test_perturb_vector.py +++ b/toqito/matrix_ops/tests/test_perturb_vector.py @@ -63,5 +63,5 @@ def test_zero_perturbation(vectors): """Test that if eps = 0, the vectors remain the same.""" perturbed_vectors = perturb_vectors(vectors, eps=0.0) for i in range(len(vectors)): - np.testing.assert_array_almost_equal(vectors[i], perturbed_vectors[i]) + assert np.allclose(vectors[i], perturbed_vectors[i]), f"Vector {i} does not match." From ca156acd6d76ef48be9b4fad3b5fa3ede0484d3a Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Thu, 24 Oct 2024 11:01:06 +0530 Subject: [PATCH 15/16] update perturb_vectors --- toqito/matrix_ops/perturb_vectors.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/toqito/matrix_ops/perturb_vectors.py b/toqito/matrix_ops/perturb_vectors.py index 058edfa5..33946f34 100644 --- a/toqito/matrix_ops/perturb_vectors.py +++ b/toqito/matrix_ops/perturb_vectors.py @@ -14,10 +14,12 @@ def perturb_vectors(vectors: list[np.ndarray], eps: float = 0.1) -> list[np.ndar :return: Resulting list of perturbed vectors by a factor of epsilon. Example: + ========== + >>> from toqito.matrix_ops import perturb_vectors >>> import numpy as np >>> vectors = [np.array([1.0, 2.0]), np.array([3.0, 4.0])] - >>> perturb_vectors(vectors, eps=0.1) + >>> perturb_vectors(vectors, eps=0.1) # doctest: +SKIP """ perturbed_vectors: list[np.ndarray] = [] From f18a7b2573caafdf6ad05891370a3c292610b608 Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Sun, 3 Nov 2024 22:00:53 +0530 Subject: [PATCH 16/16] adding output manually --- toqito/matrix_ops/perturb_vectors.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/toqito/matrix_ops/perturb_vectors.py b/toqito/matrix_ops/perturb_vectors.py index 33946f34..eba25d31 100644 --- a/toqito/matrix_ops/perturb_vectors.py +++ b/toqito/matrix_ops/perturb_vectors.py @@ -20,6 +20,8 @@ def perturb_vectors(vectors: list[np.ndarray], eps: float = 0.1) -> list[np.ndar >>> import numpy as np >>> vectors = [np.array([1.0, 2.0]), np.array([3.0, 4.0])] >>> perturb_vectors(vectors, eps=0.1) # doctest: +SKIP + array([[0.47687587, 0.87897065], + [0.58715549, 0.80947417]]) """ perturbed_vectors: list[np.ndarray] = []