-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqquine_121.py
745 lines (595 loc) · 23.1 KB
/
qquine_121.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
import numpy as np
import random
from qiskit import QuantumCircuit, Aer, execute
from math import log2, ceil, pi, sin
from numpy import savetxt, save, savez_compressed
import sys
#=====================================================================================================================
simulator = Aer.get_backend('statevector_simulator')
def disp_isv(circ, msg="", all=True, precision=1e-8):
sim_res = execute(circ, simulator).result()
statevector = sim_res.get_statevector(circ)
qb = int(log2(len(statevector)))
print("\n============ State Vector ============", msg)
s = 0
for i in statevector:
if (all == True): print(' ({:+.5f}) |{:0{}b}>'.format(i,s,qb))
else:
if (abs(i) > precision): print(' ({:+.5f}) |{:0{}b}>'.format(i,s,qb))
s = s+1
print("============..............============")
return
# 24 qubits with Hadamard on 12 qubits log size: 880 MB csv, 816 MB txt, 256 MB npy, 255 KB npz
def save_isv(statevector, mode=1):
if (mode == 1): savez_compressed('output.npz', statevector)
elif (mode == 2): save('output.npy', statevector)
elif (mode == 3):
qb = int(log2(len(statevector)))
f = open("output.txt", "w")
f.write("============ State Vector ============\n")
s = 0
for i in statevector:
f.write(' ({:+.5f}) |{:0{}b}>'.format(i,s,qb)+'\n')
s = s+1
f.write("============..............============")
f.close()
elif (mode == 4): savetxt('output.csv', statevector, delimiter=',')
else: print('Invalid mode selected')
return
#=====================================================================================================================
def nCX(k,c,t,b):
nc = len(c)
if nc == 1:
k.cx(c[0], t[0])
elif nc == 2:
k.toffoli(c[0], c[1], t[0])
else:
nch = ceil(nc/2)
c1 = c[:nch]
c2 = c[nch:]
c2.append(b[0])
nCX(k,c1,b,[nch+1])
nCX(k,c2,t,[nch-1])
nCX(k,c1,b,[nch+1])
nCX(k,c2,t,[nch-1])
return
#=====================================================================================================================
def U_init(qcirc, circ_width, fsm):
for i in fsm:
qcirc.h(i)
qcirc.barrier()
return
def U_read(qcirc, read, head, tape, ancilla):
# Reset read (prepz measures superposed states... need to uncompute)
for cell in range(0, len(tape)):
enc = format(cell, '#0'+str(len(head)+2)+'b') # 2 for '0b' prefix
for i in range(2, len(enc)):
if(enc[i] == '0'):
qcirc.x(head[(len(head)-1)-(i-2)])
qcirc.barrier(read, head)
nCX(qcirc, head+[tape[cell]], read, [ancilla[0]])
qcirc.barrier(read, head)
for i in range(2, len(enc)):
if(enc[i] == '0'):
qcirc.x(head[(len(head)-1)-(i-2)])
qcirc.barrier(read, head, tape, ancilla)
qcirc.barrier()
return
def U_fsm(qcirc, tick, fsm, state, read, write, move, ancilla):
# Description Number Encoding: {M/W}{R}
# [ M1 W1 M0 W0 ] LSQ = W0 = fsm[0]
qcirc.x(read[0]) # If read == 0
nCX(qcirc, [fsm[0],read[0]], write, [ancilla[0]]) # Update write
nCX(qcirc, [fsm[1],read[0]], move, [ancilla[0]]) # Update move
qcirc.x(read[0]) # If read == 1
nCX(qcirc, [fsm[2],read[0]], write, [ancilla[0]]) # Update write
nCX(qcirc, [fsm[3],read[0]], move, [ancilla[0]]) # Update move
qcirc.barrier()
return
def U_write(qcirc, write, head, tape, ancilla):
# Reset write (prepz measures superposed states... need to uncompute)
for cell in range(0, len(tape)):
enc = format(cell, '#0'+str(len(head)+2)+'b') # 2 for '0b' prefix
for i in range(2, len(enc)):
if(enc[i] == '0'):
qcirc.x(head[(len(head)-1)-(i-2)])
qcirc.barrier(write, head)
nCX(qcirc, head+write, [tape[cell]], [ancilla[0]])
qcirc.barrier(write, head)
for i in range(2, len(enc)):
if(enc[i] == '0'):
qcirc.x(head[(len(head)-1)-(i-2)])
qcirc.barrier(write, head, tape, ancilla)
qcirc.barrier()
return
def U_move(qcirc, move, head, ancilla):
# Increment/Decrement using Adder
reg_a = move
reg_a.extend([-1]*(len(head)-len(move)))
reg_b = head
reg_c = [-1] # No initial carry
reg_c.extend(ancilla)
reg_c.append(-1) # Ignore Head position under/overflow. Trim bits. Last carry not accounted, All-ones overflows to All-zeros
def q_carry(qcirc, q0, q1, q2, q3):
if (q1 != -1 and q2 != -1 and q3 != -1): qcirc.toffoli(q1, q2, q3)
if (q1 != -1 and q2 != -1): qcirc.cx(q1, q2)
if (q0 != -1 and q2 != -1 and q3 != -1): qcirc.toffoli(q0, q2, q3)
def q_mid(qcirc, q0, q1):
if (q0 != -1 and q1 != -1): qcirc.cx(q0, q1)
def q_sum(qcirc, q0, q1, q2):
if (q0 != -1 and q2 != -1): qcirc.cx(q0, q2)
if (q1 != -1 and q2 != -1): qcirc.cx(q1, q2)
def q_rcarry(qcirc, q0, q1, q2, q3):
if (q0 != -1 and q2 != -1 and q3 != -1): qcirc.toffoli(q0, q2, q3)
if (q1 != -1 and q2 != -1): qcirc.cx(q1, q2)
if (q1 != -1 and q2 != -1 and q3 != -1): qcirc.toffoli(q1, q2, q3)
# Quantum Adder
for i in range(0,len(head)):
q_carry(qcirc,reg_c[i],reg_a[i],reg_b[i],reg_c[i+1])
q_mid(qcirc,reg_a[i],reg_b[i])
q_sum(qcirc,reg_c[i],reg_a[i],reg_b[i])
for i in range(len(head)-2,-1,-1):
q_rcarry(qcirc,reg_c[i],reg_a[i],reg_b[i],reg_c[i+1])
q_sum(qcirc,reg_c[i],reg_a[i],reg_b[i])
qcirc.x(reg_a[0])
# Quantum Subtractor
for i in range(0,len(head)-1):
q_sum(qcirc,reg_c[i],reg_a[i],reg_b[i])
q_carry(qcirc,reg_c[i],reg_a[i],reg_b[i],reg_c[i+1])
q_sum(qcirc,reg_c[i+1],reg_a[i+1],reg_b[i+1])
q_mid(qcirc,reg_a[i+1],reg_b[i+1])
for i in range(len(head)-2,-1,-1):
q_rcarry(qcirc,reg_c[i],reg_a[i],reg_b[i],reg_c[i+1])
qcirc.x(reg_a[0])
qcirc.barrier()
return
def U_rst(qcirc, tick, fsm, state, read, write, move, ancilla):
# Reset write and move
qcirc.x(read[0])
nCX(qcirc, [fsm[0],read[0]], write, [ancilla[0]])
nCX(qcirc, [fsm[1],read[0]], move, [ancilla[0]])
qcirc.x(read[0])
nCX(qcirc, [fsm[2],read[0]], write, [ancilla[0]])
nCX(qcirc, [fsm[3],read[0]], move, [ancilla[0]])
qcirc.barrier()
return
#=====================================================================================================================
def Test_cfg_121(block): # convert config from 221 to 121
global fsm, state, move, head, read, write, tape, ancilla, test
if (block == 'none'):
return
elif (block == 'read'):
fsm = []
state = []
move = []
head = [0,1,2,3]
read = [4]
write = []
tape = [5,6,7,8,9,10,11,12,13,14,15,16]
ancilla = [17]
test = [18]
elif (block == 'fsm'):
fsm = [0,1,2,3,4,5,6,7,8,9,10,11]
state = [12,13]
move = [14]
head = []
read = [15]
write = [16]
tape = []
ancilla = [17]
test = [18,19,20]
elif (block == 'move'):
fsm = []
state = []
move = [0]
head = [1,2,3,4]
read = []
write = []
tape = []
ancilla = [5,6,7]
test = [8,9,10,11]
elif (block == 'write'):
fsm = []
state = []
move = []
head = [0,1,2,3]
read = []
write = [4]
tape = [5,6,7,8,9,10,11,12,13,14,15,16]
ancilla = [17]
test = []#[18,19,20,21,22,23,24,25,26,27,28,29]
elif (block == 'rst'):
fsm = [0,1,2,3,4,5,6,7,8,9,10,11]
state = [12,13]
move = [14]
head = []
read = [15]
write = [16]
tape = []
ancilla = [17]
test = [18,19,20,21]
elif (block == 'count'):
fsm = [0,1,2,3]
state = []
move = []
head = []
read = []
write = []
tape = []
ancilla = []
test = []
count = [4,5,6,7]
search = [8,9,10,11]
print("\n\nTEST CONFIGURATION\n\tFSM\t:",fsm,"\n\tSTATE\t:",state,"\n\tMOVE\t:",move,"\n\tHEAD\t:",head,"\n\tREAD\t:",read,"\n\tWRITE\t:",write,"\n\tTAPE\t:",tape,"\n\tANCILLA :",ancilla,"\n\tTEST\t:",test,"\n\tCOUNT\t:",count,"\n\tSEARCH\t:",search)
def Test_count(qcirc, fsm):
# Test using some superposition of fsm and then Hamming distance
qcirc.barrier()
qcirc.barrier()
return
#=====================================================================================================================
asz = 2 # Alphabet size: Binary (0 is blank/default)
ssz = 1 # State size (Initial state is all 0)
tdim = 1 # Tape dimension
csz = ceil(log2(asz)) # Character symbol size
senc = ceil(log2(ssz)) # State encoding size
transitions = ssz * asz # Number of transition arrows in FSM
dsz = transitions * (tdim + csz + senc) # Description size
machines = 2 ** dsz
print("\nNumber of "+str(asz)+"-symbol "+str(ssz)+"-state "+str(tdim)+"-dimension Quantum Parallel Universal Linear Bounded Automata: "+str(machines))
tsz = dsz # Turing Tape size (same as dsz to estimating self-replication and algorithmic probability)
hsz = ceil(log2(tsz)) # Head size
sim_tick = tsz # Number of ticks of the FSM before abort
#sim_tick = 1 # Just 1 QPULBA cycle for proof-of-concept
tlog = (sim_tick+1) * senc # Transition log # required?
nanc = 3
quinebit = 1
qnos = [dsz, tlog, tdim, hsz, csz, csz, tsz, nanc, quinebit]
fsm = list(range(sum(qnos[0:0]),sum(qnos[0:1])))
state = list(range(sum(qnos[0:1]),sum(qnos[0:2]))) # States (Binary coded)
move = list(range(sum(qnos[0:2]),sum(qnos[0:3])))
head = list(range(sum(qnos[0:3]),sum(qnos[0:4]))) # Binary coded, 0-MSB 2-LSB, [001] refers to Tape pos 1, not 4
read = list(range(sum(qnos[0:4]),sum(qnos[0:5])))
write = list(range(sum(qnos[0:5]),sum(qnos[0:6]))) # Can be MUXed with read?
tape = list(range(sum(qnos[0:6]),sum(qnos[0:7])))
ancilla = list(range(sum(qnos[0:7]),sum(qnos[0:8])))
quine = list(range(sum(qnos[0:8]),sum(qnos[0:9])))
print("\nFSM\t:",fsm,"\nSTATE\t:",state,"\nMOVE\t:",move,"\nHEAD\t:",head,"\nREAD\t:",read,"\nWRITE\t:",write,"\nTAPE\t:",tape,"\nANCILLA :",ancilla,"\nQUINE\t:",quine)
#=====================================================================================================================
test = []
unit = 'none' # 'none', 'read', 'fsm', 'write', 'move', 'rst'
qcirc_width = sum(qnos[0:9]) + len(test)
qcirc = QuantumCircuit(qcirc_width, len(quine))
# U_init(qcirc, qcirc_width, fsm)
# for tick in range(0, sim_tick):
# U_read(qcirc, read, head, tape, ancilla)
# U_fsm(qcirc, tick, fsm, state, read, write, move, ancilla)
# U_write(qcirc, write, head, tape, ancilla)
# U_move(qcirc, move, head, ancilla)
# U_rst(qcirc, tick, fsm, state, read, write, move, ancilla)
# ============ State Vector ============ Step: Run QPULBA 121
# (+0.25000+0.00000j) |0000000000000000>
# (+0.25000+0.00000j) |0000000000000100>
# (+0.25000+0.00000j) |0000000000001000>
# (+0.25000+0.00000j) |0000000000001100>
# (+0.25000+0.00000j) |0001111000000001>
# (+0.25000+0.00000j) |0001111000000101>
# (+0.25000+0.00000j) |0001111000001001>
# (+0.25000+0.00000j) |0001111000001101>
# (+0.25000+0.00000j) |0100000000000010>
# (+0.25000+0.00000j) |0100000000000110>
# (+0.25000+0.00000j) |0100000000001010>
# (+0.25000+0.00000j) |0100000000001110>
# (+0.25000+0.00000j) |0101111000000011>
# (+0.25000+0.00000j) |0101111000000111>
# (+0.25000+0.00000j) |0101111000001011>
# (+0.25000+0.00000j) |0101111000001111>
# ============..............============
def U_qpulba121(qcirc, fsm, tape, ancilla):
qcirc.h(fsm)
qcirc.cx(fsm[1], ancilla[1])
qcirc.cx(fsm[0],tape[0])
qcirc.cx(fsm[0],tape[1])
qcirc.cx(fsm[0],tape[2])
qcirc.cx(fsm[0],tape[3])
return
U_qpulba121(qcirc, fsm, tape, ancilla)
disp_isv(qcirc, "Step: Run QPULBA 121", all=False, precision=1e-4)
#=====================================================================================================================
def condition_fsm(qcirc, fsm, tape):
# Finding specific programs-output characteristics (fsm|tape)
# e.g. Self-replication
for q in fsm:
qcirc.cx(q,tape[q])
qcirc.barrier()
return
def condition_tape(qcirc, tape, target_tape):
# Finding algorithmic probability of a specific output (tape|tape*)
return
def condition_state(qcirc, state, target_state):
# Finding programs with specific end state (state|state*)
# Note: not possible in QPULBA 121
return
#=====================================================================================================================
condition_fsm(qcirc, fsm, tape)
disp_isv(qcirc, "Step: Find self-replicating programs", all=False, precision=1e-4)
#=====================================================================================================================
def U_oracle(qcirc, tape, quine):
# Mark tape with all zero Hamming distance
qcirc.x(tape)
qcirc.mct(tape,quine)
qcirc.x(tape)
return
U_oracle(qcirc, tape, quine)
disp_isv(qcirc, "Step: Quine bit", all=False, precision=1e-4)
#=====================================================================================================================
qcirc.measure(quine, 0)
disp_isv(qcirc, "Step: Post select quines", all=False, precision=1e-4)
sys.exit(0)
#=====================================================================================================================
# Number of 2-symbol 1-state 1-dimension Quantum Parallel Universal Linear Bounded Automata: 16
#
# FSM : [0, 1, 2, 3]
# STATE : []
# MOVE : [4]
# HEAD : [5, 6]
# READ : [7]
# WRITE : [8]
# TAPE : [9, 10, 11, 12]
# ANCILLA : [13, 14, 15]
# QUINE : [16]
#
# ============ State Vector ============ Step: Run QPULBA 121
# (+0.25000+0.00000j) |00000000000000000>
# (+0.25000+0.00000j) |00000000000000100>
# (+0.25000+0.00000j) |00000000000001000>
# (+0.25000+0.00000j) |00000000000001100>
# (+0.25000+0.00000j) |00001111000000001>
# (+0.25000+0.00000j) |00001111000000101>
# (+0.25000+0.00000j) |00001111000001001>
# (+0.25000+0.00000j) |00001111000001101>
# (+0.25000+0.00000j) |00100000000000010>
# (+0.25000+0.00000j) |00100000000000110>
# (+0.25000+0.00000j) |00100000000001010>
# (+0.25000+0.00000j) |00100000000001110>
# (+0.25000+0.00000j) |00101111000000011>
# (+0.25000+0.00000j) |00101111000000111>
# (+0.25000+0.00000j) |00101111000001011>
# (+0.25000+0.00000j) |00101111000001111>
# ============..............============
#
# ============ State Vector ============ Step: Find self-replicating programs
# (+0.25000+0.00000j) |00000000000000000>
# (+0.25000+0.00000j) |00000010000001101>
# (+0.25000+0.00000j) |00000100000000100>
# (+0.25000+0.00000j) |00000110000001001>
# (+0.25000+0.00000j) |00001000000001000>
# (+0.25000+0.00000j) |00001010000000101>
# (+0.25000+0.00000j) |00001100000001100>
# (+0.25000+0.00000j) |00001110000000001>
# (+0.25000+0.00000j) |00100000000001111>
# (+0.25000+0.00000j) |00100010000000010>
# (+0.25000+0.00000j) |00100100000001011>
# (+0.25000+0.00000j) |00100110000000110>
# (+0.25000+0.00000j) |00101000000000111>
# (+0.25000+0.00000j) |00101010000001010>
# (+0.25000+0.00000j) |00101100000000011>
# (+0.25000+0.00000j) |00101110000001110>
# ============..............============
#
# ============ State Vector ============ Step: Quine bit
# (+0.25000-0.00000j) |00000010000001101>
# (+0.25000-0.00000j) |00000100000000100>
# (+0.25000-0.00000j) |00000110000001001>
# (+0.25000-0.00000j) |00001000000001000>
# (+0.25000-0.00000j) |00001010000000101>
# (+0.25000-0.00000j) |00001100000001100>
# (+0.25000-0.00000j) |00001110000000001>
# (+0.25000-0.00000j) |00100010000000010>
# (+0.25000-0.00000j) |00100100000001011>
# (+0.25000-0.00000j) |00100110000000110>
# (+0.25000-0.00000j) |00101000000000111>
# (+0.25000-0.00000j) |00101010000001010>
# (+0.25000-0.00000j) |00101100000000011>
# (+0.25000-0.00000j) |00101110000001110>
# (+0.25000-0.00000j) |10000000000000000>
# (+0.25000-0.00000j) |10100000000001111>
# ============..............============
#
# ============ State Vector ============ Step: Post select quines
# (+0.70711-0.00000j) |10000000000000000>
# (+0.70711-0.00000j) |10100000000001111>
# ============..............============
#=====================================================================================================================
#=====================================================================================================================
# Code below archived for now
#=====================================================================================================================
#=====================================================================================================================
def U_oracle(sz):
# Mark fsm/tape/state with all zero Hamming distance (matches applied condition perfectly)
tgt_reg = list(range(0,sz))
oracle = QuantumCircuit(len(tgt_reg))
oracle.x(tgt_reg)
oracle.h(tgt_reg[0])
oracle.mct(tgt_reg[1:],tgt_reg[0])
oracle.h(tgt_reg[0])
oracle.x(tgt_reg)
return oracle
def U_pattern(sz):
# Mark {0000,0010,0100,0110,1000,1010,1100,1110}
tgt_reg = list(range(0,sz))
oracle = QuantumCircuit(len(tgt_reg))
oracle.x(tgt_reg)
oracle.h(tgt_reg[0])
oracle.mct(tgt_reg[1:],tgt_reg[0])
oracle.h(tgt_reg[0])
oracle.x(tgt_reg)
oracle.x([tgt_reg[0],tgt_reg[2],tgt_reg[3]])
oracle.h(tgt_reg[0])
oracle.mct(tgt_reg[1:],tgt_reg[0])
oracle.h(tgt_reg[0])
oracle.x([tgt_reg[0],tgt_reg[2],tgt_reg[3]])
oracle.x([tgt_reg[0],tgt_reg[1],tgt_reg[3]])
oracle.h(tgt_reg[0])
oracle.mct(tgt_reg[1:],tgt_reg[0])
oracle.h(tgt_reg[0])
oracle.x([tgt_reg[0],tgt_reg[1],tgt_reg[3]])
oracle.x([tgt_reg[0],tgt_reg[3]])
oracle.h(tgt_reg[0])
oracle.mct(tgt_reg[1:],tgt_reg[0])
oracle.h(tgt_reg[0])
oracle.x([tgt_reg[0],tgt_reg[3]])
oracle.x([tgt_reg[0],tgt_reg[1],tgt_reg[2]])
oracle.h(tgt_reg[0])
oracle.mct(tgt_reg[1:],tgt_reg[0])
oracle.h(tgt_reg[0])
oracle.x([tgt_reg[0],tgt_reg[1],tgt_reg[2]])
oracle.x([tgt_reg[0],tgt_reg[2]])
oracle.h(tgt_reg[0])
oracle.mct(tgt_reg[1:],tgt_reg[0])
oracle.h(tgt_reg[0])
oracle.x([tgt_reg[0],tgt_reg[2]])
oracle.x([tgt_reg[0],tgt_reg[1]])
oracle.h(tgt_reg[0])
oracle.mct(tgt_reg[1:],tgt_reg[0])
oracle.h(tgt_reg[0])
oracle.x([tgt_reg[0],tgt_reg[1]])
oracle.x([tgt_reg[0]])
oracle.h(tgt_reg[0])
oracle.mct(tgt_reg[1:],tgt_reg[0])
oracle.h(tgt_reg[0])
oracle.x([tgt_reg[0]])
return oracle
def U_aa(sz):
tgt_reg = list(range(0,sz))
diffuser = QuantumCircuit(len(tgt_reg))
diffuser.h(tgt_reg)
diffuser.x(tgt_reg)
diffuser.h(tgt_reg[0])
diffuser.mct(tgt_reg[1:],tgt_reg[0])
diffuser.h(tgt_reg[0])
diffuser.x(tgt_reg)
diffuser.h(tgt_reg)
return diffuser
def U_diffuser(sz):
# https://qiskit.org/textbook/ch-algorithms/quantum-counting.html
tgt_reg = list(range(0,sz))
diffuser = QuantumCircuit(len(tgt_reg))
diffuser.h(tgt_reg[1:])
diffuser.x(tgt_reg[1:])
diffuser.z(tgt_reg[0])
diffuser.mct(tgt_reg[1:],tgt_reg[0])
diffuser.x(tgt_reg[1:])
diffuser.h(tgt_reg[1:])
diffuser.z(tgt_reg[0])
return diffuser
def U_QFT(n):
# n-qubit QFT circuit
qft = QuantumCircuit(n)
def swap_registers(qft, n):
for qubit in range(n//2):
qft.swap(qubit, n-qubit-1)
return qft
def qft_rotations(qft, n):
# Performs qft on the first n qubits in circuit (without swaps)
if n == 0:
return qft
n -= 1
qft.h(n)
for qubit in range(n):
qft.cu1(np.pi/2**(n-qubit), qubit, n)
qft_rotations(qft, n)
qft_rotations(qft, n)
swap_registers(qft, n)
return qft
#=====================================================================================================================
oracle = U_oracle(len(tape)).to_gate()
oracle.label = "GO"
pattern = U_pattern(len(tape)).to_gate()
pattern.label = "PO"
allregs = list(range(sum(qnos[0:0]),sum(qnos[0:8])))
aa = U_aa(len(tape)).to_gate()
aa.label = "AA"
from copy import deepcopy
def count_constructors(qcirc, gi):
for i in range(gi):
qcirc.append(oracle, tape)
disp_isv(qcirc, "Step: Mark", all=False, precision=1e-4)
qcirc.append(aa, tape)
# disp_isv(qcirc, "Step: Amplify", all=False, precision=1e-4)
qcirc.measure(tape, range(len(tape)))
emulator = Aer.get_backend('qasm_simulator')
job = execute(qcirc, emulator, shots=2048)
hist = job.result().get_counts()
print(hist)
return
# def count_constructors(qcirc, gi):
# qcirc.append(oracle, tape)
# qcirc.append(aa, tape)
# qcirc.append(pattern, tape)
# qcirc.append(aa, tape)
# for i in range(gi):
# qcirc.append(oracle, tape)
# disp_isv(qcirc, "Step: Mark", all=False, precision=1e-4)
# qcirc.append(aa, tape)
# # disp_isv(qcirc, "Step: Amplify", all=False, precision=1e-4)
# qcirc.measure(tape, range(len(tape)))
# # print()
# # print(qcirc.draw())
# emulator = Aer.get_backend('qasm_simulator')
# job = execute(qcirc, emulator, shots=2048)
# hist = job.result().get_counts()
# print(hist)
# return
for i in range(0,3):
count_constructors(deepcopy(qcirc),i)
#=====================================================================================================================
# Code below archived for now
#=====================================================================================================================
def count_constructors():
#=====================================================================================================================
# Create controlled Grover oracle circuit
oracle = U_oracle(len(search)).to_gate()
c_oracle = oracle.control()
c_oracle.label = "cGO"
# Create controlled Grover diffuser circuit
# diffuser = U_diffuser(len(search)).to_gate()
allregs = list(range(sum(qnos[0:0]),sum(qnos[0:8])))
# selregs = [0,1,2,3,9,10,11,12,14] # fsm, tape, ancilla[1]
selregs = [0,9,10,11,12] # fsm[0], tape
diffuser = U_diffuser(len(selregs)).to_gate()
c_diffuser = diffuser.control()
c_diffuser.label = "cGD"
# Create inverse QFT circuit
iqft = U_QFT(len(count)).to_gate().inverse()
iqft.label = "iQFT"
#=====================================================================================================================
qcirc.h(count)
qcirc.barrier()
# Begin controlled Grover iterations
iterations = 1
for qb in count:
for i in range(iterations):
qcirc.append(c_oracle, [qb] + search)
qcirc.append(c_diffuser, [qb] + selregs)
iterations *= 2
qcirc.barrier()
# Inverse QFT
qcirc.append(iqft, count)
qcirc.barrier()
# print()
# disp_isv(qcirc, "Step: Search and count", all=False, precision=1e-4)
# Measure counting qubits
qcirc.measure(count, range(len(count)))
# print()
# print(qcirc.draw())
# sys.exit(0)
#=====================================================================================================================
emulator = Aer.get_backend('qasm_simulator')
job = execute(qcirc, emulator, shots=128)
hist = job.result().get_counts()
# print(hist)
measured_int = int(max(hist, key=hist.get),2)
theta = (measured_int/(2**len(count)))*pi*2
counter = 2**len(selregs) * (1 - sin(theta/2)**2)
print("Number of solutions = %.1f" % counter)
#=====================================================================================================================