-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10_qibits_demo.py
188 lines (133 loc) · 3.51 KB
/
10_qibits_demo.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
#!/usr/bin/env python
# coding: utf-8
# In[31]:
get_ipython().run_line_magic('matplotlib', 'inline')
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, execute, Aer, IBMQ
from qiskit.quantum_info.operators import Operator
import numpy as np
import math
# In[32]:
def mcx(n): #total n qubits
matrix = np.identity(2**n, dtype = int)
matrix[2**(n-1) - 1][2**(n-1) - 1] = 0
matrix[2**(n-1) - 1][2**n - 1] = 1
matrix[2**n - 1][2**(n-1) - 1] = 1
matrix[2**n - 1][2**n - 1] = 0
return Operator(matrix)
# In[33]:
def mark(bits, circuit, target):
n = len(bits)
texts = bits[::-1]
ctrls = []
for i in range(n):
if texts[i] == "0":
circuit.x(i)
ctrls.append(i)
elif texts[i] == "1":
ctrls.append(i)
mymcx = mcx(len(ctrls)+1)
circuit.append(mymcx, ctrls+[target])
for i in range(n):
if texts[i] == "0":
circuit.x(i)
qc.barrier()
# In[34]:
def code(texts, n):
output = []
for t in texts:
string = t.replace("C", "00")
string = string.replace("=O", "01")
string = string.replace("O=", "01")
string = string.replace("O", "10")
output.append(string)
output = [bits + "1"*(n-len(bits)) for bits in output]
return output
# In[35]:
def expand(texts):
if len(texts) == 1:
return texts
if len(texts) == 2:
return [text1 + text2 for text1 in texts[0] for text2 in texts[1]]
if len(texts) > 2:
return [text1 + text2 for text1 in texts[0] for text2 in expand(texts[1:])]
# In[36]:
p1 = [["CC"],
["C", "CO"]
]
p2 = [["O"],
["CC", "CCC", "=C"]
]
b1, b2 = [], []
t1 = expand(p1) + [text[::-1] for text in expand(p1)]
t2 = expand(p2) + [text[::-1] for text in expand(p2)]
for x in t1:
if x not in b1:
b1.append(x)
for x in t2:
if x not in b2:
b2.append(x)
# In[37]:
p1 = [["CC"],
["C", "CO"]
]
p2 = [["O"],
["CC", "CCC", "=C"]
]
b1, b2 = [], []
t1 = expand(p1) + [text[::-1] for text in expand(p1)]
t2 = expand(p2) + [text[::-1] for text in expand(p2)]
for x in t1:
if x not in b1:
b1.append(x)
for x in t2:
if x not in b2:
b2.append(x)
n_data = max(len(text) for text in b1 + b2)*2
bits1 = code(b1, n_data)
bits2 = code(b2, n_data)
n = n_data + 2 #n must >= 4
m = 2 #ans
loop_times = round(math.acos(math.sqrt(m/(2**(n-2))))/(2 * math.sqrt(m/(2**(n-2)))))
# In[38]:
q = QuantumRegister (n)
c = ClassicalRegister (n)
qc = QuantumCircuit (q, c)
#initiation
for i in range(n-2):
qc.h(i)
#Grover's iteration
for i in range(loop_times):
#Oracles
for bits in bits1: #Mark patent 1
mark(bits, qc, n-2)
for bits in bits2: #Mark patent 2
mark(bits, qc, n-1)
qc.cz(n-2, n-1) #Flip the phase
for bits in bits2[::-1]: #Disentangle patent 2
mark(bits, qc, n-1)
for bits in bits1[::-1]: #Disentangle patent 1
mark(bits, qc, n-2)
#Amplitude amplification
for i in range(n-2):
qc.h(i)
qc.x(i)
qc.h(n-3)
mymcx = mcx(n-2)
qc.append(mymcx, [*range(n-2)])
qc.h(n-3)
for i in range(n-2):
qc.x(i)
qc.h(i)
qc.draw()
# In[39]:
backend = Aer.get_backend('statevector_simulator')
job = execute(qc, backend)
result = job.result()
outputstate = result.get_statevector(qc, decimals=3)
# ans: CCCO, OCCC
# = 00000010, 10000000
# = 2, 128
# In[40]:
for i in range(2**10):
print(i, outputstate[i])
# In[ ]: