microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/compiler/qsc_codegen/src/qsharp/tests.rs
1065lines · 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 simple_entry_program_is_valid() { |
| 13 | check( |
| 14 | indoc! {r#" |
| 15 | namespace Sample { |
| 16 | @EntryPoint() |
| 17 | operation Entry() : Result { |
| 18 | use q = Qubit(); |
| 19 | H(q); |
| 20 | M(q) |
| 21 | } |
| 22 | }namespace Sample {}"#}, |
| 23 | None, |
| 24 | &expect![[r#" |
| 25 | namespace Sample { |
| 26 | @EntryPoint() |
| 27 | operation Entry() : Result { |
| 28 | use q = Qubit(); |
| 29 | H(q); |
| 30 | M(q) |
| 31 | } |
| 32 | } |
| 33 | namespace Sample {}"#]], |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | #[test] |
| 38 | fn open() { |
| 39 | check( |
| 40 | indoc! {r#" |
| 41 | namespace Sample { |
| 42 | import Std.Intrinsic as sics; |
| 43 | |
| 44 | import Std.Diagnostics.*; |
| 45 | import Std.Intrinsic as intrin; |
| 46 | @EntryPoint() |
| 47 | operation Entry() : Unit { |
| 48 | } |
| 49 | }"#}, |
| 50 | None, |
| 51 | &expect![[r#" |
| 52 | namespace Sample { |
| 53 | import Std.Intrinsic as sics; |
| 54 | import Std.Diagnostics.*; |
| 55 | import Std.Intrinsic as intrin; |
| 56 | @EntryPoint() |
| 57 | operation Entry() : Unit {} |
| 58 | }"#]], |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn newtype() { |
| 64 | check( |
| 65 | indoc! {r#" |
| 66 | namespace Sample { |
| 67 | newtype A = (First : Int, (Second : Double, Third : Bool)); |
| 68 | newtype B = (First : Result, Second : BigInt); |
| 69 | newtype C = (Int, Bool); |
| 70 | newtype D = (First : Int, Second: C); |
| 71 | newtype E = (Real : Double, Imag : Double); |
| 72 | newtype F = (Real : Double, Imaginary : Double, Bool); |
| 73 | @EntryPoint() |
| 74 | operation Entry() : Unit { |
| 75 | } |
| 76 | }"#}, |
| 77 | None, |
| 78 | &expect![[r#" |
| 79 | namespace Sample { |
| 80 | newtype A = (First : Int, (Second : Double, Third : Bool)); |
| 81 | newtype B = (First : Result, Second : BigInt); |
| 82 | newtype C = (Int, Bool); |
| 83 | newtype D = (First : Int, Second : C); |
| 84 | newtype E = (Real : Double, Imag : Double); |
| 85 | newtype F = (Real : Double, Imaginary : Double, Bool); |
| 86 | @EntryPoint() |
| 87 | operation Entry() : Unit {} |
| 88 | }"#]], |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn struct_decl() { |
| 94 | check( |
| 95 | indoc! {r#" |
| 96 | namespace Sample { |
| 97 | struct A {} |
| 98 | struct B { Only : Int } |
| 99 | struct C { First : Int, Second : Double, Third : Bool } |
| 100 | struct D { First : Int, Second: B } |
| 101 | }"#}, |
| 102 | None, |
| 103 | &expect![[r#" |
| 104 | namespace Sample { |
| 105 | struct A {} |
| 106 | struct B { |
| 107 | Only : Int |
| 108 | } |
| 109 | struct C { |
| 110 | First : Int, |
| 111 | Second : Double, |
| 112 | Third : Bool |
| 113 | } |
| 114 | struct D { |
| 115 | First : Int, |
| 116 | Second : B |
| 117 | } |
| 118 | }"#]], |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | #[test] |
| 123 | fn struct_cons() { |
| 124 | check( |
| 125 | indoc! {r#" |
| 126 | namespace Sample { |
| 127 | struct A {} |
| 128 | struct B { Only : Int } |
| 129 | struct C { First : Int, Second : Double, Third : Bool } |
| 130 | struct D { First : Int, Second: B } |
| 131 | function Foo() : Unit { |
| 132 | let a = new A {}; |
| 133 | let b = new B { Only = 1 }; |
| 134 | let c = new C { Third = true, First = 1, Second = 2.0 }; |
| 135 | let d = new D { First = 1, Second = new B { Only = 2 } }; |
| 136 | } |
| 137 | }"#}, |
| 138 | None, |
| 139 | &expect![[r#" |
| 140 | namespace Sample { |
| 141 | struct A {} |
| 142 | struct B { |
| 143 | Only : Int |
| 144 | } |
| 145 | struct C { |
| 146 | First : Int, |
| 147 | Second : Double, |
| 148 | Third : Bool |
| 149 | } |
| 150 | struct D { |
| 151 | First : Int, |
| 152 | Second : B |
| 153 | } |
| 154 | function Foo() : Unit { |
| 155 | let a = new A {}; |
| 156 | let b = new B { |
| 157 | Only = 1 |
| 158 | }; |
| 159 | let c = new C { |
| 160 | Third = true, |
| 161 | First = 1, |
| 162 | Second = 2. |
| 163 | }; |
| 164 | let d = new D { |
| 165 | First = 1, |
| 166 | Second = new B { |
| 167 | Only = 2 |
| 168 | } |
| 169 | |
| 170 | }; |
| 171 | } |
| 172 | }"#]], |
| 173 | ); |
| 174 | } |
| 175 | |
| 176 | #[test] |
| 177 | fn struct_copy_cons() { |
| 178 | check( |
| 179 | indoc! {r#" |
| 180 | namespace Sample { |
| 181 | struct A { First : Int, Second : Double, Third : Bool } |
| 182 | function Foo() : Unit { |
| 183 | let a = new A { First = 1, Second = 2.0, Third = true }; |
| 184 | let b = new A { ...a }; |
| 185 | let c = new A { ...a, Second = 3.0 }; |
| 186 | let d = new A { ...a, Second = 3.0, Third = false }; |
| 187 | } |
| 188 | }"#}, |
| 189 | None, |
| 190 | &expect![[r#" |
| 191 | namespace Sample { |
| 192 | struct A { |
| 193 | First : Int, |
| 194 | Second : Double, |
| 195 | Third : Bool |
| 196 | } |
| 197 | function Foo() : Unit { |
| 198 | let a = new A { |
| 199 | First = 1, |
| 200 | Second = 2., |
| 201 | Third = true |
| 202 | }; |
| 203 | let b = new A { |
| 204 | ...a |
| 205 | }; |
| 206 | let c = new A { |
| 207 | ...a, |
| 208 | Second = 3. |
| 209 | }; |
| 210 | let d = new A { |
| 211 | ...a, |
| 212 | Second = 3., |
| 213 | Third = false |
| 214 | }; |
| 215 | } |
| 216 | }"#]], |
| 217 | ); |
| 218 | } |
| 219 | |
| 220 | #[test] |
| 221 | fn statements() { |
| 222 | check( |
| 223 | indoc! {r#" |
| 224 | namespace A { |
| 225 | @EntryPoint() |
| 226 | operation Entry() : Unit { |
| 227 | mutable x = 7; |
| 228 | let y = 5; |
| 229 | set x = y; |
| 230 | let z = [Zero, One]; |
| 231 | mutable w = z; |
| 232 | let mask = [false, size = 10]; |
| 233 | |
| 234 | for i in Length(mask)-2 .. -1 .. 0 { |
| 235 | let nbPair = mask |
| 236 | w/ i <- true |
| 237 | w/ i + 1 <- true; |
| 238 | } |
| 239 | } |
| 240 | function RichTrippleFor(func : Int[]) : Int[] { |
| 241 | mutable res = func; |
| 242 | for m in 0..(Length(func) - 1) { |
| 243 | mutable s = 1 <<< m >>> 2; |
| 244 | set s >>>= 2; |
| 245 | set s <<<= 2; |
| 246 | for i in 0..(2 * s)..Length(func) - 1 { |
| 247 | mutable k = i + s; |
| 248 | for j in i..i + s - 1 { |
| 249 | mutable t = res[j]; |
| 250 | set res w/= j <- res[j] + res[k]; |
| 251 | set res w/= k <- t - res[k]; |
| 252 | set k = k + 1; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | return res; |
| 257 | } |
| 258 | }"#}, |
| 259 | None, |
| 260 | &expect![[r#" |
| 261 | namespace A { |
| 262 | @EntryPoint() |
| 263 | operation Entry() : Unit { |
| 264 | mutable x = 7; |
| 265 | let y = 5; |
| 266 | set x = y; |
| 267 | let z = [Zero, One]; |
| 268 | mutable w = z; |
| 269 | let mask = [false, size = 10]; |
| 270 | for i in Length(mask) - 2..-1..0 { |
| 271 | let nbPair = mask w/ i <- true w/ i + 1 <- true; |
| 272 | } |
| 273 | } |
| 274 | function RichTrippleFor(func : Int[]) : Int[] { |
| 275 | mutable res = func; |
| 276 | for m in 0..(Length(func) - 1) { |
| 277 | mutable s = 1 <<< m >>> 2; |
| 278 | set s >>>= 2; |
| 279 | set s <<<= 2; |
| 280 | for i in 0..(2 * s)..Length(func) - 1 { |
| 281 | mutable k = i + s; |
| 282 | for j in i..i + s - 1 { |
| 283 | mutable t = res[j]; |
| 284 | set res w/= j <- res[j] + res[k]; |
| 285 | set res w/= k <- t - res[k]; |
| 286 | set k = k + 1; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | return res; |
| 291 | } |
| 292 | }"#]], |
| 293 | ); |
| 294 | } |
| 295 | |
| 296 | #[test] |
| 297 | fn qubits() { |
| 298 | check( |
| 299 | indoc! {r#" |
| 300 | namespace A { |
| 301 | operation B() : Unit { |
| 302 | use q = Qubit(); |
| 303 | borrow q = Qubit(); |
| 304 | use (q1, q2) = (Qubit(), Qubit()); |
| 305 | borrow (q1, q2) = (Qubit(), Qubit()); |
| 306 | use qubits = Qubit[2]; |
| 307 | borrow qubits = Qubit[2]; |
| 308 | let inputSize = 5; |
| 309 | use (control, target) = (Qubit[inputSize], Qubit[inputSize]); |
| 310 | borrow (control, target) = (Qubit[inputSize], Qubit[inputSize]); |
| 311 | use (q,) = (Qubit(),); |
| 312 | borrow (q,) = (Qubit(),); |
| 313 | use q = Qubit() { |
| 314 | X(q); |
| 315 | X(q); |
| 316 | } |
| 317 | borrow q = Qubit() { |
| 318 | X(q); |
| 319 | X(q); |
| 320 | } |
| 321 | } |
| 322 | }"#}, |
| 323 | None, |
| 324 | &expect![[r#" |
| 325 | namespace A { |
| 326 | operation B() : Unit { |
| 327 | use q = Qubit(); |
| 328 | borrow q = Qubit(); |
| 329 | use (q1, q2) = (Qubit(), Qubit()); |
| 330 | borrow (q1, q2) = (Qubit(), Qubit()); |
| 331 | use qubits = Qubit[2]; |
| 332 | borrow qubits = Qubit[2]; |
| 333 | let inputSize = 5; |
| 334 | use (control, target) = (Qubit[inputSize], Qubit[inputSize]); |
| 335 | borrow (control, target) = (Qubit[inputSize], Qubit[inputSize]); |
| 336 | use (q, ) = (Qubit(), ); |
| 337 | borrow (q, ) = (Qubit(), ); |
| 338 | use q = Qubit() { |
| 339 | X(q); |
| 340 | X(q); |
| 341 | } |
| 342 | borrow q = Qubit() { |
| 343 | X(q); |
| 344 | X(q); |
| 345 | } |
| 346 | } |
| 347 | }"#]], |
| 348 | ); |
| 349 | } |
| 350 | |
| 351 | #[test] |
| 352 | fn boolean_ops() { |
| 353 | check( |
| 354 | indoc! {r#" |
| 355 | namespace A { |
| 356 | operation B() : Unit { |
| 357 | let a = true and false or true and (false or true); |
| 358 | let b = not a; |
| 359 | } |
| 360 | }"#}, |
| 361 | None, |
| 362 | &expect![[r#" |
| 363 | namespace A { |
| 364 | operation B() : Unit { |
| 365 | let a = true and false or true and (false or true); |
| 366 | let b = not a; |
| 367 | } |
| 368 | }"#]], |
| 369 | ); |
| 370 | } |
| 371 | |
| 372 | #[test] |
| 373 | fn unary_ops() { |
| 374 | check( |
| 375 | indoc! {r#" |
| 376 | namespace A { |
| 377 | newtype Pair = (Int, Int); |
| 378 | operation B() : Unit { |
| 379 | let a = -1; |
| 380 | let b = not false; |
| 381 | let c = +1; |
| 382 | let f = ~~~1; |
| 383 | let g = Pair(a, c); |
| 384 | let (h, i) = g!; |
| 385 | } |
| 386 | }"#}, |
| 387 | None, |
| 388 | &expect![[r#" |
| 389 | namespace A { |
| 390 | newtype Pair = (Int, Int); |
| 391 | operation B() : Unit { |
| 392 | let a = -1; |
| 393 | let b = not false; |
| 394 | let c = + 1; |
| 395 | let f = ~~~1; |
| 396 | let g = Pair(a, c); |
| 397 | let (h, i) = g!; |
| 398 | } |
| 399 | }"#]], |
| 400 | ); |
| 401 | } |
| 402 | |
| 403 | #[test] |
| 404 | fn binary_ops() { |
| 405 | check( |
| 406 | indoc! {r#" |
| 407 | namespace A { |
| 408 | operation B() : Unit { |
| 409 | let a = 1 + 2 - 3 * 4 / 5 % 6 ^ 7; |
| 410 | let b = (1 < 2); |
| 411 | let c = a <= 3 and a > 4 and a >= 5 and a == 6 or a != 7; |
| 412 | let d = 1 &&& 2 ||| 3 ^^^ 4 <<< 5 >>> 6; |
| 413 | } |
| 414 | }"#}, |
| 415 | None, |
| 416 | &expect![[r#" |
| 417 | namespace A { |
| 418 | operation B() : Unit { |
| 419 | let a = 1 + 2 - 3 * 4 / 5 % 6^7; |
| 420 | let b = (1 < 2); |
| 421 | let c = a <= 3 and a > 4 and a >= 5 and a == 6 or a != 7; |
| 422 | let d = 1 &&& 2 ||| 3 ^^^ 4 <<< 5 >>> 6; |
| 423 | } |
| 424 | }"#]], |
| 425 | ); |
| 426 | } |
| 427 | |
| 428 | #[test] |
| 429 | fn assign_update() { |
| 430 | check( |
| 431 | indoc! {r#" |
| 432 | namespace A { |
| 433 | operation B() : Unit { |
| 434 | mutable a = 1; |
| 435 | set a += 1; |
| 436 | set a &&&= a; |
| 437 | set a /= a; |
| 438 | set a /= a; |
| 439 | set a ^= a; |
| 440 | set a %= a; |
| 441 | set a *= a; |
| 442 | set a |||= a; |
| 443 | set a <<<= a; |
| 444 | set a >>>= a; |
| 445 | set a ^^^= a; |
| 446 | } |
| 447 | }"#}, |
| 448 | None, |
| 449 | &expect![[r#" |
| 450 | namespace A { |
| 451 | operation B() : Unit { |
| 452 | mutable a = 1; |
| 453 | set a += 1; |
| 454 | set a &&&= a; |
| 455 | set a /= a; |
| 456 | set a /= a; |
| 457 | set a ^= a; |
| 458 | set a %= a; |
| 459 | set a *= a; |
| 460 | set a |||= a; |
| 461 | set a <<<= a; |
| 462 | set a >>>= a; |
| 463 | set a ^^^= a; |
| 464 | } |
| 465 | }"#]], |
| 466 | ); |
| 467 | } |
| 468 | |
| 469 | #[test] |
| 470 | fn lambda_fns() { |
| 471 | check( |
| 472 | indoc! {r#" |
| 473 | namespace A { |
| 474 | import Std.Arrays.*; |
| 475 | operation B() : Unit { |
| 476 | let add = (x, y) -> x + y; |
| 477 | let intArray = [1, 2, 3, 4, 5]; |
| 478 | let sum = Fold(add, 0, intArray); |
| 479 | let incremented = Mapped(x -> x + 1, intArray); |
| 480 | |
| 481 | use control = Qubit(); |
| 482 | let cnotOnControl = q => CNOT(control, q); |
| 483 | use q = Qubit(); |
| 484 | cnotOnControl(q); |
| 485 | let incrementByOne = Add(_, 1); |
| 486 | let incrementByOneLambda = x -> Add(x, 1); |
| 487 | let five = incrementByOne(4); |
| 488 | let sumAndAddOne = AddMany(_, _, _, 1); |
| 489 | let sumAndAddOneLambda = (a, b, c) -> AddMany(a, b, c, 1); |
| 490 | let intArray = [1, 2, 3, 4, 5]; |
| 491 | let incremented = Mapped(Add(_, 1), intArray); |
| 492 | } |
| 493 | function Add(x : Int, y : Int) : Int { |
| 494 | return x + y; |
| 495 | } |
| 496 | function AddMany(a : Int, b : Int, c : Int, d : Int) : Int { |
| 497 | return a + b + c + d; |
| 498 | } |
| 499 | }"#}, |
| 500 | None, |
| 501 | &expect![[r#" |
| 502 | namespace A { |
| 503 | import Std.Arrays.*; |
| 504 | operation B() : Unit { |
| 505 | let add = (x, y) -> x + y; |
| 506 | let intArray = [1, 2, 3, 4, 5]; |
| 507 | let sum = Fold(add, 0, intArray); |
| 508 | let incremented = Mapped(x -> x + 1, intArray); |
| 509 | use control = Qubit(); |
| 510 | let cnotOnControl = q => CNOT(control, q); |
| 511 | use q = Qubit(); |
| 512 | cnotOnControl(q); |
| 513 | let incrementByOne = Add(_, 1); |
| 514 | let incrementByOneLambda = x -> Add(x, 1); |
| 515 | let five = incrementByOne(4); |
| 516 | let sumAndAddOne = AddMany(_, _, _, 1); |
| 517 | let sumAndAddOneLambda = (a, b, c) -> AddMany(a, b, c, 1); |
| 518 | let intArray = [1, 2, 3, 4, 5]; |
| 519 | let incremented = Mapped(Add(_, 1), intArray); |
| 520 | } |
| 521 | function Add(x : Int, y : Int) : Int { |
| 522 | return x + y; |
| 523 | } |
| 524 | function AddMany(a : Int, b : Int, c : Int, d : Int) : Int { |
| 525 | return a + b + c + d; |
| 526 | } |
| 527 | }"#]], |
| 528 | ); |
| 529 | } |
| 530 | |
| 531 | #[test] |
| 532 | fn ranges() { |
| 533 | check( |
| 534 | indoc! {r#" |
| 535 | namespace A { |
| 536 | import Std.Arrays.*; |
| 537 | operation B() : Unit { |
| 538 | let range = 1..3; |
| 539 | let range = 2..2..5; |
| 540 | let range = 2..2..6; |
| 541 | let range = 6..-2..2; |
| 542 | let range = 2..-2..2; |
| 543 | let range = 2..1; |
| 544 | mutable array = []; |
| 545 | for i in 0..10 { |
| 546 | set array += [i^2]; |
| 547 | } |
| 548 | let newArray = array[0..2..10]; |
| 549 | let newArray = array[...4]; |
| 550 | let newArray = array[5...]; |
| 551 | let newArray = array[2..3...]; |
| 552 | let newArray = array[...3..7]; |
| 553 | let newArray = array[...]; |
| 554 | let newArray = array[...-3...]; |
| 555 | } |
| 556 | }"#}, |
| 557 | None, |
| 558 | &expect![[r#" |
| 559 | namespace A { |
| 560 | import Std.Arrays.*; |
| 561 | operation B() : Unit { |
| 562 | let range = 1..3; |
| 563 | let range = 2..2..5; |
| 564 | let range = 2..2..6; |
| 565 | let range = 6..-2..2; |
| 566 | let range = 2..-2..2; |
| 567 | let range = 2..1; |
| 568 | mutable array = []; |
| 569 | for i in 0..10 { |
| 570 | set array += [i^2]; |
| 571 | } |
| 572 | let newArray = array[0..2..10]; |
| 573 | let newArray = array[...4]; |
| 574 | let newArray = array[5...]; |
| 575 | let newArray = array[2..3...]; |
| 576 | let newArray = array[...3..7]; |
| 577 | let newArray = array[...]; |
| 578 | let newArray = array[...-3...]; |
| 579 | } |
| 580 | }"#]], |
| 581 | ); |
| 582 | } |
| 583 | |
| 584 | #[test] |
| 585 | fn unary_functors() { |
| 586 | check( |
| 587 | indoc! {r#" |
| 588 | namespace A { |
| 589 | operation B() : Unit { |
| 590 | let v = q => H(q); |
| 591 | use qubit = Qubit(); |
| 592 | Adjoint v(qubit); |
| 593 | Controlled Adjoint v([qubit], qubit); |
| 594 | Adjoint Controlled v([qubit], qubit); |
| 595 | Controlled Controlled Adjoint v([qubit], ([qubit], qubit)); |
| 596 | Controlled Adjoint Controlled v([qubit], ([qubit], qubit)); |
| 597 | Adjoint Controlled Controlled v([qubit], ([qubit], qubit)); |
| 598 | } |
| 599 | }"#}, |
| 600 | None, |
| 601 | &expect![[r#" |
| 602 | namespace A { |
| 603 | operation B() : Unit { |
| 604 | let v = q => H(q); |
| 605 | use qubit = Qubit(); |
| 606 | Adjoint v(qubit); |
| 607 | Controlled Adjoint v([qubit], qubit); |
| 608 | Adjoint Controlled v([qubit], qubit); |
| 609 | Controlled Controlled Adjoint v([qubit], ([qubit], qubit)); |
| 610 | Controlled Adjoint Controlled v([qubit], ([qubit], qubit)); |
| 611 | Adjoint Controlled Controlled v([qubit], ([qubit], qubit)); |
| 612 | } |
| 613 | }"#]], |
| 614 | ); |
| 615 | } |
| 616 | |
| 617 | #[test] |
| 618 | fn field_access_and_string_interning() { |
| 619 | check( |
| 620 | indoc! {r#" |
| 621 | namespace A { |
| 622 | import Std.Math.*; |
| 623 | function ComplexAsString(x : Complex) : String { |
| 624 | if x.Imag < 0.0 { |
| 625 | $"{x.Real} - {AbsD(x.Imag)}i" |
| 626 | } else { |
| 627 | $"{x.Real} + {x.Imag}i" |
| 628 | } |
| 629 | } |
| 630 | }"#}, |
| 631 | None, |
| 632 | &expect![[r#" |
| 633 | namespace A { |
| 634 | import Std.Math.*; |
| 635 | function ComplexAsString(x : Complex) : String { |
| 636 | if x.Imag < 0. { |
| 637 | $"{x.Real} - {AbsD(x.Imag)}i" |
| 638 | } else { |
| 639 | $"{x.Real} + {x.Imag}i" |
| 640 | } |
| 641 | } |
| 642 | }"#]], |
| 643 | ); |
| 644 | } |
| 645 | |
| 646 | #[test] |
| 647 | fn if_exprs() { |
| 648 | check( |
| 649 | indoc! {r#" |
| 650 | namespace A { |
| 651 | function A() : Unit { |
| 652 | mutable x = 0; |
| 653 | // if |
| 654 | if true or false { |
| 655 | set x = 1; |
| 656 | } |
| 657 | // if else |
| 658 | if true and false { |
| 659 | set x = 2; |
| 660 | } else { |
| 661 | set x = 3; |
| 662 | } |
| 663 | // if elif |
| 664 | if true and false { |
| 665 | set x = 4; |
| 666 | } elif true or false { |
| 667 | set x = 5; |
| 668 | } |
| 669 | // if elif else |
| 670 | if true and false { |
| 671 | set x = 4; |
| 672 | } elif true or false { |
| 673 | set x = 5; |
| 674 | } else { |
| 675 | set x = 6; |
| 676 | } |
| 677 | // if elif elif else |
| 678 | if true and false { |
| 679 | set x = 4; |
| 680 | } elif true or false { |
| 681 | set x = 5; |
| 682 | } elif true or false { |
| 683 | set x = 5; |
| 684 | } else { |
| 685 | set x = 6; |
| 686 | } |
| 687 | } |
| 688 | }"#}, |
| 689 | None, |
| 690 | &expect![[r#" |
| 691 | namespace A { |
| 692 | function A() : Unit { |
| 693 | mutable x = 0; |
| 694 | if true or false { |
| 695 | set x = 1; |
| 696 | } |
| 697 | if true and false { |
| 698 | set x = 2; |
| 699 | } else { |
| 700 | set x = 3; |
| 701 | } |
| 702 | if true and false { |
| 703 | set x = 4; |
| 704 | } elif true or false { |
| 705 | set x = 5; |
| 706 | } |
| 707 | if true and false { |
| 708 | set x = 4; |
| 709 | } elif true or false { |
| 710 | set x = 5; |
| 711 | } else { |
| 712 | set x = 6; |
| 713 | } |
| 714 | if true and false { |
| 715 | set x = 4; |
| 716 | } elif true or false { |
| 717 | set x = 5; |
| 718 | } elif true or false { |
| 719 | set x = 5; |
| 720 | } else { |
| 721 | set x = 6; |
| 722 | } |
| 723 | } |
| 724 | }"#]], |
| 725 | ); |
| 726 | } |
| 727 | |
| 728 | #[test] |
| 729 | fn copy_update_range_indices() { |
| 730 | check( |
| 731 | indoc! {r#" |
| 732 | namespace A { |
| 733 | operation A() : Result[] { |
| 734 | let mask = [false, size = 6]; |
| 735 | for i in Length(mask) - 2 ..-1.. 0 { |
| 736 | let nbPair = mask w/ i... <- [true, true]; |
| 737 | Message($"{nbPair}"); |
| 738 | } |
| 739 | return []; |
| 740 | } |
| 741 | }"#}, |
| 742 | None, |
| 743 | &expect![[r#" |
| 744 | namespace A { |
| 745 | operation A() : Result[] { |
| 746 | let mask = [false, size = 6]; |
| 747 | for i in Length(mask) - 2..-1..0 { |
| 748 | let nbPair = mask w/ i... <- [true, true]; |
| 749 | Message($"{nbPair}"); |
| 750 | } |
| 751 | return []; |
| 752 | } |
| 753 | }"#]], |
| 754 | ); |
| 755 | } |
| 756 | |
| 757 | #[test] |
| 758 | fn for_loops() { |
| 759 | check( |
| 760 | indoc! {r#" |
| 761 | namespace A { |
| 762 | operation A() : Unit { |
| 763 | // For loop over `Range` |
| 764 | for i in 0..5 { |
| 765 | for j in 0..4 { |
| 766 | for k in 0..3 { |
| 767 | let x = i * j * k; |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | // For loop over `Array` |
| 772 | for element in [10, 11, 12] { |
| 773 | let x = 7 * element; |
| 774 | } |
| 775 | // For loop over array slice |
| 776 | let array = [1.0, 2.0, 3.0, 4.0]; |
| 777 | for element in array[2...] { |
| 778 | let x = 2.0 * element; |
| 779 | } |
| 780 | } |
| 781 | }"#}, |
| 782 | None, |
| 783 | &expect![[r#" |
| 784 | namespace A { |
| 785 | operation A() : Unit { |
| 786 | for i in 0..5 { |
| 787 | for j in 0..4 { |
| 788 | for k in 0..3 { |
| 789 | let x = i * j * k; |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | for element in [10, 11, 12] { |
| 794 | let x = 7 * element; |
| 795 | } |
| 796 | let array = [1., 2., 3., 4.]; |
| 797 | for element in array[2...] { |
| 798 | let x = 2. * element; |
| 799 | } |
| 800 | } |
| 801 | }"#]], |
| 802 | ); |
| 803 | } |
| 804 | |
| 805 | #[test] |
| 806 | fn while_loops() { |
| 807 | check( |
| 808 | indoc! {r#" |
| 809 | namespace A { |
| 810 | operation A() : Unit { |
| 811 | mutable x = 0; |
| 812 | while x < 30 { |
| 813 | mutable y = 0; |
| 814 | while y < 3 { |
| 815 | mutable z = 0; |
| 816 | while z < 1 { |
| 817 | set z += 1; |
| 818 | set x += 1; |
| 819 | } |
| 820 | set y += 1; |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | }"#}, |
| 825 | None, |
| 826 | &expect![[r#" |
| 827 | namespace A { |
| 828 | operation A() : Unit { |
| 829 | mutable x = 0; |
| 830 | while x < 30 { |
| 831 | mutable y = 0; |
| 832 | while y < 3 { |
| 833 | mutable z = 0; |
| 834 | while z < 1 { |
| 835 | set z += 1; |
| 836 | set x += 1; |
| 837 | } |
| 838 | set y += 1; |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | }"#]], |
| 843 | ); |
| 844 | } |
| 845 | |
| 846 | #[test] |
| 847 | fn repeat_loops() { |
| 848 | check( |
| 849 | indoc! {r#" |
| 850 | namespace A { |
| 851 | operation A() : Unit { |
| 852 | mutable x = 0; |
| 853 | repeat { |
| 854 | set x += 1; |
| 855 | } until x > 3; |
| 856 | use qubit = Qubit(); |
| 857 | repeat { |
| 858 | H(qubit); |
| 859 | } until M(qubit) == Zero |
| 860 | fixup { |
| 861 | Reset(qubit); |
| 862 | } |
| 863 | } |
| 864 | }"#}, |
| 865 | None, |
| 866 | &expect![[r#" |
| 867 | namespace A { |
| 868 | operation A() : Unit { |
| 869 | mutable x = 0; |
| 870 | repeat { |
| 871 | set x += 1; |
| 872 | } until x > 3; |
| 873 | use qubit = Qubit(); |
| 874 | repeat { |
| 875 | H(qubit); |
| 876 | } until M(qubit) == Zero |
| 877 | fixup { |
| 878 | Reset(qubit); |
| 879 | } |
| 880 | } |
| 881 | }"#]], |
| 882 | ); |
| 883 | } |
| 884 | |
| 885 | #[test] |
| 886 | fn ternary() { |
| 887 | check( |
| 888 | indoc! {r#" |
| 889 | namespace A { |
| 890 | operation A() : Unit { |
| 891 | let fahrenheit = 40; |
| 892 | let absoluteValue = fahrenheit > 0 ? fahrenheit | fahrenheit * -1; |
| 893 | } |
| 894 | }"#}, |
| 895 | None, |
| 896 | &expect![[r#" |
| 897 | namespace A { |
| 898 | operation A() : Unit { |
| 899 | let fahrenheit = 40; |
| 900 | let absoluteValue = fahrenheit > 0 ? fahrenheit | fahrenheit * -1; |
| 901 | } |
| 902 | }"#]], |
| 903 | ); |
| 904 | } |
| 905 | |
| 906 | #[test] |
| 907 | fn within_apply() { |
| 908 | check( |
| 909 | indoc! {r#" |
| 910 | namespace A { |
| 911 | operation A() : Unit { |
| 912 | use qubit = Qubit(); |
| 913 | within { |
| 914 | H(qubit); |
| 915 | } apply { |
| 916 | X(qubit); |
| 917 | } |
| 918 | } |
| 919 | }"#}, |
| 920 | None, |
| 921 | &expect![[r#" |
| 922 | namespace A { |
| 923 | operation A() : Unit { |
| 924 | use qubit = Qubit(); |
| 925 | within { |
| 926 | H(qubit); |
| 927 | } apply { |
| 928 | X(qubit); |
| 929 | } |
| 930 | } |
| 931 | }"#]], |
| 932 | ); |
| 933 | } |
| 934 | |
| 935 | #[test] |
| 936 | fn type_decls() { |
| 937 | check( |
| 938 | indoc! {r#" |
| 939 | namespace A { |
| 940 | operation A() : Unit { |
| 941 | newtype Point3d = (X : Double, Y : Double, Z : Double); |
| 942 | newtype DoubleInt = (Double, ItemName : Int); |
| 943 | newtype Nested = (Double, (ItemName : Int, String)); |
| 944 | let point = Point3d(1.0, 2.0, 3.0); |
| 945 | let x : Double = point.X; |
| 946 | let (x, _, _) = point!; |
| 947 | let unwrappedTuple = point!; |
| 948 | } |
| 949 | }"#}, |
| 950 | None, |
| 951 | &expect![[r#" |
| 952 | namespace A { |
| 953 | operation A() : Unit { |
| 954 | newtype Point3d = (X : Double, Y : Double, Z : Double); |
| 955 | newtype DoubleInt = (Double, ItemName : Int); |
| 956 | newtype Nested = (Double, (ItemName : Int, String)); |
| 957 | let point = Point3d(1., 2., 3.); |
| 958 | let x : Double = point.X; |
| 959 | let (x, _, _) = point!; |
| 960 | let unwrappedTuple = point!; |
| 961 | } |
| 962 | }"#]], |
| 963 | ); |
| 964 | } |
| 965 | |
| 966 | #[test] |
| 967 | fn pauli() { |
| 968 | check( |
| 969 | indoc! {r#" |
| 970 | namespace A { |
| 971 | operation A() : Unit { |
| 972 | use q = Qubit(); |
| 973 | mutable pauliDimension = PauliX; |
| 974 | // Measuring along a dimension returns a `Result`: |
| 975 | let result = Measure([pauliDimension], [q]); |
| 976 | set pauliDimension = PauliY; |
| 977 | let result = Measure([pauliDimension], [q]); |
| 978 | set pauliDimension = PauliZ; |
| 979 | let result = Measure([pauliDimension], [q]); |
| 980 | set pauliDimension = PauliI; |
| 981 | let result = Measure([pauliDimension], [q]); |
| 982 | } |
| 983 | }"#}, |
| 984 | None, |
| 985 | &expect![[r#" |
| 986 | namespace A { |
| 987 | operation A() : Unit { |
| 988 | use q = Qubit(); |
| 989 | mutable pauliDimension = PauliX; |
| 990 | let result = Measure([pauliDimension], [q]); |
| 991 | set pauliDimension = PauliY; |
| 992 | let result = Measure([pauliDimension], [q]); |
| 993 | set pauliDimension = PauliZ; |
| 994 | let result = Measure([pauliDimension], [q]); |
| 995 | set pauliDimension = PauliI; |
| 996 | let result = Measure([pauliDimension], [q]); |
| 997 | } |
| 998 | }"#]], |
| 999 | ); |
| 1000 | } |
| 1001 | |
| 1002 | #[test] |
| 1003 | fn bases_and_readable_values() { |
| 1004 | check( |
| 1005 | indoc! {r#" |
| 1006 | namespace A { |
| 1007 | operation A() : Unit { |
| 1008 | let foo = 0x42; |
| 1009 | let foo = 0o42; |
| 1010 | let foo = 42; |
| 1011 | let foo = 0b101010; |
| 1012 | let integer : Int = 42; |
| 1013 | let unit : Unit = (); |
| 1014 | let binaryBigInt : BigInt = 0b101010L; |
| 1015 | let octalBigInt = 0o52L; |
| 1016 | let decimalBigInt = 42L; |
| 1017 | let hexadecimalBigInt = 0x2aL; |
| 1018 | let foo : BigInt = 2L^74; |
| 1019 | let foo = foo + 1L; |
| 1020 | let foo = foo % 2L; |
| 1021 | let foo = foo^2; |
| 1022 | let foo = 1e-9; |
| 1023 | let foo = 1E-15; |
| 1024 | let foo = 1000_0000; |
| 1025 | } |
| 1026 | }"#}, |
| 1027 | None, |
| 1028 | &expect![[r#" |
| 1029 | namespace A { |
| 1030 | operation A() : Unit { |
| 1031 | let foo = 66; |
| 1032 | let foo = 34; |
| 1033 | let foo = 42; |
| 1034 | let foo = 42; |
| 1035 | let integer : Int = 42; |
| 1036 | let unit : Unit = (); |
| 1037 | let binaryBigInt : BigInt = 42L; |
| 1038 | let octalBigInt = 42L; |
| 1039 | let decimalBigInt = 42L; |
| 1040 | let hexadecimalBigInt = 42L; |
| 1041 | let foo : BigInt = 2L^74; |
| 1042 | let foo = foo + 1L; |
| 1043 | let foo = foo % 2L; |
| 1044 | let foo = foo^2; |
| 1045 | let foo = 0.000000001; |
| 1046 | let foo = 0.000000000000001; |
| 1047 | let foo = 10000000; |
| 1048 | } |
| 1049 | }"#]], |
| 1050 | ); |
| 1051 | } |
| 1052 | |
| 1053 | #[test] |
| 1054 | fn complex_literals() { |
| 1055 | check( |
| 1056 | "function Foo() : Complex { 3.0 + 4.0i }", |
| 1057 | None, |
| 1058 | &expect![[r#" |
| 1059 | namespace test { |
| 1060 | function Foo() : Complex { |
| 1061 | 3. + 4.i |
| 1062 | } |
| 1063 | }"#]], |
| 1064 | ); |
| 1065 | } |