def hubo_simulated_annealing(bhom: BinaryHigherOrderModel, state: list, schedule: list, var_type): adj_dict = bhom.adj_dict() state = np.array(state) if SPIN == cast_var_type(var_type): for beta, mc_steps in schedule: for _ in range(mc_steps): for i in bhom.indices: de = -2 * state[i] * np.sum([ d * np.prod(state[_inds]) for _inds, d in adj_dict[i] ]) de += -2 * state[i] * bhom.interactions[0][i] if de < 0 or np.random.uniform(0, 1) < np.exp(-beta * de): state[i] *= -1 return state elif BINARY == cast_var_type(var_type): for beta, mc_steps in schedule: for _ in range(mc_steps): for i in bhom.indices: de = (1 - 2 * state[i]) * np.sum([ d * np.prod(state[_inds]) for _inds, d in adj_dict[i] ]) de += (1 - 2 * state[i]) * bhom.interactions[0][i] if de <= 0 or np.random.uniform(0, 1) < np.exp(-beta * de): state[i] = 0 if state[i] == 1 else 1 return state else: raise ValueError("var_type should be SPIN or BINARY")
def sample_hubo(self, interactions: list, var_type, beta_min=None, beta_max=None, schedule=None, num_sweeps=100, num_reads=1, init_state=None, seed=None): """sampling from higher order unconstrainted binary optimization Args: interactions (list of dict): ordered by degree of interaction. [linear, quadratic, ...] var_type (str, openjij.VarType): "SPIN" or "BINARY" beta_min (float, optional): Minimum beta (initial inverse temperature). Defaults to None. beta_max (float, optional): Maximum beta (final inverse temperature). Defaults to None. schedule (list, optional): [description]. Defaults to None. num_sweeps (int, optional): [description]. Defaults to 100. num_reads (int, optional): [description]. Defaults to 1. init_state (list, optional): initial state. Defaults to None. seed (int, optional): [description]. Defaults to None. Returns: [type]: [description] """ self._setting_overwrite(beta_min=beta_min, beta_max=beta_max, num_sweeps=num_sweeps, num_reads=num_reads) bhom = BinaryHigherOrderModel(interactions) if schedule or self.schedule: self._schedule = self._convert_validation_schedule( schedule if schedule else self.schedule) self.schedule_info = {'schedule': 'custom schedule'} else: schedule = default_schedule( bhom, beta_min=self._schedule_setting['beta_min'], beta_max=self._schedule_setting['beta_max'], num_sweeps=self._schedule_setting['num_sweeps']) self.schedule_info = { 'beta_max': schedule[-1][0], 'beta_min': schedule[0][0], 'num_sweeps': self._schedule_setting['num_sweeps'] } response = hubo_sa_sampling( bhom, var_type, schedule=schedule, schedule_info=self.schedule_info, num_sweeps=self._schedule_setting['num_sweeps'], num_reads=self._schedule_setting['num_reads'], init_state=init_state, seed=seed) return response
def sample_hubo(self, interactions: list, var_type, beta_min=None, beta_max=None, schedule=None, num_sweeps=100, num_reads=1, init_state=None, seed=None): """sampling from higher order unconstrainted binary optimization Args: interactions (list of dict): ordered by degree of interaction. [linear, quadratic, ...] var_type (str, openjij.VarType): "SPIN" or "BINARY" beta_min (float, optional): Minimum beta (initial inverse temperature). Defaults to None. beta_max (float, optional): Maximum beta (final inverse temperature). Defaults to None. schedule (list, optional): [description]. Defaults to None. num_sweeps (int, optional): [description]. Defaults to 100. num_reads (int, optional): [description]. Defaults to 1. init_state (list, optional): initial state. Defaults to None. seed (int, optional): [description]. Defaults to None. Returns: [type]: [description] """ self._setting_overwrite(beta_min=beta_min, beta_max=beta_max, num_sweeps=num_sweeps, num_reads=num_reads) bhom = BinaryHigherOrderModel(interactions) if schedule or self.schedule: self._schedule = self._convert_validation_schedule( schedule if schedule else self.schedule) self.schedule_info = {'schedule': 'custom schedule'} else: schedule = default_schedule( bhom, beta_min=self._schedule_setting['beta_min'], beta_max=self._schedule_setting['beta_max'], num_sweeps=self._schedule_setting['num_sweeps']) self.schedule_info = { 'beta_max': schedule[-1][0], 'beta_min': schedule[0][0], 'num_sweeps': self._schedule_setting['num_sweeps'] } init_state = init_state if init_state else np.random.choice( [1, -1], len(bhom.indices)) response = openjij.Response(var_type=var_type, indices=bhom.indices) execution_time = [] @measure_time def exec_sampling(): for _ in range(num_reads): _exec_time, state = measure_time(hubo_simulated_annealing)( bhom, init_state, schedule, var_type=var_type) execution_time.append(_exec_time) response.states.append(state) response.energies.append(bhom.calc_energy(state)) sampling_time, _ = exec_sampling() response.info['sampling_time'] = sampling_time * 10**6 # micro sec response.info['execution_time'] = np.mean( execution_time) * 10**6 # micro sec response.info['list_exec_times'] = np.array( execution_time) * 10**6 # micro sec response.update_ising_states_energies(response.states, response.energies) response.info['schedule'] = self.schedule_info return response
def sample_hubo(self, interactions: list, var_type, beta_min=None, beta_max=None, schedule=None, num_sweeps=100, num_reads=1, init_state=None, seed=None): """sampling from higher order unconstrainted binary optimization. Args: interactions (list): ordered by degree of interaction. var_type (str, openjij.VarType): "SPIN" or "BINARY" beta_min (float, optional): Minimum beta (initial inverse temperature). Defaults to None. beta_max (float, optional): Maximum beta (final inverse temperature). Defaults to None. schedule (list, optional): schedule list. Defaults to None. num_sweeps (int, optional): number of sweeps. Defaults to 100. num_reads (int, optional): number of reads. Defaults to 1. init_state (list, optional): initial state. Defaults to None. seed (int, optional): [description]. Defaults to None. Returns: :class:`openjij.sampler.response.Response`: results Examples:: >>> sampler = oj.SASampler() >>> h = {0: -1} >>> J = {(0, 1): -1} >>> K = {(0, 1, 2): 1} >>> response = sampler.sample_hubo([h, J, K], var_type="SPIN") """ self._setting_overwrite(beta_min=beta_min, beta_max=beta_max, num_sweeps=num_sweeps, num_reads=num_reads) bhom = BinaryHigherOrderModel(interactions) if schedule or self.schedule: self._schedule = self._convert_validation_schedule( schedule if schedule else self.schedule) self.schedule_info = {'schedule': 'custom schedule'} else: schedule = default_schedule( bhom, beta_min=self._schedule_setting['beta_min'], beta_max=self._schedule_setting['beta_max'], num_sweeps=self._schedule_setting['num_sweeps']) self.schedule_info = { 'beta_max': schedule[-1][0], 'beta_min': schedule[0][0], 'num_sweeps': self._schedule_setting['num_sweeps'] } response = hubo_sa_sampling( bhom, var_type, schedule=schedule, schedule_info=self.schedule_info, num_sweeps=self._schedule_setting['num_sweeps'], num_reads=self._schedule_setting['num_reads'], init_state=init_state, seed=seed) return response