Exemplo n.º 1
0
    def test_smoke(self):
        child_sampler = MockSampler()
        sampler = VirtualGraphComposite(child_sampler, {'a': [0]},
                                        flux_bias_num_reads=1)

        # depending on how recenlty flux bias data was gathered, this may be true
        child_sampler.flux_biases_flag = False

        if sampler.flux_biases:
            sampler.sample_ising({'a': -1}, {})
            self.assertTrue(
                child_sampler.flux_biases_flag
            )  # true when some have been provided to sample_ising
Exemplo n.º 2
0
    def test_instantiation_triangle(self):
        sampler = FixedEmbeddingComposite(MockSampler(), {'a': [0, 4], 'b': [1, 5], 'c': [2, 6]})

        dtest.assert_sampler_api(sampler)  # checks adj consistent with nodelist/edgelist

        self.assertEqual(sampler.nodelist, ['a', 'b', 'c'])
        self.assertEqual(sampler.edgelist, [('a', 'b'), ('a', 'c'), ('b', 'c')])
    def test_max_cut(self):
        sampler = EmbeddingComposite(MockSampler())

        m = 2
        n = 2
        t = 2

        hoff = 2 * t
        voff = n * hoff
        mi = m * voff
        ni = n * hoff

        edges = []

        # tile edges
        edges.extend((k0, k1) for i in range(0, ni, hoff)
                     for j in range(i, mi, voff) for k0 in range(j, j + t)
                     for k1 in range(j + t, j + 2 * t))
        # horizontal edges
        edges.extend((k, k + hoff) for i in range(t, 2 * t)
                     for j in range(i, ni - hoff, hoff)
                     for k in range(j, mi, voff))
        # vertical edges
        edges.extend((k, k + voff) for i in range(t)
                     for j in range(i, ni, hoff)
                     for k in range(j, mi - voff, voff))

        J = {edge: 1 for edge in edges}
        h = {v: 0 for v in set().union(*J)}

        response = sampler.sample_ising(h, J)
Exemplo n.º 4
0
    def test_singleton_variables(self):
        sampler = EmbeddingComposite(MockSampler())

        h = {0: -1., 4: 2}
        J = {}

        response = sampler.sample_ising(h, J)

        # nothing failed and we got at least one response back
        self.assertGreaterEqual(len(response), 1)
Exemplo n.º 5
0
    def test_sample_ising(self):
        mock_sampler = MockSampler()  # C4 structured sampler

        sampler = TilingComposite(mock_sampler, 2, 2)

        h = {
            node: random.uniform(-1, 1)
            for node in sampler.structure.nodelist
        }
        J = {(u, v): random.uniform(-1, 1)
             for u, v in sampler.structure.edgelist}

        response = sampler.sample_ising(h, J)
Exemplo n.º 6
0
    def test_too_many_nodes(self):
        mock_sampler = MockSampler()  # C4 structured sampler

        sampler = TilingComposite(mock_sampler, 2, 2)

        h = {0: -1, 1: 1}
        J = {}

        response = sampler.sample_ising(h, J)

        __, num_columns = response.samples_matrix.shape

        self.assertEqual(num_columns, 2)
    def test_sample_qubo(self):
        sampler = EmbeddingComposite(MockSampler())

        Q = {(0, 0): .1, (0, 4): -.8, (4, 4): 1}

        response = sampler.sample_qubo(Q)

        # nothing failed and we got at least one response back
        self.assertGreaterEqual(len(response), 1)

        for sample in response.samples():
            for u, v in Q:
                self.assertIn(v, sample)
                self.assertIn(u, sample)

        for sample, energy in response.data(['sample', 'energy']):
            self.assertAlmostEqual(dimod.qubo_energy(sample, Q), energy)
    def test_sample_ising_unstructured_not_integer_labelled(self):
        sampler = EmbeddingComposite(MockSampler())

        h = {'a': -1., 'b': 2}
        J = {('a', 'b'): 1.5}

        response = sampler.sample_ising(h, J)

        # nothing failed and we got at least one response back
        self.assertGreaterEqual(len(response), 1)

        for sample in response.samples():
            for v in h:
                self.assertIn(v, sample)

        for sample, energy in response.data(['sample', 'energy']):
            self.assertAlmostEqual(dimod.ising_energy(sample, h, J), energy)
    def test_sample_ising(self):
        sampler = EmbeddingComposite(MockSampler())

        h = {0: -1., 4: 2}
        J = {(0, 4): 1.5}

        response = sampler.sample_ising(h, J)

        # nothing failed and we got at least one response back
        self.assertGreaterEqual(len(response), 1)

        for sample in response.samples():
            self.assertIsInstance(sample, Mapping)
            self.assertEqual(set(sample), set(h))

        for sample, energy in response.data(['sample', 'energy']):
            self.assertIsInstance(sample, Mapping)
            self.assertEqual(set(sample), set(h))
            self.assertAlmostEqual(dimod.ising_energy(sample, h, J), energy)
