-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathbasic_arithmetic.py
286 lines (253 loc) · 14.8 KB
/
basic_arithmetic.py
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
# pylint: disable=wrong-or-nonexistent-copyright-notice
# -*- coding: utf-8 -*-
"""Creates and simulates basic arithmetic circuits
=== EXAMPLE OUTPUT ===
Execute Adder
0: ───────────@───────────────────────────────────@───────────────@───
│ │ │
1: ───@───@───┼───────────────────────────────────┼───@───@───@───┼───
│ │ │ │ │ │ │ │
2: ───@───X───@───────────────────────────────────@───X───@───X───X───
│ │ │ │
3: ───X───────X───────@───────@───────────────@───X───────X───────────
│ │ │
4: ───────────@───@───┼───────┼───@───@───@───┼───────────────────────
│ │ │ │ │ │ │ │
5: ───────────@───X───@───────@───X───@───X───X───────────────────────
│ │ │ │
6: ───────────X───────X───@───X───────X───────────────────────────────
│
7: ───────────────────@───┼───────────────────────────────────────────
│ │
8: ───────────────────X───X───────────────────────────────────────────
000 + 000 = 000
000 + 001 = 001
000 + 010 = 010
000 + 011 = 011
001 + 000 = 001
001 + 001 = 010
001 + 010 = 011
001 + 011 = 100
010 + 000 = 010
010 + 001 = 011
010 + 010 = 100
010 + 011 = 101
011 + 000 = 011
011 + 001 = 100
011 + 010 = 101
011 + 011 = 110
Execute Multiplier
0: ─────────────Adder:0───────────────────────Adder:0───────────────Adder:0─────
│ │ │
1: ─X───────────Adder:1───X───────────────────Adder:1───────────────Adder:1─────
│ │ │ │ │
2: ─┼───────────Adder:2───┼───────────────────Adder:2───────────────Adder:2─────
│ │ │ │ │
3: ─┼───────────Adder:3───┼───────────────────Adder:3───────────────Adder:3─────
│ │ │ │ │
4: ─┼───X───────Adder:4───┼───X───────X───────Adder:4───X───────────Adder:4─────
│ │ │ │ │ │ │ │ │
5: ─┼───┼───────Adder:5───┼───┼───────┼───────Adder:5───┼───────────Adder:5─────
│ │ │ │ │ │ │ │ │
6: ─┼───┼───────Adder:6───┼───┼───────┼───────Adder:6───┼───────────Adder:6─────
│ │ │ │ │ │ │ │ │
7: ─┼───┼───X───Adder:7───┼───┼───X───┼───X───Adder:7───┼───X───X───Adder:7───X─
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │
8: ─┼───┼───┼───Adder:8───┼───┼───┼───┼───┼───Adder:8───┼───┼───┼───Adder:8───┼─
│ │ │ │ │ │ │ │ │ │ │ │
9: ─@───┼───┼─────────────@───┼───┼───@───┼─────────────@───┼───@─────────────@─
│ │ │ │ │ │ │ │ │ │ │ │
10:─┼───@───┼─────────────┼───@───┼───┼───@─────────────┼───@───┼─────────────┼─
│ │ │ │ │ │ │ │ │ │ │ │
11:─┼───┼───@─────────────┼───┼───@───┼───┼─────────────┼───┼───┼─────────────┼─
│ │ │ │ │ │ │ │ │ │ │ │
12:─@───@───@─────────────@───@───@───┼───┼─────────────┼───┼───┼─────────────┼─
│ │ │ │ │ │
13:───────────────────────────────────@───@─────────────@───@───┼─────────────┼─
│ │
14:─────────────────────────────────────────────────────────────@─────────────@─
000 * 000 = 000
000 * 001 = 000
000 * 010 = 000
000 * 011 = 000
001 * 000 = 000
001 * 001 = 001
001 * 010 = 010
001 * 011 = 011
010 * 000 = 000
010 * 001 = 010
010 * 010 = 100
010 * 011 = 110
011 * 000 = 000
011 * 001 = 011
011 * 010 = 110
011 * 011 = 001
"""
import cirq
class Adder(cirq.Gate):
"""A quantum circuit to calculate a + b
-----------@--- ---@------------
| |
---@---@---+--- ---+---@---@--- -------@---
[Carry]: | | | [Uncarry]: | | | |
---@---X---@--- ---@---X---@--- [Sum]:---@---+---
| | | | | |
---X-------X--- ---X-------X--- ---X---X---
----- ------- ---
c0: --| |------------------------------------| |--| |-----
| | | | | |
a0: --| |------------------------------------| |--|Sum|-----
|Carry| |Uncarry| | |
b0: --| |------------------------------------| |--| |--M--
| | ----- ------- --- | | ---
c1: --| |--| |---------| |--| |--| |------------
----- | | | | | | -------
a1: -----------| |---------| |--|Sum|-----------------------
|Carry| |Uncarry| | |
b1: -----------| |---------| |--| |--------------------M--
| | --- | | ---
c2: -----------| |--| |--| |------------------------------
----- | | -------
a2: --------------------|Sum|-----------------------------------------
| |
b2: --------------------| |--------------------------------------M--
---
"""
def __init__(self, num_qubits):
super(Adder, self)
self._num_qubits = num_qubits
def num_qubits(self):
return self._num_qubits
def carry(self, *qubits):
c0, a, b, c1 = qubits
yield cirq.TOFFOLI(a, b, c1)
yield cirq.CNOT(a, b)
yield cirq.TOFFOLI(c0, b, c1)
def uncarry(self, *qubits):
c0, a, b, c1 = qubits
yield cirq.TOFFOLI(c0, b, c1)
yield cirq.CNOT(a, b)
yield cirq.TOFFOLI(a, b, c1)
def carry_sum(self, *qubits):
c0, a, b = qubits
yield cirq.CNOT(a, b)
yield cirq.CNOT(c0, b)
def _decompose_(self, qubits):
n = int(len(qubits) / 3)
c = qubits[0::3]
a = qubits[1::3]
b = qubits[2::3]
for i in range(n - 1):
yield self.carry(c[i], a[i], b[i], c[i + 1])
yield self.carry_sum(c[n - 1], a[n - 1], b[n - 1])
for i in range(n - 2, -1, -1):
yield self.uncarry(c[i], a[i], b[i], c[i + 1])
yield self.carry_sum(c[i], a[i], b[i])
class Multiplier(cirq.Gate):
"""A quantum circuit to calculate y * x
- - -
c0: --------------| |-----------------------| |---------------| |---------
| | | | | |
a0: --X-----------| |---X-------------------| |---------------| |---------
| | | | | | | |
b0: --+-----------| |---+-------------------| |---------------| |------M--
| | | | | | | |
c1: --+-----------|A|---+-------------------|A|---------------|A|---------
| |d| | |d| |d|
a1: --+---X-------|d|---+---X-------X-------|d|---X-----------|d|---------
| | |e| | | | |e| | |e|
b1: --+---+-------|r|---+---+-------+-------|r|---+-----------|r|------M--
| | | | | | | | | | | |
c2: --+---+-------| |---+---+-------+-------| |---+-----------| |---------
| | | | | | | | | | | |
a2: --+---+---X---| |---+---+---X---+---X---| |---+---X---X---| |---X-----
| | | | | | | | | | | | | | | | | |
b3: --+---+---+---| |---+---+---+---+---+---| |---+---+---+---| |---+--M--
| | | - | | | | | - | | | - |
y0: --@---+---+---------@---+---+---@---+---------@---+---@---------@-----
| | | | | | | | | | | |
y1: --+---@---+---------+---@---+---+---@---------+---@---+---------+-----
| | | | | | | | | | | |
y2: --+---+---@---------+---+---@---+---+---------+---+---+---------+-----
| | | | | | | | | | | |
x0: --@---@---@---------@---@---@---+---+---------+---+---+---------+-----
| | | | | |
x1: --------------------------------@---@---------@---@---+---------+-----
| |
x2: ------------------------------------------------------@---------@-----
"""
def __init__(self, num_qubits):
super(Multiplier, self)
self._num_qubits = num_qubits
def num_qubits(self):
return self._num_qubits
def _decompose_(self, qubits):
n = int(len(qubits) / 5)
# c = qubits[0:n*3:3]
a = qubits[1 : n * 3 : 3]
# b = qubits[2::3]
y = qubits[n * 3 : n * 4]
x = qubits[n * 4 :]
for i, x_i in enumerate(x):
# a = (y*(2**i))*x_i
for a_qubit, y_qubit in zip(a[i:], y[: n - i]):
yield cirq.TOFFOLI(x_i, y_qubit, a_qubit)
# b += a
yield Adder(3 * n).on(*qubits[: 3 * n])
# a = 0
for a_qubit, y_qubit in zip(a[i:], y[: n - i]):
yield cirq.TOFFOLI(x_i, y_qubit, a_qubit)
def init_qubits(x_bin, *qubits):
for x, qubit in zip(x_bin, list(qubits)[::-1]):
if x == '1':
yield cirq.X(qubit)
def experiment_adder(p, q, n=3):
a_bin = f'{p:08b}'[-n:]
b_bin = f'{q:08b}'[-n:]
qubits = cirq.LineQubit.range(3 * n)
# c = qubits[0::3]
a = qubits[1::3]
b = qubits[2::3]
circuit = cirq.Circuit(
init_qubits(a_bin, *a),
init_qubits(b_bin, *b),
Adder(n * 3).on(*qubits),
cirq.measure(*b, key='result'),
)
simulator = cirq.Simulator()
result = simulator.run(circuit, repetitions=1).measurements['result']
sum_bin = ''.join(result[0][::-1].astype(int).astype(str))
print(f'{a_bin} + {b_bin} = {sum_bin}')
def experiment_multiplier(p, q, n=3):
y_bin = f'{p:08b}'[-n:]
x_bin = f'{q:08b}'[-n:]
qubits = cirq.LineQubit.range(5 * n)
# c = qubits[0:n*3:3]
# a = qubits[1:n*3:3]
b = qubits[2 : n * 3 : 3]
y = qubits[n * 3 : n * 4]
x = qubits[n * 4 :]
circuit = cirq.Circuit(
init_qubits(x_bin, *x),
init_qubits(y_bin, *y),
Multiplier(5 * n).on(*qubits),
cirq.measure(*b, key='result'),
)
simulator = cirq.Simulator()
result = simulator.run(circuit, repetitions=1)
sum_bin = ''.join(result.measurements['result'][0][::-1].astype(int).astype(str))
print(f'{y_bin} * {x_bin} = {sum_bin}')
def main(n=3):
print('Execute Adder')
print(cirq.Circuit(cirq.decompose(Adder(3 * n).on(*cirq.LineQubit.range(3 * n)))))
for p in range(2 * 2):
for q in range(2 * 2):
experiment_adder(p, q, n)
print('')
print('Execute Multiplier')
print(cirq.Circuit(cirq.decompose(Multiplier(5 * n).on(*cirq.LineQubit.range(5 * n)))))
for p in range(2 * 2):
for q in range(2 * 2):
experiment_multiplier(p, q, n)
if __name__ == '__main__':
main()