コード例 #1
0
    def test_sampleset1_append1(self):
        sampleset = dimod.SampleSet.from_samples({'a': -1, 'b': 1}, dimod.SPIN, energy=0)
        new = dimod.append_variables(sampleset, {'c': -1, 'd': -1})

        target = dimod.SampleSet.from_samples({'a': -1, 'b': 1, 'c': -1, 'd': -1}, dimod.SPIN, energy=0)

        self.assertEqual(new, target)
コード例 #2
0
    def test_two_samplesets(self):
        sampleset0 = dimod.SampleSet.from_samples([{'a': -1, 'b': 1}, {'a': -1, 'b': -1}],
                                                  dimod.SPIN, energy=[-2, 2])
        sampleset1 = dimod.SampleSet.from_samples([{'c': -1, 'd': 1}, {'c': -1, 'd': -1}],
                                                  dimod.SPIN, energy=[-1, 1])

        target = dimod.SampleSet.from_samples([{'a': -1, 'b': 1, 'c': -1, 'd': 1},
                                               {'a': -1, 'b': -1, 'c': -1, 'd': -1}],
                                              dimod.SPIN, energy=[-2, 2])

        self.assertEqual(dimod.append_variables(sampleset0, sampleset1), target)
コード例 #3
0
    def test_overlapping_variables(self):
        sampleset = dimod.SampleSet.from_samples([{'a': -1, 'b': 1}, {'a': -1, 'b': -1}],
                                                 dimod.SPIN, energy=0)

        with self.assertRaises(ValueError):
            dimod.append_variables(sampleset, [{'c': -1, 'd': -1, 'a': -1}])
コード例 #4
0
def sample_markov_network(MN,
                          sampler=None,
                          fixed_variables=None,
                          return_sampleset=False,
                          **sampler_args):
    """Samples from a markov network using the provided sampler.

    Parameters
    ----------
    G : NetworkX graph
        A Markov Network as returned by :func:`.markov_network`

    sampler
        A binary quadratic model sampler. A sampler is a process that
        samples from low energy states in models defined by an Ising
        equation or a Quadratic Unconstrained Binary Optimization
        Problem (QUBO). A sampler is expected to have a 'sample_qubo'
        and 'sample_ising' method. A sampler is expected to return an
        iterable of samples, in order of increasing energy. If no
        sampler is provided, one must be provided using the
        `set_default_sampler` function.

    fixed_variables : dict
        A dictionary of variable assignments to be fixed in the markov network.

    return_sampleset : bool (optional, default=False)
        If True, returns a :obj:`dimod.SampleSet` rather than a list of samples.

    **sampler_args
        Additional keyword parameters are passed to the sampler.

    Returns
    -------
    samples : list[dict]/:obj:`dimod.SampleSet`
        A list of samples ordered from low-to-high energy or a sample set.

    Examples
    --------

    >>> import dimod
    ...
    >>> potentials = {('a', 'b'): {(0, 0): -1,
    ...                            (0, 1): .5,
    ...                            (1, 0): .5,
    ...                            (1, 1): 2}}
    >>> MN = dnx.markov_network(potentials)
    >>> sampler = dimod.ExactSolver()
    >>> samples = dnx.sample_markov_network(MN, sampler)
    >>> samples[0]     # doctest: +SKIP
    {'a': 0, 'b': 0}

    >>> import dimod
    ...
    >>> potentials = {('a', 'b'): {(0, 0): -1,
    ...                            (0, 1): .5,
    ...                            (1, 0): .5,
    ...                            (1, 1): 2}}
    >>> MN = dnx.markov_network(potentials)
    >>> sampler = dimod.ExactSolver()
    >>> samples = dnx.sample_markov_network(MN, sampler, return_sampleset=True)
    >>> samples.first       # doctest: +SKIP
    Sample(sample={'a': 0, 'b': 0}, energy=-1.0, num_occurrences=1)

    >>> import dimod
    ...
    >>> potentials = {('a', 'b'): {(0, 0): -1,
    ...                            (0, 1): .5,
    ...                            (1, 0): .5,
    ...                            (1, 1): 2},
    ...               ('b', 'c'): {(0, 0): -9,
    ...                            (0, 1): 1.2,
    ...                            (1, 0): 7.2,
    ...                            (1, 1): 5}}
    >>> MN = dnx.markov_network(potentials)
    >>> sampler = dimod.ExactSolver()
    >>> samples = dnx.sample_markov_network(MN, sampler, fixed_variables={'b': 0})
    >>> samples[0]           # doctest: +SKIP
    {'a': 0, 'c': 0, 'b': 0}

    Notes
    -----
    Samplers by their nature may not return the optimal solution. This
    function does not attempt to confirm the quality of the returned
    sample.

    """

    bqm = markov_network_bqm(MN)

    if fixed_variables:
        # we can modify in-place since we just made it
        bqm.fix_variables(fixed_variables)

    sampleset = sampler.sample(bqm, **sampler_args)

    if fixed_variables:
        # add the variables back in
        sampleset = dimod.append_variables(sampleset, fixed_variables)

    if return_sampleset:
        return sampleset
    else:
        return list(map(dict, sampleset.samples()))