Quantum Switch Case#
- qswitch(operand, case, case_function_list, method='sequential')[source]#
Executes a switch - case statement distinguishing between a list of given in-place functions.
- Parameters:
- operandQuantumVariable
The quantum argument on which to execute the case function.
- caseQuantumFloat
A QuantumFloat specifying which case function should be executed.
- case_function_listlist[callable]
A list of functions, performing some in-place operation on
operand
.- methodstr, optional
The compilation method. Available are
parallel
andsequential
.parallel
is exponentially fast but requires more temporary qubits. The default is “sequential”.
Examples
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}