catalyst.ctrl

ctrl(f: Union[Callable, pennylane.operation.Operator], control: List[Any], control_values: Optional[List[Any]] = None, work_wires: Optional[List[Any]] = None) Callable[source]

Create a method that applies a controlled version of the provided op. This function is the Catalyst version of the qml.ctrl that supports Catalyst hybrid operations such as loops and conditionals.

Parameters
  • f (Callable or Operator) – A PennyLane operation or a Python function containing PennyLane quantum operations.

  • control (Wires) – The control wire(s).

  • control_values (List[bool], optional) – The value(s) the control wire(s) should take. Integers other than 0 or 1 will be treated as int(bool(x)).

  • work_wires (Any) – Any auxiliary wires that can be used in the decomposition

Returns

If an Operator is provided, returns a Controlled version of the Operator. If a function is provided, returns a function with the same call signature that creates a controlled version of the provided function.

Return type

(function or Operator)

Raises

ValueError – invalid parameter values, measurements are among the controlled operations.

Example

@qjit
@qml.qnode(qml.device("lightning.qubit", wires=2))
def workflow(theta, w, cw):
    qml.Hadamard(wires=[0])
    qml.Hadamard(wires=[1])

    def func(arg):
      qml.RX(theta, wires=arg)

    @cond(theta > 0.0)
    def cond_fn():
      qml.RY(theta, wires=w)

    catalyst.ctrl(func, control=[cw])(w)
    catalyst.ctrl(cond_fn, control=[cw])()
    catalyst.ctrl(qml.RZ, control=[cw])(theta, wires=w)
    catalyst.ctrl(qml.RY(theta, wires=w), control=[cw])
    return qml.probs()
>>> workflow(jnp.pi/4, 1, 0)
Array([0.25, 0.25, 0.03661165, 0.46338835], dtype=float64)