microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.27.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/pip/tests/test_callable_passing.py

303lines · modecode

1# Copyright (c) Microsoft Corporation.
2# Licensed under the MIT License.
3
4import qsharp
5from expecttest import assert_expected_inline
6from textwrap import dedent
7
8
9def test_python_callable_passed_to_python_callable() -> None:
10 qsharp.init()
11 qsharp.eval("""
12 function InvokeWithFive(f : Int -> Int) : Int {
13 f(5)
14 }
15 function AddOne(x : Int) : Int {
16 x + 1
17 }
18 """)
19 from qsharp.code import InvokeWithFive, AddOne
20 assert InvokeWithFive(AddOne) == 6
21
22
23def test_python_callable_passed_to_qsharp_callable() -> None:
24 qsharp.init()
25 qsharp.eval("""
26 function InvokeWithFive(f : Int -> Int) : Int {
27 f(5)
28 }
29 function AddOne(x : Int) : Int {
30 x + 1
31 }
32 """)
33 from qsharp.code import InvokeWithFive
34 f = qsharp.eval("AddOne")
35 assert InvokeWithFive(f) == 6
36
37
38def test_run_qsharp_callable_passed_to_qsharp_callable() -> None:
39 qsharp.init()
40 qsharp.eval("""
41 function InvokeWithFive(f : Int -> Int) : Int {
42 f(5)
43 }
44 function AddOne(x : Int) : Int {
45 x + 1
46 }
47 """)
48 invoke_with_five = qsharp.eval("InvokeWithFive")
49 add_one = qsharp.eval("AddOne")
50 res = qsharp.run(invoke_with_five, 1, add_one)[0]
51 assert res == 6
52
53
54def test_run_qsharp_callable_passed_to_python_callable() -> None:
55 qsharp.init()
56 qsharp.eval("""
57 function InvokeWithFive(f : Int -> Int) : Int {
58 f(5)
59 }
60 function AddOne(x : Int) : Int {
61 x + 1
62 }
63 """)
64 from qsharp.code import InvokeWithFive
65 add_one = qsharp.eval("AddOne")
66 res = qsharp.run(InvokeWithFive, 1, add_one)[0]
67 assert res == 6
68
69
70def test_python_callable_with_unsupported_types_passed_to_python_callable() -> None:
71 qsharp.init()
72 qsharp.eval("""
73 function MakeRange() : Range {
74 1..10
75 }
76 function SumRangeFromMaker(maker : Unit -> Range) : Int {
77 mutable sum = 0;
78 for v in maker() {
79 sum += v;
80 }
81 sum
82 }
83 """)
84 from qsharp.code import MakeRange, SumRangeFromMaker
85 assert SumRangeFromMaker(MakeRange) == 55
86
87
88def test_qsharp_closure_from_python_callable_passed_to_python_callable() -> None:
89 qsharp.init()
90 qsharp.eval("""
91 function InvokeWithFive(f : Int -> Int) : Int {
92 f(5)
93 }
94 function MakeAdd(inc : Int) : Int -> Int {
95 x -> x + inc
96 }
97 """)
98 from qsharp.code import InvokeWithFive, MakeAdd
99 assert InvokeWithFive(MakeAdd(1)) == 6
100
101
102def test_qir_from_python_callable_passed_to_python_callable() -> None:
103 qsharp.init(target_profile=qsharp.TargetProfile.Base)
104 qsharp.eval("""
105 operation InvokeWithQubits(nQubits : Int, f : Qubit[] => Unit) : Unit {
106 use qs = Qubit[nQubits];
107 f(qs)
108 }
109 operation AllH(qs : Qubit[]) : Unit {
110 ApplyToEach(H, qs);
111 }
112 """)
113 from qsharp.code import InvokeWithQubits, AllH
114 qir = qsharp.compile(InvokeWithQubits, 3, AllH)
115 assert_expected_inline(str(qir), """\
116%Result = type opaque
117%Qubit = type opaque
118
119@0 = internal constant [4 x i8] c"0_t\\00"
120
121define i64 @ENTRYPOINT__main() #0 {
122block_0:
123 call void @__quantum__rt__initialize(i8* null)
124 call void @__quantum__qis__h__body(%Qubit* inttoptr (i64 0 to %Qubit*))
125 call void @__quantum__qis__h__body(%Qubit* inttoptr (i64 1 to %Qubit*))
126 call void @__quantum__qis__h__body(%Qubit* inttoptr (i64 2 to %Qubit*))
127 call void @__quantum__rt__tuple_record_output(i64 0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @0, i64 0, i64 0))
128 ret i64 0
129}
130
131declare void @__quantum__rt__initialize(i8*)
132
133declare void @__quantum__qis__h__body(%Qubit*)
134
135declare void @__quantum__rt__tuple_record_output(i64, i8*)
136
137attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="base_profile" "required_num_qubits"="3" "required_num_results"="0" }
138attributes #1 = { "irreversible" }
139
140; module flags
141
142!llvm.module.flags = !{!0, !1, !2, !3}
143
144!0 = !{i32 1, !"qir_major_version", i32 1}
145!1 = !{i32 7, !"qir_minor_version", i32 0}
146!2 = !{i32 1, !"dynamic_qubit_management", i1 false}
147!3 = !{i32 1, !"dynamic_result_management", i1 false}
148""")
149
150
151def test_qir_from_qsharp_callable_passed_to_python_callable() -> None:
152 qsharp.init(target_profile=qsharp.TargetProfile.Base)
153 qsharp.eval("""
154 operation InvokeWithQubits(nQubits : Int, f : Qubit[] => Unit) : Unit {
155 use qs = Qubit[nQubits];
156 f(qs)
157 }
158 operation AllH(qs : Qubit[]) : Unit {
159 ApplyToEach(H, qs);
160 }
161 """)
162 from qsharp.code import InvokeWithQubits
163 all_h = qsharp.eval("AllH")
164 qir = qsharp.compile(InvokeWithQubits, 3, all_h)
165 assert_expected_inline(str(qir), """\
166%Result = type opaque
167%Qubit = type opaque
168
169@0 = internal constant [4 x i8] c"0_t\\00"
170
171define i64 @ENTRYPOINT__main() #0 {
172block_0:
173 call void @__quantum__rt__initialize(i8* null)
174 call void @__quantum__qis__h__body(%Qubit* inttoptr (i64 0 to %Qubit*))
175 call void @__quantum__qis__h__body(%Qubit* inttoptr (i64 1 to %Qubit*))
176 call void @__quantum__qis__h__body(%Qubit* inttoptr (i64 2 to %Qubit*))
177 call void @__quantum__rt__tuple_record_output(i64 0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @0, i64 0, i64 0))
178 ret i64 0
179}
180
181declare void @__quantum__rt__initialize(i8*)
182
183declare void @__quantum__qis__h__body(%Qubit*)
184
185declare void @__quantum__rt__tuple_record_output(i64, i8*)
186
187attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="base_profile" "required_num_qubits"="3" "required_num_results"="0" }
188attributes #1 = { "irreversible" }
189
190; module flags
191
192!llvm.module.flags = !{!0, !1, !2, !3}
193
194!0 = !{i32 1, !"qir_major_version", i32 1}
195!1 = !{i32 7, !"qir_minor_version", i32 0}
196!2 = !{i32 1, !"dynamic_qubit_management", i1 false}
197!3 = !{i32 1, !"dynamic_result_management", i1 false}
198""")
199
200
201def test_qir_from_qsharp_closure_passed_to_python_callable() -> None:
202 qsharp.init(target_profile=qsharp.TargetProfile.Base)
203 qsharp.eval("""
204 operation InvokeWithQubits(nQubits : Int, f : Qubit[] => Unit) : Unit {
205 use qs = Qubit[nQubits];
206 f(qs)
207 }
208 """)
209 from qsharp.code import InvokeWithQubits
210 apply_h = qsharp.eval("ApplyToEach(H, _)")
211 qir = qsharp.compile(InvokeWithQubits, 3, apply_h)
212 assert_expected_inline(str(qir), """\
213%Result = type opaque
214%Qubit = type opaque
215
216@0 = internal constant [4 x i8] c"0_t\\00"
217
218define i64 @ENTRYPOINT__main() #0 {
219block_0:
220 call void @__quantum__rt__initialize(i8* null)
221 call void @__quantum__qis__h__body(%Qubit* inttoptr (i64 0 to %Qubit*))
222 call void @__quantum__qis__h__body(%Qubit* inttoptr (i64 1 to %Qubit*))
223 call void @__quantum__qis__h__body(%Qubit* inttoptr (i64 2 to %Qubit*))
224 call void @__quantum__rt__tuple_record_output(i64 0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @0, i64 0, i64 0))
225 ret i64 0
226}
227
228declare void @__quantum__rt__initialize(i8*)
229
230declare void @__quantum__qis__h__body(%Qubit*)
231
232declare void @__quantum__rt__tuple_record_output(i64, i8*)
233
234attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="base_profile" "required_num_qubits"="3" "required_num_results"="0" }
235attributes #1 = { "irreversible" }
236
237; module flags
238
239!llvm.module.flags = !{!0, !1, !2, !3}
240
241!0 = !{i32 1, !"qir_major_version", i32 1}
242!1 = !{i32 7, !"qir_minor_version", i32 0}
243!2 = !{i32 1, !"dynamic_qubit_management", i1 false}
244!3 = !{i32 1, !"dynamic_result_management", i1 false}
245""")
246
247
248def test_circuit_from_python_callable_passed_to_python_callable() -> None:
249 qsharp.init(target_profile=qsharp.TargetProfile.Base)
250 qsharp.eval("""
251 operation InvokeWithQubits(nQubits : Int, f : Qubit[] => Unit) : Unit {
252 use qs = Qubit[nQubits];
253 f(qs)
254 }
255 operation AllH(qs : Qubit[]) : Unit {
256 ApplyToEach(H, qs);
257 }
258 """)
259 from qsharp.code import InvokeWithQubits, AllH
260 circuit = qsharp.circuit(InvokeWithQubits, 3, AllH)
261 assert_expected_inline(str(circuit), """q_0 ── H ──
262q_1 ── H ──
263q_2 ── H ──
264""")
265
266
267def test_circuit_from_qsharp_callable_passed_to_python_callable() -> None:
268 qsharp.init(target_profile=qsharp.TargetProfile.Base)
269 qsharp.eval("""
270 operation InvokeWithQubits(nQubits : Int, f : Qubit[] => Unit) : Unit {
271 use qs = Qubit[nQubits];
272 f(qs)
273 }
274 operation AllH(qs : Qubit[]) : Unit {
275 ApplyToEach(H, qs);
276 }
277 """)
278 from qsharp.code import InvokeWithQubits
279 all_h = qsharp.eval("AllH")
280 circuit = qsharp.circuit(InvokeWithQubits, 3, all_h)
281 assert_expected_inline(str(circuit), """q_0 ── H ──
282q_1 ── H ──
283q_2 ── H ──
284""")
285
286
287def test_circuit_from_qsharp_closure_passed_to_python_callable() -> None:
288 qsharp.init(target_profile=qsharp.TargetProfile.Base)
289 qsharp.eval("""
290 operation InvokeWithQubits(nQubits : Int, f : Qubit[] => Unit) : Unit {
291 use qs = Qubit[nQubits];
292 f(qs)
293 }
294 """)
295 from qsharp.code import InvokeWithQubits
296 apply_h = qsharp.eval("ApplyToEach(H, _)")
297 circuit = qsharp.circuit(InvokeWithQubits, 3, apply_h)
298 assert_expected_inline(str(circuit), """q_0 ── H ──
299q_1 ── H ──
300q_2 ── H ──
301""")
302
303
304