Skip to content

Commit

Permalink
Added more Messages to the samples/Language (#1561)
Browse files Browse the repository at this point in the history
#1470

---------

Co-authored-by: Joshua Aragon <[email protected]>
Co-authored-by: Mariia Mykhailova <[email protected]>
  • Loading branch information
3 people authored May 23, 2024
1 parent 9e7dbee commit ec6df4a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 1 deletion.
14 changes: 14 additions & 0 deletions samples/language/Range.qs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,27 @@ namespace MyQuantumApp {

// The range 1, 2, 3.
let range = 1..3;
Message($"Range: {range}");

// The range 2, 4.
let range = 2..2..5;
Message($"Range: {range}");

// The range 2, 4, 6.
let range = 2..2..6;
Message($"Range: {range}");

// The range 6, 4, 2.
let range = 6..-2..2;
Message($"Range: {range}");

// The range 2.
let range = 2..-2..2;
Message($"Range: {range}");

// The empty range.
let range = 2..1;
Message($"Range: {range}");

// Ranges are used in for-loop expressions. They must be closed ranges.

Expand All @@ -40,31 +46,39 @@ namespace MyQuantumApp {
for i in 0..10 {
set array += [i^2];
}
Message($"Array: {array}");

// Ranges can be used to create array slices.

// The array [0, 4, 16, 36, 64, 100].
let newArray = array[0..2..10];
Message($"Array slice [0..2..10]: {newArray}");

// Open ranges can also be used to create array slices.

// The array [0, 1, 4, 9, 16].
let newArray = array[...4];
Message($"Array slice [...4]: {newArray}");

// The array [25, 36, 49, 64, 81, 100].
let newArray = array[5...];
Message($"Array slice [5...]: {newArray}");

// The array [4, 25, 64].
let newArray = array[2..3...];
Message($"Array slice [2..3...]: {newArray}");

// The array [0, 9, 36].
let newArray = array[...3..7];
Message($"Array slice [...3..7]: {newArray}");

// The array [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100].
let newArray = array[...];
Message($"Array slice [...]: {newArray}");

// The array [100, 49, 16, 1].
let newArray = array[...-3...];
Message($"Array slice [...-3...]: {newArray}");

return range;
}
Expand Down
1 change: 1 addition & 0 deletions samples/language/Result.qs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace MyQuantumApp {

// Measure the qubit.
let measurement = M(q);
Message($"Measurement: {measurement}");

// Reset the qubit.
Reset(q);
Expand Down
2 changes: 2 additions & 0 deletions samples/language/String.qs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ namespace MyQuantumApp {

// Strings can be concatenated with `+`
let myString = myString + "Bar";
Message(myString);

// Q# supports string interpolation with the `$` prefix.
let myString = $"interpolated: {myString}";
Message(myString);

return myString;
}
Expand Down
3 changes: 2 additions & 1 deletion samples/language/Ternary.qs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
namespace MyQuantumApp {
@EntryPoint()
operation Main() : Unit {
let fahrenheit = 40;
let fahrenheit = -40;

// The below ternary expression sets the value of `fahrenheit` to its absolute value.
// `fahrenheit` if `fahrenheit` is positive,
// `fahrenheit * -1` if `fahrenheit` is negative.
let absoluteValue = fahrenheit > 0 ? fahrenheit | fahrenheit * -1;
Message($"Absolute value: {absoluteValue}");
}
}
2 changes: 2 additions & 0 deletions samples/language/Tuple.qs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ namespace MyQuantumApp {
operation Main() : (Int, String) {
// A tuple of type `String`, `Int`, and `Double`
let myTuple = ("Id", 0, 1.);
Message($"Tuple: {myTuple}");

// A tuple of type `Pauli`, and a nested tuple of type `(Int, Int)`.
// The type annotation is provided for clarity, but not necessary.
let myTuple : (Pauli, (Int, Int)) = (PauliX, (3, 1));
Message($"Tuple: {myTuple}");

return (0, "Foo");
}
Expand Down
4 changes: 4 additions & 0 deletions samples/language/Variables.qs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ namespace MyQuantumApp {
operation Main() : Unit {
// Immutable variables are declared with the `let` keyword:
let immutableInt = 42;
Message($"Immutable Int: {immutableInt}");

// Mutable variables are declared with the `mutable` keyword:
mutable mutableInt = 43;
Message($"Mutable Int: {mutableInt}");

// Mutable variables can be mutated with the `set` keyword:
set mutableInt -= 1;
Message($"Mutable Int after mutation: {mutableInt}");

// All variables can be shadowed by symbols introduced later in scope.
// This is not mutation, rather, this is declaring a new variable
// entirely.
let immutableInt = 43;
let immutableInt = 0;
Message($"Shadowed Immutable Int: {immutableInt}");

// UDTs can also be updated with copy-and-update expressions (`w/`)
// or evaluate-and-reassign expressions (`w/=`).
Expand Down

0 comments on commit ec6df4a

Please sign in to comment.