Beispiel #1
0
def sample_markov_network(MN, sampler=None, fixed_variables=None,
                          return_sampleset=False,
                          **sampler_args):


    bqm = markov_network_bqm(MN)

    # use the FixedVar
    fv_sampler = dimod.FixedVariableComposite(sampler)

    sampleset = fv_sampler.sample(bqm, fixed_variables=fixed_variables,
                                  **sampler_args)

    if return_sampleset:
        return sampleset
    else:
        return list(map(dict, sampleset.samples()))
Beispiel #2
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]
    {'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
    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]
    {'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)

    # use the FixedVar
    fv_sampler = dimod.FixedVariableComposite(sampler)

    sampleset = fv_sampler.sample(bqm, fixed_variables=fixed_variables,
                                  **sampler_args)

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