def get_next_op_param(self, result): """ method: get_energy_param_lists args: result- the data for the recently calculated result returns: dict with number of energy evaluations required, array of optimized energies for each operator in pool, array of optimized parameters for the energy values in energy array """ if self.parameters_per_step == 1: args = tuple() kwargs = {'hp': self.ham_list, 'he': result['expec list']} Ha_list = list(parallel_map(get_Ha, self._operator_pool.pool, task_kwargs = kwargs, num_processes = len(psutil.Process().cpu_affinity()))) Hc_list = list(parallel_map(get_Hc, self._operator_pool.pool, task_kwargs = kwargs, num_processes = len(psutil.Process().cpu_affinity()))) grads, evals = multi_circuit_eval( result['current_circuit'], self.commutators, qi=self.quantum_instance, drop_dups=self._drop_duplicate_circuits ) #ziplist = list(zip(Hc_list, Ha_list, grads)) ziplist = list(zip(Hc_list, Ha_list, grads)) energy_array = list(parallel_map(get_optimal_array, ziplist, num_processes = len(psutil.Process().cpu_affinity()))) optimal_energy_index = np.argmin(energy_array) #optimal_energy_index = np.where(np.array(energy_array) == np.array(energy_array).min()) #optimal_energy_index = optimal_energy_index[0] #print(optimal_energy_index) #if len(optimal_energy_index) > 1: # entry = np.random.randint(0,len(optimal_energy_index) - 1) # optimal_energy_index = optimal_energy_index[entry] #else: # optimal_energy_index = optimal_energy_index[0] optimal_op = self._operator_pool.pool[optimal_energy_index] optimal_param = -np.arctan2(np.real(Ha_list[optimal_energy_index]),2*np.real(grads[optimal_energy_index][0])) return evals, optimal_op, optimal_param
def post_process_result(self, result, vqe, last_result) -> Dict: result = super().post_process_result(result, vqe, last_result) result['current_ops'] = deepcopy(vqe._var_form._operator_pool) result['expec list'], evals = multi_circuit_eval(result['current_circuit'], self.ham_list, qi = self._operator_selector.quantum_instance) result['num op choice evals'], result['optimal op'], result['optimal param'] = self._operator_selector.get_next_op_param(result) print(result['energy']) if self._compute_hessian: hessian = self._operator_selector._hessian(circuit=result['current_circuit']) else: hessian = None result['hessian'] = hessian return result
def post_process_result(self, result, vqe, last_result) -> Dict: result = super().post_process_result(result, vqe, last_result) result['current_ops'] = deepcopy(vqe._var_form._operator_pool) expec_list, evals = multi_circuit_eval( result['current_circuit'], self.ham_list, qi=self._operator_selector.quantum_instance) result['num op choice evals'] = evals result['Hc_list'], result[ 'Ha_list'] = self.operator_selector.sort_energies(expec_list) if self._compute_hessian: hessian = self._operator_selector._hessian( circuit=result['current_circuit']) else: hessian = None result['hessian'] = hessian return result
def _measure_energies(self, wavefunc, *current_ops): """ method: measure_energies finds the meausred energy for each new operator at pi/2 and -pi/2 args: circuit - the current optimal circuit current_ops - the current optimal operators returns: dictionary with: evaluation energy lists with parameter values at pi/2 and -pi/2 """ #measure energies (theta = 0, theta = pi/4, theta = -pi/4) args = [] kwargs = { 'ham': self._hamiltonian, 'energy_step_tol': self.energy_tol, 'parameter': np.pi / 4 } op_list_pi4 = list( parallel_map(Generate_roto_op, self._operator_pool.pool, args, kwargs, aqua_globals.num_processes)) kwargs['parameter'] = -np.pi / 4 op_list_negpi4 = list( parallel_map(Generate_roto_op, self._operator_pool.pool, args, kwargs, aqua_globals.num_processes)) op_list = op_list_pi4 + op_list_negpi4 del op_list_pi4 del op_list_negpi4 E_list, evals = np.real( multi_circuit_eval(wavefunc, op_list, self.quantum_instance, self._drop_duplicate_circuits)) del op_list E_list, E_list_std = list(zip(*E_list)) cutoff = int(len(E_list) / 2) Energy_pi4 = np.array(E_list[0:cutoff]) Energy_negpi4 = np.array(E_list[cutoff:]) return { 'energy pi4': Energy_pi4, 'energy negpi4': Energy_negpi4, 'num evals': evals }
backend = Aer.get_backend('statevector_simulator') qi = QuantumInstance(backend) pool_info = {'names': [], 'exp vals': []} evals = 0 num_qubits = 4 pool = PauliPool.from_all_pauli_strings( num_qubits) #all possible pauli strings q = QuantumRegister(num_qubits, name='q') circ = Zero(num_qubits).construct_circuit('circuit', q) for op in pool.pool: pool_info['names'].append(op.print_details()) pool_info['exp vals'], evals = multi_circuit_eval(circ, pool.pool, qi, True) for i in range(0, (len(pool_info['exp vals']))): pool_info['exp vals'][i] = complex(pool_info['exp vals'][i][0]) print('evals', evals) out_file = open("Pauli_values.csv", "w+") pool_info_df = pd.DataFrame(pool_info) pool_info_df.to_csv('Pauli_values.csv') out_file.close()