Skip to content

Commit

Permalink
Add messages to samples in /samples/language (#1509)
Browse files Browse the repository at this point in the history
Part of #1470

---------

Co-authored-by: Joshua Aragon <jaragon32@microsoft.com>
Co-authored-by: Manvi-Agrawal <40084144+Manvi-Agrawal@users.noreply.github.com>
Co-authored-by: Mariia Mykhailova <michaylova@gmail.com>
  • Loading branch information
4 people committed May 21, 2024
1 parent 58a187a commit 5df4684
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 12 deletions.
2 changes: 1 addition & 1 deletion samples/language/Array.qs
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down
8 changes: 8 additions & 0 deletions samples/language/BigInt.qs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
20 changes: 17 additions & 3 deletions samples/language/BitwiseOperators.qs
Original file line number Diff line number Diff line change
Expand Up @@ -14,58 +14,72 @@ 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.
// If the right-hand integer is negative, it reverses the direction of the bit-shift.

// 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.
// If the right-hand integer is negative, it reverses the direction of the bit-shift.

// 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}");
}
}
8 changes: 7 additions & 1 deletion samples/language/Bool.qs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
16 changes: 16 additions & 0 deletions samples/language/ComparisonOperators.qs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
}
5 changes: 2 additions & 3 deletions samples/language/ConditionalBranching.qs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
}
3 changes: 3 additions & 0 deletions samples/language/CopyAndUpdateOperator.qs
Original file line number Diff line number Diff line change
Expand Up @@ -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})");
}
}
12 changes: 10 additions & 2 deletions samples/language/DataTypes.qs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

}
13 changes: 13 additions & 0 deletions samples/language/Int.qs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
6 changes: 4 additions & 2 deletions samples/language/LambdaExpression.qs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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}");
}
}
1 change: 1 addition & 0 deletions samples/language/Operations.qs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions samples/language/PartialApplication.qs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions samples/language/Pauli.qs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 5df4684

Please sign in to comment.