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

[MLIR] Disallow MeasureOp, PythonCallOp and CustomCallOp in gradients. #204

Merged

Conversation

erick-xanadu
Copy link
Contributor

@erick-xanadu erick-xanadu commented Jul 13, 2023

Context: MeasureOp, PythonCallOp and CustomCallOp are allowed in gradients even though we don't support them yet. This leads to logic errors and bad UI/UX.

Description of the Change: Verify that these operations are not reachable from the grad op.

Benefits: Better UI/UX.

[sc-42761]

@erick-xanadu erick-xanadu requested a review from dime10 July 13, 2023 19:12
@erick-xanadu erick-xanadu marked this pull request as ready for review July 13, 2023 19:28
Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @erick-xanadu! I'm curious on the underlying issue - is it because measure is returning an int?

@erick-xanadu
Copy link
Contributor Author

It is because the measurement is non-deterministic. If I'm wrong about this I can close this PR. I don't believe we currently handle the case for differentiating the following function in general.

def f(x):
  qml.RX(x, wires=0)
  return measure(0)

@erick-xanadu erick-xanadu marked this pull request as draft July 13, 2023 22:06
@dime10
Copy link
Collaborator

dime10 commented Jul 14, 2023

This is tricky, because you can use them with finite-diff if the overall function is deterministic. For example:

def f(x):
    qml.RY(x, wires=0)
    m = measure(0)
    cond(m)(lambda: qml.PauliX(0) and None)()
    return qml.expval(qml.PauliZ(0))

But even a quantum function with

def f(x):
    qml.RY(x, wires=0)
    return measure(0)

that is post-processed into a deterministic output somehow could also be differentiable.

Having said that, I could see for UX reasons that we might want to disallow those cases to prevent misuse on non-differentiable functions. But I'm not yet convinced that is the right move.

@maliasadi maliasadi added the compiler Pull requests that update the compiler label Dec 6, 2023
@erick-xanadu erick-xanadu added this to the v0.6.0 milestone Apr 29, 2024
@erick-xanadu erick-xanadu force-pushed the eochoa/2023-07-13/disallow-gradient-containing-measure branch from 9d7dbe3 to 1757cf5 Compare April 29, 2024 16:17
@erick-xanadu erick-xanadu marked this pull request as ready for review April 29, 2024 16:20
@erick-xanadu erick-xanadu changed the base branch from main to v0.6.0-rc April 29, 2024 16:20
@erick-xanadu erick-xanadu changed the title [MLIR] Disallow MeasureOp in gradient arguments. [MLIR] Disallow MeasureOp, PythonCallOp and CustomCallOp in gradients. Apr 29, 2024
Copy link
Collaborator

@dime10 dime10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @erick-xanadu, looks great!

frontend/test/pytest/test_gradient.py Show resolved Hide resolved
mlir/lib/Quantum/Transforms/annotate_function.cpp Outdated Show resolved Hide resolved
mlir/lib/Quantum/Transforms/annotate_function.cpp Outdated Show resolved Hide resolved
mlir/lib/Quantum/Transforms/annotate_function.cpp Outdated Show resolved Hide resolved
@erick-xanadu
Copy link
Contributor Author

@josh146 (or anyone 😅 ) can you provide a hand and move this shortcut ticket [sc-42761] to the current quarter / iteration?

@erick-xanadu erick-xanadu merged commit 1304da7 into v0.6.0-rc Apr 30, 2024
36 checks passed
@erick-xanadu erick-xanadu deleted the eochoa/2023-07-13/disallow-gradient-containing-measure branch April 30, 2024 19:00
sergei-mironov pushed a commit that referenced this pull request May 1, 2024
…dients. (#204)

**Context:** MeasureOp, PythonCallOp and CustomCallOp are allowed in
gradients even though we don't support them yet. This leads to logic
errors and bad UI/UX.

**Description of the Change:** Verify that these operations are not
reachable from the `grad` op.

**Benefits:** Better UI/UX.

[sc-42761]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler Pull requests that update the compiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants