def _get_iter_components(self, bqm): components = connected_components(bqm) if self.rolling and self.key: return iter(sorted(components, key=self.key, reverse=self.reverse)) else: return components
def sample(self, bqm, *, components=None, **parameters): """Sample from the provided binary quadratic model. Args: bqm (:class:`dimod.BinaryQuadraticModel`): Binary quadratic model to be sampled from. components (list(set)): A list of disjoint set of variables that fully partition the variables **parameters: Parameters for the sampling method, specified by the child sampler. Returns: :class:`dimod.SampleSet` """ # make sure the BQM is shapeable bqm = as_bqm(bqm, cls=[AdjVectorBQM, AdjDictBQM]) # solve the problem on the child system child = self.child variables = bqm.variables if components is None: components = list(connected_components(bqm)) if isinstance(components, set): components = [components] sampleset = None fixed_value = min(bqm.vartype.value) for component in components: bqm_copy = bqm.copy() bqm_copy.fix_variables( {i: fixed_value for i in (variables - component)}) if sampleset is None: # here .truncate(1) is used to pick the best solution only. The other options # for future development is to combine all sample with all. # This way you'd get the same behaviour as the ExactSolver sampleset = child.sample(bqm_copy, **parameters).truncate(1) else: sampleset = append_variables( sampleset.truncate(1), child.sample(bqm_copy, **parameters).truncate(1)) if sampleset is None: return SampleSet.from_samples_bqm({}, bqm) else: return SampleSet.from_samples_bqm(sampleset, bqm)