dimod.embedding.weighted_random

weighted_random(sample, embedding)[source]

Determine the sample values of chains by weighed random choice.

Parameters:
  • sample (Mapping) – Sample as a dict of form {t: val, …}, where t is a variable in the target graph and val its associated value as determined by a binary quadratic model sampler.
  • embedding (dict) – Mapping from source graph to target graph as a dict of form {s: {t, …}, …}, where s is a source-model variable and t is a target-model variable.
Yields:

dict – The unembedded sample. When there is a chain break, the value is chosen randomly, weighted by frequency of the chain’s values.

Examples

This example unembeds a sample from a target graph that chains nodes 0 and 1 to represent source node a and nodes 2, 3, and 4 to represent source node b. The sample has broken chains for both source nodes.

>>> import dimod
>>> embedding = {'a': {0, 1}, 'b': {2, 3, 4}}
>>> samples = {0: 1, 1: 0, 2: 1, 3: 0, 4: 1}
>>> next(dimod.embedding.weighted_random(samples, embedding), 'No sample')  
{'a': 0, 'b': 1}