Exemplo n.º 10
0
    def test_sample_qubo(self):
        sampler = TilingComposite(MockSampler(), 2, 2)

        Q = {(u, v): random.uniform(-1, 1)
             for u, v in sampler.structure.edgelist}
        Q.update({(node, node): random.uniform(-1, 1)
                  for node in sampler.structure.nodelist})

        response = sampler.sample_qubo(Q)

        # nothing failed and we got at least one response back per tile
        self.assertGreaterEqual(len(response), len(sampler.embeddings))

        for sample in response.samples():
            for u, v in Q:
                self.assertIn(v, sample)
                self.assertIn(u, sample)

        for sample, energy in response.data(['sample', 'energy']):
            self.assertAlmostEqual(dimod.qubo_energy(sample, Q), energy)
Exemplo n.º 11
0
    def test_sample_ising(self):
        sampler = TilingComposite(MockSampler(), 2, 2)

        h = {
            node: random.uniform(-1, 1)
            for node in sampler.structure.nodelist
        }
        J = {(u, v): random.uniform(-1, 1)
             for u, v in sampler.structure.edgelist}

        response = sampler.sample_ising(h, J)

        # nothing failed and we got at least one response back per tile
        self.assertGreaterEqual(len(response), len(sampler.embeddings))

        for sample in response.samples():
            for v in h:
                self.assertIn(v, sample)

        for sample, energy in response.data(['sample', 'energy']):
            self.assertAlmostEqual(dimod.ising_energy(sample, h, J), energy)
Exemplo n.º 12
0
    def test_tile_around_hole(self):

        # Create a chimera graph with the following structure:
        # OOOX
        # OOOO
        # OOOO
        # OOOO
        # where O: complete cell, X: incomplete cell
        mock_sampler = MockSampler(
            broken_nodes=[8 * 3])  # C4 structured sampler with a node missing
        hardware_graph = dnx.chimera_graph(4)  # C4

        sampler = TilingComposite(mock_sampler, 2, 2, 4)
        # Given the above chimera graph, check that the embeddings are as follows:
        # 00XX
        # 0011
        # 2211
        # 22XX
        # where 0,1,2: belongs to correspoding embedding, X: not used in any embedding
        self.assertSetEqual(
            {v
             for s in sampler.embeddings[0].values() for v in s}, {
                 linear_index
                 for linear_index, (i, j, u, k) in hardware_graph.nodes(
                     data='chimera_index') if i in (0, 1) and j in (0, 1)
             })
        self.assertSetEqual(
            {v
             for s in sampler.embeddings[1].values() for v in s}, {
                 linear_index
                 for linear_index, (i, j, u, k) in hardware_graph.nodes(
                     data='chimera_index') if i in (1, 2) and j in (2, 3)
             })
        self.assertSetEqual(
            {v
             for s in sampler.embeddings[2].values() for v in s}, {
                 linear_index
                 for linear_index, (i, j, u, k) in hardware_graph.nodes(
                     data='chimera_index') if i in (2, 3) and j in (0, 1)
             })
Exemplo n.º 13
0
    def test_instantiation_empty(self):
        sampler = FixedEmbeddingComposite(MockSampler(), {})

        dtest.assert_sampler_api(sampler)  # checks adj consistent with nodelist/edgelist

        self.assertEqual(sampler.edgelist, [])
 def test_instantiation_smoketest(self):
     sampler = EmbeddingComposite(MockSampler())
Exemplo n.º 15
0
    def test_sample_bqm_triangle(self):
        sampler = FixedEmbeddingComposite(MockSampler(), {'a': [0, 4], 'b': [1, 5], 'c': [2, 6]})

        resp = sampler.sample_ising({'a': 1, 'b': 1, 'c': 0}, {})

        self.assertEqual(set(resp.variable_labels), {'a', 'b', 'c'})
Exemplo n.º 16
0
 def setUp(self):
     sampler = MockSampler()
     dit.assert_sampler_api(sampler)
     dit.assert_structured_api(sampler)
Exemplo n.º 17
0
    def test_instantiation_smoketest(self):
        sampler = EmbeddingComposite(MockSampler())

        dtest.assert_sampler_api(sampler)
Exemplo n.º 18
0
 def setUp(self):
     self.sampler = MockSampler()