From a735ce8cbd36322314ef323bf9468cf0dfd199dd Mon Sep 17 00:00:00 2001 From: albi3ro Date: Thu, 8 Sep 2022 13:34:04 -0400 Subject: [PATCH 1/3] qml.grouping.is_pauli_word returns false for non-observables --- doc/releases/changelog-dev.md | 2 ++ pennylane/grouping/utils.py | 2 +- tests/grouping/test_grouping_utils.py | 8 ++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 5ae70719f35..9e7121dbca0 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -296,6 +296,8 @@ and the diagonalising gates using the factors/summands instead of using the full matrix. [(#3022)](https://github.com/PennyLaneAI/pennylane/pull/3022) +* `qml.grouping.is_pauli_word` now returns `False` for non-Observables, instead of raising an error. +

Breaking changes

* Measuring an operator that might not be hermitian as an observable now raises a warning instead of an diff --git a/pennylane/grouping/utils.py b/pennylane/grouping/utils.py index af2c2db0454..dbc94eff30b 100644 --- a/pennylane/grouping/utils.py +++ b/pennylane/grouping/utils.py @@ -74,7 +74,7 @@ def is_pauli_word(observable): """ if not isinstance(observable, Observable): - raise TypeError(f"Expected {Observable} instance, instead got {type(observable)} instance.") + return False pauli_word_names = ["Identity", "PauliX", "PauliY", "PauliZ"] if isinstance(observable, Tensor): diff --git a/tests/grouping/test_grouping_utils.py b/tests/grouping/test_grouping_utils.py index 74335e58f56..ab0ff27d82a 100644 --- a/tests/grouping/test_grouping_utils.py +++ b/tests/grouping/test_grouping_utils.py @@ -256,6 +256,14 @@ def test_is_pauli_word(self): assert not is_pauli_word(observable_3) assert not is_pauli_word(observable_4) + def test_is_pauli_word_non_observable(self): + """Test that non-observables are not Pauli Words.""" + + class DummyOp(qml.operation.Operator): + num_wires = 1 + + assert not is_pauli_word(DummyOp(1)) + def test_are_identical_pauli_words(self): """Tests for determining if two Pauli words have the same ``wires`` and ``name`` attributes.""" From ca7486ec7a70580d07adef033754135c2f651c2f Mon Sep 17 00:00:00 2001 From: Christina Lee Date: Thu, 8 Sep 2022 13:41:28 -0400 Subject: [PATCH 2/3] Update doc/releases/changelog-dev.md --- doc/releases/changelog-dev.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 9e7121dbca0..86dfb9700c5 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -297,6 +297,7 @@ [(#3022)](https://github.com/PennyLaneAI/pennylane/pull/3022) * `qml.grouping.is_pauli_word` now returns `False` for non-Observables, instead of raising an error. + [(#3039)](https://github.com/PennyLaneAI/pennylane/pull/3039)

Breaking changes

From 53556dc297750b9d860682671d7fa05fe019560b Mon Sep 17 00:00:00 2001 From: Christina Lee Date: Thu, 8 Sep 2022 13:57:04 -0400 Subject: [PATCH 3/3] Update doc/releases/changelog-dev.md Co-authored-by: Jay Soni --- doc/releases/changelog-dev.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 86dfb9700c5..35034a4ed0f 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -296,7 +296,7 @@ and the diagonalising gates using the factors/summands instead of using the full matrix. [(#3022)](https://github.com/PennyLaneAI/pennylane/pull/3022) -* `qml.grouping.is_pauli_word` now returns `False` for non-Observables, instead of raising an error. +* `qml.grouping.is_pauli_word` now returns `False` for operators that don't inherit from `qml.Observable`, instead of raising an error. [(#3039)](https://github.com/PennyLaneAI/pennylane/pull/3039)

Breaking changes