forked from microsoft/QuantumKatas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReferenceImplementation.qs
175 lines (148 loc) · 7.21 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
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
173
174
175
// 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.GraphColoring {
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Diagnostics;
//////////////////////////////////////////////////////////////////
// Part I. Colors representation and manipulation
//////////////////////////////////////////////////////////////////
// Task 1.1. Initialize register to a color
operation InitializeColor_Reference (C : Int, register : Qubit[]) : Unit is Adj {
let N = Length(register);
// Convert C to an array of bits in little endian format
let binaryC = IntAsBoolArray(C, N);
// Value "true" corresponds to bit 1 and requires applying an X gate
ApplyPauliFromBitString(PauliX, true, binaryC, register);
}
// Task 1.2. Read color from a register
operation MeasureColor_Reference (register : Qubit[]) : Int {
return ResultArrayAsInt(MultiM(register));
}
// Task 1.3. Read coloring from a register
operation MeasureColoring_Reference (K : Int, register : Qubit[]) : Int[] {
let N = Length(register) / K;
let colorPartitions = Partitioned(ConstantArray(K - 1, N), register);
return ForEach(MeasureColor_Reference, colorPartitions);
}
// Task 1.4. 2-bit color equality oracle
operation ColorEqualityOracle_2bit_Reference (c0 : Qubit[], c1 : Qubit[], target : Qubit) : Unit is Adj+Ctl {
// Small case solution with no extra qubits allocated:
// iterate over all bit strings of length 2, and do a controlled X on the target qubit
// with control qubits c0 and c1 in states described by each of these bit strings.
// For a better solution, see task 1.5.
for (color in 0..3) {
let binaryColor = IntAsBoolArray(color, 2);
(ControlledOnBitString(binaryColor + binaryColor, X))(c0 + c1, target);
}
}
// Task 1.5. N-bit color equality oracle (no extra qubits)
operation ColorEqualityOracle_Nbit_Reference (c0 : Qubit[], c1 : Qubit[], target : Qubit) : Unit is Adj+Ctl {
within {
for ((q0, q1) in Zipped(c0, c1)) {
// compute XOR of q0 and q1 in place (storing it in q1)
CNOT(q0, q1);
}
} apply {
// if all XORs are 0, the bit strings are equal
(ControlledOnInt(0, X))(c1, target);
}
}
//////////////////////////////////////////////////////////////////
// Part II. Vertex coloring problem
//////////////////////////////////////////////////////////////////
// Task 2.1. Classical verification of vertex coloring
function IsVertexColoringValid_Reference (V : Int, edges: (Int, Int)[], colors: Int[]) : Bool {
for ((start, end) in edges) {
if (colors[start] == colors[end]) {
return false;
}
}
return true;
}
// Task 2.2. Oracle for verifying vertex coloring
operation VertexColoringOracle_Reference (V : Int, edges : (Int, Int)[], colorsRegister : Qubit[], target : Qubit) : Unit is Adj+Ctl {
let nEdges = Length(edges);
using (conflictQubits = Qubit[nEdges]) {
within {
for (((start, end), conflictQubit) in Zipped(edges, conflictQubits)) {
// Check that endpoints of the edge have different colors:
// apply ColorEqualityOracle_Nbit_Reference oracle; if the colors are the same the result will be 1, indicating a conflict
ColorEqualityOracle_Nbit_Reference(colorsRegister[start * 2 .. start * 2 + 1],
colorsRegister[end * 2 .. end * 2 + 1], conflictQubit);
}
} apply {
// If there are no conflicts (all qubits are in 0 state), the vertex coloring is valid
(ControlledOnInt(0, X))(conflictQubits, target);
}
}
}
// Task 2.3. Using Grover's search to find vertex coloring
operation GroversAlgorithm_Reference (V : Int, oracle : ((Qubit[], Qubit) => Unit is Adj)) : Int[] {
// This task is similar to task 2.2 from SolveSATWithGrover kata, but the percentage of correct solutions is potentially higher.
mutable coloring = new Int[V];
// Note that coloring register has the number of qubits that is twice the number of vertices (2 qubits per vertex).
using ((register, output) = (Qubit[2 * V], Qubit())) {
mutable correct = false;
mutable iter = 1;
repeat {
Message($"Trying search with {iter} iterations");
GroversAlgorithm_Loop(register, oracle, iter);
let res = MultiM(register);
// to check whether the result is correct, apply the oracle to the register plus ancilla after measurement
oracle(register, output);
if (MResetZ(output) == One) {
set correct = true;
// Read off coloring
set coloring = MeasureColoring_Reference(V, register);
}
ResetAll(register);
} until (correct or iter > 10) // the fail-safe to avoid going into an infinite loop
fixup {
set iter += 1;
}
if (not correct) {
fail "Failed to find a coloring";
}
}
return coloring;
}
// Grover loop implementation taken from SolveSATWithGrover kata.
operation OracleConverterImpl (markingOracle : ((Qubit[], Qubit) => Unit is Adj), register : Qubit[]) : Unit is Adj {
using (target = Qubit()) {
within {
// Put the target into the |-⟩ state
X(target);
H(target);
} apply {
// Apply the marking oracle; since the target is in the |-⟩ state,
// flipping the target if the register satisfies the oracle condition will apply a -1 factor to the state
markingOracle(register, target);
}
// We put the target back into |0⟩ so we can return it
}
}
operation GroversAlgorithm_Loop (register : Qubit[], oracle : ((Qubit[], Qubit) => Unit is Adj), iterations : Int) : Unit {
let phaseOracle = OracleConverterImpl(oracle, _);
ApplyToEach(H, register);
for (_ in 1 .. iterations) {
phaseOracle(register);
within {
ApplyToEachA(H, register);
ApplyToEachA(X, register);
} apply {
Controlled Z(Most(register), Tail(register));
}
}
}
}