dimod.iter_unembed

iter_unembed(target_samples, embedding, chain_break_method=None)[source]

Yield unembedded samples.

iter_unembed() is an iterator (to reduce memory footprint) used by unembed_response() for unembedding; you may use it directly, for example, to increase performance for responses with large numbers of samples of which you need only a small portion, say those with lowest energy.

Parameters:
  • target_samples (iterable[mapping[variable, value]]) – Iterable of samples. Each sample is a dict of form {t: val, …}, where t is a variable in the target graph and val its associated value.
  • 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.
  • chain_break_method (function, optional, default=:func:.majority_vote) – Method used to resolve chain breaks.
Yields:

dict[variable, value] – An unembedded sample as a dict of form {s: val, …}, where s is a variable in the source graph and val its associated value.

Examples

This example demonstrates the use of iter_unembed() to derive samples for a triangular source graph from synthetic samples of a square target graph.

>>> import dimod
>>> # Synthetic samples from a square-structured target problem
>>> samples = [{0: +1, 1: -1, 2: +1, 3: +1},
...            {0: -1, 1: -1, 2: -1, 3: -1},
...            {0: +1, 1: +1, 2: +1, 3: +1}]
>>> # Embedding from source to target graphs
>>> embedding = {'a': {0}, 'b': {1}, 'c': {2, 3}}
>>> for source_sample in dimod.iter_unembed(samples, embedding):  
...     print(source_sample)
...
{'a': 1, 'b': -1, 'c': 1}
{'a': -1, 'b': -1, 'c': -1}
{'a': 1, 'b': 1, 'c': 1}

This example uses iter_unembed() to unembed samples of a binary quadratic model for a triangular source graph that is embedded in a square-structured graph with dimod reference structured sampler, StructureComposite, using the dimod reference ExactSolver sampler. Note that this flow is used by dwave-system’s EmbeddingComposite.

>>> import dimod
>>> # Binary quadratic model for a triangular source graph
>>> bqm = dimod.BinaryQuadraticModel.from_ising({}, {('a', 'b'): 1, ('b', 'c'): 1, ('a', 'c'): 1})
>>> # Structured dimod sampler with a structure defined by a square graph
>>> sampler = dimod.StructureComposite(dimod.ExactSolver(), [0, 1, 2, 3], [(0, 1), (1, 2), (2, 3), (0, 3)])
>>> # Embedding from source to target graph
>>> embedding = {'a': {0}, 'b': {1}, 'c': {2, 3}}
>>> # Embed the BQM
>>> target_bqm = dimod.embed_bqm(bqm, embedding, sampler.adjacency)
>>> # Sample (the response is in the form of a target structure)
>>> target_response = sampler.sample(target_bqm)
>>> for datum in target_response.data():    
...     print(datum)
...
Sample(sample={0: 1, 1: -1, 2: -1, 3: -1}, energy=-1.0)
Sample(sample={0: 1, 1: 1, 2: -1, 3: -1}, energy=-1.0)
Sample(sample={0: -1, 1: 1, 2: -1, 3: -1}, energy=-1.0)
Sample(sample={0: 1, 1: -1, 2: 1, 3: -1}, energy=-1.0)
>>> # Snipped above response for brevity
>>> # Unembed
>>> source_response = dimod.Response.from_dicts(dimod.iter_unembed(target_response,
...                          embedding),
...                          {'energy': target_response.data_vectors['energy']})
>>> for datum in source_response.data():    
...     print(datum)
...
Sample(sample={'a': 1, 'c': -1, 'b': 1}, energy=-1.0)
Sample(sample={'a': -1, 'c': -1, 'b': 1}, energy=-1.0)
Sample(sample={'a': 1, 'c': 1, 'b': -1}, energy=-1.0)
Sample(sample={'a': -1, 'c': 1, 'b': 1}, energy=-1.0)
>>> # Snipped above response for brevity