num_qubits, list(pauli_decomp.values()), list(pauli_decomp.keys())) #print('Beta values are ' + str(hamiltonian.return_betas())) ansatz = acp.initial_ansatz(num_qubits) 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)
L = np.array([[-0.1,-0.25j,0.25j,0],[-0.25j,-0.05-0.1j,0,0.25j],[0.25j,0,-0.05+0.1j,-0.25j],[0.1,0.25j,-0.25j,0]]) #Converting L^dag L into Hamiltonian LdagL = np.array([[0.145,0.025+0.0375j,0.025-0.0375j,-0.125],[0.025-0.0375j,0.1375,-0.125,-0.025-0.0125j],[0.025+0.0375j,-0.125,0.1375,-0.025+0.0125j],[-0.125,-0.025+0.0125j,-0.025-0.0125j,0.125]]) pauli_decomp = pcp.paulinomial_decomposition(L) hamiltonian = hcp.generate_arbitary_hamiltonian(num_qubits, list(pauli_decomp.values()), list(pauli_decomp.keys())) #print('Beta values are ' + str(hamiltonian.return_betas())) ansatz = acp.initial_ansatz(num_qubits) #Run IQAE for k in range(1, uptowhatK + 1): print(k) #Generate Ansatz for this round 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") #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) ########################################## #Start of the classical post-processing. # ########################################## 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) IQAE_instance.evaluate()
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)
#Need this for pruning acp.set_initial_ansatz_alpha_for_pruning(ansatz, num_steps) #finalresults finalresults = [] finalresults.append(ansatz) #Run TTQS for k in range(1, uptowhatK + 1): print('Currently at K = ' + str(k)) #Generate Ansatz for this round #ansatz = acp.gen_next_ansatz(ansatz, hamiltonian, num_qubits) #By default, there is no processing when generating next Ansatz ansatz = acp.gen_next_ansatz(ansatz, hamiltonian, num_qubits, method="no_processing", pruning_condition=0.1) #Set initial alphas for Ansatz #Only 'start_with_initial_state' has been implemented thus far. #This basically sets the state we want to evolve as the random, initial state we are using to generate the E and D matrices, for convenience 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_with_qiskit_circuits( expectation_calculator) #print(E_mat_evaluated)
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)