def QS_plotter_forobservable(num_qubits,ansatzlist,times,whatKs,qstype,observable,initial_state, evalmethod='matrix_multiplication', expectation_calculator = None,line_styles=[],linewidths=2): """ evalmethod can either be "qiskit_circuits" or "matrix_multiplication" """ if evalmethod == "qiskit_circuits" and expectation_calculator == None: raise(RuntimeError("You need to pass in the expectation_calculator as an argument (see the Qiskit_helperfunctions package).")) if qstype == 'TQS': name = 'TQS' if qstype == 'QAS': name = 'QAS' if qstype == 'CQFF': name = 'CQFF' lscount=0 for i in range(len(ansatzlist)): if i in whatKs: print('Preparing observable for plotting for K = ' + str(i)) ansatz = ansatzlist[i] O_matrix_uneval = mcp.unevaluatedmatrix(num_qubits, ansatz, observable, "O") if evalmethod == 'matrix_multiplication': print('Evaluating Observable Matrix classically') Omat = O_matrix_uneval.evaluate_matrix_by_matrix_multiplicaton(initial_state) elif evalmethod == "qiskit_circuits": print("Evaluating Observable Matrix with Qiskit circuits") Omat = O_matrix_uneval.evaluate_matrix_with_qiskit_circuits(expectation_calculator) result_pauli_string, result_alphas = ansatz.get_alphas() result_alphas = list(zip(*result_alphas)) observable_vals = [] for time_idx in range(len(times)): time = times[time_idx] alpha = result_alphas[time_idx] alpha = np.array(alpha) observable_value = alpha.conj().T @ Omat @ alpha observable_value = observable_value.real observable_vals.append(observable_value) lab = name + ' K=' + str(i) if len(line_styles)!=0: plt.plot(times, observable_vals,label=lab,linestyle=line_styles[lscount],linewidth=linewidths) lscount=lscount+1 else: plt.plot(times, observable_vals,label=lab,linewidth=linewidths)
def getdata_forbetamatrix_observable(num_qubits,ansatzlist,whatKs,observable,initial_state,betamatrixlist,evalmethod='matrix_multiplication',expectation_calculator=None): if evalmethod == "qiskit_circuits" and expectation_calculator == None: raise(RuntimeError("You need to pass in the expectation_calculator as an argument (see the Qiskit_helperfunctions package).")) allresultlist = [] #betacounter = 0 for i in range(len(ansatzlist)): if i in whatKs: print('Preparing observable for plotting for K = ' + str(i)) ansatz = ansatzlist[i] O_matrix_uneval = mcp.unevaluatedmatrix(num_qubits, ansatz, observable, "O") if evalmethod == 'matrix_multiplication': print('Evaluating Observable Matrix classically') Omat = O_matrix_uneval.evaluate_matrix_by_matrix_multiplicaton(initial_state) elif evalmethod == "qiskit_circuits": print("Evaluating Observable Matrix with Qiskit circuits") Omat = O_matrix_uneval.evaluate_matrix_with_qiskit_circuits(expectation_calculator) result = 0 #for a in range(len(betamatrixlist[i-1])): # for b in range(len(betamatrixlist[i-1])): # result = result + betamatrixlist[i-1][a][b]*Omat[b][a] result = result + np.trace(betamatrixlist[i-1]@Omat) allresultlist.append(result) #betacounter = betacounter+1 return allresultlist
ansatzlist = [] ansatzlist.append(ansatz) betamatrixlist = [] #Run IQAE for k in range(1, uptowhatK + 1): print('##########################################') print('K = ' + str(k)) #Generate Ansatz for this round ansatz = acp.gen_next_ansatz(ansatz, hamiltonian, num_qubits, method='random_selection_new', num_new_to_add=numberofnewstatestoadd) ansatzlist.append(ansatz) 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 E_mat_evaluated = E_mat_uneval.evaluate_matrix_by_matrix_multiplicaton(
def generate_IQAE_results(num_qubits, listofrandomhamiltonians, noise_term_function): finalresults = [] for hamiltonian in listofrandomhamiltonians: gammas, L_terms = noise_term_function(num_qubits) for k in range(1, uptowhatK + 1): print('##########################################') print('K = ' + str(k)) # max_k_val = k #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)) 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_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() density_mat, groundstateenergy = IQAE_instance.get_density_matrix_results( ) #We ONLY append the highest K den mat (should be most accurate result) finalresults.append(density_mat)
def big_ass_loop(delta,Gamma,mu, observable_obj_list): """ Here, observable_obj_list refers to a list of observable objects """ hamiltonian = generate_XXZ_hamiltonian(num_qubits, delta) gammas, L_terms = generate_nonlocaljump_gamma_and_Lterms(num_qubits,Gamma,mu) ansatz = acp.initial_ansatz(num_qubits) #get the steady state using qutip(lol) 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') #compute the theoretical observable expectation values observable_matrixforms = [observable.to_matrixform() for observable in observable_obj_list] theoretical_expectation_values = [np.trace(qtp_rho_ss.full() @ observable_matform) for observable_matform in observable_matrixforms] #%% #compute GQAS matrices fidelity_results = dict() observable_expectation_results = dict() for k in range(1, uptowhatK + 1): print('##########################################') print('K = ' +str(k)) # max_k_val = k #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) O_matrices_uneval = [] for observable in observable_obj_list: O_matrix_uneval = mcp.unevaluatedmatrix(num_qubits, ansatz, observable, "O") O_matrices_uneval.append(O_matrix_uneval) 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) O_matrices_evaluated = [i.evaluate_matrix_with_qiskit_circuits(expectation_calculator) for i in O_matrices_uneval] 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) O_matrices_evaluated = [i.evaluate_matrix_by_matrix_multiplicaton(initial_state) for i in O_matrices_uneval] 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. # ########################################## 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_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() result_dictionary = pp.analyze_density_matrix(num_qubits,initial_state,IQAE_instance,E_mat_evaluated,ansatz,hamiltonian,gammas,L_terms,qtp_rho_ss,O_matrices_evaluated) observable_expectation_results[k] = result_dictionary['observable_expectation'] fidelity_results[k] = result_dictionary['fidelity'] #if round(fidelity, 6) == 1: # print("breaking loop as fidelity = 1 already") # #raise(RuntimeError("Fidelity = 1!")) # break #print('JON: Got %s results'%len(fidelity_results)) return (observable_expectation_results, theoretical_expectation_values, fidelity_results)
#finalresults finalresults = [] finalresults.append(ansatz) #Run QAS for k in range(1, uptowhatK + 1): print(k) #Generate Ansatz for this round ansatz = acp.gen_next_ansatz(ansatz, hamiltonian, num_qubits) #Set initial alphas for Ansatz acp.set_initial_alphas(num_qubits, ansatz, 'start_with_initial_state') E_mat_uneval = mcp.unevaluatedmatrix(num_qubits, ansatz, hamiltonian, "E") D_mat_uneval = mcp.unevaluatedmatrix(num_qubits, ansatz, hamiltonian, "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 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) #Get starting alphas startingstrings, startingalphas = ansatz.get_alphas() #initialize QAS instance QAS_instance = pp.QAS(num_qubits, D_mat_evaluated, E_mat_evaluated, startingalphas) QAS_instance.numberstep(num_steps)