From 3736dab040ff59c9328c1bd871a46e99ec6aff35 Mon Sep 17 00:00:00 2001 From: Anurav Modak Date: Wed, 18 Oct 2023 10:50:28 +0530 Subject: [PATCH 1/9] Add __iadd__ method to PauliSentence Signed-off-by: Anurav Modak --- pennylane/pauli/pauli_arithmetic.py | 9 +++++++++ tests/pauli/test_pauli_arithmetic.py | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/pennylane/pauli/pauli_arithmetic.py b/pennylane/pauli/pauli_arithmetic.py index 69fe9ebd528..52d2ba7069b 100644 --- a/pennylane/pauli/pauli_arithmetic.py +++ b/pennylane/pauli/pauli_arithmetic.py @@ -342,6 +342,15 @@ def __add__(self, other): larger_ps[key] += smaller_ps[key] return larger_ps + + def __iadd__(self, other): + """Inplace addition of two Pauli sentence together by adding terms of other to self""" + for key in other: + if key in self: + self[key] += other[key] + else: + self[key] = other[key] + return self def __copy__(self): """Copy the PauliSentence instance.""" diff --git a/tests/pauli/test_pauli_arithmetic.py b/tests/pauli/test_pauli_arithmetic.py index 86a5b429641..9a26477ad2c 100644 --- a/tests/pauli/test_pauli_arithmetic.py +++ b/tests/pauli/test_pauli_arithmetic.py @@ -409,6 +409,16 @@ def test_add(self, string1, string2, result): (ps5, "Can't get the matrix of an empty PauliSentence."), ) + @pytest.mark.parametrize("string1, string2, result", tup_ps_add) + def test_iadd(self, string1, string2, result): + """Test that the correct result of inplace addition is produced and other object is not changed.""" + original_string2=copy(string2) + string1 += string2 + string1.simplify() + + assert string1 == result # Check if the modified object matches the expected result + assert original_string2 == string2 # Ensure the original object is not modified + @pytest.mark.parametrize("ps, match", ps_match) def test_to_mat_error_empty(self, ps, match): """Test that an appropriate error is raised when an empty From fde6465e72815939b1fa20e39a879431136f563f Mon Sep 17 00:00:00 2001 From: Anurav Modak Date: Wed, 25 Oct 2023 22:55:43 +0530 Subject: [PATCH 2/9] Update changelog-dev.md --- doc/releases/changelog-dev.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index b904b6e4aa8..ce3f6223f12 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -217,6 +217,9 @@ of the computed qubit operator, if imaginary components are smaller than a threshold. [(#4639)](https://github.com/PennyLaneAI/pennylane/pull/4639) +* Added `__iadd__` method to PauliSentence, which enables inplace-addition using `+=`, we no longer need to perform a copy, leading to performance improvements. +[(#4662)](https://github.com/PennyLaneAI/pennylane/pull/4662) +

Breaking changes 💔

