diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 5ae70719f35..35034a4ed0f 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -296,6 +296,9 @@ 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 operators that don't inherit from `qml.Observable`, instead of raising an error. + [(#3039)](https://github.com/PennyLaneAI/pennylane/pull/3039) +

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."""