microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
library/core/core.qs
75lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | namespace Std.Core { |
| 5 | /// # Summary |
| 6 | /// Returns the number of elements in the input array `a`. |
| 7 | /// |
| 8 | /// # Input |
| 9 | /// ## a |
| 10 | /// Input array. |
| 11 | /// |
| 12 | /// # Output |
| 13 | /// The total number of elements in the input array `a`. |
| 14 | /// |
| 15 | /// # Example |
| 16 | /// ```qsharp |
| 17 | /// Message($"{ Length([0, 0, 0]) }"); // Prints 3 |
| 18 | /// ``` |
| 19 | function Length<'T>(a : 'T[]) : Int { |
| 20 | body intrinsic; |
| 21 | } |
| 22 | |
| 23 | /// # Summary |
| 24 | /// Creates an array of given `length` with all elements equal to given |
| 25 | /// `value`. `length` must be a non-negative integer. |
| 26 | /// |
| 27 | /// # Description |
| 28 | /// Use this function to create an array of length `length` where each |
| 29 | /// element is equal to `value`. This way of creating an array is preferred |
| 30 | /// over other methods if all elements of the array must be the same and |
| 31 | /// the length is known upfront. |
| 32 | /// |
| 33 | /// # Input |
| 34 | /// ## value |
| 35 | /// The value of each element of the new array. |
| 36 | /// ## length |
| 37 | /// Length of the new array. |
| 38 | /// |
| 39 | /// # Output |
| 40 | /// A new array of length `length`, such that every element is `value`. |
| 41 | /// |
| 42 | /// # Example |
| 43 | /// ```qsharp |
| 44 | /// // Create an array of 3 Boolean values, each equal to `true` |
| 45 | /// let array = Repeated(true, 3); |
| 46 | /// ``` |
| 47 | function Repeated<'T>(value : 'T, length : Int) : 'T[] { |
| 48 | if length < 0 { |
| 49 | fail "Length must be a non-negative integer"; |
| 50 | } |
| 51 | |
| 52 | mutable output = []; |
| 53 | for _ in 1..length { |
| 54 | set output += [value]; |
| 55 | } |
| 56 | |
| 57 | output |
| 58 | } |
| 59 | |
| 60 | /// # Summary |
| 61 | /// Represents a complex number by its real and imaginary components. |
| 62 | /// The real component can be accessed via the `Real` field, and the imaginary |
| 63 | /// component via the `Imag` field. Complex literals can be written using the |
| 64 | /// form `a + bi`, where `a` is the Double literal for the real part and |
| 65 | /// `b` is the Double literal for the imaginary part. |
| 66 | /// |
| 67 | /// # Example |
| 68 | /// The following snippet defines the imaginary unit 𝑖 = 0 + 1𝑖: |
| 69 | /// ```qsharp |
| 70 | /// let imagUnit = 1.0i; |
| 71 | /// ``` |
| 72 | struct Complex { Real : Double, Imag : Double } |
| 73 | |
| 74 | export Length, Repeated, Complex; |
| 75 | } |
| 76 | |