qrisp.redirect_qfunction#

redirect_qfunction(function_to_redirect)[source]#

Decorator to turn a function returning a QuantumVariable into an in-place function. This can be helpful for manual uncomputation if we have a function returning some QuantumVariable, but we want the result to operate on some other variable, which is supposed to be uncomputed.

Parameters:
function_to_redirectfunction

A function returning a QuantumVariable.

Returns:
redirected_functionfunction

A function which performs the same operation as the input but now has the keyword argument target. Every instruction that would have been executed on the input functions result is executed on the QuantumVariable specified by target instead.

Raises:
Exception

Given function did not return a QuantumVariable

Exception

Tried to redirect quantum function into QuantumVariable of differing size

Examples

We create a function that determins the AND value of its inputs and redirect it onto another QuantumBool.

from qrisp import QuantumBool, mcx, redirect_qfunction

#This function has only two arguments and returns its result
def AND(a, b):

    res = QuantumBool()

    mcx([a,b], res)

    return res

a = QuantumBool(name = "a")
b = QuantumBool(name = "b")
c = QuantumBool(name = "c")

#This function has two arguments and the keyword argument target
redirected_AND = redirect_qfunction(AND)

redirected_AND(a, b, target = c)
>>> print(a.qs)
QuantumCircuit:
--------------
b.0: ──■──
       │
a.0: ──■──
     ┌─┴─┐
c.0: ┤ X ├
     └───┘
Live QuantumVariables:
---------------------
QuantumBool b
QuantumBool a
QuantumBool c