def default_beta_range(self, model): self._check_argument_type("model", model, PhysicalModel) if len(model._raw_interactions[constants.INTERACTION_LINEAR]) == 0 and len(model._raw_interactions[constants.INTERACTION_QUADRATIC]) == 0: raise ValueError("Model cannot be empty.") return neal.default_beta_range(model.to_bqm())
def next(self, state, **runopts): bqm = state.problem # get a reasonable beta range beta_hot, beta_cold = neal.default_beta_range(bqm) # generate betas for all branches/replicas betas = np.geomspace(beta_hot, beta_cold, self.num_replicas) # create num_replicas with betas spaced with geometric progression states = hybrid.States(*[state.updated(beta=b) for b in betas]) return states
def next(self, state, **runopts): bqm = state.problem # get a reasonable beta range beta_hot, beta_cold = neal.default_beta_range(bqm) # generate betas if self.interpolation == 'linear': beta_schedule = np.linspace(beta_hot, beta_cold, self.length) elif self.interpolation == 'geometric': beta_schedule = np.geomspace(beta_hot, beta_cold, self.length) else: raise ValueError("Beta schedule type {} not implemented".format( self.interpolation)) # store the schedule in output state return state.updated(beta_schedule=beta_schedule)
def test_default_beta_range(self): bqm = dimod.BinaryQuadraticModel.from_ising({'a': 1}, {'bc': 1}) self.assertEqual(neal.default_beta_range(bqm), neal.default_beta_range(bqm.binary))
bqm = dimod.BinaryQuadraticModel.from_coo(fp) print("BQM: {} nodes, {} edges, {:.2f} density".format( len(bqm), len(bqm.quadratic), hybrid.bqm_density(bqm))) # PT workflow, one approach: store temperatures/betas in states n_sweeps = 10000 n_replicas = 10 n_iterations = 10 # states are randomly initialized state = hybrid.State.from_problem(bqm) # get a reasonable beta range beta_hot, beta_cold = neal.default_beta_range(bqm) # generate betas for all branches/replicas betas = np.geomspace(beta_hot, beta_cold, n_replicas) # create n_replicas with geometric distribution of betas (inverse temperature) replicas = hybrid.States(*[state.updated(beta=b) for b in betas]) # run replicas update/swap for n_iterations # (after each update/sampling step, do n_replicas-1 swap operations) update = hybrid.Map(FixedTemperatureSampler(num_sweeps=n_sweeps)) swap = SwapReplicasDownsweep() workflow = hybrid.Loop(update | swap, max_iter=n_iterations) \ | hybrid.MergeSamples(aggregate=True) solution = workflow.run(replicas).result()