qml.register_condition

register_condition(condition, qfunc=None)[source]

Binds a condition to a decomposition rule for when it is applicable.

Note

This function is only relevant when the new experimental graph-based decomposition system (introduced in v0.41) is enabled via enable_graph(). This new way of performing 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:
  • condition (Callable) – a function which takes the resource parameters of an operator as arguments and returns True or False based on whether the decomposition rule is applicable to an operator with the given resource parameters.

  • qfunc (Callable) – the quantum function that implements the decomposition. If None, returns a decorator for acting on a function.

Returns:

a data structure that represents a decomposition rule, which contains a PennyLane quantum function representing the decomposition, and its resource function.

Return type:

DecompositionRule

Example

This function can be used as a decorator to bind a condition function to a quantum function that implements a decomposition rule.

import pennylane as qml
from pennylane.math.decomposition import zyz_rotation_angles

# The parameters must be consistent with ``qml.QubitUnitary.resource_keys``
def _zyz_condition(num_wires):
    return num_wires == 1

@qml.register_condition(_zyz_condition)
@qml.register_resources({qml.RZ: 2, qml.RY: 1, qml.GlobalPhase: 1})
def zyz_decomposition(U, wires, **__):
    # Assumes that U is a 2x2 unitary matrix
    phi, theta, omega, phase = zyz_rotation_angles(U, return_global_phase=True)
    qml.RZ(phi, wires=wires[0])
    qml.RY(theta, wires=wires[0])
    qml.RZ(omega, wires=wires[0])
    qml.GlobalPhase(-phase)

# This decomposition will be ignored for `QubitUnitary` on more than one wire.
qml.add_decomps(qml.QubitUnitary, zyz_decomposition)

Contents

Using PennyLane

Release news

Development

API

Internals