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)
def sample(self, bqm, fixed_variables=None, **parameters): """Sample from the provided binary quadratic model. Args: bqm (:obj:`dimod.BinaryQuadraticModel`): Binary quadratic model to be sampled from. fixed_variables (dict): A dictionary of variable assignments. **parameters: Parameters for the sampling method, specified by the child sampler. Returns: :obj:`dimod.SampleSet` """ if not fixed_variables: # None is falsey return self.child.sample(bqm, **parameters) # make sure that we're shapeable and that we have a BQM we can mutate bqm_copy = as_bqm(bqm, cls=[AdjVectorBQM, AdjDictBQM, AdjMapBQM], copy=True) bqm_copy.fix_variables(fixed_variables) sampleset = self.child.sample(bqm_copy, **parameters) def _hook(sampleset): # make RoofDualityComposite non-blocking if sampleset.variables: if len(sampleset): return sampleset.append_variables(fixed_variables) else: return sampleset.from_samples_bqm((np.empty((0, len(bqm))), bqm.variables), bqm=bqm) # there are only fixed variables, make sure that the correct number # of samples are returned samples = [fixed_variables]*max(len(sampleset), 1) return sampleset.from_samples_bqm(samples, bqm=bqm) return SampleSet.from_future(sampleset, _hook)
def sample(self, bqm, **parameters): """Sample from the provided binary quadratic model. Args: bqm (:class:`dimod.BinaryQuadraticModel`): Binary quadratic model to be sampled from. fixed_variables (dict, optional, default=None): A dictionary of variable assignments used when ``self.algorithm`` is 'explicit'. strict (bool, optional, default=True): Only used if ``self.algorithm`` is 'roof_duality'. If True, only fixes variables for which assignments are true for all minimizing points (strong persistency). If False, also fixes variables for which the assignments are true for some but not all minimizing points (weak persistency). **parameters: Parameters for the sampling method, specified by the child sampler. Returns: :class:`dimod.SampleSet` """ if self.algorithm == 'explicit': fixed_variables = parameters.pop('fixed_variables', None) if fixed_variables is None: msg = ( "No fixed_variables passed in when algorithm is 'explicit'. " "Passing problem to child sampler without fixing.") warnings.warn(msg) return self.child.sample(bqm, **parameters) elif self.algorithm == 'roof_duality': fixed_variables = roof_duality(bqm, strict=parameters.pop( 'strict', True)) # make sure that we're shapeable and that we have a BQM we can mutate bqm_copy = as_bqm(bqm, cls=[AdjVectorBQM, AdjDictBQM], copy=True) bqm_copy.fix_variables(fixed_variables) sampleset = self.child.sample(bqm_copy, **parameters) def _hook(sampleset): # make RoofDualityComposite non-blocking if sampleset.variables: if len(sampleset): return append_variables(sampleset, fixed_variables) else: return sampleset.from_samples_bqm((np.empty( (0, len(bqm))), bqm.variables), bqm=bqm) # there are only fixed variables, make sure that the correct number # of samples are returned samples = [fixed_variables] * max(len(sampleset), 1) return sampleset.from_samples_bqm(samples, bqm=bqm) return SampleSet.from_future(sampleset, _hook)