This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Tests.qs
172 lines (142 loc) · 6.47 KB
/
Tests.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
//////////////////////////////////////////////////////////////////////
// This file contains testing harness for all tasks.
// You should not modify anything in this file.
//////////////////////////////////////////////////////////////////////
namespace Quantum.Kata.SingleQubitSystemMeasurements {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Random;
//////////////////////////////////////////////////////////////////
// "Framework" operation for testing single-qubit tasks for distinguishing states of one qubit
// with Bool return
operation DistinguishTwoStates (statePrep : ((Qubit, Int) => Unit is Adj), testImpl : (Qubit => Bool), stateName : String[], checkFinalState : Bool) : Unit {
let nTotal = 100;
let nStates = 2;
mutable misclassifications = [0, size = nStates];
use q = Qubit();
for i in 1 .. nTotal {
// get a random bit to define whether qubit will be in a state corresponding to true return (1) or to false one (0)
// state = 0 false return
// state = 1 true return
let state = DrawRandomInt(0, 1);
// do state prep: convert |0⟩ to outcome with false return or to outcome with true return depending on state
statePrep(q, state);
// get the solution's answer and verify if NOT a match, then differentiate what kind of mismatch
let ans = testImpl(q);
if ans != (state == 1) {
set misclassifications w/= state <- misclassifications[state] + 1;
}
// If the final state is to be verified, check if it matches the measurement outcome
if checkFinalState {
Adjoint statePrep(q, state);
AssertQubit(Zero, q);
} else {
Reset(q);
}
}
mutable totalMisclassifications = 0;
for i in 0 .. nStates - 1 {
if misclassifications[i] != 0 {
set totalMisclassifications += misclassifications[i];
Message($"Misclassified {stateName[i]} as {stateName[1 - i]} in {misclassifications[i]} test runs.");
}
}
// This check will tell the total number of failed classifications
Fact(totalMisclassifications == 0, $"{totalMisclassifications} test runs out of {nTotal} returned incorrect state (see output for details).");
}
// ------------------------------------------------------
// Exercise 2. Distinguish |0❭ and |1❭
// ------------------------------------------------------
operation StatePrep_IsQubitZero (q : Qubit, state : Int) : Unit is Adj {
if state == 0 {
// convert |0⟩ to |1⟩
X(q);
}
}
@Test("QuantumSimulator")
operation T2_IsQubitZero () : Unit {
DistinguishTwoStates(StatePrep_IsQubitZero, IsQubitZero, ["|1⟩", "|0⟩"], false);
}
// ------------------------------------------------------
// Exercise 3. Distinguish |+❭ and |-❭ using Measure operation
// ------------------------------------------------------
operation StatePrep_IsQubitMinus (q : Qubit, state : Int) : Unit is Adj {
if state == 1 {
// convert |0⟩ to |-⟩
X(q);
H(q);
} else {
// convert |0⟩ to |+⟩
H(q);
}
}
@Test("QuantumSimulator")
operation T3_IsQubitMinus () : Unit {
DistinguishTwoStates(StatePrep_IsQubitMinus, IsQubitMinus, ["|+⟩", "|-⟩"], false);
}
// ------------------------------------------------------
// Exercise 5. Distinguish specific orthogonal states
// ------------------------------------------------------
// |ψ₊⟩ = 0.6 * |0⟩ + 0.8 * |1⟩,
// |ψ₋⟩ = -0.8 * |0⟩ + 0.6 * |1⟩.
operation StatePrep_IsQubitPsiPlus (q : Qubit, state : Int) : Unit is Adj {
if state == 0 {
// convert |0⟩ to |ψ₋⟩
X(q);
Ry(2.0 * ArcTan2(0.8, 0.6), q);
} else {
// convert |0⟩ to |ψ₊⟩
Ry(2.0 * ArcTan2(0.8, 0.6), q);
}
}
@Test("QuantumSimulator")
operation T5_IsQubitPsiPlus () : Unit {
DistinguishTwoStates(StatePrep_IsQubitPsiPlus, IsQubitPsiPlus,
["|ψ₋⟩", "|ψ₊⟩"], false);
}
// ------------------------------------------------------
// Exercise 6. Distinguish states |A❭ and |B❭
// ------------------------------------------------------
// |A⟩ = cos(alpha) * |0⟩ - i sin(alpha) * |1⟩,
// |B⟩ = - i sin(alpha) * |0⟩ + cos(alpha) * |1⟩.
operation StatePrep_IsQubitA (alpha : Double, q : Qubit, state : Int) : Unit is Adj {
if state == 0 {
// convert |0⟩ to |B⟩
X(q);
Rx(2.0 * alpha, q);
} else {
// convert |0⟩ to |A⟩
Rx(2.0 * alpha, q);
}
}
@Test("QuantumSimulator")
operation T6_IsQubitA () : Unit {
for i in 0 .. 10 {
let alpha = (PI() * IntAsDouble(i)) / 10.0;
DistinguishTwoStates(StatePrep_IsQubitA(alpha, _, _), IsQubitA(alpha, _),
[$"|B⟩ = -i sin({i}π/10)|0⟩ + cos({i}π/10)|1⟩", $"|A⟩ = cos({i}π/10)|0⟩ + i sin({i}π/10)|1⟩"], false);
}
}
// ------------------------------------------------------
// Exercise 7. Measure state in {|A❭, |B❭} basis
// ------------------------------------------------------
// |A⟩ = cos(alpha) * |0⟩ - i sin(alpha) * |1⟩,
// |B⟩ = - i sin(alpha) * |0⟩ + cos(alpha) * |1⟩.
// We can use the StatePrep_IsQubitA operation for the testing
@Test("QuantumSimulator")
operation T7_MeasureInABBasis () : Unit {
for i in 0 .. 10 {
let alpha = (PI() * IntAsDouble(i)) / 10.0;
DistinguishTwoStates(StatePrep_IsQubitA(alpha, _, _),
q => MeasureInABBasis(alpha, q) == Zero, // IsResultZero(MeasureInABBasis(alpha, _),_),
[$"|B⟩=(-i sin({i}π/10)|0⟩ + cos({i}π/10)|1⟩)", $"|A⟩=(cos({i}π/10)|0⟩ + i sin({i}π/10)|1⟩)"], true);
}
}
}