Beispiel #1
0
    def sample_poly(self, poly, fixed_variables=None, **parameters):
        """Sample from the provided binary quadratic model.

        Args:
            poly (:obj:`dimod.BinaryPolynomial`):
                Binary polynomial 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`

        """

        child = self.child
        if fixed_variables is None:
            sampleset = child.sample_poly(poly, **parameters)
            return sampleset
        else:
            poly_copy = fix_variables(poly, fixed_variables)
            sampleset = child.sample_poly(poly_copy, **parameters)
            if len(sampleset):
                return append_variables(sampleset, fixed_variables)
            elif fixed_variables:
                return type(sampleset).from_samples_bqm(fixed_variables,
                                                        bqm=poly)
            else:
                return sampleset
Beispiel #2
0
        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)
    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)