-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Capture] Add a
QmlPrimitive
class to differentiate between differe…
…nt types of primitives (#6847) This PR adds a `QmlPrimitive` subclass of `jax.core.Primitive`. This class contains a `prim_type` property set using a new `PrimitiveType` enum. `PrimitiveType`s currently available are "default", "operator", "measurement", "transform", and "higher_order". This can be made more or less fine grained as needed, but should be enough to differentiate between different types of primitives for now. Additionally, this PR: * updates `NonInterpPrimitive` to be a subclass of `QmlPrimitive` * updates all existing PennyLane primitives to be either `QmlPrimitive` or `NonInterpPrimitive`. See [this comment](#6851 (comment)) to see the logic used to determine which `Primitive` subclass is used for each primitive. * updates `PlxprInterpreter.eval` and `CancelInversesInterpreter.eval` to use this `prim_type` property. [sc-82420] --------- Co-authored-by: Pietropaolo Frisoni <[email protected]>
- Loading branch information
1 parent
fdf34ec
commit 3e1521b
Showing
16 changed files
with
209 additions
and
73 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
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,64 @@ | ||
# 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. | ||
""" | ||
This submodule offers custom primitives for the PennyLane capture module. | ||
""" | ||
from enum import Enum | ||
from typing import Union | ||
|
||
import jax | ||
|
||
|
||
class PrimitiveType(Enum): | ||
"""Enum to define valid set of primitive classes""" | ||
|
||
DEFAULT = "default" | ||
OPERATOR = "operator" | ||
MEASUREMENT = "measurement" | ||
HIGHER_ORDER = "higher_order" | ||
TRANSFORM = "transform" | ||
|
||
|
||
# pylint: disable=too-few-public-methods,abstract-method | ||
class QmlPrimitive(jax.core.Primitive): | ||
"""A subclass for JAX's Primitive that differentiates between different | ||
classes of primitives.""" | ||
|
||
_prim_type: PrimitiveType = PrimitiveType.DEFAULT | ||
|
||
@property | ||
def prim_type(self): | ||
"""Value of Enum representing the primitive type to differentiate between various | ||
sets of PennyLane primitives.""" | ||
return self._prim_type.value | ||
|
||
@prim_type.setter | ||
def prim_type(self, value: Union[str, PrimitiveType]): | ||
"""Setter for QmlPrimitive.prim_type.""" | ||
self._prim_type = PrimitiveType(value) | ||
|
||
|
||
# pylint: disable=too-few-public-methods,abstract-method | ||
class NonInterpPrimitive(QmlPrimitive): | ||
"""A subclass to JAX's Primitive that works like a Python function | ||
when evaluating JVPTracers and BatchTracers.""" | ||
|
||
def bind_with_trace(self, trace, args, params): | ||
"""Bind the ``NonInterpPrimitive`` with a trace. | ||
If the trace is a ``JVPTrace``or a ``BatchTrace``, binding falls back to a standard Python function call. | ||
Otherwise, the bind call of JAX's standard Primitive is used.""" | ||
if isinstance(trace, (jax.interpreters.ad.JVPTrace, jax.interpreters.batching.BatchTrace)): | ||
return self.impl(*args, **params) | ||
return super().bind_with_trace(trace, args, params) |
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
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
Oops, something went wrong.