Skip to content

Commit 7aafa36

Browse files
committed
implemented qswitch
1 parent f06f670 commit 7aafa36

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

src/qrisp/alg_primitives/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
from qrisp.alg_primitives.arithmetic import *
2525
from qrisp.alg_primitives.iterable_processing import *
2626
from qrisp.alg_primitives.dicke_state_prep import *
27+
from qrisp.alg_primitives.switch_case import *
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""
2+
\********************************************************************************
3+
* Copyright (c) 2023 the Qrisp authors
4+
*
5+
* This program and the accompanying materials are made available under the
6+
* terms of the Eclipse Public License 2.0 which is available at
7+
* http://www.eclipse.org/legal/epl-2.0.
8+
*
9+
* This Source Code may also be made available under the following Secondary
10+
* Licenses when the conditions for such availability set forth in the Eclipse
11+
* Public License, v. 2.0 are satisfied: GNU General Public License, version 2
12+
* with the GNU Classpath Exception which is
13+
* available at https://www.gnu.org/software/classpath/license.html.
14+
*
15+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16+
********************************************************************************/
17+
"""
18+
19+
from qrisp.core.gate_application_functions import h
20+
from qrisp.alg_primitives.qft import QFT
21+
from qrisp.core import QuantumArray
22+
from qrisp.qtypes import QuantumBool
23+
from qrisp.environments import conjugate, control
24+
from qrisp.alg_primitives import demux
25+
26+
def qswitch(operand, case, case_function_list, method = "sequential"):
27+
"""
28+
Executes a switch - case statement distinguishing between a list of
29+
given in-place functions.
30+
31+
Parameters
32+
----------
33+
operand : QuantumVariable
34+
The QuantumVariable to operate on.
35+
case : QuantumFloat
36+
The QuantumFloat indicating which functions to execute.
37+
case_function_list : list[callable]
38+
The list of functions which are executed depending on the case.
39+
40+
41+
"""
42+
43+
if method == "sequential":
44+
45+
for i in range(len(case_function_list)):
46+
with i == case:
47+
case_function_list[i](operand)
48+
49+
elif method == "parallel":
50+
51+
# Idea: Use demux function to move operand and enabling bool into QuantumArray
52+
# to execute cases in parallel.
53+
case_amount = len(case_function_list)
54+
55+
# This QuantumArray acts as an addressable QRAM via the demux function
56+
enable = QuantumArray(qtype = QuantumBool(), shape = (case_amount,))
57+
enable[0].flip()
58+
59+
qa = QuantumArray(qtype = operand, shape = ((case_amount,)))
60+
61+
with conjugate(demux)(operand, case, qa, parallelize_qc = True):
62+
with conjugate(demux)(enable[0], case, enable, parallelize_qc = True):
63+
for i in range(case_amount):
64+
with control(enable[i]):
65+
case_function_list[i](qa[i])
66+
67+
qa.delete()
68+
69+
enable[0].flip()
70+
enable.delete()
71+
72+
else:
73+
raise Exception("Don't know compile method {method} for switch-case structure.")

tests/test_qswitch.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
\********************************************************************************
3+
* Copyright (c) 2023 the Qrisp authors
4+
*
5+
* This program and the accompanying materials are made available under the
6+
* terms of the Eclipse Public License 2.0 which is available at
7+
* http://www.eclipse.org/legal/epl-2.0.
8+
*
9+
* This Source Code may also be made available under the following Secondary
10+
* Licenses when the conditions for such availability set forth in the Eclipse
11+
* Public License, v. 2.0 are satisfied: GNU General Public License, version 2
12+
* with the GNU Classpath Exception which is
13+
* available at https://www.gnu.org/software/classpath/license.html.
14+
*
15+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16+
********************************************************************************/
17+
"""
18+
19+
from qrisp import inpl_mult, h, QuantumFloat, qswitch, multi_measurement
20+
def test_qswitch():
21+
22+
# Some sample case functions
23+
def f0(x): x += 1
24+
def f1(x): inpl_mult(x, 3, treat_overflow = False)
25+
def f2(x): pass
26+
def f3(x): h(x[1])
27+
case_function_list = [f0, f1, f2, f3]
28+
29+
# Create operand and case variable
30+
operand = QuantumFloat(4)
31+
operand[:] = 1
32+
case = QuantumFloat(2)
33+
h(case)
34+
35+
# Execute switch_case function
36+
qswitch(operand, case, case_function_list)
37+
38+
# Simulate
39+
assert multi_measurement([case, operand]) == {(0, 2): 0.25, (1, 3): 0.25, (2, 1): 0.25, (3, 1): 0.125, (3, 3): 0.125}
40+
# Yields {(0, 2): 0.25, (1, 3): 0.25, (2, 1): 0.25, (3, 1): 0.125, (3, 3): 0.125}
41+
42+
# Create operand and case variable
43+
operand = QuantumFloat(4)
44+
operand[:] = 1
45+
case = QuantumFloat(2)
46+
h(case)
47+
48+
# Execute switch_case function
49+
qswitch(operand, case, case_function_list, method = "parallel")
50+
51+
# Simulate
52+
assert multi_measurement([case, operand]) == {(0, 2): 0.25, (1, 3): 0.25, (2, 1): 0.25, (3, 1): 0.125, (3, 3): 0.125}
53+
# Yields {(0, 2): 0.25, (1, 3): 0.25, (2, 1): 0.25, (3, 1): 0.125, (3, 3): 0.125}

0 commit comments

Comments
 (0)