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