forked from microsoft/Quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.qs
52 lines (45 loc) · 1.41 KB
/
Utils.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// This file includes some utility functions to set and
// verify |+⟩ and |-⟩ quantum states.
namespace Microsoft.Quantum.Samples.Teleportation {
open Microsoft.Quantum.Random;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Math;
/// # Summary
/// Sets the qubit's state to |+⟩.
operation SetToPlus(q: Qubit) : Unit {
Reset(q);
H(q);
}
/// # Summary
/// Sets the qubit's state to |−⟩.
operation SetToMinus(q: Qubit) : Unit {
Reset(q);
X(q);
H(q);
}
/// # Summary
/// Returns true if qubit is |+⟩ (assumes qubit is either |+⟩ or |−⟩)
operation MeasureIsPlus(q: Qubit) : Bool {
return (Measure([PauliX], [q]) == Zero);
}
/// # Summary
/// Returns true if qubit is |−⟩ (assumes qubit is either |+> or |−⟩)
operation MeasureIsMinus(q: Qubit) : Bool {
return (Measure([PauliX], [q]) == One);
}
/// # Summary
/// Randomly prepares the qubit into |+⟩ or |−⟩
operation PrepareRandomMessage(q: Qubit) : Unit {
let choice = DrawRandomBool(0.5);
if (choice) {
Message("Sending |−⟩");
SetToMinus(q);
} else {
Message("Sending |+⟩");
SetToPlus(q);
}
}
}