qml.resource_rep

resource_rep(op_type, **params)[source]

Binds an operator type with additional resource parameters.

Note

This function is only relevant when the new experimental graph-based decomposition system (introduced in v0.41) is enabled via qml.decomposition.enable_graph(). This new way of doing decompositions is generally more resource efficient and accommodates multiple alternative decomposition rules for an operator. In this new system, custom decomposition rules are defined as quantum functions, and it is currently required that every decomposition rule declares its required resources using register_resources().

Parameters
  • op_type – the operator class to create a resource representation for.

  • **params – parameters relevant to the resource estimate of the operator’s decompositions. This should be consistent with op_type.resource_keys.

Returns

a lightweight representation of the operator.

Return type

CompressedResourceOp

Example

The resource parameters of an operator are a minimal set of information required to determine the resource estimate of its decompositions. To check the required set of keyword arguments for an operator type, refer to the resource_keys attribute of the operator class:

>>> qml.MultiRZ.resource_keys
{'num_wires'}

When calling resource_rep for MultiRZ, num_wires must be provided as a keyword argument.

>>> rep = resource_rep(qml.MultiRZ, num_wires=3)
>>> rep
MultiRZ, {'num_wires': 3}
>>> type(rep)
<class 'pennylane.decomposition.resources.CompressedResourceOp'>

See also

See how this function is used in the context of defining a decomposition rule using register_resources()

The same approach applies also to symbolic operators. For example, if the decomposition of an operator contains a controlled operation:

def my_decomp(wires):
    qml.ctrl(
        qml.MultiRZ(wires=wires[:3]),
        control=wires[3:5],
        control_values=[0, 1],
        work_wires=wires[5]
    )

To declare this controlled operator in the resource function, we find the resource keys of qml.ops.Controlled:

>>> qml.ops.Controlled.resource_keys
{
    'base_class',
    'base_params',
    'num_control_wires',
    'num_zero_control_values',
    'num_work_wires'
}

Then the resource representation can be created as follows:

>>> qml.resource_rep(
...     qml.ops.Controlled,
...     base_class=qml.ops.MultiRZ,
...     base_params={'num_wires': 3},
...     num_control_wires=2,
...     num_zero_control_values=1,
...     num_work_wires=1
... )
Controlled, {'base_class': <class 'pennylane.ops.qubit.parametric_ops_multi_qubit.MultiRZ'>, 'base_params': {'num_wires': 3}, 'num_control_wires': 2, 'num_zero_control_values': 1, 'num_work_wires': 1}

Alternatively, use the utility function controlled_resource_rep():

>>> qml.decomposition.controlled_resource_rep(
...     base_class=qml.ops.MultiRZ,
...     base_params={'num_wires': 3},
...     num_control_wires=2,
...     num_zero_control_values=1,
...     num_work_wires=1
... )
Controlled, {'base_class': <class 'pennylane.ops.qubit.parametric_ops_multi_qubit.MultiRZ'>, 'base_params': {'num_wires': 3}, 'num_control_wires': 2, 'num_zero_control_values': 1, 'num_work_wires': 1}

Contents

Using PennyLane

Release news

Development

API

Internals