dimod.embed_ising

embed_ising(souce_h, source_J, embedding, target_adjacency, chain_strength=1.0)[source]

Embed an Ising problem onto a target graph.

Parameters:
  • source_h (dict[variable, bias]/list[bias]) – Linear biases of the Ising problem. If a list, the list’s indices are used as variable labels.
  • source_J (dict[(variable, variable), bias]) – Quadratic biases of the Ising problem.
  • 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.
  • target_adjacency (dict/networkx.Graph) – Adjacency of the target graph as a dict of form {t: Nt, …}, where t is a target-graph variable and Nt is its set of neighbours.
  • chain_strength (float, optional) – Magnitude of the quadratic bias (in SPIN-space) applied between variables to form a chain. Note that the energy penalty of chain breaks is 2 * chain_strength.
Returns:

A 2-tuple:

dict[variable, bias]: Linear biases of the target Ising problem.

dict[(variable, variable), bias]: Quadratic biases of the target Ising problem.

Return type:

tuple

Examples

This example embeds a fully connected \(K_3\) graph onto a square target graph. Embedding is accomplished by an edge contraction operation on the target graph: target-nodes 2 and 3 are chained to represent source-node c.

>>> import dimod
>>> import networkx as nx
>>> # Ising problem for a triangular source graph
>>> h = {}
>>> J = {('a', 'b'): 1, ('b', 'c'): 1, ('a', 'c'): 1}
>>> # Target graph is a square graph
>>> target = nx.cycle_graph(4)
>>> # Embedding from source to target graph
>>> embedding = {'a': {0}, 'b': {1}, 'c': {2, 3}}
>>> # Embed the Ising problem
>>> target_h, target_J = dimod.embed_ising(h, J, embedding, target)
>>> target_J[(0, 1)] == J[('a', 'b')]
True
>>> target_J        
{(0, 1): 1.0, (0, 3): 1.0, (1, 2): 1.0, (2, 3): -1.0}

This example embeds a fully connected \(K_3\) graph onto the target graph of a dimod reference structured sampler, StructureComposite, using the dimod reference ExactSolver sampler with a square graph specified. Target-nodes 2 and 3 are chained to represent source-node c.

>>> import dimod
>>> # Ising problem for a triangular source graph
>>> h = {}
>>> J = {('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 Ising problem
>>> target_h, target_J = dimod.embed_ising(h, J, embedding, sampler.adjacency)
>>> # Sample
>>> response = sampler.sample_ising(target_h, target_J)
>>> for sample in response.samples(n=3, sorted_by='energy'):   
...     print(sample)
...
{0: 1, 1: -1, 2: -1, 3: -1}
{0: 1, 1: 1, 2: -1, 3: -1}
{0: -1, 1: 1, 2: -1, 3: -1}