-
Notifications
You must be signed in to change notification settings - Fork 0
/
QRL_unmod_stable.py
202 lines (166 loc) · 4.97 KB
/
QRL_unmod_stable.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
#This code tries to clone a qubit state, called the Environment here, into another qubit called the Agent
import numpy
import math
from math import pi, cos, sin
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, IBMQ, Aer, available_backends, execute, register, get_backend
from qiskit.tools.visualization import circuit_drawer
from qiskit.tools.qi.qi import state_fidelity
import random
import array
#import Qconfig_IBMQ_network
#import Qconfig_IBMQ_experience
api_token='55bc88103549da44c2e47840f4f2768d286c5fa8975960c49b202c55a802117bbf970051e687ddf1d7403ca25dc46947d6fa844881841e41a6d5d31d33998c04'
#from IBMQuantumExperience import IBMQuantumExperience
#IBMQ.load_accounts()
#IBMQ.stored_accounts()
IBMQ.enable_account(api_token)
IBMQ.load_accounts()
#print("Available backends:")
#IBMQ.backends()
#IBMQ.get_backend()
backend = IBMQ.get_backend('ibmq_qasm_simulator')
#backend = Aer.get_backend('statevector_simulator')
#print(backend)
shots = 1 # Number of shots to run the program (experiment); maximum is 8192 shots.
max_credits = 3 # Maximum number of credits to spend on executions.
delta = 2*pi/3
eta = 1.2
aa = random.uniform(-delta/2, delta/2)
bb = random.uniform(-delta/2, delta/2)
a = array.array('d',[aa])
b = array.array('d',[bb])
ex = 3.130756514997222
ey = 3.9944049908247625
ez = 2.6848785549652145
env = [
cos(ex/2)*complex(1,0),
sin(ex/2)*complex(cos(ey), sin(ey))
]
env
'''
print (str(ex))
print (str(ey))
print (str(ez))
'''
#Fidelty
fidelity = 0
#old fidelity
ofidelity = 0
def updDelta (num, delta):
if(num == 1):
delta = delta/eta
else:
delta = delta*eta
return 0;
cycle = 39
'''
q = QuantumRegister(1, 'q')
c = ClassicalRegister(1)
qc = QuantumCircuit(q,c)
qc.u3(ex, ey, ez, q[0])
qc.measure(q[0], c[0])
job = execute(qc, backend=backend, shots=shots, max_credits=max_credits)
result = job.result()
outcome = int(str(result.get_counts(qc))[2])
'''
#state_fidelity(env, [q[2]])
for i in range(1, cycle+1):
print("\nIteration Number = " + str(i))
backend = IBMQ.get_backend('ibmq_qasm_simulator')
#Creating Gates
#q0 is |A>
#q1 is |R>
#q2 is |E>
q = QuantumRegister(3, 'q')
c = ClassicalRegister(1, 'c')
qc = QuantumCircuit(q,c)
#qc.initialize(env, [q[2]])
#Initializing Environment
qc.u3(ex, ey, ez, q[2])
#qc.initialize(env, [q[2]])
# state_fidelity(env, q[2])
for j in range(1, i):
qc.rz(-b[j],q[2])
for k in range(1, j):
qc.rx(a[j],q[0])
qc.rz(b[j],q[0])
qc.rx(a[j],q[0])
qc.rz(b[j],q[0])
for k in range(1, j):
qc.rz(-b[j],q[0])
qc.rx(-a[j],q[0])
qc.rx(-a[j],q[2])
#Copying env to registe
qc.cx(q[2],q[1])
qc.measure(q[1], c[0])
#qc.cx(q[1], q[4])
#backend_sim = Aer.get_backend('qasm_simulator')
#result = execute(qc, backend_sim).result()
#print(result.get_counts(qc))
#i = 1
#for i in range(1,3):
job = execute(qc, backend=backend, shots=shots, max_credits=max_credits)
result = job.result()
outcome = int(str(result.get_counts(qc))[2])
#qc_state = result.get_statevector(qc)
#print(qc_state)
#state_fidelity(env, qc_state)
print('Result = ' + str(result.get_counts(qc)))
print ('Outcome = ' + str(outcome))
aa = random.uniform(-delta/2, delta/2)
print ('aa = ' + str(aa))
bb = random.uniform(-delta/2, delta/2)
print ('bb = ' + str(bb))
if(outcome == 1):
delta = delta*eta
a.append(aa)
b.append(bb)
else:
delta = delta/eta
a.append(0)
b.append(0)
#circuit_drawer(qc)
q = QuantumRegister(1, 'q')
qc2 = QuantumCircuit(q)
qc2.rz(0, q[0])
for j in range(1, i+1):
for k in range(1, j):
qc2.rz(-b[j],q[0])
qc2.rx(-a[j],q[0])
qc2.rx(a[j],q[0])
qc2.rz(b[j],q[0])
for k in range(1, j):
qc2.rx(a[j],q[0])
qc2.rz(b[j],q[0])
backend = Aer.get_backend('statevector_simulator')
job = execute(qc2, backend)
qc2_state = job.result().get_statevector(qc2)
qc2_state
fidelity = state_fidelity(env, qc2_state)
print('Fidelity = ' + str(state_fidelity(env, qc2_state)))
#if((fidelity>=0.9)&(ofidelity <= 0.9)):
# delta = pi/8
#print('Delta = ' + str(delta))
ofidelity = fidelity
#circuit_drawer(qc2)
q = QuantumRegister(1, 'q')
qc = QuantumCircuit(q)
for j in range(1, cycle+1):
for k in range(1, j):
qc.rz(-b[j],q[0])
qc.rx(-a[j],q[0])
qc.rx(a[j],q[0])
qc.rz(b[j],q[0])
for k in range(1, j):
qc.rx(a[j],q[0])
qc.rz(b[j],q[0])
backend = Aer.get_backend('statevector_simulator')
job = execute(qc, backend)
qc_state = job.result().get_statevector(qc)
qc_state
print('Final Fidelity = ' + str(state_fidelity(env, qc_state)))
#circuit_drawer(qc)
#counts_exp = result_exp.get_counts(qc)
#print(counts_exp)
#plot_histogram(counts_exp)
#circuit_drawer(qc)