def _sample(*args, **kwargs): # avoid circular import from dimod.sampleset import SampleSet iterator = f(*args, **kwargs) # do the blocking part next(iterator) return SampleSet.from_future(None, lambda _: next(iterator))
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(*args, **kwargs): iterator = f(*args, **kwargs) # resolve blocking part now, and make hook for the non-blocking part return SampleSet.from_future(next(iterator), lambda _: next(iterator))
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)