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