|
| 1 | +import Std.Diagnostics.*; |
| 2 | +import Std.Math.PI as Pi; |
| 3 | + |
| 4 | +/// A doc comment describing a struct. |
| 5 | +struct Complex { Re : Double, Im : Double } |
| 6 | + |
| 7 | +@EntryPoint() |
| 8 | +operation Main() : Result[] { |
| 9 | + // literals: decimal, hex, binary, octal, BigInt, float, separators |
| 10 | + let ints = [42, 0xFF, 0b1010, 0o52, 1_000]; |
| 11 | + let big = 42L; |
| 12 | + let approx = 1.0e-3; |
| 13 | + |
| 14 | + // qubit allocation (single + tuple), gates, measurement, reset |
| 15 | + use qs = Qubit[3]; |
| 16 | + use (control, target) = (Qubit(), Qubit()); |
| 17 | + for i in 0..Length(qs) - 1 { |
| 18 | + H(qs[i]); |
| 19 | + } |
| 20 | + Controlled X([control], target); |
| 21 | + Rx(Pi() / 4.0, target); |
| 22 | + |
| 23 | + // structs: construction, copy-and-update, field access |
| 24 | + let c = new Complex { Re = 1.0, Im = 0.0 }; |
| 25 | + let c2 = new Complex { ...c, Im = 2.0 }; |
| 26 | + let magnitudeSquared = c2.Re * c2.Re + c2.Im * c2.Im; |
| 27 | + |
| 28 | + // operators: comparison, bitwise, ternary, ranges |
| 29 | + let flags = (5 &&& 3) ||| (1 <<< 2); |
| 30 | + let sign = magnitudeSquared > 0.0 ? One | Zero; |
| 31 | + let slice = ints[1..2]; |
| 32 | + |
| 33 | + // lambdas, partial application, string interpolation |
| 34 | + let addOne = x -> x + 1; |
| 35 | + let applyX = q => X(q); |
| 36 | + Message($"c2 = {c2.Re} + {c2.Im}i, sign = {sign}"); |
| 37 | + |
| 38 | + // control flow: if / elif / else, within-apply, mutation without `set` |
| 39 | + mutable counter = 0; |
| 40 | + counter += 1; |
| 41 | + if counter == 1 { |
| 42 | + Message("one"); |
| 43 | + } elif counter == 2 { |
| 44 | + Message("two"); |
| 45 | + } else { |
| 46 | + Message("many"); |
| 47 | + } |
| 48 | + |
| 49 | + within { |
| 50 | + H(qs[0]); |
| 51 | + } apply { |
| 52 | + Z(qs[0]); |
| 53 | + } |
| 54 | + |
| 55 | + ResetAll(qs); |
| 56 | + return MeasureEachZ(qs); |
| 57 | +} |
| 58 | + |
| 59 | +function Sum2<'T : Add>(a : 'T, b : 'T) : 'T { |
| 60 | + a + b |
| 61 | +} |
| 62 | + |
| 63 | +export Main, Sum2, Complex; |
0 commit comments