-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.qs
94 lines (83 loc) · 3.27 KB
/
demo.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) Sarah Kaiser. All rights reserved.
// Licensed under the MIT License.
namespace OSD.Demo {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Measurement;
/// # Summary
/// Prints out the standard "Hello, world!" message with a
/// bonus favorite number passed as input.
///
/// # Description
/// Takes an `Int` as input and prints the standard test message
/// as well as what integer the user likes.
///
/// # Input
/// ## FavoriteNumber
/// The integer you think is the coolest.
function HelloWorld(favoriteNumber : Int) : Unit {
Message($"Hello, world! I like the number {favoriteNumber}.");
}
/// # Summary
/// Generates a random value from {0,1} by measuring a qubit in
/// an equal superposition.
///
/// # Description
/// Given a qubit initially in the |0⟩ state, applies the H operation
/// to that qubit such that it has the state (1/√2) |0⟩ + (1/√2) |1⟩.
/// Measurement of this state returns a One Result with probability 0.5
/// and a Zero Result with probability 0.5.
operation Qrng() : Result {
use qubit = Qubit();
H(qubit);
return M(qubit);
}
/// # Summary
/// Generates a random value from {0,1} by measuring a qubit in
/// an equal superposition, and show some diagnostics of the
/// target machine.
///
/// # Description
/// Given a qubit initially in the |0⟩ state, applies the H operation
/// to that qubit such that it has the state (1/√2) |0⟩ + (1/√2) |1⟩.
/// Measurement of this state returns a One Result with probability 0.5
/// and a Zero Result with probability 0.5.
operation QrngWithDiagnostics() : Result {
use qubit = Qubit();
Message("Here is what the simulator uses to record a qubit in the 0 state:");
DumpRegister((), [qubit]);
Message(" ");
H(qubit);
Message("After using H(qubit) to prepare a superposition state:");
DumpRegister((), [qubit]);
return MResetZ(qubit);
}
/// # Summary
/// Generates a pair of entangled qubits and then measures them.
///
/// # Description
/// Given two qubits initially in the |0⟩ state, applies the H operation
/// to qubit 1 such that it has the state (1/√2) |0⟩ + (1/√2) |1⟩.
/// Then qubit 1 is used as the control for a CNOT operation on qubit 2.
/// Measurement of this state returns a (0,0) Result with probability 0.5
/// and a (1,1) Result with probability 0.5.
operation EntangleQubits(verbose : Bool) : (Result, Result) {
// Preparing two qubits
use (qubit1, qubit2) = (Qubit(), Qubit());
if (verbose) {
Message("State that two qubits has:");
DumpRegister((), [qubit1, qubit2]);
}
// The operations on the qubits needed to entangle them
H(qubit1);
CNOT(qubit1, qubit2);
if (verbose) {
Message(" ");
Message("After entangling the two qubits:");
DumpRegister((), [qubit1, qubit2]);
}
// Finally, measure and reset the qubits
return (MResetZ(qubit1), MResetZ(qubit2));
}
}