From 5df46842091593083a369322c2448504b3eccb66 Mon Sep 17 00:00:00 2001 From: Joshua Aragon Date: Tue, 21 May 2024 09:48:48 -0700 Subject: [PATCH] Add messages to samples in /samples/language (#1509) Part of #1470 --------- Co-authored-by: Joshua Aragon Co-authored-by: Manvi-Agrawal <40084144+Manvi-Agrawal@users.noreply.github.com> Co-authored-by: Mariia Mykhailova --- samples/language/Array.qs | 2 +- samples/language/BigInt.qs | 8 ++++++++ samples/language/BitwiseOperators.qs | 20 +++++++++++++++++--- samples/language/Bool.qs | 8 +++++++- samples/language/ComparisonOperators.qs | 16 ++++++++++++++++ samples/language/ConditionalBranching.qs | 5 ++--- samples/language/CopyAndUpdateOperator.qs | 3 +++ samples/language/DataTypes.qs | 12 ++++++++++-- samples/language/Int.qs | 13 +++++++++++++ samples/language/LambdaExpression.qs | 6 ++++-- samples/language/Operations.qs | 1 + samples/language/PartialApplication.qs | 2 ++ samples/language/Pauli.qs | 2 ++ 13 files changed, 86 insertions(+), 12 deletions(-) diff --git a/samples/language/Array.qs b/samples/language/Array.qs index b46aed1926..30f0d1d096 100644 --- a/samples/language/Array.qs +++ b/samples/language/Array.qs @@ -23,7 +23,7 @@ namespace MyQuantumApp { // A basic Int Array literal let intArray : Int[] = [1, 2, 3, 4]; - Message($"Integer Array : {intArray} of length {Length(intArray)}"); + Message($"Integer Array: {intArray} of length {Length(intArray)}"); // A basic String Array literal let stringArray = ["a", "string", "array"]; diff --git a/samples/language/BigInt.qs b/samples/language/BigInt.qs index 09d8e7047a..ec75395184 100644 --- a/samples/language/BigInt.qs +++ b/samples/language/BigInt.qs @@ -11,16 +11,24 @@ namespace MyQuantumApp { operation Main() : BigInt { // Numbers can be declared in hex, octal, decimal, or binary. let foo = 0x42L; + Message($"Hexadecimal BigInt: {foo}"); let foo = 0o42L; + Message($"Octal BigInt: {foo}"); let foo = 42L; + Message($"Decimal BigInt: {foo}"); let foo = 0b101010L; + Message($"Binary BigInt: {foo}"); // Numbers can be operated upon in the usual ways, with addition (+), subtraction (-), // multiplication (*), division (/), modulo (%), and exponentiation (^). let foo = foo + 1L; + Message($"Addition result: {foo}"); let foo = foo % 2L; + Message($"Modulo result: {foo}"); // `BigInt`s being raised to an exponent take an `Int` as the exponent. let foo = foo^2; + Message($"Exponentiation result: {foo}"); + return foo; } } diff --git a/samples/language/BitwiseOperators.qs b/samples/language/BitwiseOperators.qs index 0ee4890183..289eb6f316 100644 --- a/samples/language/BitwiseOperators.qs +++ b/samples/language/BitwiseOperators.qs @@ -14,33 +14,41 @@ namespace MyQuantumApp { // The integer value -6. let integer = ~~~5; + Message($"Bitwise NOT: {integer}"); // The integer value 4. let integer = ~~~-5; + Message($"Bitwise NOT: {integer}"); - // `&&&` performs a bitwise AND on the bits of two integer. + // `&&&` performs a bitwise AND on the bits of two integers. // The integer value 4. let integer = 5 &&& 6; + Message($"Bitwise AND: {integer}"); // The integer value 2. let integer = -5 &&& 6; + Message($"Bitwise AND: {integer}"); - // `|||` performs a bitwise OR on the bits of two integer. + // `|||` performs a bitwise OR on the bits of two integers. // The integer value 7. let integer = 5 ||| 6; + Message($"Bitwise OR: {integer}"); // The integer value -1. let integer = -5 ||| 6; + Message($"Bitwise OR: {integer}"); - // `^^^` performs a bitwise XOR on the bits of two integer. + // `^^^` performs a bitwise XOR on the bits of two integers. // The integer value 3. let integer = 5 ^^^ 6; + Message($"Bitwise XOR: {integer}"); // The integer value -3. let integer = -5 ^^^ 6; + Message($"Bitwise XOR: {integer}"); // `>>>` performs a signed right bit-shift of a magnitude specified by the // right-hand integer on the bits of the left-hand integer. @@ -48,12 +56,15 @@ namespace MyQuantumApp { // The integer value 1. let integer = 5 >>> 2; + Message($"Right Bit-shift: {integer}"); // The integer value -2. let integer = -5 >>> 2; + Message($"Right Bit-shift: {integer}"); // The integer value 20. let integer = 5 >>> -2; + Message($"Right Bit-shift: {integer}"); // `<<<` performs a signed left bit-shift of a magnitude specified by the // right-hand integer on the bits of the left-hand integer. @@ -61,11 +72,14 @@ namespace MyQuantumApp { // The integer value 20. let integer = 5 <<< 2; + Message($"Left Bit-shift: {integer}"); // The integer value -20. let integer = -5 <<< 2; + Message($"Left Bit-shift: {integer}"); // The integer value 1. let integer = 5 <<< -2; + Message($"Left Bit-shift: {integer}"); } } diff --git a/samples/language/Bool.qs b/samples/language/Bool.qs index 29ebb2700e..7c9d8f59b4 100644 --- a/samples/language/Bool.qs +++ b/samples/language/Bool.qs @@ -10,18 +10,24 @@ namespace MyQuantumApp { operation Main() : Bool { // `Bool`s can be operated upon with boolean operators: let andOp = true and true; + Message($"AND operation: {andOp}"); + let orOp = false or true; + Message($"OR operation: {orOp}"); // Comparisons return booleans: let eqComparison = 1 == 2; + Message($"Equality comparison: {eqComparison}"); // `if` expressions use boolean expressions as their conditions: if (2 == 2) { + Message("2 equals 2"); // do something } else { + Message("2 does not equal 2"); // do something else } return true; } -} \ No newline at end of file +} diff --git a/samples/language/ComparisonOperators.qs b/samples/language/ComparisonOperators.qs index befdbcb37b..c068ba1064 100644 --- a/samples/language/ComparisonOperators.qs +++ b/samples/language/ComparisonOperators.qs @@ -14,63 +14,79 @@ namespace MyQuantumApp { // The Boolean value `true`. let boolean = 4 == 4; + Message($"Equality comparison: {boolean}"); // The Boolean value `false`. let boolean = 4 == 6; + Message($"Equality comparison: {boolean}"); // `!=` tests if the first value is not equivalent to the second value. // It is the opposite of `==`. // The Boolean value `false`. let boolean = 4 != 4; + Message($"Inequality comparison: {boolean}"); // The Boolean value `true`. let boolean = 4 != 6; + Message($"Inequality comparison: {boolean}"); // `<` tests if the first value is strictly less than the second value. // The Boolean value `false`. let boolean = 4 < 4; + Message($"Less than comparison: {boolean}"); // The Boolean value `true`. let boolean = 4 < 6; + Message($"Less than comparison: {boolean}"); // The Boolean value `false`. let boolean = 6 < 4; + Message($"Less than comparison: {boolean}"); // `<=` tests if the first value is less than or equivalent to // the second value. // The Boolean value `true`. let boolean = 4 <= 4; + Message($"Less than or equal comparison: {boolean}"); // The Boolean value `true`. let boolean = 4 <= 6; + Message($"Less than or equal comparison: {boolean}"); // The Boolean value `false`. let boolean = 6 <= 4; + Message($"Less than or equal comparison: {boolean}"); // `>` tests if the first value is strictly greater than the second value. // The Boolean value `false`. let boolean = 4 > 4; + Message($"Greater than comparison: {boolean}"); // The Boolean value `false`. let boolean = 4 > 6; + Message($"Greater than comparison: {boolean}"); // The Boolean value `true`. let boolean = 6 > 4; + Message($"Greater than comparison: {boolean}"); // `>=` tests if the first value is greater than or equivalent to // the second value. // The Boolean value `true`. let boolean = 4 >= 4; + Message($"Greater than or equal comparison: {boolean}"); // The Boolean value `false`. let boolean = 4 >= 6; + Message($"Greater than or equal comparison: {boolean}"); // The Boolean value `true`. let boolean = 6 >= 4; + Message($"Greater than or equal comparison: {boolean}"); } } diff --git a/samples/language/ConditionalBranching.qs b/samples/language/ConditionalBranching.qs index 876a3d9960..c3a1263d80 100644 --- a/samples/language/ConditionalBranching.qs +++ b/samples/language/ConditionalBranching.qs @@ -32,10 +32,9 @@ namespace MyQuantumApp { } else { Message("It is way too hot"); } - - let fahrenheit = 40; - + let fahrenheit = -40; // `if` can also be used as an expression, to conditionally return a value. let absoluteValue = if fahrenheit > 0 { fahrenheit } else { fahrenheit * -1 }; + Message($"Absolute value of {fahrenheit} is {absoluteValue}"); } } diff --git a/samples/language/CopyAndUpdateOperator.qs b/samples/language/CopyAndUpdateOperator.qs index 33ea3e0b07..94e053aaac 100644 --- a/samples/language/CopyAndUpdateOperator.qs +++ b/samples/language/CopyAndUpdateOperator.qs @@ -19,15 +19,18 @@ namespace MyQuantumApp { // `new_array` is an array with values `[10, 11, 100, 13]`. // `array` is unchanged. let new_array = array w/ 2 <- 100; + Message($"Updated array: {new_array}"); // `new_array` is an array with values `[10, 100, 12, 200]`. // `array` is unchanged. let new_array = array w/ 1 <- 100 w/ 3 <- 200; + Message($"Updated array: {new_array}"); // `new_struct` is a Pair with value `Pair(20, 100)`. // `struct` is unchanged. let new_struct = struct w/ second <- 100; + Message($"Updated struct: (first:{new_struct::first}, second:{new_struct::second})"); } } diff --git a/samples/language/DataTypes.qs b/samples/language/DataTypes.qs index 3276b461ea..57e618dad1 100644 --- a/samples/language/DataTypes.qs +++ b/samples/language/DataTypes.qs @@ -9,7 +9,7 @@ /// and operation types. namespace MyQuantumApp { - /// In the below code, all varibles have type annotations to showcase their type. + /// In the below code, all variables have type annotations to showcase their type. @EntryPoint() operation MeasureOneQubit() : Unit { // Notably, Qubits are allocated with the `use` keyword instead of declared with the `let` @@ -27,9 +27,16 @@ namespace MyQuantumApp { // BigInt literals are always suffixed with an L, and can be declared in // binary, octal, decimal, or hexadecimal. let binaryBigInt : BigInt = 0b101010L; + Message($"Binary BigInt: {binaryBigInt}"); + let octalBigInt = 0o52L; + Message($"Octal BigInt: {octalBigInt}"); + let decimalBigInt = 42L; + Message($"Decimal BigInt: {decimalBigInt}"); + let hexadecimalBigInt = 0x2aL; + Message($"Hexadecimal BigInt: {hexadecimalBigInt}"); // A double-precision 64-bit floating-point number. let double = 42.0; @@ -63,11 +70,12 @@ namespace MyQuantumApp { newtype ComplexBool = (Real : Double, Imaginary : Double, Bool); // Instantiation of the above UDT. let complex = ComplexBool(42.0, 0.0, false); + let (real, imaginary, anonymous) = complex!; + Message($"Complex Bool: (real: {real}, imaginary: {imaginary}, anonymous: {anonymous})"); // A function that takes an integer and returns a boolean. This variable declaration // uses a Lambda function as its right hand side. // The function signature is provided as an annotation here, for clarity. let functionType : Int => Bool = (int) => int == 0; } - } diff --git a/samples/language/Int.qs b/samples/language/Int.qs index 481838d7d9..b8c4b61423 100644 --- a/samples/language/Int.qs +++ b/samples/language/Int.qs @@ -10,15 +10,28 @@ namespace MyQuantumApp { operation Main() : Int { // Numbers can be declared in hex, octal, decimal, or binary. let foo = 0x42; + Message($"Hexadecimal: {foo}"); + let foo = 0o42; + Message($"Octal: {foo}"); + let foo = 42; + Message($"Decimal: {foo}"); + let foo = 0b101010; + Message($"Binary: {foo}"); // Numbers can be operated upon in the usual ways, with addition (+), subtraction (-), // multiplication (*), division (/), modulo (%), and exponentiation (^). let foo = foo + 1; + Message($"After addition: {foo}"); + let foo = foo % 2; + Message($"After modulo: {foo}"); + let foo = foo^2; + Message($"After exponentiation: {foo}"); + return foo; } } \ No newline at end of file diff --git a/samples/language/LambdaExpression.qs b/samples/language/LambdaExpression.qs index 26e9c64672..822475b36d 100644 --- a/samples/language/LambdaExpression.qs +++ b/samples/language/LambdaExpression.qs @@ -3,7 +3,7 @@ /// /// # Description /// A lambda expression creates an anonymous function or operation, typically -/// used for local encapsulated functionality. Lambdas offer low sytactic +/// used for local encapsulated functionality. Lambdas offer low syntactic /// overhead, foregoing a fully namespaced function declaration, making them /// useful for single-use or utility functions. namespace MyQuantumApp { @@ -13,6 +13,7 @@ namespace MyQuantumApp { // A lambda function is defined with an arrow `->`. // The below function takes two inputs and adds them. let add = (x, y) -> x + y; + Message($"Lambda add function result: {add(2, 3)}"); // A lambda operation is defined with a fat arrow `=>`. // The below operation closes over `qubit` and applies @@ -26,10 +27,11 @@ namespace MyQuantumApp { // that combines two elements into one. let intArray = [1, 2, 3, 4, 5]; let sum = Fold(add, 0, intArray); + Message($"Sum of array using Fold: {sum}"); // `Map` takes a callable and applies it to all elements in // an array let incremented = Mapped(x -> x + 1, intArray); - + Message($"Array after incrementing each element using Map: {incremented}"); } } \ No newline at end of file diff --git a/samples/language/Operations.qs b/samples/language/Operations.qs index 3a8b423e2a..928a1ec372 100644 --- a/samples/language/Operations.qs +++ b/samples/language/Operations.qs @@ -16,6 +16,7 @@ namespace MyQuantumApp { H(q); // Now we measure the qubit in Z-basis using the `M` operation. let result = M(q); + Message($"Measurement result: {result}"); // We reset the qubit before releasing it using the `Reset` operation. Reset(q); // Finally, we return the result of the measurement. diff --git a/samples/language/PartialApplication.qs b/samples/language/PartialApplication.qs index 9c39360072..9413fb1930 100644 --- a/samples/language/PartialApplication.qs +++ b/samples/language/PartialApplication.qs @@ -19,6 +19,7 @@ namespace MyQuantumApp { // we can add `1` to any number using our partially applied function let five = incrementByOne(4); + Message($"five = incrementByOne(4) => {five}"); // More than one underscore can be used to define a function that takes // multiple arguments. @@ -30,6 +31,7 @@ namespace MyQuantumApp { let intArray = [1, 2, 3, 4, 5]; // The below expression increments all values in an array by 1 let incremented = Mapped(Add(_, 1), intArray); + Message($"Incremented array: {incremented}"); } function Add(x : Int, y : Int) : Int { diff --git a/samples/language/Pauli.qs b/samples/language/Pauli.qs index a7ee59fe12..ca59ae427e 100644 --- a/samples/language/Pauli.qs +++ b/samples/language/Pauli.qs @@ -11,9 +11,11 @@ namespace MyQuantumApp { // A `Pauli` can be declared as a literal. let pauliDimension = PauliX; + Message($"Pauli dimension: {pauliDimension}"); // Measuring along a dimension returns a `Result`: let result = Measure([pauliDimension], [q]); + Message($"Measurement result: {result}"); return result; }