iter_count += 1 print('New iteration ', iter_count) previous_energy = current_energy element_to_add, element_result = EnergyUtils.\ largest_individual_vqe_energy_reduction_elements(vqe_runner_2, ansatz_element_pool, ansatz=ansatz, ansatz_parameters=var_parameters, excited_state=excited_state, global_cache=global_cache) element_energy_reduction = element_result.fun print(element_to_add.element) result = vqe_runner.vqe_run(ansatz=ansatz + [element_to_add], init_guess_parameters=var_parameters + list(element_result.x), excited_state=excited_state, cache=global_cache) current_energy = result.fun delta_e = previous_energy - current_energy var_parameters = list(result.x) if delta_e > 0: ansatz.append(element_to_add) # save iteration data try: element_qubits = element_to_add.qubits except AttributeError: element_qubits = []
ansatz = state.ansatz_elements var_parameters = state.parameters ansatz = ansatz # var_parameters = list(df['var_parameters'])[:49] var_parameters = var_parameters global_cache = GlobalCache(molecule) global_cache.calculate_exc_gen_sparse_matrices_dict(ansatz) global_cache.calculate_commutators_sparse_matrices_dict(ansatz) optimizer = 'BFGS' optimizer_options = {'gtol': 1e-8} vqe_runner = VQERunner(molecule, backend=QiskitSimBackend, optimizer=optimizer, optimizer_options=None, print_var_parameters=False, use_ansatz_gradient=True) energy = vqe_runner.vqe_run(ansatz=ansatz, init_guess_parameters=var_parameters, init_state_qasm=None, cache=global_cache) # ansatz_grad = QiskitSimBackend.ansatz_gradient(var_parameters, ansatz, molecule, cache=global_cache) print(energy) # print(ansatz_grad)
init_ansatz_length = len(ansatz_elements) while previous_energy - current_energy >= delta_e_threshold and iter_count <= max_ansatz_elements: iter_count += 1 print('New iteration ', iter_count) previous_energy = current_energy element_to_add, grad = GradientUtils.\ get_largest_gradient_elements(ansatz_element_pool, molecule, backend=vqe_runner.backend, ansatz_parameters=ansatz_parameters, ansatz=ansatz_elements, global_cache=global_cache)[0] print(element_to_add.element) result = vqe_runner.vqe_run(ansatz=ansatz_elements + [element_to_add], init_guess_parameters=ansatz_parameters + [0], cache=global_cache) current_energy = result.fun delta_e = previous_energy - current_energy # get initial guess for the var. params. for the next iteration ansatz_parameters = list(result.x) if delta_e > 0: ansatz_elements.append(element_to_add) # save iteration data element_qubits = element_to_add.qubits gate_count = IterVQEQasmUtils.gate_count_from_ansatz(ansatz_elements, molecule.n_orbitals)
AnsatzElement.spin_complement_orbital(qubit_pair_2[0]), AnsatzElement.spin_complement_orbital(qubit_pair_2[1]) ] if set(qubit_pair_1) != set(comp_qubit_pair_1) or set( qubit_pair_2) != set(comp_qubit_pair_2): compl_element_to_add = get_spin_compl_exc( element_to_add.qubit_pair_1, element_to_add.qubit_pair_2, ansatz_element_type, molecule.n_orbitals) print('Add complement element: ', compl_element_to_add.element) else: raise Exception('Wrong element order ..') if compl_element_to_add is None: result = vqe_runner.vqe_run( ansatz=ansatz_elements + [element_to_add], init_guess_parameters=var_parameters + [0]) else: result = vqe_runner.vqe_run(ansatz=ansatz_elements + [element_to_add, compl_element_to_add], init_guess_parameters=var_parameters + [0, 0]) current_energy = result.fun delta_e = previous_energy - current_energy # get initial guess for the var. params. for the next iteration var_parameters = list(result.x) if delta_e > 0: ansatz_elements.append(element_to_add)
LogUtils.log_config() # Create a UCCSD ansatz for the specified molecule ansatz = UCCSDExcitations(q_system.n_orbitals, q_system.n_electrons, 'f_exc').get_all_elements() # choose a backend to calculate expectation values backend = MatrixCacheBackend # create a cache of precomputed values to accelerate the simulation (optional) global_cache = GlobalCache(q_system) global_cache.calculate_exc_gen_sparse_matrices_dict(ansatz) # global_cache.calculate_commutators_sparse_matrices_dict(ansatz) # Create a VQE runner, and specify the minimizer optimizer = 'BFGS' optimizer_options = {'gtol': 10e-8} vqe_runner = VQERunner(q_system, backend=backend, print_var_parameters=False, use_ansatz_gradient=True, optimizer=optimizer, optimizer_options=optimizer_options) t0 = time.time() result = vqe_runner.vqe_run(ansatz=ansatz, cache=global_cache) # initial_var_parameters=var_parameters) t = time.time() logging.critical(result) print(result) print('Time for running: ', t - t0) print('Pizza')
ansatz = ansatz_1() init_qasm = None global_cache = GlobalCache(e_system, excited_state=0) global_cache.calculate_exc_gen_sparse_matrices_dict(ansatz) backend = MatrixCacheBackend optimizer = 'BFGS' optimizer_options = {'gtol': 10e-8, 'maxiter': 10} vqe_runner = VQERunner(e_system, backend=backend, print_var_parameters=False, use_ansatz_gradient=True, optimizer=optimizer, optimizer_options=optimizer_options) result = vqe_runner.vqe_run(ansatz=ansatz, cache=global_cache, init_state_qasm=init_qasm) parameters = result.x statevector = global_cache.get_statevector(ansatz, parameters, init_state_qasm=init_qasm) statevector = statevector.todense() operator = 'elenaananana' # TODO: TOVA trqbva da e klas openfermion.FermionicOperator operator = openfermion.jordan_wigner(operator) operator = openfermion.get_sparse_operator(operator, n_orbitals) expectation_value = statevector.dot(operator).dot(statevector.conj().transpose()) print(expectation_value) print('yolo')
rs = [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3] df_count = 0 for r in rs: molecule = H6(r=r) vqe_runner = VQERunner(molecule, backend=MatrixCacheBackend, use_ansatz_gradient=True, optimizer=optimizer, optimizer_options=optimizer_options) global_cache = GlobalCache(molecule) global_cache.calculate_exc_gen_sparse_matrices_dict(ansatz) # result = vqe_runner.vqe_run(ansatz, var_parameters, cache=global_cache) result = vqe_runner.vqe_run(ansatz, cache=global_cache) del global_cache fci_E = molecule.fci_energy fci_energies.append(fci_E) # next var parameters if len(ansatz) == 0: var_parameters = [] E = result n_iters = 1 else: var_parameters = list(result.x) E = result.fun n_iters = result.nfev
time_stamp, ansatz_element_type=ansatz_element_type, frozen_els=frozen_els) message = 'Add new element to final ansatz {}. Energy {}. Energy change {}, var. parameters: {}' \ .format(element_to_add.element, current_energy, delta_e, var_parameters) logging.info(message) print(message) else: message = 'No contribution to energy decrease. Stop adding elements to the final ansatz' logging.info(message) print(message) break print('Added element ', ansatz_elements[-1].element) # save data. Not required? save_data(df_data, molecule, time_stamp, ansatz_element_type=ansatz_element_type) # calculate the VQE for the final ansatz vqe_runner_final = VQERunner(molecule, backend=QiskitSimBackend, ansatz=ansatz_elements) final_result = vqe_runner_final.vqe_run(ansatz=ansatz_elements) t = time.time() print(final_result) print('Ciao')
molecule = LiH(r=r) ground_state = DataUtils.ansatz_from_data_frame(gs_dfs[j], molecule) molecule.H_lower_state_terms = [[ abs(molecule.hf_energy) * 2, ground_state ]] vqe_runner = VQERunner(molecule, backend=MatrixCacheBackend, use_ansatz_gradient=True, optimizer=optimizer, optimizer_options=optimizer_options) global_cache = GlobalCache(molecule, excited_state=excited_state) global_cache.calculate_exc_gen_sparse_matrices_dict(ansatz) result = vqe_runner.vqe_run(ansatz, var_parameters, cache=global_cache, excited_state=excited_state) del global_cache exact_E = molecule.calculate_energy_eigenvalues(excited_state + 1)[excited_state] exact_energies.append(exact_E) # next var parameters if len(ansatz) == 0: var_parameters = [] E = result n_iters = 1 else: var_parameters = list(result.x)
df_count += 1 results_data_frame['var_parameters'] = list( result.x)[:df_count] # save data DataUtils.save_data(results_data_frame, molecule, time_stamp, ansatz_element_type=ansatz_element_type, frozen_els=frozen_els, iter_vqe_type='exc_iqeb') message = 'Add new element to final ansatz {}. Energy {}. Energy change {}, var. parameters: {}' \ .format(element_to_add.element, current_energy, delta_e, ansatz_parameters) logging.info(message) else: message = 'No contribution to energy decrease. Stop adding elements to the final ansatz' logging.info(message) break print('Added element ', ansatz[-1].element) # calculate the VQE for the final ansatz final_result = vqe_runner.vqe_run(ansatz=ansatz, cache=global_cache, excited_state=excited_state) t = time.time() print(final_result) print('Ciao')