-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGatesMeasurements.qs
68 lines (63 loc) · 2.41 KB
/
GatesMeasurements.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
namespace CallableExamples {
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Measurement;
/// # Summary
/// The collection of examples of representing gates and measurements in Q#.
operation GatesMeasurementsExamples() : Unit {
Message("============================== Q# callables: quantum gates and measurements ==============================");
// Example 1: Non-parameterized quantum gates.
Message("\nExample 1: Non-parameterized quantum gates.");
// Allocate two qubits in the |00⟩ state.
use qs = Qubit[2] {
// Apply the Hadamard gate to the first qubit.
H(qs[0]);
// Apply the CNOT gate to the qubits.
CNOT(qs[0], qs[1]);
// The state is now (|00⟩ + |11⟩)/√2.
DumpMachine();
ResetAll(qs);
}
// Example 2: Quantum gates with additional non-qubit inputs.
Message("\nExample 2: Quantum gates with additional non-qubit inputs.");
// Allocate a qubit in the |0⟩ state.
use q = Qubit() {
// Apply Ry gate.
Ry(0.5, q);
// The state is now 0.97|0⟩ + 0.25|1⟩.
DumpMachine();
Reset(q);
}
// Example 3: Quantum measurement in computational basis.
Message("\nExample 3: Quantum measurement in computational basis.");
// Allocate a qubit in the |0⟩ state.
use q = Qubit() {
Ry(0.5, q);
// Measure the qubit.
let m = M(q);
// Yields Zero with probability 94%
// and One with probability 6%.
Message($"{m}");
}
// Allocate two qubits in the |00⟩ state.
use qs = Qubit[2] {
H(qs[0]);
CNOT(qs[0], qs[1]);
let ms = MultiM(qs);
// Yields [Zero, Zero] with probability 50%
// and [One, One] with probability 50%.
Message($"{ms}");
}
// Example 4: Joint measurement.
Message("\nExample 4: Joint measurement.");
// Allocate two qubits in the |00⟩ state.
use qs = Qubit[2] {
H(qs[0]);
CNOT(qs[0], qs[1]);
let parity = Measure([PauliZ, PauliZ], qs);
// Yields Zero with probability 100%.
Message($"{parity}");
ResetAll(qs);
}
}
}