PennyLaneAI / catalyst

A JIT compiler for hybrid quantum programs in PennyLane
https://docs.pennylane.ai/projects/catalyst
Apache License 2.0
122 stars 27 forks source link

Adds runtime validation within QJIT functions #925

Closed simba611 closed 1 month ago

simba611 commented 1 month ago

Context: JITing Python functions can provide order(s) of magnitude performance boost during execution, by extracting a representation of the computation and turning it into optimized binary code. However, it also makes it more difficult to understand and debug the program when things go wrong, for instance by not having access to the Python debugger or being able to easily reason about the program's execution.

To counter-act this, we can introduce new utility functions for users that help increase the confidence in their programs and detect error states at runtime.

Description of the Change: Adds a debugging function catalyst.catalyst_assert(bool, str) that asserts at runtime that the given condition holds, and if not raises the provided error message. Also adds a disable-assertion pass, which can be conditionally run based on an option to the compiler @qjit(disable_assertions=True) which is off by default.

Benefits: Makes for an easier debugging experience.

Possible Drawbacks:

Related GitHub Issues: Resolves #823

dime10 commented 1 month ago

Hi @simba611, thank you for tackling this issue! I will have a look at the code shortly :)

codecov[bot] commented 1 month ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 97.92%. Comparing base (93c05e5) to head (5474928). Report is 11 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #925 +/- ## ========================================== - Coverage 97.98% 97.92% -0.07% ========================================== Files 71 72 +1 Lines 10546 10275 -271 Branches 960 1166 +206 ========================================== - Hits 10334 10062 -272 Misses 169 169 - Partials 43 44 +1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.