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

Improve error message for adjoint(measure) #1188

Closed
dime10 opened this issue Oct 7, 2024 · 0 comments
Closed

Improve error message for adjoint(measure) #1188

dime10 opened this issue Oct 7, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@dime10
Copy link
Contributor

dime10 commented Oct 7, 2024

The error message should be more actionable, telling the user that they cannot use adjoint on a measure operation.

https://github.com/QSAR-UBC/shortalyst-dev/pull/7#discussion_r1782000436, excerpt below:

glassnotes on Sep 30, 2024
@dime10 @josh146 I'm running into an issue here. I'd like to measure this wire and apply an operation based on the outcome, but I am getting a ValueError: Quantum measurements are not allowed. I'm using PL 0.38 and Catalyst 0.8.1.

To replicate, check out the branch and run python shors_algorithm.py 15.

I've included the stack trace in case it's helpful. I would post a bug report, but I haven't been able to make a minimal example yet. I suspect it has to do with nesting. This call is in a function called from a for loop within a function that is also called in a for loop, within a QNode, within the top-level qjitted function. As I reread that explanation, it sounds pretty ridiculous 😆

  File "/home/olivia/.venv/shortalyst/lib/python3.12/site-packages/catalyst/autograph/ag_primitives.py", line 579, in converted_call
    return ag_converted_call(fn, args, kwargs, caller_fn_scope, options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/olivia/.venv/shortalyst/lib/python3.12/site-packages/malt/impl/api.py", line 319, in converted_call
    return _call_unconverted(f, args, kwargs, options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/olivia/.venv/shortalyst/lib/python3.12/site-packages/malt/impl/api.py", line 399, in _call_unconverted
    return f(*args)
           ^^^^^^^^
  File "/home/olivia/.venv/shortalyst/lib/python3.12/site-packages/catalyst/api_extensions/quantum_operators.py", line 407, in __call__
    tracing_artifacts = self.trace_body(args, kwargs)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/olivia/.venv/shortalyst/lib/python3.12/site-packages/catalyst/api_extensions/quantum_operators.py", line 439, in trace_body
    _check_no_measurements(quantum_tape)
  File "/home/olivia/.venv/shortalyst/lib/python3.12/site-packages/catalyst/api_extensions/quantum_operators.py", line 725, in _check_no_measurements
    raise ValueError(msg)
ValueError: Quantum measurements are not allowed

josh146 on Sep 30, 2024
🤔 It seems that this logic is called to verify that MCMs are not placed inside adjoint() regions -- I take it you are not within an adjoint region at this point in the code?

Side note though @dime10, we should update this error message to be more actionable lol.

@dime10 dime10 added the enhancement New feature or request label Oct 7, 2024
sengthai added a commit that referenced this issue Jan 13, 2025
)

**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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants