-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**Context:** callback cache should take into account input types as well as output types. The following simple example illustrates when a callback would have different mismatching inputs yet always returns the same type. ```python @pure_callback def always_return_float(x) -> float: if x == 0.0: return x else: return x + 0.0 ``` **Description of the Change:** Adds input types as part of the cache key **Benefits:** No error. **Possible Drawbacks:** **Related GitHub Issues:** Fixes #851
- Loading branch information
1 parent
da0ec10
commit 5d910b6
Showing
3 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2024 Xanadu Quantum Technologies Inc. | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
|
||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# RUN: %PYTHON %s | FileCheck %s | ||
|
||
import pennylane as qml | ||
from catalyst import pure_callback | ||
|
||
|
||
def i(x): | ||
return x | ||
|
||
|
||
# CHECK-LABEL: module @one_callback_cached | ||
@qml.qjit | ||
# CHECK-NOT: catalyst.callback @callback | ||
# CHECK-LABEL: func.func public @jit_one_callback_cached | ||
def one_callback_cached(x: float): | ||
"""Single callback is created, but called twice""" | ||
c = pure_callback(i, float) | ||
return c(x), c(x) | ||
|
||
|
||
# CHECK-LABEL: catalyst.callback @callback | ||
# CHECK-NOT: catalyst.callback @callback | ||
print(one_callback_cached.mlir) | ||
|
||
|
||
@pure_callback | ||
def always_return_float(x) -> float: | ||
if x == 0.0: | ||
return x | ||
else: | ||
return x + 0.0 | ||
|
||
|
||
# CHECK-LABEL: module @test2 | ||
@qml.qjit | ||
# CHECK-NOT: catalyst.callback @callback | ||
# CHECK-LABEL: func.func public @jit_test2 | ||
def test2(): | ||
return always_return_float(0.0), always_return_float(1) | ||
|
||
|
||
# CHECK-LABEL: catalyst.callback @callback | ||
# CHECK-LABEL: catalyst.callback @callback | ||
# CHECK-NOT: catalyst.callback @callback | ||
|
||
print(test2.mlir) |