catalyst.ctrl¶
- ctrl(f: Callable | Operator, control: List[Any], control_values: List[Any] | None = None, work_wires: List[Any] | None = None, work_wire_type: str = 'borrowed') Callable[source]¶
Create a method that applies a controlled version of the provided op. This function is the Catalyst version of the
qp.ctrlthat 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
work_wire_type (str) – The type of work wire(s), can be
"zeroed"or"borrowed".
- 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
import pennylane as qp import catalyst from catalyst import qjit @qjit @qp.qnode(qp.device("lightning.qubit", wires=2)) def workflow(theta, w, cw): qp.Hadamard(wires=[0]) qp.Hadamard(wires=[1]) def func(arg): qp.RX(theta, wires=arg) @cond(theta > 0.0) def cond_fn(): qp.RY(theta, wires=w) catalyst.ctrl(func, control=[cw])(w) catalyst.ctrl(cond_fn, control=[cw])() catalyst.ctrl(qp.RZ, control=[cw])(theta, wires=w) catalyst.ctrl(qp.RY(theta, wires=w), control=[cw]) return qp.probs()
>>> workflow(jnp.pi/4, 1, 0) Array([0.25, 0.25, 0.03661165, 0.46338835], dtype=float64)