diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 00011f80627..a48d37fd7ab 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -294,6 +294,9 @@

Bug fixes 🐛

+* Avoid bounded value failures due to numerical noise with calls to `np.random.binomial`. + [(#5447)](https://github.com/PennyLaneAI/pennylane/pull/5447) + * Using `@` with legacy Hamiltonian instances now properly de-queues the previously existing operations. [(#5454)](https://github.com/PennyLaneAI/pennylane/pull/5455) diff --git a/pennylane/devices/qubit/apply_operation.py b/pennylane/devices/qubit/apply_operation.py index ca9db1063ad..61ea0956a47 100644 --- a/pennylane/devices/qubit/apply_operation.py +++ b/pennylane/devices/qubit/apply_operation.py @@ -265,7 +265,15 @@ def apply_mid_measure( return np.zeros_like(state) wire = op.wires probs = qml.devices.qubit.measure(qml.probs(wire), state) - sample = np.random.binomial(1, probs[1]) + + try: # pragma: no cover + sample = np.random.binomial(1, probs[1]) + except ValueError as e: # pragma: no cover + if probs[1] > 1: # MachEps error, safe to catch + sample = np.random.binomial(1, np.round(probs[1], 15)) + else: # Other general error, continue to fail + raise e + mid_measurements[op] = sample if op.postselect is not None and sample != op.postselect: return np.zeros_like(state)