qp.templates.core.subroutine_resource_rep

subroutine_resource_rep(subroutine, *args, **kwargs)[source]

Generate a CompressedResourceOp similar to resource_rep() that is more specifically targeted for use with Subroutine instances.

Parameters:

subroutine (Subroutine) – the subroutine we are going to use in a decomposition.

Returns:

a condensed representation of the subroutine that can be used in specifying the resources of another function.

Return type:

pennylane.decomposition.CompressedResourceOp

Warning

Note that the following features only work with tape-based PennyLane, and do not work with Catalyst.

Suppose we have a Subroutine we want to use in the decomposition of another Operator.

from functools import partial

def S_resources(params, wires, rotation):
    return {rotation: params.shape[0]}

@partial(qp.templates.Subroutine, static_argnames="rotation", compute_resources=S_resources)
def S(params, wires, rotation):
    for x in params:
        rotation(x, wires)

We can add S to the resources of another Operator by using this function together with an abstract form of the arguments it will be called with, using AbstractArray and AbstractWires. These can be created by indexing into the Float() and Wire() abstract types respectively.

from pennylane.typing import Float, Wire
from pennylane.templates import subroutine_resource_rep

class MyOp(qp.operation.Operation):
    pass

abstract_params = Float[4]
abstract_wires = Wire[1] # a single wire
S_rep = subroutine_resource_rep(S, abstract_params, abstract_wires, qp.RX)

@qp.decomposition.register_resources({S_rep: 1})
def my_op_decomposition(wires):
    # data of shape (4, ) and dtype float
    params = np.array([1.0, 2.0, 3.0, 4.0])
    S(params, wires, qp.RX)

qp.add_decomps(MyOp, my_op_decomposition)

We can now see MyOp decompose into the relevant subroutine:

qp.decomposition.enable_graph()

@qp.qnode(qp.device('reference.qubit', wires=1))
def c():
    MyOp(wires=0)
    return qp.state()
>>> print(qp.draw(qp.transforms._tape_decompose(c, max_expansion=1))())
0: ──S(M0)─┤  State

M0 =
[1. 2. 3. 4.]