microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
cesarzc/ssa-panic

Branches

Tags

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

Clone

HTTPS

Download ZIP

compiler/qsc_codegen/src/qsharp/spec_decls.rs

238lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#![allow(clippy::too_many_lines)]
5#![allow(clippy::needless_raw_string_hashes)]
6
7use expect_test::expect;
8use indoc::indoc;
9
10use super::test_utils::check;
11
12#[test]
13fn body_with_implicit_return() {
14 check(
15 indoc! {r#"
16 namespace A {
17 operation B() : Int {
18 let x = 5;
19 x
20 }
21 }"#},
22 None,
23 &expect![[r#"
24 namespace A {
25 operation B() : Int {
26 let x = 5;
27 x
28 }
29 }"#]],
30 );
31}
32
33#[test]
34fn attributes() {
35 check(
36 indoc! {r#"
37 namespace Sample {
38 @EntryPoint()
39 @Config(Unrestricted)
40 operation Entry() : Unit {}
41 }"#},
42 None,
43 &expect![[r#"
44 namespace Sample {
45 @EntryPoint()
46 @Config(Unrestricted)
47 operation Entry() : Unit {}
48 }"#]],
49 );
50}
51
52#[test]
53fn comments_are_omitted() {
54 check(
55 indoc! {r#"
56 // NS comment
57 namespace A {
58 // op comment here
59 operation B() : Unit {
60 // comment here
61 // another comment
62 } // trailing comment
63 }"#},
64 None,
65 &expect![[r#"
66 namespace A {
67 operation B() : Unit {}
68 }"#]],
69 );
70}
71
72#[test]
73fn visibility() {
74 check(
75 indoc! {r#"
76 // NS comment
77 namespace A {
78 // op comment here
79 internal operation B() : Unit {
80 // comment here
81 // another comment
82 } // trailing comment
83 }"#},
84 None,
85 &expect![[r#"
86 namespace A {
87 internal operation B() : Unit {}
88 }"#]],
89 );
90}
91
92#[test]
93fn callable_specs() {
94 check(
95 indoc! {r#"
96 namespace Sample {
97 @EntryPoint()
98 operation Entry() : Result {
99 use q = Qubit();
100 // comment here
101 H(q);
102 // implicit return
103 M(q)
104 }
105 operation Op1(q: Qubit[]) : Unit is Ctl + Adj {
106 body ... {
107 Microsoft.Quantum.Intrinsic.H(q[0]);
108 }
109 adjoint invert;
110 controlled distribute;
111 controlled adjoint auto;
112 }
113 operation op2(q: Qubit) : Unit is Adj + Ctl {
114 body ... {
115 H(q);
116 }
117 adjoint self;
118 controlled auto;
119 controlled adjoint invert;
120 }
121 operation op3(q: Qubit) : Unit is Ctl + Adj {
122 body ... {
123 H(q);
124 }
125 adjoint auto;
126 controlled adjoint self;
127 }
128 operation op4() : Unit {
129 body intrinsic;
130 }
131 operation op5(q: Qubit) : Unit is Ctl {
132 body ... {
133 H(q);
134 }
135 controlled auto;
136 }
137 operation op6(q: Qubit) : Unit is Adj {
138 body ... {
139 H(q);
140 }
141 adjoint auto;
142 }
143 operation op7() : Unit is Adj * Adj {
144 body ... {}
145 }
146 operation op8() : Unit is (Adj) {}
147 operation op9(bar: () => Unit is Ctl) : Unit {}
148 operation op10(bar: () => Unit is Adj) : Unit {}
149 operation op11(bar: () => Unit is Adj + Ctl) : Unit {}
150 operation op12(b: Unit => Unit is Adj) : Unit {}
151 }"#},
152 None,
153 &expect![[r#"
154 namespace Sample {
155 @EntryPoint()
156 operation Entry() : Result {
157 use q = Qubit();
158 H(q);
159 M(q)
160 }
161 operation Op1(q : Qubit[]) : Unit is Ctl + Adj {
162 body ... {
163 Microsoft.Quantum.Intrinsic.H(q[0]);
164 }
165 adjoint invert;
166 controlled distribute;
167 controlled adjoint auto;
168 }
169 operation op2(q : Qubit) : Unit is Adj + Ctl {
170 body ... {
171 H(q);
172 }
173 adjoint self;
174 controlled auto;
175 controlled adjoint invert;
176 }
177 operation op3(q : Qubit) : Unit is Ctl + Adj {
178 body ... {
179 H(q);
180 }
181 adjoint auto;
182 controlled adjoint self;
183 }
184 operation op4() : Unit {
185 body intrinsic;
186 }
187 operation op5(q : Qubit) : Unit is Ctl {
188 body ... {
189 H(q);
190 }
191 controlled auto;
192 }
193 operation op6(q : Qubit) : Unit is Adj {
194 body ... {
195 H(q);
196 }
197 adjoint auto;
198 }
199 operation op7() : Unit is Adj * Adj {
200 body ... {}
201 }
202 operation op8() : Unit is (Adj) {}
203 operation op9(bar : () => Unit is Ctl) : Unit {}
204 operation op10(bar : () => Unit is Adj) : Unit {}
205 operation op11(bar : () => Unit is Adj + Ctl) : Unit {}
206 operation op12(b : Unit => Unit is Adj) : Unit {}
207 }"#]],
208 );
209}
210
211#[test]
212fn callable_core_types() {
213 check(
214 indoc! {r#"
215 namespace A {
216 operation B() : Int {
217 let x = 5;
218 x
219 }
220 function C() : Int {
221 let x = 42;
222 x
223 }
224 }"#},
225 None,
226 &expect![[r#"
227 namespace A {
228 operation B() : Int {
229 let x = 5;
230 x
231 }
232 function C() : Int {
233 let x = 42;
234 x
235 }
236 }"#]],
237 );
238}
239