Esempio n. 1
0
    def test_num_variables(self):
        G = nx.Graph()
        G.add_nodes_from(range(15))

        Q = dnx.vertex_color_qubo(G, 7)
        bqm = dimod.BinaryQuadraticModel.from_qubo(Q)
        self.assertEqual(len(bqm.quadratic), len(G) * 7 * (7 - 1) / 2)

        # add one edge
        G.add_edge(0, 1)
        Q = dnx.vertex_color_qubo(G, 7)
        bqm = dimod.BinaryQuadraticModel.from_qubo(Q)
        self.assertEqual(len(bqm.quadratic), len(G) * 7 * (7 - 1) / 2 + 7)
Esempio n. 2
0
    def test_chromatic_number(self):
        G = nx.cycle_graph('abcd')

        # when the chromatic number is fixed this is exactly vertex_color
        self.assertEqual(
            dnx.min_vertex_color_qubo(G, chromatic_lb=2, chromatic_ub=2),
            dnx.vertex_color_qubo(G, 2))
Esempio n. 3
0
    def test_4cycle(self):
        G = nx.cycle_graph('abcd')

        Q = dnx.vertex_color_qubo(G, 2)

        sampleset = dimod.ExactSolver().sample_qubo(Q)

        # check that the ground state is a valid coloring
        ground_energy = sampleset.first.energy

        colorings = []
        for sample, en in sampleset.data(['sample', 'energy']):
            if en > ground_energy:
                break

            coloring = {}
            for (v, c), val in sample.items():
                if val:
                    coloring[v] = c

            self.assertTrue(dnx.is_vertex_coloring(G, coloring))

            colorings.append(coloring)

        # there are two valid colorings
        self.assertEqual(len(colorings), 2)

        self.assertEqual(ground_energy, -len(G))
Esempio n. 4
0
    def test_single_node(self):
        G = nx.Graph()
        G.add_node('a')

        # a single color
        Q = dnx.vertex_color_qubo(G, ['red'])

        self.assertEqual(Q, {(('a', 'red'), ('a', 'red')): -1})
Esempio n. 5
0
    def test_docstring_stats(self):
        # get a complex-ish graph
        G = nx.karate_club_graph()

        colors = range(10)

        Q = dnx.vertex_color_qubo(G, colors)

        bqm = dimod.BinaryQuadraticModel.from_qubo(Q)

        self.assertEqual(len(bqm), len(G) * len(colors))
        self.assertEqual(
            len(bqm.quadratic),
            len(G) * len(colors) * (len(colors) - 1) / 2 +
            len(G.edges) * len(colors))