-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBasics.qs
75 lines (63 loc) · 2.21 KB
/
Basics.qs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
namespace CallableExamples {
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Math;
// Defining Q# functions.
function Sum(a : Int, b : Int) : Int {
return a + b;
}
function Product(a : Int, b : Int) : Int {
return a * b;
}
// Defining Q# operations.
operation ApplyRyArray(
qs : Qubit[],
angles : Double[]
) : Unit {
for (q, angle) in Zipped(qs, angles) {
Ry(angle, q);
}
}
// Two ways of using tuple arguments:
// 1. Embed tuple deconstruction in the arguments.
function AddInputs1(
a : Int,
(b : Int, c : Int, d : Int),
(e : Int, f : Int)
) : Int {
return a + b + c + d + e + f;
}
// 2. Define types of tuple arguments.
function AddInputs2(
a : Int,
t1 : (Int, Int, Int),
t2 : (Int, Int)
) : Int {
// Deconstruct tuple arguments in body.
let (b, c, d) = t1;
let (e, f) = t2;
return a + b + c + d + e + f;
}
/// # Summary
/// The collection of examples of defining and calling operations and functions.
operation BasicsExamples() : Unit {
Message("============================== Q# callables: the basics ==============================");
// Example 1: Calling an operation or a function with non-Unit return type.
Message("\nExample 1: Calling an operation or a function with non-Unit return type.");
let sum = Sum(5, 13);
Message($"Sum = {sum}");
let sumOfAll1 = AddInputs1(1, (2, 3, 4), (5, 6));
let sumOfAll2 = AddInputs2(1, (2, 3, 4), (5, 6));
Message($"Sum of all arguments = {sumOfAll1} = {sumOfAll2}");
// Example 2: Calling an operation of a function with Unit return type.
Message("\nExample 2: Calling an operation of a function with Unit return type.");
// Allocate two qubits in the |00⟩ state.
use qs = Qubit[2];
ApplyRyArray(qs, [0.5 * PI(), 0.5 * PI()]);
// Print the qubits' state to see it is
// (|00⟩ + |01⟩ + |10⟩ + |11⟩) / 2.
DumpMachine();
ResetAll(qs);
}
}