def test_embed_bqm_BINARY(self): Q = { ('a', 'a'): 0, ('a', 'b'): -1, ('b', 'b'): 0, ('c', 'c'): 0, ('b', 'c'): -1 } bqm = dimod.BinaryQuadraticModel.from_qubo(Q) embedding = {'a': {0}, 'b': {1}, 'c': {2, 3}} embedded_bqm = dwave.embedding.embed_bqm(bqm, embedding, nx.cycle_graph(4), chain_strength=1) # check that the energy has been preserved for config in itertools.product((0, 1), repeat=3): sample = dict(zip(('a', 'b', 'c'), config)) target_sample = { u: sample[v] for v, chain in embedding.items() for u in chain } # no chains broken self.assertAlmostEqual(bqm.energy(sample), embedded_bqm.energy(target_sample))
def sample(self, bqm, **kwargs): """Sample from the specified binary quadratic model. Args: bqm (:obj:`dimod.BinaryQuadraticModel`): Binary quadratic model to be sampled from. **kwargs: Optional keyword arguments for the sampling method, specified per solver. Returns: :class:`dimod.SampleSet` Examples: This example submits a simple Ising problem of just two variables on a D-Wave system. Because the problem fits in a single :term:`Chimera` unit cell, it is tiled across the solver's entire Chimera graph, resulting in multiple samples (the exact number depends on the working Chimera graph of the D-Wave system). >>> from dwave.system import DWaveSampler, EmbeddingComposite >>> from dwave.system import TilingComposite ... >>> qpu_2000q = DWaveSampler(solver={'topology__type': 'chimera'}) >>> sampler = EmbeddingComposite(TilingComposite(qpu_2000q, 1, 1, 4)) >>> response = sampler.sample_ising({},{('a', 'b'): 1}) >>> len(response) # doctest: +SKIP 246 See `Ocean Glossary <https://docs.ocean.dwavesys.com/en/stable/concepts/index.html>`_ for explanations of technical terms in descriptions of Ocean tools. """ # apply the embeddings to the given problem to tile it across the child sampler embedded_bqm = dimod.BinaryQuadraticModel.empty(bqm.vartype) __, __, target_adjacency = self.child.structure for embedding in self.embeddings: embedded_bqm.update( dwave.embedding.embed_bqm(bqm, embedding, target_adjacency)) # solve the problem on the child system tiled_response = self.child.sample(embedded_bqm, **kwargs) responses = [] for embedding in self.embeddings: embedding = { v: chain for v, chain in embedding.items() if v in bqm.variables } responses.append( dwave.embedding.unembed_sampleset(tiled_response, embedding, bqm)) answer = dimod.concatenate(responses) answer.info.update(tiled_response.info) return answer
def test_embedding_with_extra_chains(self): embedding = {0: [0, 1], 1: [2], 2: [3]} G = nx.cycle_graph(4) bqm = dimod.BinaryQuadraticModel.from_qubo({(0, 0): 1}) target_bqm = dwave.embedding.embed_bqm(bqm, embedding, G) for v, c in embedding.items(): if v in bqm.variables: for q in c: self.assertIn(q, target_bqm) else: for q in c: self.assertNotIn(q, target_bqm)
def test_embed_bqm_NAE3SAT_to_square(self): h = {'a': 0, 'b': 0, 'c': 0} J = {('a', 'b'): 1, ('b', 'c'): 1, ('a', 'c'): 1} bqm = dimod.BinaryQuadraticModel.from_ising(h, J) embedding = {'a': {0}, 'b': {1}, 'c': {2, 3}} embedded_bqm = dwave.embedding.embed_bqm(bqm, embedding, nx.cycle_graph(4), chain_strength=1) self.assertEqual( embedded_bqm, dimod.BinaryQuadraticModel( { 0: 0, 1: 0, 2: 0, 3: 0 }, { (0, 1): 1, (1, 2): 1, (2, 3): -1, (0, 3): 1 }, 1.0, # offset the energy from satisfying chains dimod.SPIN)) # check that the energy has been preserved for config in itertools.product((-1, 1), repeat=3): sample = dict(zip(('a', 'b', 'c'), config)) target_sample = { u: sample[v] for v, chain in embedding.items() for u in chain } # no chains broken self.assertAlmostEqual(bqm.energy(sample), embedded_bqm.energy(target_sample))