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