Skip to content

Commit

Permalink
Improve error message for invalid measurement in adjoint() region (#1425
Browse files Browse the repository at this point in the history
)

**Context:**
Improve the error message when using measure within an `adjoin()`
region.

**Example**
```python
import pennylane as qml
from pennylane import qjit
from catalyst import measure

dev = qml.device("lightning.qubit", wires=1)

@qjit
@qml.qnode(dev)
def circuit():
    def foo():
        measure(wires=0)
    qml.adjoint(foo)()
    return qml.expval(qml.PauliZ(0))

print(circuit())
```

**Description of the Change:**
Write and insert an error message `"Measurements are not invertible and
cannot be used within an adjoint() region."`

**Related GitHub Issues:**
#1188

---------

Co-authored-by: David Ittah <dime10@users.noreply.github.com>
  • Loading branch information
sengthai and dime10 authored Jan 13, 2025
1 parent 50e9dd5 commit 55d424b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
6 changes: 5 additions & 1 deletion doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
* `from_plxpr` now uses the `qml.capture.PlxprInterpreter` class for reduced code duplication.
[(#1398)](https://github.com/PennyLaneAI/catalyst/pull/1398)

* Improve the error message for invalid measurement in `adjoin()` or `ctrl()` region.
[(#1425)](https://github.com/PennyLaneAI/catalyst/pull/1425)

* Replace `ValueRange` with `ResultRange` and `Value` with `OpResult` to better align with the semantics of `**QubitResult()` functions like `getNonCtrlQubitResults()`. This change ensures clearer intent and usage. Improve the `matchAndRewrite` function by using `replaceAllUsesWith` instead of for loop.
[(#1426)](https://github.com/PennyLaneAI/catalyst/pull/1426)


<h3>Documentation 📝</h3>

<h3>Contributors ✍️</h3>
Expand All @@ -31,4 +35,4 @@ This release contains contributions from (in alphabetical order):

Christina Lee
Mehrdad Malekmohammadi
Sengthai Heng
Sengthai Heng
9 changes: 5 additions & 4 deletions frontend/catalyst/api_extensions/quantum_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,14 +712,15 @@ def ctrl_distribute(
def _check_no_measurements(tape: QuantumTape) -> None:
"""Check the nested quantum tape for the absense of quantum measurements of any kind"""

msg = "Quantum measurements are not allowed"

if len(tape.measurements) > 0:
raise ValueError(msg)
raise ValueError("Measurement process cannot be used within an adjoint() or ctrl() region.")
for op in tape.operations:
if has_nested_tapes(op):
for r in [r for r in op.regions if r.quantum_tape is not None]:
_check_no_measurements(r.quantum_tape)
else:
if isinstance(op, MidCircuitMeasure):
raise ValueError(msg)
raise ValueError(
"Mid-circuit measurements cannot be used "
"within an adjoint() or ctrl() region."
)
2 changes: 1 addition & 1 deletion frontend/test/pytest/test_adjoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def func():
qml.RX(np.pi / 2, wires=0)
qml.sample()

with pytest.raises(ValueError, match="Quantum measurements are not allowed"):
with pytest.raises(ValueError, match="Measurement process cannot be used"):

@qjit
@qml.qnode(qml.device("lightning.qubit", wires=2))
Expand Down
4 changes: 2 additions & 2 deletions frontend/test/pytest/test_quantum_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def _func1():
C_ctrl(_func1, control=[1], control_values=[True])()
return qml.state()

with pytest.raises(ValueError, match="measurements are not allowed"):
with pytest.raises(ValueError, match="Mid-circuit measurements cannot be used"):
qjit(circuit)(0.1)

def test_qctrl_no_end_circuit_measurements(self, backend):
Expand All @@ -305,7 +305,7 @@ def _func1():
C_ctrl(_func1, control=[1], control_values=[True])()
return qml.state()

with pytest.raises(ValueError, match="measurements are not allowed"):
with pytest.raises(ValueError, match="Measurement process cannot be used"):
qjit(circuit)(0.1)

def test_qctrl_wires(self, backend):
Expand Down

0 comments on commit 55d424b

Please sign in to comment.