-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.qs
270 lines (231 loc) · 8.83 KB
/
Graph.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
namespace QBitGraph {
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Arrays;
operation QuantumGraphIsomorphism(adjMatrix1 : Bool[][], adjMatrix2 : Bool[][]) : Bool {
// Check if both matrices have the same size
let size1 = Length(adjMatrix1);
let size2 = Length(adjMatrix2);
if (size1 != size2) {
fail "Matrices have different sizes.";
}
// Calculate the number of vertices and edges in the graph
let nVertices = size1;
let nEdges = CountEdges(adjMatrix1);
// Allocate qubits to store the adjacency matrices and the intermediate results
use qs = Qubit[2 * nVertices * nVertices + 2 * nVertices + 1];
// Prepare the initial state
PrepareInitialState(qs, adjMatrix1, adjMatrix2, nVertices);
// Apply the isomorphism checking algorithm
IsomorphismCheck(qs, nVertices, nEdges);
// Measure the final result
let result = M(qs[2 * nVertices * nVertices + 2 * nVertices]);
// Reset all qubits
ResetAll(qs);
return result == One;
}
operation CountEdges(adjMatrix : Bool[][]) : Int {
mutable nEdges = 0;
for i in 0 .. Length(adjMatrix) - 1 {
for j in i + 1 .. Length(adjMatrix) - 1 {
if (adjMatrix[i][j]) {
set nEdges += 1;
}
}
}
return nEdges;
}
operation PrepareInitialState(qs : Qubit[], adjMatrix1 : Bool[][], adjMatrix2 : Bool[][], nVertices : Int) : Unit {
// Encode the adjacency matrices as quantum states
let state1 = EncodeAdjacencyMatrix(adjMatrix1);
let state2 = EncodeAdjacencyMatrix(adjMatrix2);
// Apply the Hadamard gate to the first set of qubits
for i in 0 .. 2 * nVertices * nVertices - 1 {
if (i < 2 * nVertices * nVertices / 2) {
H(qs[i]);
}
}
// Apply the controlled-not gate to the second set of qubits
for i in 0 .. 2 * nVertices * nVertices - 1 {
if (i >= 2 * nVertices * nVertices / 2) {
let controlIndex = i - 2 * nVertices * nVertices / 2;
let targetIndex = controlIndex + 2 * nVertices * nVertices;
Controlled X([qs[controlIndex]], qs[targetIndex]);
}
}
// Initialize the final qubit in the |1> state
X(qs[2 * nVertices * nVertices + 2 * nVertices]);
}
operation EncodeAdjacencyMatrix(adjMatrix : Bool[][]) : Int[] {
let nVertices = Length(adjMatrix);
mutable state = Int[nVertices * nVertices];
for i in 0 .. nVertices - 1 {
for j in 0 .. nVertices - 1 {
if (adjMatrix[i][j]) {
set state[(i * nVertices) + j] = 1;
} else {
set state[(i * nVertices) + j] = 0;
}
}
}
return state;
}
operation IsomorphismCheck(qs : Qubit[], nVertices : Int, nEdges : Int) : Unit {
// Apply the Grover's algorithm
for i in 0 .. Floor(Sqrt(2.0 * PI() / 4.0 * Sqrt(2.0 * nVertices * nVertices))) - 1 {
GroverIteration(qs, nVertices, nEdges);
}
}
operation GroverIteration(qs : Qubit[], nVertices : Int, nEdges : Int) : Unit {
// Apply the oracle
Oracle(qs, nVertices, nEdges);
// Apply the diffusion operator
DiffusionOperator(qs, nVertices);
}
operation Oracle(qs : Qubit[], nVertices : Int, nEdges : Int) : Unit {
// Apply the oracle to the first set of qubits
for i in 0 .. 2 * nVertices * nVertices - 1 {
if (i < 2 * nVertices * nVertices / 2) {
Controlled X([qs[i]], qs[2 * nVertices * nVertices + 2 * nVertices]);
}
}
// Apply the oracle to the second set of qubits
for i in 0 .. 2 * nVertices * nVertices - 1 {
if (i >= 2 * nVertices * nVertices / 2) {
let controlIndex = i - 2 * nVertices * nVertices / 2;
let targetIndex = controlIndex + 2 * nVertices * nVertices;
Controlled X([qs[controlIndex]], qs[2 * nVertices * nVertices + 2 * nVertices]);
}
}
// Apply the oracle to the final qubit
for i in 0 .. 2 * nVertices * nVertices - 1 {
if (i < 2 * nVertices * nVertices / 2) {
Controlled X([qs[i]], qs[2 * nVertices * nVertices + 2 * nVertices]);
}
}
}
operation DiffusionOperator(qs : Qubit[], nVertices : Int) : Unit {
// Apply the Hadamard gate to the first set of qubits
for i in 0 .. 2 * nVertices * nVertices - 1 {
if (i < 2 * nVertices * nVertices / 2) {
H(qs[i]);
}
}
// Apply the controlled-not gate to the second set of qubits
for i in 0 .. 2 * nVertices * nVertices - 1 {
if (i >= 2 * nVertices * nVertices / 2) {
let controlIndex = i - 2 * nVertices * nVertices / 2;
let targetIndex = controlIndex + 2 * nVertices * nVertices;
Controlled X([qs[controlIndex]], qs[targetIndex]);
}
}
// Apply the Hadamard gate to the first set of qubits
for i in 0 .. 2 * nVertices * nVertices - 1 {
if (i < 2 * nVertices * nVertices / 2) {
H(qs[i]);
}
}
}
operation ResetAll(qs : Qubit[]) : Unit {
for i in 0 .. Length(qs) - 1 {
Reset(qs[i]);
}
}
operation Length(adjMatrix : Bool[][]) : Int {
return Length(adjMatrix[0]);
}
operation Size(adjMatrix1 : Bool[][], adjMatrix2 : Bool[][]) : Int {
return Length(adjMatrix1);
}
operation PrintAdjacencyMatrix(adjMatrix : Bool[][]) : Unit {
for i in 0 .. Length(adjMatrix) - 1 {
for j in 0 .. Length(adjMatrix) - 1 {
if (adjMatrix[i][j]) {
Message($"1 ");
} else {
Message($"0 ");
}
}
MessageLine("");
}
}
operation PrintAdjacencyMatrix(adjMatrix : Int[]) : Unit {
let nVertices = Int(Sqrt(Length(adjMatrix)));
for i in 0 .. nVertices - 1 {
for j in 0 .. nVertices - 1 {
Message($"{adjMatrix[(i * nVertices) + j]} ");
}
MessageLine("");
}
}
operation PrintAdjacencyMatrix(adjMatrix : Double[]) : Unit {
let nVertices = Int(Sqrt(Length(adjMatrix)));
for i in 0 .. nVertices - 1 {
for j in 0 .. nVertices - 1 {
Message($"{adjMatrix[(i * nVertices) + j]} ");
}
MessageLine("");
}
}
operation PrintAdjacencyMatrix(adjMatrix : Complex[]) : Unit {
let nVertices = Int(Sqrt(Length(adjMatrix)));
for i in 0 .. nVertices - 1 {
for j in 0 .. nVertices - 1 {
Message($"{adjMatrix[(i * nVertices) + j]} ");
}
MessageLine("");
}
}
operation PrintAdjacencyMatrix(adjMatrix : Qubit[]) : Unit {
let nVertices = Int(Sqrt(Length(adjMatrix)));
for i in 0 .. nVertices - 1 {
for j in 0 .. nVertices - 1 {
Message($"{adjMatrix[(i * nVertices) + j]} ");
}
MessageLine("");
}
}
operation PrintAdjacencyMatrix(adjMatrix : Result[]) : Unit {
let nVertices = Int(Sqrt(Length(adjMatrix)));
for i in 0 .. nVertices - 1 {
for j in 0 .. nVertices - 1 {
Message($"{adjMatrix[(i * nVertices) + j]} ");
}
MessageLine("");
}
}
operation PrintAdjacencyMatrix(adjMatrix : String[]) : Unit {
let nVertices = Int(Sqrt(Length(adjMatrix)));
for i in 0 .. nVertices - 1 {
for j in 0 .. nVertices - 1 {
Message($"{adjMatrix[(i * nVertices) + j]} ");
}
MessageLine("");
}
}
operation PrintAdjacencyMatrix(adjMatrix : Int[][]) : Unit {
for i in 0 .. Length(adjMatrix) - 1 {
for j in 0 .. Length(adjMatrix) - 1 {
Message($"{adjMatrix[i][j]} ");
}
MessageLine("");
}
}
operation PrintAdjacencyMatrix(adjMatrix : Double[][]) : Unit {
for i in 0 .. Length(adjMatrix) - 1 {
for j in 0 .. Length(adjMatrix) - 1 {
Message($"{adjMatrix[i][j]} ");
}
MessageLine("");
}
}
operation PrintAdjacencyMatrix(adjMatrix : Complex[][]) : Unit {
for i in 0 .. Length(adjMatrix) - 1 {
for j in 0 .. Length(adjMatrix) - 1 {
Message($"{adjMatrix[i][j]} ");
}
MessageLine("");
}
}
}