forked from microsoft/QuantumKatas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReferenceImplementation.qs
87 lines (77 loc) · 2.55 KB
/
ReferenceImplementation.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
//////////////////////////////////////////////////////////////////////
// This file contains reference solutions to all tasks.
// The tasks themselves can be found in Tasks.qs file.
// We recommend that you try to solve the tasks yourself first,
// but feel free to look up the solution if you get stuck.
//////////////////////////////////////////////////////////////////////
namespace Quantum.Kata.QEC_BitFlipCode {
open Microsoft.Quantum.Primitive;
open Microsoft.Quantum.Canon;
// Task 1. Parity Measurements
operation MeasureParity_Reference (register : Qubit[]) : Result {
body {
return Measure([PauliZ; PauliZ; PauliZ], register);
}
}
// Task 2. Encoding Codewords
operation Encode_Reference(register : Qubit[]) : () {
body {
ApplyToEachA(CNOT(Head(register), _), Rest(register));
}
adjoint auto;
}
// Task 3. Error Detection I
operation DetectErrorOnLeftQubit_Reference (register : Qubit[]) : Result {
body {
return Measure([PauliZ; PauliZ], register[0..1]);
}
}
// Task 4. Error Correction I
operation CorrectErrorOnLeftQubit_Reference (register : Qubit[]) : () {
body {
if (Measure([PauliZ; PauliZ], register[0..1]) == One) {
X(register[0]);
}
}
}
// Task 5. Error Detection II
operation DetectErrorOnAnyQubit_Reference (register : Qubit[]) : Int {
body {
let m1 = Measure([PauliZ; PauliZ], register[0..1]);
let m2 = Measure([PauliZ; PauliZ], register[1..2]);
if (m1 == One && m2 == Zero) {
return 1;
}
if (m1 == One && m2 == One) {
return 2;
}
if (m1 == Zero && m2 == One) {
return 3;
}
return 0;
}
}
// Task 6. Error Correction II
operation CorrectErrorOnAnyQubit_Reference (register : Qubit[]) : () {
body {
let idx = DetectErrorOnAnyQubit_Reference(register);
if (idx > 0) {
X(register[idx - 1]);
}
}
}
// Task 7. Logical X Gate
operation LogicalX_Reference (register : Qubit[]) : () {
body {
ApplyToEach(X, register);
}
}
// Task 8. Logical Z Gate
operation LogicalZ_Reference (register : Qubit[]) : () {
body {
ApplyToEach(Z, register);
}
}
}