dimod.embed_bqm¶
-
embed_bqm(source_bqm, embedding, target_adjacency, chain_strength=1.0)[source]¶ Embed a binary quadratic model onto a target graph.
Parameters: - source_bqm (
BinaryQuadraticModel) – Binary quadratic model to embed. - 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 variable in the target graph and Nt is its set of neighbours. - chain_strength (float, optional) – Magnitude of the quadratic bias (in SPIN-space) applied between variables to create chains. Note that the energy penalty of chain breaks is 2 * chain_strength.
Returns: Target binary quadratic model.
Return type: 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 >>> # Binary quadratic model for a triangular source graph >>> bqm = dimod.BinaryQuadraticModel.from_ising({}, {('a', 'b'): 1, ('b', 'c'): 1, ('a', 'c'): 1}) >>> # Target graph is a graph >>> target = nx.cycle_graph(4) >>> # Embedding from source to target graphs >>> embedding = {'a': {0}, 'b': {1}, 'c': {2, 3}} >>> # Embed the BQM >>> target_bqm = dimod.embed_bqm(bqm, embedding, target) >>> target_bqm.quadratic[(0, 1)] == bqm.quadratic[('a', 'b')] True >>> target_bqm.quadratic {(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 >>> # 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 >>> response = sampler.sample(target_bqm) >>> response.samples_matrix matrix([[-1, -1, -1, -1], [ 1, -1, -1, -1], [ 1, 1, -1, -1], [-1, 1, -1, -1], [-1, 1, 1, -1], >>> # Snipped above response for brevity
- source_bqm (