@@ -411,3 +414,4 @@ Daniel F. Nino, Mudit Pandey, Matthew Silverman, Jay Soni, +Anurav Modak. From 6e424df40ed911e2b883fc220ff628a21824214b Mon Sep 17 00:00:00 2001 From: Anurav Modak Date: Wed, 25 Oct 2023 22:56:56 +0530 Subject: [PATCH 3/9] Improvements Co-authored-by: Christina Lee --- pennylane/pauli/pauli_arithmetic.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pennylane/pauli/pauli_arithmetic.py b/pennylane/pauli/pauli_arithmetic.py index 52d2ba7069b..57cd0488f14 100644 --- a/pennylane/pauli/pauli_arithmetic.py +++ b/pennylane/pauli/pauli_arithmetic.py @@ -342,7 +342,6 @@ def __add__(self, other): larger_ps[key] += smaller_ps[key] return larger_ps - def __iadd__(self, other): """Inplace addition of two Pauli sentence together by adding terms of other to self""" for key in other: From 4e2fc462fdfc47e300b6f418b8807d47ce0269c8 Mon Sep 17 00:00:00 2001 From: Anurav Modak Date: Wed, 25 Oct 2023 22:57:21 +0530 Subject: [PATCH 4/9] Improvements Co-authored-by: Christina Lee --- tests/pauli/test_pauli_arithmetic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/pauli/test_pauli_arithmetic.py b/tests/pauli/test_pauli_arithmetic.py index 9a26477ad2c..ea69198549e 100644 --- a/tests/pauli/test_pauli_arithmetic.py +++ b/tests/pauli/test_pauli_arithmetic.py @@ -412,7 +412,7 @@ def test_add(self, string1, string2, result): @pytest.mark.parametrize("string1, string2, result", tup_ps_add) def test_iadd(self, string1, string2, result): """Test that the correct result of inplace addition is produced and other object is not changed.""" - original_string2=copy(string2) + original_string1=copy(string1) string1 += string2 string1.simplify() From e63e4cbc6b4502dac271e4e2af10cc5ce0c62cc7 Mon Sep 17 00:00:00 2001 From: Anurav Modak Date: Mon, 30 Oct 2023 21:18:24 +0530 Subject: [PATCH 5/9] Improvements Co-authored-by: Christina Lee --- tests/pauli/test_pauli_arithmetic.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/pauli/test_pauli_arithmetic.py b/tests/pauli/test_pauli_arithmetic.py index ea69198549e..c27810049d9 100644 --- a/tests/pauli/test_pauli_arithmetic.py +++ b/tests/pauli/test_pauli_arithmetic.py @@ -412,13 +412,13 @@ def test_add(self, string1, string2, result): @pytest.mark.parametrize("string1, string2, result", tup_ps_add) def test_iadd(self, string1, string2, result): """Test that the correct result of inplace addition is produced and other object is not changed.""" - original_string1=copy(string1) - string1 += string2 - string1.simplify() - - assert string1 == result # Check if the modified object matches the expected result - assert original_string2 == string2 # Ensure the original object is not modified + copied_string1 = copy(string1) + copied_string2 = copy(string2) + copied_string1 += copied_string2 + copied_string1.simplify() + assert copied_string1 == result # Check if the modified object matches the expected result + assert copied_string2 == string2 # Ensure the original object is not modified @pytest.mark.parametrize("ps, match", ps_match) def test_to_mat_error_empty(self, ps, match): """Test that an appropriate error is raised when an empty From 0f017b7d1bb2172fb3078de7a9a7635f914fd086 Mon Sep 17 00:00:00 2001 From: Anurav Modak Date: Mon, 30 Oct 2023 21:25:49 +0530 Subject: [PATCH 6/9] Improvements Co-authored-by: Christina Lee --- pennylane/pauli/pauli_arithmetic.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pennylane/pauli/pauli_arithmetic.py b/pennylane/pauli/pauli_arithmetic.py index 57cd0488f14..2b137225199 100644 --- a/pennylane/pauli/pauli_arithmetic.py +++ b/pennylane/pauli/pauli_arithmetic.py @@ -342,6 +342,7 @@ def __add__(self, other): larger_ps[key] += smaller_ps[key] return larger_ps + def __iadd__(self, other): """Inplace addition of two Pauli sentence together by adding terms of other to self""" for key in other: From 24d2c9d2c825122db60573b0bff0ff7224f537f9 Mon Sep 17 00:00:00 2001 From: Anurav Modak Date: Mon, 30 Oct 2023 21:31:13 +0530 Subject: [PATCH 7/9] Improvements Co-authored-by: Christina Lee --- tests/pauli/test_pauli_arithmetic.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/pauli/test_pauli_arithmetic.py b/tests/pauli/test_pauli_arithmetic.py index c27810049d9..de7f2984b97 100644 --- a/tests/pauli/test_pauli_arithmetic.py +++ b/tests/pauli/test_pauli_arithmetic.py @@ -419,6 +419,7 @@ def test_iadd(self, string1, string2, result): assert copied_string1 == result # Check if the modified object matches the expected result assert copied_string2 == string2 # Ensure the original object is not modified + @pytest.mark.parametrize("ps, match", ps_match) def test_to_mat_error_empty(self, ps, match): """Test that an appropriate error is raised when an empty From ca1ff35448021b8fcc7d6a3776d0d45bc67a5dc6 Mon Sep 17 00:00:00 2001 From: Anurav Modak Date: Mon, 30 Oct 2023 22:06:07 +0530 Subject: [PATCH 8/9] Improvements Co-authored-by: Christina Lee --- tests/pauli/test_pauli_arithmetic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/pauli/test_pauli_arithmetic.py b/tests/pauli/test_pauli_arithmetic.py index de7f2984b97..2fbf8f2384b 100644 --- a/tests/pauli/test_pauli_arithmetic.py +++ b/tests/pauli/test_pauli_arithmetic.py @@ -418,7 +418,7 @@ def test_iadd(self, string1, string2, result): copied_string1.simplify() assert copied_string1 == result # Check if the modified object matches the expected result - assert copied_string2 == string2 # Ensure the original object is not modified + assert copied_string2 == string2 # Ensure the original object is not modified @pytest.mark.parametrize("ps, match", ps_match) def test_to_mat_error_empty(self, ps, match): From a6bd157aa324be6b45c454ab186e23927327bfc2 Mon Sep 17 00:00:00 2001 From: Anurav Modak Date: Mon, 30 Oct 2023 22:16:55 +0530 Subject: [PATCH 9/9] Improvements Co-authored-by: Christina Lee --- tests/pauli/test_pauli_arithmetic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/pauli/test_pauli_arithmetic.py b/tests/pauli/test_pauli_arithmetic.py index 2fbf8f2384b..e67ecbc8cb7 100644 --- a/tests/pauli/test_pauli_arithmetic.py +++ b/tests/pauli/test_pauli_arithmetic.py @@ -417,8 +417,8 @@ def test_iadd(self, string1, string2, result): copied_string1 += copied_string2 copied_string1.simplify() - assert copied_string1 == result # Check if the modified object matches the expected result - assert copied_string2 == string2 # Ensure the original object is not modified + assert copied_string1 == result # Check if the modified object matches the expected result + assert copied_string2 == string2 # Ensure the original object is not modified @pytest.mark.parametrize("ps, match", ps_match) def test_to_mat_error_empty(self, ps, match):