forked from microsoft/QuantumKatas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReferenceImplementation.qs
357 lines (278 loc) · 13.2 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// 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.RippleCarryAdder {
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
//////////////////////////////////////////////////////////////////
// Part I. Simple adder outputting to empty Qubits
//////////////////////////////////////////////////////////////////
// Task 1.1. Summation of two bits
operation LowestBitSum_Reference (a : Qubit, b : Qubit, sum : Qubit) : Unit is Adj {
CNOT(a, sum);
CNOT(b, sum);
}
// Task 1.2. Carry of two bits
operation LowestBitCarry_Reference (a : Qubit, b : Qubit, carry : Qubit) : Unit is Adj {
CCNOT(a, b, carry);
}
// Task 1.3. One-bit adder
operation OneBitAdder_Reference (a : Qubit, b : Qubit, sum : Qubit, carry : Qubit) : Unit is Adj {
LowestBitSum_Reference(a, b, sum);
LowestBitCarry_Reference(a, b, carry);
}
// Task 1.4. Summation of 3 bits
operation HighBitSum_Reference (a : Qubit, b : Qubit, carryin : Qubit, sum : Qubit) : Unit is Adj {
CNOT(a, sum);
CNOT(b, sum);
CNOT(carryin, sum);
}
// Task 1.5. Carry of 3 bits
operation HighBitCarry_Reference (a : Qubit, b : Qubit, carryin : Qubit, carryout : Qubit) : Unit is Adj {
CCNOT(a, b, carryout);
CCNOT(a, carryin, carryout);
CCNOT(b, carryin, carryout);
}
// Task 1.6. Two-bit adder
operation TwoBitAdder_Reference (a : Qubit[], b : Qubit[], sum : Qubit[], carry : Qubit) : Unit is Adj {
use internalCarry = Qubit();
LowestBitSum_Reference(a[0], b[0], sum[0]);
LowestBitCarry_Reference(a[0], b[0], internalCarry);
HighBitSum_Reference(a[1], b[1], internalCarry, sum[1]);
HighBitCarry_Reference(a[1], b[1], internalCarry, carry);
// Clean up ancillary qubit
Adjoint LowestBitCarry_Reference(a[0], b[0], internalCarry);
}
// Task 1.7. N-bit adder
operation ArbitraryAdder_Reference (a : Qubit[], b : Qubit[], sum : Qubit[], carry : Qubit) : Unit is Adj {
let N = Length(a);
if N == 1 {
OneBitAdder_Reference(a[0], b[0], sum[0], carry);
}
else {
use internalCarries = Qubit[N-1];
within {
LowestBitCarry_Reference(a[0], b[0], internalCarries[0]);
for i in 1..N-2 {
HighBitCarry_Reference(a[i], b[i], internalCarries[i-1], internalCarries[i]);
}
}
apply {
LowestBitSum_Reference(a[0], b[0], sum[0]);
for i in 1..N-1 {
HighBitSum_Reference(a[i], b[i], internalCarries[i-1], sum[i]);
}
HighBitCarry_Reference(a[N-1], b[N-1], internalCarries[N-2], carry);
}
}
}
// A slightly simpler solution - more uniform, but slightly slower, and requires one extra qubit
operation ArbitraryAdder_Simplified_Reference (a : Qubit[], b : Qubit[], sum : Qubit[], carry : Qubit) : Unit is Adj {
let N = Length(a);
use internalCarries = Qubit[N];
let carries = internalCarries + [carry];
for i in 0 .. N-1 {
HighBitSum_Reference(a[i], b[i], carries[i], sum[i]);
HighBitCarry_Reference(a[i], b[i], carries[i], carries[i+1]);
}
// Clean up the ancilla
for i in N-2 .. -1 .. 0 {
Adjoint HighBitCarry_Reference(a[i], b[i], carries[i], carries[i+1]);
}
}
// The challenge solution - the sum qubits are used to store the carry bits, and the sum is calculated as they get cleaned up
operation ArbitraryAdder_Challenge_Reference (a : Qubit[], b : Qubit[], sum : Qubit[], carry : Qubit) : Unit is Adj {
let N = Length(a);
// Calculate carry bits
LowestBitCarry_Reference(a[0], b[0], sum[0]);
for i in 1 .. N-1 {
HighBitCarry_Reference(a[i], b[i], sum[i - 1], sum[i]);
}
CNOT(sum[N-1], carry);
// Clean sum qubits and compute sum
for i in N-1 .. -1 .. 1 {
Adjoint HighBitCarry_Reference(a[i], b[i], sum[i - 1], sum[i]);
HighBitSum_Reference(a[i], b[i], sum[i - 1], sum[i]);
}
Adjoint LowestBitCarry_Reference(a[0], b[0], sum[0]);
LowestBitSum_Reference(a[0], b[0], sum[0]);
}
//////////////////////////////////////////////////////////////////
// Part II. Simple in-place adder
//////////////////////////////////////////////////////////////////
// Task 2.1. In-place summation of two bits
operation LowestBitSumInPlace_Reference (a : Qubit, b : Qubit) : Unit is Adj {
CNOT(a, b);
}
// Task 2.2. In-place one-bit adder
operation OneBitAdderInPlace_Reference (a : Qubit, b : Qubit, carry : Qubit) : Unit is Adj {
LowestBitCarry_Reference(a, b, carry);
LowestBitSumInPlace_Reference(a, b);
}
// Task 2.3. In-place summation of three bits
operation HighBitSumInPlace_Reference (a : Qubit, b : Qubit, carryin : Qubit) : Unit is Adj {
CNOT(a, b);
CNOT(carryin, b);
}
// Task 2.4. In-place two-bit adder
operation TwoBitAdderInPlace_Reference (a : Qubit[], b : Qubit[], carry : Qubit) : Unit is Adj {
use internalCarry = Qubit();
// Set up the carry bits
LowestBitCarry_Reference(a[0], b[0], internalCarry);
HighBitCarry_Reference(a[1], b[1], internalCarry, carry);
// Calculate sums and clean up the ancilla
HighBitSumInPlace_Reference(a[1], b[1], internalCarry);
Adjoint LowestBitCarry_Reference(a[0], b[0], internalCarry);
LowestBitSumInPlace_Reference(a[0], b[0]);
}
// Task 2.5. In-place N-bit adder
operation ArbitraryAdderInPlace_Reference (a : Qubit[], b : Qubit[], carry : Qubit) : Unit is Adj {
let N = Length(a);
use internalCarries = Qubit[N];
// Set up the carry bits
LowestBitCarry_Reference(a[0], b[0], internalCarries[0]);
for i in 1 .. N-1 {
HighBitCarry_Reference(a[i], b[i], internalCarries[i - 1], internalCarries[i]);
}
CNOT(internalCarries[N-1], carry);
// Clean up carry bits and compute sum
for i in N-1 .. -1 .. 1 {
Adjoint HighBitCarry_Reference(a[i], b[i], internalCarries[i - 1], internalCarries[i]);
HighBitSumInPlace_Reference(a[i], b[i], internalCarries[i - 1]);
}
Adjoint LowestBitCarry_Reference(a[0], b[0], internalCarries[0]);
LowestBitSumInPlace_Reference(a[0], b[0]);
}
//////////////////////////////////////////////////////////////////
// Part III*. Improved in-place adder
//////////////////////////////////////////////////////////////////
// Task 3.1. Majority gate
operation Majority_Reference (a : Qubit, b : Qubit, c : Qubit) : Unit is Adj {
CNOT(a, b);
CNOT(a, c);
CCNOT(b, c, a);
}
// Task 3.2. UnMajority and Add gate
operation UnMajorityAdd_Reference (a : Qubit, b : Qubit, c : Qubit) : Unit is Adj {
CCNOT(b, c, a);
CNOT(a, c);
CNOT(c, b);
}
// Task 3.3. One-bit majority-UMA adder
operation OneBitMajUmaAdder_Reference (a : Qubit, b : Qubit, carry : Qubit) : Unit is Adj {
use tempCarry = Qubit();
Majority_Reference(a, b, tempCarry);
CNOT(a, carry);
UnMajorityAdd_Reference(a, b, tempCarry);
}
// Task 3.4. Two-bit majority-UMA adder
operation TwoBitMajUmaAdder_Reference (a : Qubit[], b : Qubit[], carry : Qubit) : Unit is Adj {
use tempCarry = Qubit();
// We only need the extra qubit so we have 3 to pass to the majority gate for the lowest bits
Majority_Reference(a[0], b[0], tempCarry);
Majority_Reference(a[1], b[1], a[0]);
// Save last carry bit
CNOT(a[1], carry);
// Restore inputs/ancilla and compute sum
UnMajorityAdd_Reference(a[1], b[1], a[0]);
UnMajorityAdd_Reference(a[0], b[0], tempCarry);
}
// Task 3.5. N-bit majority-UMA adder
operation ArbitraryMajUmaAdder_Reference (a : Qubit[], b : Qubit[], carry : Qubit) : Unit is Adj {
let N = Length(a);
use tempCarry = Qubit();
let carries = [tempCarry] + a;
// Compute carry bits
for i in 0 .. N-1 {
Majority_Reference(a[i], b[i], carries[i]);
}
// Save last carry bit
CNOT(carries[N], carry);
// Restore inputs and ancilla, compute sum
for i in N-1 .. -1 .. 0 {
UnMajorityAdd_Reference(a[i], b[i], carries[i]);
}
}
//////////////////////////////////////////////////////////////////
// Part IV*. In-place subtractor
//////////////////////////////////////////////////////////////////
// Task 4.1. N-bit subtractor
operation Subtractor_Reference (a : Qubit[], b : Qubit[], borrowBit : Qubit) : Unit is Adj {
// transform b into 2ᴺ - 1 - b
ApplyToEachA(X, b);
// compute (2ᴺ - 1 - b) + a = 2ᴺ - 1 - (b - a) using existing adder
// if this produced a carry, then (2ᴺ - 1 - (b - a)) > 2ᴺ - 1, so (b - a) < 0, and we need a borrow
// this means we can use the carry qubit from the addition as the borrow qubit
ArbitraryMajUmaAdder_Reference(a, b, borrowBit);
// transform 2ᴺ - 1 - (b - a) into b - a
ApplyToEachA(X, b);
}
//////////////////////////////////////////////////////////////////
// Part V. Addition and subtraction modulo 2ᴺ
//////////////////////////////////////////////////////////////////
// Task 5.1. Adder Modulo 2ᴺ
operation AdderModuloN_Reference (a : Qubit[], b : Qubit[], sum : Qubit[]) : Unit is Adj {
// Modification of challenge solution of task 1.7 (last carry bit isn't calculated).
// Sum qubits are used to store the carry bits, and the sum is calculated as they get cleaned up.
// (It's also possible to use a similar modification of regular solution of task 1.7.)
let N = Length(a);
// Calculate carry bits and Store them in Sum
LowestBitCarry_Reference(a[0], b[0], sum[0]);
for i in 1 .. N-1 {
HighBitCarry_Reference(a[i], b[i], sum[i - 1], sum[i]);
}
// No need to calculated last (N+1)th carry bit, as addition is modulo 2ᴺ.
// Clean sum qubits (uncompute carries after they aren't needed) and compute sum
for i in N-1 .. -1 .. 1 {
Adjoint HighBitCarry_Reference(a[i], b[i], sum[i - 1], sum[i]); // Restore sum[i] to state 0
HighBitSum_Reference(a[i], b[i], sum[i - 1], sum[i]); // Compute sum[i]
}
Adjoint LowestBitCarry_Reference(a[0], b[0], sum[0]); // Restore sum[0] to state 0
LowestBitSum_Reference(a[0], b[0], sum[0]); // Compute sum[i]
}
// Task 5.2. Two's Complement
operation TwosComplement_Reference (a : Qubit[]) : Unit is Adj {
// Transform a into 2ᴺ - 1 - a ("one's complement")
ApplyToEachA(X, a);
// Increment mod 2ᴺ.
// Since we are incrementing by 1, we flip the next most significant bit only if all previous bits are 0.
for prefix in Prefixes(a) {
(ControlledOnInt(0, X))(Most(prefix), Tail(prefix));
}
}
// Task 5.3. Subtractor Modulo 2ᴺ
operation SubtractorModuloN_Reference (a : Qubit[], b : Qubit[], diff : Qubit[]) : Unit is Adj {
within {
// Transform a into its Two's Complement 2ᴺ - a
TwosComplement_Reference(a);
} apply {
// Add 2ᴺ - a and b to get (2ᴺ + b - a) mod 2ᴺ = (b-a) mod 2ᴺ
AdderModuloN_Reference(a, b, diff);
}
}
// Task 5.4. In-place adder modulo 2ᴺ
operation InPlaceAdderModuloN_Reference (a : Qubit[], b : Qubit[]) : Unit is Adj {
// Modification of task 3.5 solution (last carry bit isn't calculated)
use tempCarry = Qubit();
let carries = [tempCarry] + a;
// Compute carry bits
ApplyToEachA(Majority_Reference, Zipped3(a, b, carries));
// No need to save last (N+1)th carry bit
// Restore inputs and ancilla, compute sum
ApplyToEachA(UnMajorityAdd_Reference, Reversed(Zipped3(a, b, carries)));
}
// Task 5.5. In-place subtractor modulo 2ᴺ
operation InPlaceSubtractorModuloN_Reference (a : Qubit[], b : Qubit[]) : Unit is Adj {
// Notice that Task 5.4 is actually the Adjoint of Task 5.3.
// Task 5.3 maps (a,b) -> (a,(a+b)mod2ᴺ). Let c = (a+b)mod2ᴺ
// Task 5.4 maps (a,b) -> (a,(b-a)mod2ᴺ). So Task 5.4 will maps (a,c) -> (a,(c-a)mod2ᴺ) = (a,((a+b)mod2ᴺ-a)mod2ᴺ) = (a,b).
Adjoint InPlaceAdderModuloN_Reference(a, b);
}
}