-
Notifications
You must be signed in to change notification settings - Fork 0
/
forkhtoteststuff.py
407 lines (331 loc) · 16.8 KB
/
forkhtoteststuff.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
#%%
import numpy as np
import ansatz_class_package as acp
import pauli_class_package as pcp
import hamiltonian_class_package as hcp
import matrix_class_package as mcp
import post_processing as pp
import scipy as scp
import qutip
import math
import pandas as pd
optimizer = 'feasibility_sdp'#'eigh' , 'eig', 'sdp','feasibility_sdp'
eigh_inv_cond = 10**(-6)
eig_inv_cond = 10**(-6)
use_qiskit = False
degeneracy_tol = 5
sdp_tolerance_bound = 0
howmanyrandominstances = 1
num_qubits = 4
uptowhatK = 2
which_hamiltonian = "sai_ring"
# which_hamiltonian = "bulk_dephasing"
#Generate initial state
random_generator = np.random.default_rng(497)
initial_state = acp.Initialstate(num_qubits, "efficient_SU2", random_generator, 1)
random_selection_new = False
if random_selection_new == True:
numberofnewstatestoadd = 10 #Only will be used if 'random_selection_new' is selected
# numberofnewstatestoadd = 6 #Only will be used if 'random_selection_new' is selected
#%% IBMQ STUFF
if use_qiskit:
import Qiskit_helperfunctions as qhf #IBMQ account is loaded here in this import
hub, group, project = "ibm-q-nus", "default", "reservations"
#IMBQ account is loaded in the importing of Qiskit_helperfunctions for now, so this part is commented out (KH, 5 may 2021)
#load IBMQ account. This step is needed if you want to run on the actual quantum computer
#Other parameters for running on the quantum computer. Choose 1 to uncomment.
# sim = "noiseless_qasm"
# quantum_com = "ibmq_bogota" #which quantum computer to take the noise profile from
# num_shots = 30000 #max is 1000000
sim = "noisy_qasm"
quantum_com = "ibmq_bogota" #which quantum computer to take the noise profile from
num_shots = 8192 #max is 8192
# sim = "real"
# quantum_com = "ibmq_rome" #which quantum computer to actually run on
# num_shots = 8192 #max is 8192
quantum_computer_choice_results = qhf.choose_quantum_computer(hub, group, project, quantum_com)
#Example on how to create artificial noise model
#couplingmap = [[0,1],[1,2],[2,3],[3,4]]
#quantum_computer_choice_results = qhf.create_quantum_computer_simulation(couplingmap,depolarizingnoise=True,depolarizingnoiseparameter=0.03,bitfliperror=True,bitfliperrorparameter=0.03,measerror=True,measerrorparameter=0.03)
mitigate_meas_error = True
meas_filter = qhf.measurement_error_mitigator(num_qubits, sim, quantum_computer_choice_results, shots = num_shots)
# mitigate_meas_error = False
# meas_filter = None
#expectation calculator here is an object that has a method that takes in a paulistring object P, and returns a <psi|P|psi>.
#This expectation calculator also stores previously calculated expectation values, so one doesn't need to compute the same expectation value twice.
expectation_calculator = qhf.expectation_calculator(initial_state, sim, quantum_computer_choice_results, meas_error_mitigate = mitigate_meas_error, meas_filter = meas_filter)
#%%
def generate_XXZ_hamiltonian(num_qubits, delta):
#epsilon = 0.5
if num_qubits == 1:
raise(RuntimeError('Cannot generate Hamiltonian with 1 qubit'))
else:
hamiltonian = hcp.heisenberg_xyz_model(num_qubits, jx = 1, jy = 1, jz = delta)
return hamiltonian
def generate_nonlocaljump_gamma_and_Lterms(num_qubits,Gamma,mu):
#gammas_to_append = 1
gammas = []
L_terms = []
if num_qubits == 1:
raise(RuntimeError('Cannot generate non-local jump terms with 1 qubit'))
else:
gammas.append(1)
cof = np.sqrt(Gamma*(1-mu))
L_terms.append(hcp.generate_arbitary_hamiltonian(num_qubits,[0.25*cof,0.25j*cof,-0.25j*cof,0.25*cof],['1'+'0'*(num_qubits-2)+'1','2'+'0'*(num_qubits-2)+'1','1'+'0'*(num_qubits-2)+'2','2'+'0'*(num_qubits-2)+'2']))
gammas.append(1)
cof = np.sqrt(Gamma*(1+mu))
L_terms.append(hcp.generate_arbitary_hamiltonian(num_qubits,[0.25*cof,-0.25j*cof,0.25j*cof,0.25*cof],['1'+'0'*(num_qubits-2)+'1','2'+'0'*(num_qubits-2)+'1','1'+'0'*(num_qubits-2)+'2','2'+'0'*(num_qubits-2)+'2']))
return (gammas, L_terms)
def generate_bulk_dephasing(num_qubits):
gammas = []
L_terms = []
if num_qubits == 1:
raise(RuntimeError("One qubit case not considered"))
else:
for i in range(num_qubits):
pauli_string_deconstructed = ["0"]*num_qubits
pauli_string_deconstructed[i] = "3"
pauli_string_str = "".join(pauli_string_deconstructed)
L_i = hcp.generate_arbitary_hamiltonian(num_qubits, [1], [pauli_string_str])
# print(L_i.to_matrixform())
gammas.append(1)
L_terms.append(L_i)
return (gammas, L_terms)
def generate_total_magnetisation_matform(num_qubits):
def make_sigma_z_string(i):
pauli_string_deconstructed = ["0"]*num_qubits
pauli_string_deconstructed[i] = "3"
pauli_string_str = "".join(pauli_string_deconstructed)
return pauli_string_str
p_strings = [make_sigma_z_string(i) for i in range(num_qubits)]
betas = [1 for i in range(num_qubits)]
M = hcp.generate_arbitary_hamiltonian(num_qubits, betas, p_strings)
return M.to_matrixform()
def generate_parity_operator_matform(num_qubits):
dim = 2**num_qubits
P_mat = np.zeros((dim,dim))
# test_mat = np.zeros(dim)
for i in range(dim):
ket_bitstring = np.binary_repr(i)
ket_bitstring = (num_qubits - len(ket_bitstring))*"0" + ket_bitstring
ket = np.zeros(dim)
ket[i] = 1
bra_bitstring = ket_bitstring[::-1]
bra_index = int(bra_bitstring,2)
# print(ket_bitstring, bra_bitstring)
bra = np.zeros(dim)
bra[bra_index] = 1
P_mat += np.outer(ket,bra)
spinflips = pcp.paulistring(num_qubits,[1]*num_qubits,1).get_matrixform()
return P_mat @ spinflips
#%%
if which_hamiltonian == "sai_ring":
#Gamma and mu is for Sai ring model
Gamma = 0.5
mu = 0.4
delta = 0.3
#Sai ring hamiltonian
hamiltonian = generate_XXZ_hamiltonian(num_qubits, delta)
gammas, L_terms = generate_nonlocaljump_gamma_and_Lterms(num_qubits,Gamma,mu)
elif which_hamiltonian == "bulk_dephasing":
delta = 0.3
hamiltonian = generate_XXZ_hamiltonian(num_qubits, delta)
gammas, L_terms = generate_bulk_dephasing(num_qubits)
ansatz = acp.initial_ansatz(num_qubits)
#%%
#get the steady state using qutip(lol)
#This one is probably inaccurate!
qtp_hamiltonian = qutip.Qobj(hamiltonian.to_matrixform())
qtp_Lterms = [qutip.Qobj(i.to_matrixform()) for i in L_terms]
qtp_C_ops = [np.sqrt(gammas[i]) * qtp_Lterms[i] for i in range(len(qtp_Lterms))]
qtp_rho_ss = qutip.steadystate(qtp_hamiltonian, qtp_C_ops,method='iterative-gmres')
qtp_rho_ss_matform = qtp_rho_ss.full()
S = generate_parity_operator_matform(num_qubits) #only used for Sai ring Hamiltonian
M = generate_total_magnetisation_matform(num_qubits)
S_m = scp.linalg.expm(1j*M*np.pi/(num_qubits*2))
# %%
fidelity_results = dict()
for k in range(uptowhatK):
#Generate Ansatz for this round
if random_selection_new:
ansatz = acp.gen_next_ansatz(ansatz, hamiltonian, num_qubits,method='random_selection_new',num_new_to_add=numberofnewstatestoadd)
else:
ansatz = acp.gen_next_ansatz(ansatz, hamiltonian, num_qubits)
E_mat_uneval = mcp.unevaluatedmatrix(num_qubits, ansatz, hamiltonian, "E")
D_mat_uneval = mcp.unevaluatedmatrix(num_qubits, ansatz, hamiltonian, "D")
if optimizer == 'feasibility_sdp':
R_mats_uneval = []
F_mats_uneval = []
for thisL in L_terms:
R_mats_uneval.append(mcp.unevaluatedmatrix(num_qubits,ansatz,thisL,"D"))
thisLdagL = hcp.multiply_hamiltonians(hcp.dagger_hamiltonian(thisL),thisL)
F_mats_uneval.append(mcp.unevaluatedmatrix(num_qubits,ansatz,thisLdagL,"D"))
#Here is where we should be able to specify how to evaluate the matrices.
#However only the exact method (classical matrix multiplication) has been
#implemented so far
if use_qiskit:
E_mat_evaluated = E_mat_uneval.evaluate_matrix_with_qiskit_circuits(expectation_calculator)
D_mat_evaluated = D_mat_uneval.evaluate_matrix_with_qiskit_circuits(expectation_calculator)
else:
E_mat_evaluated = E_mat_uneval.evaluate_matrix_by_matrix_multiplicaton(initial_state)
D_mat_evaluated = D_mat_uneval.evaluate_matrix_by_matrix_multiplicaton(initial_state)
if optimizer == 'feasibility_sdp':
R_mats_evaluated = []
for r in R_mats_uneval:
if use_qiskit:
R_mats_evaluated.append(r.evaluate_matrix_with_qiskit_circuits(expectation_calculator))
else:
R_mats_evaluated.append(r.evaluate_matrix_by_matrix_multiplicaton(initial_state))
F_mats_evaluated = []
for f in F_mats_uneval:
if use_qiskit:
F_mats_evaluated.append(f.evaluate_matrix_with_qiskit_circuits(expectation_calculator))
else:
F_mats_evaluated.append(f.evaluate_matrix_by_matrix_multiplicaton(initial_state))
##########################################
#Start of the classical post-processing. #
##########################################
randombetainitializations = []
for i in range(howmanyrandominstances):
randombetainitializations.append(random_generator.random((len(D_mat_evaluated),len(D_mat_evaluated))))
# print(randombetainitializations[i])
results_dictionary = []
for betainitialpoint in randombetainitializations:
if optimizer == 'feasibility_sdp':
IQAE_instance = pp.IQAE_Lindblad(num_qubits, D_mat_evaluated, E_mat_evaluated,R_matrices = R_mats_evaluated,F_matrices = F_mats_evaluated,gammas = gammas)
else:
IQAE_instance = pp.IQAE_Lindblad(num_qubits, D_mat_evaluated, E_mat_evaluated)
IQAE_instance.define_beta_initialpoint(betainitialpoint)
IQAE_instance.define_optimizer(optimizer, eigh_invcond=eigh_inv_cond,eig_invcond=eig_inv_cond,degeneracy_tol=degeneracy_tol,sdp_tolerance_bound=sdp_tolerance_bound)
IQAE_instance.evaluate()
# IQAE_instance.evaluate(kh_test=False)
#all_energies,all_states = IQAE_instance.get_results_all()
results_dictionary.append(pp.analyze_density_matrix(num_qubits,initial_state,IQAE_instance,E_mat_evaluated,ansatz,hamiltonian,gammas,L_terms,qtp_rho_ss,[], verbose=False))
# observable_expectation_results[k] = result_dictionary['observable_expectation']
# fidelity_results[k] = result_dictionary['fidelity']
'''The results_dictionary is a list of all the result_dictionaries generated for each random beta initial point'''
# Useful functions
result = results_dictionary[0] #since all the results are the same, just take the first one
rho = result['rho']
S_m_eigvals = np.array(list(set(scp.linalg.eigvals(S_m)))) #set because unique eigvals only
def handle_S_m_degeneracy(rhoStart, which_index=1):
"""
here, which_index is such that:
which_index = 0 returns NESS corresponding to M = 2
which_index = 1 returns NESS corresponding to M = 0
which_index = 2 returns NESS corresponding to M = -2
which_index = 3 returns NESS corresponding to M = 4
which_index = 4 returns NESS corresponding to M = -4
"""
rhoTemp = rhoStart
for i in range(num_qubits):
for j in range(num_qubits):
if i == j:
continue
else:
rhoTempTemp = rhoTemp - S_m @ rhoStart @ S_m.conj().T
factor = 1 - np.exp(1j*(S_m_eigvals[i] - S_m_eigvals[j]))
rhoTemp = rhoTemp - (1/factor) * rhoTempTemp
rhoPhys = rhoTemp
vanderMat = np.vander(S_m_eigvals, increasing = True).T
vanderMatInv = scp.linalg.inv(vanderMat)
matrices = []
for i in range(len(S_m_eigvals)):
factor = np.linalg.matrix_power(S_m, i)
matrices.append(factor @ rhoPhys)
matrices = np.array(matrices)
index = which_index
vanderMatVec = vanderMatInv[index]
last = vanderMatVec[-1] * matrices[-1]
for j in range(len(matrices) - 1):
last += vanderMatVec[j] * matrices[j]
last = last / np.trace(last)
last_dot = pp.evaluate_rho_dot(last, hamiltonian, gammas, L_terms)
print(np.max(np.max(last_dot)))
return last
def fidelity_checker(rho1, rho2):
rho1 = rho1 / np.trace(rho1)
rho2 = rho2 / np.trace(rho2)
qtp_rho1 = qutip.Qobj(rho1)
qtp_rho2 = qutip.Qobj(rho2)
fidelity = qutip.metrics.fidelity(qtp_rho1, qtp_rho2)
return fidelity
def handle_S_degeneracy(rho):
rho_prime = S @ rho @ S.conjugate().transpose()
rho_phys = 0.5*(rho + rho_prime) #works
rho_phys = rho_phys / np.trace(rho_phys)
print("fidelity between rho and rho phys is", fidelity_checker(rho, rho_phys))
rhopp = (rho_phys + S @ rho_phys)
rhopp = rhopp / np.trace(rhopp)
rhomm = (rho_phys - S @ rho_phys)
rhomm = rhomm / np.trace(rhomm)
results = dict()
results["rho_phys"] = rho_phys
results["rhopp"] = rhopp
results["rhomm"] = rhomm
return results
#%%
if which_hamiltonian == "bulk_dephasing":
# IF USING BULK DEPHASING HAMILTONIAN
# Here we apply the algorithm for multiple NESS for the case of S_m, since there is a degeneracy due to S_m
rhotst = handle_S_m_degeneracy(rho, which_index=1)
for i in range(len(rhotst)):
for j in range(len(rhotst)):
rhotst[i,j] = np.round(rhotst[i,j],10)
display(pd.DataFrame(rhotst))
rhotst_dot = pp.evaluate_rho_dot(rhotst, hamiltonian, gammas, L_terms)
print('Max value rho_dot is: ' + str(np.max(np.max(rhotst_dot))))
eigvals,eigvecs = scp.linalg.eigh(M)
projector = np.zeros((2**num_qubits, 2**num_qubits)) + 1j* np.zeros((2**num_qubits, 2**num_qubits))
for j in range(len(eigvals)):
eigval = eigvals[j]
if eigval == 0:
eigvec = eigvecs[:,j]
eigvec = eigvec / np.sqrt(np.vdot(eigvec, eigvec))
projector += np.outer(eigvec, eigvec)
subspace_size = np.count_nonzero(eigvals == 0)
theoretical_ss = (1/subspace_size) * projector @ np.eye(2**num_qubits)@ projector.conj().T
print(fidelity_checker(rhotst, theoretical_ss))
elif which_hamiltonian == "sai_ring":
# IF USING RING HAMILTONIAN
# Here we apply the algorithm for multiple NESS for the case of S_m, since there is a degeneracy due to S_m
#For this case, we are considering the case of even N in order to get the eigenvalue corresponding to M = 0 (i.e, S_m = 1). In this subspace, we expect rho_pp and rho_mm.
# For our case, for even N, the number of eigenvalues of S_m is N+1. We can take this into consideration in our algorithm.
# Hence, for N = 4, we have 5*5 possible NESS, 5 of which are legit. Hence in general, for the front part of our algo, we need to run it 5*5 - 5 times to eliminate the 20 non-physical NESS
#First we handle the S_m degeneracy to get the steady state with zero magnetisation, then we handle the S degeneracy to get rho_pp and rho_ss in the zero magnetisation subspace
rhotst = handle_S_m_degeneracy(rho, which_index=1)
rhoResults = handle_S_degeneracy(rhotst)
rhopp = rhoResults["rhopp"]
rhomm = rhoResults["rhomm"]
# qtp_rhotst = handle_S_m_degeneracy(qtp_rho_ss_matform) #this does nothing!
# since qtp_rho_ss_matform is already in the 4 magnetisation subspace
# qtp_rhoResults = handle_S_degeneracy(qtp_rho_ss_matform)
# # qtp_rhoResults = handle_S_degeneracy(qtp_rhotst)
# qtp_rhopp = qtp_rhoResults["rhopp"]
# qtp_rhomm = qtp_rhoResults["rhomm"]
# qtp_rhophys = qtp_rhoResults["rho_phys"]
rho_dot_pp = pp.evaluate_rho_dot(rhopp, hamiltonian,gammas, L_terms)
print('Max value rho_dot_pp is: ' + str(np.max(np.max(rho_dot_pp))))
rho_dot_mm = pp.evaluate_rho_dot(rhomm, hamiltonian,gammas, L_terms)
print('Max value rho_dot_mm is: ' + str(np.max(np.max(rho_dot_mm))))
print("tr(rhopp@S), tr(rhopp@M)")
print("Supposed to get S = 1, M = 0")
print(np.trace(rhopp@S), np.trace(rhopp@M))
print("tr(rhomm@S), tr(rhomm@M)")
print("Supposed to get S = -1, M = 0")
print(np.trace(rhomm@S), np.trace(rhomm@M))
# qtp_rho_dot_pp = pp.evaluate_rho_dot(qtp_rhopp, hamiltonian,gammas, L_terms)
# print('Max value qtp_rho_dot_pp is: ' + str(np.max(np.max(qtp_rho_dot_pp))))
# qtp_rho_dot_mm = pp.evaluate_rho_dot(qtp_rhomm, hamiltonian,gammas, L_terms)
# print('Max value qtp_rho_dot_mm is: ' + str(np.max(np.max(qtp_rho_dot_pp))))
# rhopp_fidelity = fidelity_checker(qtp_rhopp, rhopp)
# rhomm_fidelity = fidelity_checker(qtp_rhomm, rhomm)
# print("rhopp_fidelity, rhomm_fidelity is", rhopp_fidelity,rhomm_fidelity)
#Progress so far:
# I think my general method works as long as the coefficients in front of the obtained density matrix do not vanish. If they do, we just get 0 lol.
# This happened for the qutip result because the qutip solver is so powerful
# that it directly gives a steady state that is already solely in one of the
# symmetry sectors of the M operator (M = 4). When we try to find the plus or
# the minus NESS corresponding to the symmetry sectors of the S operator, we
# don't get anywhere.
# I think we bobian have to screw the qutip solver