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

Resource operators for Adjoint, Controlled, and Pow #6592

Open
wants to merge 6 commits into
base: resource_qft
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pennylane/labs/resource_estimation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,15 @@

"""

from .resource_operator import ResourceOperator, ResourcesNotDefined
from .resource_operator import ResourceOperator, ResourcesNotDefined, ResourceSymbolicOperator
from .resource_container import CompressedResourceOp, Resources

from .ops import (
ResourceAdjoint,
ResourceCNOT,
ResourceControlled,
ResourceControlledPhaseShift,
ResourcePow,
ResourceHadamard,
ResourceRZ,
ResourceSWAP,
Expand Down
3 changes: 3 additions & 0 deletions pennylane/labs/resource_estimation/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
)

from .op_math import (
ResourceAdjoint,
ResourceCNOT,
ResourceControlled,
ResourceControlledPhaseShift,
ResourcePow,
)
1 change: 1 addition & 0 deletions pennylane/labs/resource_estimation/ops/op_math/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
r"""This module contains experimental resource estimation functionality. """

from .controlled_ops import *
from .symbolic import *
33 changes: 33 additions & 0 deletions pennylane/labs/resource_estimation/ops/op_math/symbolic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pennylane as qml
import pennylane.labs.resource_estimation as re
from pennylane.ops.op_math.adjoint import AdjointOperation

#pylint: disable=too-many-ancestors

class ResourceAdjoint(AdjointOperation, re.ResourceSymbolicOperator):
"""Resource class for Adjoint"""

@staticmethod
def _resource_decomp(*args, **kwargs, base_class, base_params):
try:
return base_class.adjoint_resource_rep()

class ResourceControlled(qml.ops.Controlled, re.ResourceSymbolicOperator):
"""Resource class for Controlled"""

@staticmethod
def _resource_decomp(*args, **kwargs):
pass

def resource_params(self):
pass

class ResourcePow(qml.ops.Pow, re.ResourceSymbolicOperator):
"""Resource class for Pow"""

@staticmethod
def _resource_decomp(*args, **kwargs):
pass

def resource_params(self):
pass
33 changes: 33 additions & 0 deletions pennylane/labs/resource_estimation/resource_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
if TYPE_CHECKING:
from pennylane.labs.resource_estimation import CompressedResourceOp

import pennylane.labs.resource_estimation as re


class ResourceOperator(ABC):
r"""This is an abstract class that defines the methods a PennyLane Operator
Expand Down Expand Up @@ -95,6 +97,37 @@ def resource_rep_from_op(self) -> CompressedResourceOp:
"""Returns a compressed representation directly from the operator"""
return self.__class__.resource_rep(**self.resource_params())

@classmethod
def adjoint_resource_rep(cls, *args, **kwargs) -> CompressedResourceOp:
"""Returns a compressed representation of the adjoint of the operator"""
raise CompressedRepNotDefined

@classmethod
def controlled_resource_rep(cls, *args, **kwargs) -> CompressedResourceOp:
"""Returns a compressed representation of the controlled version of the operator"""
raise CompressedRepNotDefined

@classmethod
def pow_resource_rep(cls, exponent, *args, **kwargs) -> CompressedResourceOp:
"""Returns a compressed representation of the operator raised to a power"""
raise CompressedRepNotDefined

class ResourceSymbolicOperator(ResourceOperator):
"""Base class for resources of symbolic operators"""

#pylint: disable=no-member
def resource_params(self):
return {
"base_class": type(self.base),
"base_params": self.base.resource_params()
}

@classmethod
def resource_rep(cls, base_class, base_params, **kwargs):
return re.CompressedResourceOp(cls, {"base_class": base_class, "base_params": base_params})

class ResourcesNotDefined(Exception):
"""Exception to be raised when a ``ResourceOperator`` does not implement _resource_decomp"""

class CompressedRepNotDefined(Exception):
"""Exception to be raised when a compressed representation is not defined"""
Loading