qp.decomposition.register_condition

register_condition(condition, qfunc=None)[source]

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

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 qp
from pennylane.math.decomposition import zyz_rotation_angles

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

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

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