qrisp.custom_control#
- custom_control(func)[source]#
The
custom_control
decorator allows to specify the controlled version of the decorated function. If this function is called within a ControlEnvironment or a ConditionEnvironment the controlled version is executed instead.Specific controlled versions of quantum functions are very common in many scientific publications. This is because the general control procedure can signifcantly increase resource demands.
In order to use the
custom_control
decorator, you need to add thectrl
keyword to your function signature. If called within a controlled context, this keyword will receive the corresponding control qubit.For more details consult the examples section.
- Parameters:
- funcfunction
A function of QuantumVariables, which has the
ctrl
keyword.
- Returns:
- adaptive_control_functionfunction
A function which will execute it’s controlled version, if called within a ControlEnvironment or a ConditionEnvironment.
Examples
We create a swap function with custom control.
from qrisp import mcx, cx, custom_control @custom_control def swap(a, b, ctrl = None): if ctrl is None: cx(a, b) cx(b, a) cx(a, b) else: cx(a, b) mcx([ctrl, b], a) cx(a, b)
Test the non-controlled version:
from qrisp import QuantumBool a = QuantumBool() b = QuantumBool() swap(a, b) print(a.qs)
QuantumCircuit: -------------- ┌───┐ a.0: ──■──┤ X ├──■── ┌─┴─┐└─┬─┘┌─┴─┐ b.0: ┤ X ├──■──┤ X ├ └───┘ └───┘ Live QuantumVariables: --------------------- QuantumBool a QuantumBool b
Test the controlled version:
from qrisp import control a = QuantumBool() b = QuantumBool() ctrl_qbl = QuantumBool() with control(ctrl_qbl): swap(a,b) print(a.qs.transpile(1))
┌───┐ a.0: ──■──┤ X ├──■── ┌─┴─┐└─┬─┘┌─┴─┐ b.0: ┤ X ├──■──┤ X ├ └───┘ │ └───┘ ctrl_qbl.0: ───────■───────