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

Allow specifying error type to be raised by decompose #5669

Merged
merged 10 commits into from
May 8, 2024

Conversation

lillian542
Copy link
Contributor

@lillian542 lillian542 commented May 7, 2024

Context:
As the decompose transform is used in various contexts, it would be nice to specify the error type that should be raised if its not possible to get a suitable decomposition for one of the operators (i.e. devices raise DeviceError, transforms and gradients raise DecompositionUndefinedError, catalyst raises CompileError)

Description of the Change:
An error kwarg is added to decompose and its helper function _operator_decomposition_gen so that the error type can be specified. It continues to default to DeviceError

Benefits:
We don't have to duplicate many lines of code to get an identical function with a different error type

Possible Drawbacks:
More kwargs

Related GitHub Issues:
N/A

Related Shortcut Stories:
[sc-61450]

@lillian542 lillian542 marked this pull request as ready for review May 7, 2024 18:21
Copy link
Contributor

github-actions bot commented May 7, 2024

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

1 similar comment
Copy link
Contributor

github-actions bot commented May 7, 2024

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@lillian542 lillian542 changed the title Decomp error type [WIP] Allow specifying error type to be raised by decompose May 7, 2024
Copy link

codecov bot commented May 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.68%. Comparing base (ab8d739) to head (b64174c).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5669      +/-   ##
==========================================
- Coverage   99.69%   99.68%   -0.02%     
==========================================
  Files         412      412              
  Lines       38663    38367     -296     
==========================================
- Hits        38545    38245     -300     
- Misses        118      122       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lillian542
Copy link
Contributor Author

[sc-61450]

@lillian542 lillian542 changed the title [WIP] Allow specifying error type to be raised by decompose Allow specifying error type to be raised by decompose May 7, 2024
@dwierichs dwierichs self-requested a review May 7, 2024 19:56
Copy link
Contributor

@dwierichs dwierichs left a comment

Choose a reason for hiding this comment

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

The change looks good to me, thanks @lillian542! 🎉

Would be good to update the Args: section as the comment details.
Also, do we want a basic test that asserts the error kwarg being used correctly?
And a changelog entry? 🤔

pennylane/devices/preprocess.py Show resolved Hide resolved
@trbromley
Copy link
Contributor

Thanks @lillian542!

it would be nice to specify the error type that should be raised

Are you able to speak more towards that motivation? I guess this is needed for Catalyst integration, but I'm just curious to understand more.

@lillian542
Copy link
Contributor Author

Also, do we want a basic test that asserts the error kwarg being used correctly?
And a changelog entry? 🤔

@dwierichs - why yes. Yes we do want those things 🙈

@lillian542
Copy link
Contributor Author

Are you able to speak more towards that motivation? I guess this is needed for Catalyst integration, but I'm just curious to understand more.

@trbromley - this function was originally used only in preprocessing for standard devices (which we want to raise a DeviceError if it gets an operation it can't use or decompose).

As of this week, it's also used in transforms, where no device is present, and it would be better to raise the original DecompositionUndefinedError. For catalyst, the issue that we hit in decompose is whether or not it can be compiled via qjit for the given device, and everything in that part of the pipe line raises a CompileError.

So the motivation is to keep error types consistent with what process is failing, even though at the end of the day they all come down to a DecompositionUndefinedError arising in different contexts.

An alternative would be to always use DecompositionUndefinedError. That would be accurate, but inconvenient in try-except blocks where we would like to, for example, try to run preprocessing and have special handling if a DeviceError is raised.

Copy link
Contributor

@dwierichs dwierichs left a comment

Choose a reason for hiding this comment

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

Thanks @lillian542
Just 1.5 comments regarding the docstring.

pennylane/devices/preprocess.py Outdated Show resolved Hide resolved
pennylane/devices/preprocess.py Outdated Show resolved Hide resolved
pennylane/devices/preprocess.py Outdated Show resolved Hide resolved
tests/devices/test_preprocess.py Show resolved Hide resolved
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
@lillian542 lillian542 enabled auto-merge (squash) May 8, 2024 21:39
@lillian542 lillian542 merged commit e0d0a32 into master May 8, 2024
38 checks passed
@lillian542 lillian542 deleted the decomp-error-type branch May 8, 2024 21:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants