def test_impossible_model(self): graph = nx.path_graph(3) configurations = { (-1, -1, -1): 0, (-1, +1, -1): 0, (+1, -1, -1): 0, (+1, +1, +1): 0 } decision_variables = (0, 1, 2) linear_energy_ranges = {v: (-2., 2.) for v in graph} quadratic_energy_ranges = {(u, v): (-1., 1.) for u, v in graph.edges} with self.assertRaises(pm.ImpossiblePenaltyModel): maxgap.generate_ising(graph, configurations, decision_variables, linear_energy_ranges, quadratic_energy_ranges, None)
def test_disjoint(self): graph = dnx.chimera_graph(1, 1, 3) graph.add_edge(8, 9) configurations = {(-1, -1, -1): 0, (+1, +1, -1): 0} decision_variables = (0, 1, 8) linear_energy_ranges = {v: (-2., 2.) for v in graph} quadratic_energy_ranges = {(u, v): (-1., 1.) for u, v in graph.edges} h, J, offset, gap = maxgap.generate_ising(graph, configurations, decision_variables, linear_energy_ranges, quadratic_energy_ranges, None) self.check_generated_ising_model(configurations, decision_variables, h, J, offset, gap) self.check_linear_energy_ranges(h, linear_energy_ranges) self.check_quadratic_energy_ranges(J, quadratic_energy_ranges) graph = dnx.chimera_graph(1, 1, 3) graph.add_edge(8, 9) configurations = {(-1, -1, +1, -1): 0, (+1, +1, -1, -1): 0} decision_variables = (0, 1, 3, 8) linear_energy_ranges = {v: (-2., 2.) for v in graph} quadratic_energy_ranges = {(u, v): (-1., 1.) for u, v in graph.edges} h, J, offset, gap = maxgap.generate_ising(graph, configurations, decision_variables, linear_energy_ranges, quadratic_energy_ranges, None) self.check_generated_ising_model(configurations, decision_variables, h, J, offset, gap) self.check_linear_energy_ranges(h, linear_energy_ranges) self.check_quadratic_energy_ranges(J, quadratic_energy_ranges)
def test_specify_z3(self): """Test a simple model specifying yices as the smt solver. Combined with the other test_specify_... tests, serves as a smoke test for the smt_solver_name parameter. """ graph = nx.complete_graph(3) configurations = {(-1, -1): 0, (1, 1): 0} decision_variables = [0, 1] linear_energy_ranges = {v: (-2., 2.) for v in graph} quadratic_energy_ranges = {(u, v): (-1., 1.) for u, v in graph.edges} h, J, offset, gap = maxgap.generate_ising(graph, configurations, decision_variables, linear_energy_ranges, quadratic_energy_ranges, 'z3') self.check_generated_ising_model(configurations, decision_variables, h, J, offset, gap)
def test_one_aux(self): graph = nx.complete_graph(3) configurations = {(-1, -1): 0, (1, 1): 0} linear_energy_ranges = {v: (-2., 2.) for v in graph} quadratic_energy_ranges = {(u, v): (-1., 1.) for u, v in graph.edges} decision_variables = [0, 1] h, J, offset, gap = maxgap.generate_ising(graph, configurations, decision_variables, linear_energy_ranges, quadratic_energy_ranges, None) self.check_generated_ising_model(configurations, decision_variables, h, J, offset, gap) self.check_linear_energy_ranges(h, linear_energy_ranges) self.check_quadratic_energy_ranges(J, quadratic_energy_ranges)
def test_multiplication(self): graph = nx.complete_graph(4) configurations = {(x, y, x * y): 0 for x, y in itertools.product((-1, 1), repeat=2)} decision_variables = [0, 1, 2] linear_energy_ranges = {v: (-2., 2.) for v in graph} quadratic_energy_ranges = {(u, v): (-1., 1.) for u, v in graph.edges} h, J, offset, gap = maxgap.generate_ising(graph, configurations, decision_variables, linear_energy_ranges, quadratic_energy_ranges, None) self.check_generated_ising_model(configurations, decision_variables, h, J, offset, gap) self.check_linear_energy_ranges(h, linear_energy_ranges) self.check_quadratic_energy_ranges(J, quadratic_energy_ranges)
def test_basic(self): """A typical use case, an AND gate on a chimera tile.""" graph = dnx.chimera_graph(1, 1, 4) configurations = { (-1, -1, -1): 0, (-1, +1, -1): 0, (+1, -1, -1): 0, (+1, +1, +1): 0 } decision_variables = (0, 1, 2) linear_energy_ranges = {v: (-2., 2.) for v in graph} quadratic_energy_ranges = {(u, v): (-1., 1.) for u, v in graph.edges} h, J, offset, gap = maxgap.generate_ising(graph, configurations, decision_variables, linear_energy_ranges, quadratic_energy_ranges, None) self.check_generated_ising_model(configurations, decision_variables, h, J, offset, gap) self.check_linear_energy_ranges(h, linear_energy_ranges) self.check_quadratic_energy_ranges(J, quadratic_energy_ranges)
def test_restricted_energy_ranges(self): """Create asymmetric energy ranges and test against that.""" graph = dnx.chimera_graph(1, 1, 3) configurations = { (-1, -1, -1): 0, (-1, +1, -1): 0, (+1, -1, -1): 0, (+1, +1, +1): 0 } decision_variables = (0, 1, 2) linear_energy_ranges = {v: (-1., 2.) for v in graph} quadratic_energy_ranges = {(u, v): (-1., .5) for u, v in graph.edges} h, J, offset, gap = maxgap.generate_ising(graph, configurations, decision_variables, linear_energy_ranges, quadratic_energy_ranges, None) self.check_generated_ising_model(configurations, decision_variables, h, J, offset, gap) self.check_linear_energy_ranges(h, linear_energy_ranges) self.check_quadratic_energy_ranges(J, quadratic_energy_ranges)