forked from SaraM92/NQC-Workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOur_Qiskit_Functions.py
314 lines (287 loc) · 8.61 KB
/
Our_Qiskit_Functions.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
from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit, Aer, execute
import numpy as np
import math as m
import scipy as sci
S_simulator = Aer.backends(name='statevector_simulator')[0]
M_simulator = Aer.backends(name='qasm_simulator')[0]
#Displaying Results
def Wavefunction( obj , *args, **kwargs):
#Displays the waveftmction of the quantum system
if(type(obj) == QuantumCircuit ):
statevec = execute( obj, S_simulator, shots=1 ).result().get_statevector()
if(type(obj) == np.ndarray):
statevec = obj
sys = False
NL = False
dec = 5
if 'precision' in kwargs:
dec = int( kwargs['precision'] )
if 'column' in kwargs:
NL = kwargs['column']
if 'systems' in kwargs:
systems = kwargs['systems']
sys = True
last_sys = int(len(systems)-1)
show_systems = []
for s_chk in np.arange(len(systems)):
if( type(systems[s_chk]) != int ):
raise Exception('systems must be an array of all integers')
if 'show_systems' in kwargs:
show_systems = kwargs['show_systems']
if( len(systems)!= len(show_systems) ):
raise Exception('systems and show_systems need to be arrays of equal length')
for ls in np.arange(len(show_systems)):
if((show_systems[ls] != True) and (show_systems[ls] != False)):
raise Exception('show_systems must be an array of Truth Values')
if(show_systems[ls] == True):
last_sys = int(ls)
else:
for ss in np.arange(len(systems)):
show_systems.append(True)
wavefunction = ''
qubits = int(m.log(len(statevec),2))
for i in np.arange( int(len(statevec))):
#print(wavefunction)
value = round(statevec[i].real, dec) + round(statevec[i].imag, dec) * 1j
if( (value.real != 0) or (value.imag != 0)):
state = list(Binary(int(i),int(2**qubits)))
state.reverse()
state_str = ''
#print(state)
if( sys == True ): #Systems and SharSystems
k = 0
for s in np.arange(len(systems)):
if(show_systems[s] == True):
if(int(s) != last_sys):
state.insert(int(k + systems[s]), '>|' )
k = int(k + systems[s] + 1)
else:
k = int(k + systems[s])
else:
for s2 in np.arange(systems[s]):
del state[int(k)]
for j in np.arange(len(state)):
if(type(state[j])!= str):
state_str = state_str + str(int(state[j]))
else:
state_str = state_str + state[j]
#print(state_str)
#print(value)
if( (value.real != 0) and (value.imag != 0) ):
if( value.imag > 0):
wavefunction = wavefunction + str(value.real) + '+' + str(value.imag) + 'j |' + state_str + '> '
else:
wavefunction = wavefunction + str(value.real) + '' + str(value.imag) + 'j |' + state_str + '> '
if( (value.real !=0 ) and (value.imag ==0) ):
wavefunction = wavefunction + str(value.real) + ' |' + state_str + '> '
if( (value.real == 0) and (value.imag != 0) ):
wavefunction = wavefunction + str(value.imag) + 'j |' + state_str + '> '
if(NL):
wavefunction = wavefunction + '\n'
#print(NL)
#print(wavefunction)
return wavefunction
def Measurement(quantumcircuit, *args, **kwargs):
#Displays the measurement results of a quantum circuit
p_M = True
S = 1
ref = False
NL = False
if 'shots' in kwargs:
S = int(kwargs['shots'])
if 'return_M' in kwargs:
ret = kwargs['return_M']
if 'print_M' in kwargs:
p_M = kwargs['print_M']
if 'column' in kwargs:
NL = kwargs['column']
M1 = execute(quantumcircuit, M_simulator, shots=S).result().get_counts(quantumcircuit)
M2 = {}
k1 = list(M1.keys())
v1 = list(M1.values())
for k in np.arange(len(k1)):
key_list = list(k1[k])
new_key = ''
for j in np.arange(len(key_list)):
new_key = new_key+key_list[len(key_list)-(j+1)]
M2[new_key] = v1[k]
if(p_M):
k2 = list(M2.keys())
v2 = list(M2.values())
measurements = ''
for i in np.arange(len(k2)):
m_str = str(v2[i])+'|'
for j in np.arange(len(k2[i])):
if(k2[i][j] == '0'):
m_str = m_str + '0'
if(k2[i][j] == '1'):
m_str = m_str + '1'
if( k2[i][j] == ' ' ):
m_str = m_str +'>|'
m_str = m_str + '> '
if(NL):
m_str = m_str + '\n'
measurements = measurements + m_str
#print(measurements)
return measurements
if(ref):
return M2
#Math Operations
def Oplus(bit1,bit2):
'''Adds too bits of O's and 1's (modulo 2)'''
bit = np.zeros(len(bit1))
for i in np.arange( len(bit) ):
if( (bit1[i]+bit2[i])%2 == 0 ):
bit[i] = 0
else:
bit[i] = 1
return bit
def Binary(number,total):
#Converts a number to binary, right to left LSB 152 153 o
qubits = int(m.log(total,2))
N = number
b_num = np.zeros(qubits)
for i in np.arange(qubits):
if( N/((2)**(qubits-i-1)) >= 1 ):
b_num[i] = 1
N = N - 2 ** (qubits-i-1)
B = []
for j in np.arange(len(b_num)):
B.append(int(b_num[j]))
return B
def From_Binary(s):
num = 0
for i in np.arange(len(s)):
num = num + s[int(0-(i+1))] * 2 ** (i)
return num
def B2D(in_bi):
len_in = len(in_bi)
in_bi = in_bi[::-1]
dec = 0
for i in range(0,len_in):
if in_bi[i] != '0':
dec += 2**i
return dec
# Custom Gates
def x_Transformation(qc, qreg, state):
#Tranforms the state of the system, applying X gates according to as in the vector 'state'
for j in np.arange(len(state)):
if( int(state[j]) == 0 ):
qc.x( qreg[int(j)] )
def n_NOT(qc, control, target, anc):
#performs an n-NOT gate
n = len(control)
instructions = []
active_ancilla = []
q_unused = []
q = 0
a = 0
while( (n > 0) or (len(q_unused) != 0) or (len(active_ancilla) != 0) ):
if( n > 0 ):
if( (n-2) >= 0 ):
instructions.append( [control[q], control[q+1], anc[a]] )
active_ancilla.append(a)
a += 1
q += 2
n = n - 2
if( (n-2) == -1 ):
q_unused.append( q )
n = n - 1
elif( len(q_unused) != 0 ):
if(len(activeancilla)!=1):
instructions.append( [control[q], anc[active_ancilla[0]], anc[a]] )
del active_ancilla[0]
del q_unused[0]
active_ancilla.append(a)
a = a + 1
else:
instructions.append( [control[q], anc[active_ancilla[0]], target] )
del active_ancilla[0]
del q_unused[0]
elif( len(active_ancilla) != 0 ):
if( len(active_ancilla) > 2 ):
instructions.append( [anc[active_ancilla[0]], anc[active_ancilla[1]], anc[a]] )
active_ancilla.append(a)
del active_ancilla[0]
del active_ancilla[0]
a = a + 1
elif( len(active_ancilla) == 2):
instructions.append([anc[active_ancilla[0]], anc[active_ancilla[1]], target])
del active_ancilla[0]
del active_ancilla[0]
for i in np.arange( len(instructions) ):
qc.ccx( instructions[i][0], instructions[i][1], instructions[i][2] )
del instructions[-1]
for i in np.arange( len(instructions) ):
qc.ccx( instructions[0-(i+1)][0], instructions[0-(i+1)][1], instructions[0-(i+1)][2] )
def Control_Instruction( qc, vec ):
#Ammends the proper quantum circuit instruction based on the input 'vec'
#Used for the function 'n_Control_U
if( vec[0] == 'X' ):
qc.cx( ver[1], vec[2] )
if( vec[0] == 'Z' ):
qc.cz( ver[1], vec[2] )
if( vec[0] == 'PRASE' ):
qc.cu1( vec[2], vec[1], vec[3] )
if( vec[0] == 'SWAP' ):
qc.cswap( vec[1], vec[2], vec[3] )
def X_Transformation(qc, qreg, state):
for j in np.arange(len(state)):
if( int(state[j]) == 0):
qc.x( qreg[int(j)])
def sinmons_solver(E,N):
'''Returns an array of s_prime candidates
'''
s_primes = []
for s in np.ararge(1,2**N):
sp = Binary( int(s), 2**N )
candidate = True
for e in np.arange( len(E) ):
value = 0
for i in np.arange( N ):
value = value + sp[i]*E[e][i]
if(value%2==1):
candidate=False
if(candidate):
s_primes.append(sp)
return s_primes
def Grover_Oracle(mark, qc, q, an1, an2):
'''
picks out the marked state and applies a negative phase
'''
qc.h( an1[0] )
X_Transformation(qc, q, mark)
if( len(mark) > 2 ):
n_NOT( qc, q, an1[0], an2 )
if( len(mark) == 2 ):
qc.ccx( q[0], q[1], an1[0] )
X_Transformation(qc, q, mark)
qc.h( an1[0] )
def Grover_Diffusion(mark, qc, q, an1, an2):
'''
ammends the instructions for a Grover Diffusion Operation to the Quartu rcuit
'''
zeros_state = []
for i in np.arange( len(mark) ):
zeros_state.append( 0 )
qc.h( q[int(i)] )
Grover_Oracle(zeros_state, qc, q, an1, an2)
for j in np.arange( len(mark) ):
qc.h( q[int(j)] )
def Grover(Q, marked):
'''
Amends all the instructions for a Grover Search
'''
q = QuantumRegister(Q,name='q')
an1 = QuantumRegister(1,name='anc')
an2 = QuantumRegister(Q-2,name='nanc')
c = ClassicalRegister(Q,name='c')
qc = QuantumCircuit(q,an1,an2,c,name='qc')
for j in np.arange(Q):
qc.h( q[int(j)] )
qc.x( an1[0] )
iterations = round( m.pi/4 * 2**(Q/2.0) )
for i in np.arange( iterations ):
Grover_Oracle(marked, qc, q, an1, an2)
Grover_Diffusion(marked, qc, q, an1, an2)
return qc, q, an1, an2, c