From 5ca54ff38f0f1dc181a51c9b1eb187cc65547532 Mon Sep 17 00:00:00 2001 From: Cody Wang Date: Thu, 24 Oct 2024 15:40:29 -0700 Subject: [PATCH 1/2] fix: Raise exception for `probs` with observable --- setup.py | 2 +- src/braket/pennylane_plugin/translation.py | 2 ++ test/unit_tests/test_translation.py | 11 +++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index f82ce6d0..b586489f 100644 --- a/setup.py +++ b/setup.py @@ -53,7 +53,7 @@ }, extras_require={ "test": [ - "autoray<0.7.0", # autoray.tensorflow_diag no longer works + "autoray<0.7.0", # autoray.tensorflow_diag no longer works "docutils>=0.19", "flaky", "pre-commit", diff --git a/src/braket/pennylane_plugin/translation.py b/src/braket/pennylane_plugin/translation.py index aab1a20f..76c34622 100644 --- a/src/braket/pennylane_plugin/translation.py +++ b/src/braket/pennylane_plugin/translation.py @@ -576,6 +576,8 @@ def translate_result_type( # noqa: C901 observable = measurement.obs if return_type is ObservableReturnTypes.Probability: + if observable: + raise qml.DeviceError("Probability result type not supported for observables") return Probability(targets) if return_type is ObservableReturnTypes.State: diff --git a/test/unit_tests/test_translation.py b/test/unit_tests/test_translation.py index 5c719e68..6d07d2fe 100644 --- a/test/unit_tests/test_translation.py +++ b/test/unit_tests/test_translation.py @@ -826,6 +826,17 @@ def test_translate_result_type_unsupported_obs(): translate_result_type(tape.measurements[0], [0], frozenset()) +def test_translate_result_type_probs_observable(): + """Tests if a DeviceError is raised by translate_result_type for a Probability return type + with an observable attached""" + mp = qml.probs(op=qml.X(wires=0)) + + with pytest.raises( + qml.DeviceError, match="Probability result type not supported for observables" + ): + translate_result_type(mp, [0], frozenset()) + + def test_translate_result(): result_dict = _result_meta() result_dict["resultTypes"] = [ From 27bd6a9b1768a5d56acaf7df3151fbc8c1e86358 Mon Sep 17 00:00:00 2001 From: Cody Wang Date: Mon, 28 Oct 2024 12:39:24 -0700 Subject: [PATCH 2/2] Update src/braket/pennylane_plugin/translation.py Co-authored-by: Christina Lee --- src/braket/pennylane_plugin/translation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/braket/pennylane_plugin/translation.py b/src/braket/pennylane_plugin/translation.py index 76c34622..d8cad22c 100644 --- a/src/braket/pennylane_plugin/translation.py +++ b/src/braket/pennylane_plugin/translation.py @@ -576,7 +576,7 @@ def translate_result_type( # noqa: C901 observable = measurement.obs if return_type is ObservableReturnTypes.Probability: - if observable: + if observable and observable.diagonalizing_gates(): raise qml.DeviceError("Probability result type not supported for observables") return Probability(targets)