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

Fixed a crash when using float32 in callbacks #916

Closed mehrdad2m closed 1 month ago

mehrdad2m commented 1 month ago

Context: This PR fixes an unexpected crash in initializeCotangents when using float32 in callbacks. The crash happens when creating ConstantFloatOp operation. e.g. testcase test_scalar_in_array_out_float32_wrong in frontend/test/pytest/test_callback.py would result in the following crash message: Assertion failed: (succeeded(ConcreteT::verify(getDefaultDiagnosticEmitFn(ctx), args...))), function get, file StorageUniquerSupport.h, line 181.

Description of the Change: Since APFloat(0.0) is always a float64, changed the constructor to use the precision semantics of the desired float

Benefits: Will print better assertion error when using float32 in callbacks. The improved assertion error is:

File "/Users/mehrdad.malek/catalyst/frontend/catalyst/api_extensions/callbacks.py", line 415, in _check_types
    raise TypeError(msg)
TypeError: Callback closure expected type ShapedArray(float32[2]) but observed ShapedArray(float64[2]) in its return value

Possible Drawbacks: callbacks will still raise an exception. Although this happens only when value returned from pure_callback was not dtyped float32. if both observed and expected output of the pure_callback are dtyped float32, there would be no assertion raised.

Related GitHub Issues: https://github.com/PennyLaneAI/catalyst/issues/844

[sc-68042]

codecov[bot] commented 1 month ago

Codecov Report

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

Project coverage is 97.98%. Comparing base (f1c846d) to head (3807912).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #916 +/- ## ======================================= Coverage 97.98% 97.98% ======================================= Files 71 71 Lines 10497 10497 Branches 956 956 ======================================= Hits 10285 10285 Misses 169 169 Partials 43 43 ```

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