dwave_networkx.algorithms.markov.sample_markov_network#

sample_markov_network(MN, sampler=None, fixed_variables=None, return_sampleset=False, **sampler_args)[source]#

Samples from a markov network using the provided sampler.

Parameters:
  • G (NetworkX graph) – A Markov Network as returned by 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 dimod.SampleSet rather than a list of samples.

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

Returns:

samples – A list of samples ordered from low-to-high energy or a sample set.

Return type:

list[dict]/dimod.SampleSet

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.