microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/language/DataTypes.qs
73lines · modecode
| 1 | /// # Sample |
| 2 | /// Data Types |
| 3 | /// |
| 4 | /// # Description |
| 5 | /// Q# has a pragmatic and intuitive type system. All data types in Q# are immutable. The available |
| 6 | /// primitive data types are: `Unit`, `Int`, `BigInt`, `Double`, `Bool`, `String`, `Qubit`, `Result`, |
| 7 | /// `Pauli`, and `Range`. In addition to these primitive types, Q# offers primitive aggregate types |
| 8 | /// as well, `Array` and `Tuple`; composite types (a.k.a. user-defined types or UDTs); and function |
| 9 | /// and operation types. |
| 10 | namespace MyQuantumApp { |
| 11 | |
| 12 | /// In the below code, all varibles have type annotations to showcase their type. |
| 13 | @EntryPoint() |
| 14 | operation MeasureOneQubit() : Unit { |
| 15 | // Notably, Qubits are allocated with the `use` keyword instead of declared with the `let` |
| 16 | // keyword. |
| 17 | // The resulting value represents an opaque identifier by which virtual quantum memory |
| 18 | // can be addressed. Values of type Qubit are instantiated via allocation. |
| 19 | use q : Qubit = Qubit(); |
| 20 | |
| 21 | // A 64-bit signed integer. |
| 22 | let integer : Int = 42; |
| 23 | |
| 24 | // The singleton type whose only value is (). |
| 25 | let unit : Unit = (); |
| 26 | |
| 27 | // BigInt literals are always suffixed with an L, and can be declared in |
| 28 | // binary, octal, decimal, or hexadecimal. |
| 29 | let binaryBigInt : BigInt = 0b101010L; |
| 30 | let octalBigInt = 0o52L; |
| 31 | let decimalBigInt = 42L; |
| 32 | let hexadecimalBigInt = 0x2aL; |
| 33 | |
| 34 | // A double-precision 64-bit floating-point number. |
| 35 | let double = 42.0; |
| 36 | |
| 37 | // Boolean values. Possible values are `true` or `false`. |
| 38 | let bool = true; |
| 39 | |
| 40 | // Text as values that consist of a sequence of UTF-16 code units. |
| 41 | let string = ""; |
| 42 | |
| 43 | // Represents the result of a projective measurement onto the eigenspaces |
| 44 | // of a quantum operator with eigenvalues ±1. Possible values are `Zero` or `One`. |
| 45 | let result = One; |
| 46 | |
| 47 | // A single-qubit Pauli matrix. Possible values are PauliI, PauliX, PauliY, or PauliZ. |
| 48 | let pauli = [PauliX, PauliY, PauliZ]; |
| 49 | |
| 50 | // Represents an ordered sequence of equally spaced Int values. |
| 51 | // Values may represent sequences in ascending or descending order. |
| 52 | let range = 1..100; |
| 53 | |
| 54 | // A collection that contains a sequence of values of the same type. |
| 55 | let array_of_ints = [1, 2, 3]; |
| 56 | |
| 57 | // A tuple contains a fixed number of items of potentially different types. |
| 58 | // Tuples containing a single element are equivalent to the element they contain. |
| 59 | let tuple = (1, "one", One); |
| 60 | |
| 61 | // A user-defined-type (UDT) consisting of two named parameters, `Real` and `Imaginary`, |
| 62 | // and one anonymous parameter of Boolean type. |
| 63 | newtype ComplexBool = (Real : Double, Imaginary : Double, Bool); |
| 64 | // Instantiation of the above UDT. |
| 65 | let complex = ComplexBool(42.0, 0.0, false); |
| 66 | |
| 67 | // A function that takes an integer and returns a boolean. This variable declaration |
| 68 | // uses a Lambda function as its right hand side. |
| 69 | // The function signature is provided as an annotation here, for clarity. |
| 70 | let functionType : Int => Bool = (int) => int == 0; |
| 71 | } |
| 72 | |
| 73 | } |
| 74 | |