qp.templates.core.subroutine_resource_rep¶
- subroutine_resource_rep(subroutine, *args, **kwargs)[source]¶
Generate a
CompressedResourceOpsimilar toresource_rep()that is more specifically targeted for use withSubroutineinstances.- 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:
Warning
Note that the following features only work with tape-based PennyLane, and do not work with Catalyst.
Suppose we have a
Subroutinewe want to use in the decomposition of anotherOperator.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
Sto the resources of anotherOperatorby using this function together with an abstract form of the arguments it will be called with, usingAbstractArrayandAbstractWires. These can be created by indexing into theFloat()andWire()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}, name="my_op_decomposition") 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
MyOpdecompose into the relevant subroutine:@qp.qnode(qp.device('reference.qubit', wires=1)) def c(): MyOp(wires=0) return qp.state()
>>> print(qp.draw(qp.transforms._tape_decompose(c, gate_set=qp.gate_sets.ROTATIONS_PLUS_CNOT, max_expansion=1))()) 0: ──S(M0)─┤ State M0 = [1. 2. 3. 4.]