Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise an error if qml.probs is called without wires or obs #2438

Merged
merged 9 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ the `decimals` and `show_matrices` keywords are added. `qml.drawer.tape_text(tap

<h3>Bug fixes</h3>

* Fixes a bug by adding a comprehensible error message for calling `qml.probs`
without passing wires or an observable.
[(#2438)](https://github.com/PennyLaneAI/pennylane/pull/2438)

* Call `pip show` with the subprocess library to avoid outputting a common warning.
[(#2422)](https://github.com/PennyLaneAI/pennylane/pull/2422)

Expand Down
7 changes: 7 additions & 0 deletions pennylane/measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def diagonalizing_gates(self):
List[.Operation]: the operations that diagonalize the observables
"""
try:
# pylint: disable=no-member
return self.expand().operations
except qml.operation.DecompositionUndefinedError:
return []
Expand Down Expand Up @@ -517,6 +518,12 @@ def circuit():
the computational basis
"""
# pylint: disable=protected-access

if wires is None and op is None:
raise qml.QuantumFunctionError(
"qml.probs requires either the wires or the observable to be passed."
)

if isinstance(op, qml.Hamiltonian):
raise qml.QuantumFunctionError("Hamiltonians are not supported for rotating probabilities.")

Expand Down
19 changes: 19 additions & 0 deletions tests/test_prob.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,25 @@ def circuit():
circuit()


def test_probs_no_wires_obs_raises():
"""Test that an informative error is raised when no wires or observables
are passed to qml.probs."""
num_wires = 1

dev = qml.device("default.qubit", wires=num_wires, shots=None)

@qml.qnode(dev)
def circuit_probs():
qml.RY(0.34, wires=0)
return qml.probs()

with pytest.raises(
qml.QuantumFunctionError,
match="qml.probs requires either the wires or the observable to be passed.",
):
circuit_probs()


@pytest.mark.parametrize(
"operation", [qml.SingleExcitation, qml.SingleExcitationPlus, qml.SingleExcitationMinus]
)
Expand Down