Quantum Switch Case#

qswitch(operand, case, case_function, method='auto')[source]#

Executes a switch - case statement distinguishing between a list of given in-place functions.

Parameters:
operandQuantumVariable

The argument on which the case function operates.

caseQuantumFloat

The index specifying which case should be executed.

case_functionlist[callable] or callable

A list of functions, performing some in-place operation on operand, or a function case_function(i, operand) performing some in-place operation on operand depending on a nonnegative integer index i specifying the case.

methodstr, optional

The compilation method. Available are sequential, parallel, tree and auto. parallel is exponentially fast but requires more temporary qubits. tree uses balanced binaray trees. The default is auto.

Examples

First, we consider the case where case_function is a list of functions:

We create some sample functions:

from qrisp import *

def f0(x): x += 1
def f1(x): inpl_mult(x, 3, treat_overflow = False)
def f2(x): pass
def f3(x): h(x[1])
case_function_list = [f0, f1, f2, f3]

Create operand and case variable:

operand = QuantumFloat(4)
operand[:] = 1
case = QuantumFloat(2)
h(case)

Execute switch - case function:

>>> qswitch(operand, case, case_function_list)

Simulate:

>>> print(multi_measurement([case, operand]))
{(0, 2): 0.25, (1, 3): 0.25, (2, 1): 0.25, (3, 1): 0.125, (3, 3): 0.125}

Second, we consider the case where case_function is a function:

def case_function(i, qv):
    x(qv[i])

operand = QuantumFloat(4)
case = QuantumFloat(2)
h(case)

qswitch(operand, case, case_function)

Simulate:

>>> print(multi_measurement([case, operand]))
{(0, 1): 0.25, (1, 2): 0.25, (2, 4): 0.25, (3, 8): 0.25}