def test_return_embedding(self): nodelist = [0, 1, 2] edgelist = [(0, 1), (1, 2), (0, 2)] sampler = EmbeddingComposite( dimod.StructureComposite(dimod.NullSampler(), nodelist, edgelist)) sampleset = sampler.sample_ising({'a': -1}, {'ac': 1}, return_embedding=True) self.assertIn('embedding', sampleset.info['embedding_context']) embedding = sampleset.info['embedding_context']['embedding'] self.assertEqual(set(embedding), {'a', 'c'}) self.assertIn('chain_break_method', sampleset.info['embedding_context']) self.assertEqual( sampleset.info['embedding_context']['chain_break_method'], 'majority_vote') # the default self.assertIn('embedding_parameters', sampleset.info['embedding_context']) self.assertEqual( sampleset.info['embedding_context']['embedding_parameters'], {}) # the default self.assertIn('chain_strength', sampleset.info['embedding_context']) self.assertEqual(sampleset.info['embedding_context']['chain_strength'], 1.0) # the default # default False sampleset = sampler.sample_ising({'a': -1}, {'ac': 1}) self.assertNotIn('embedding_context', sampleset.info)
def test_return_embedding_as_class_variable(self): nodelist = [0, 1, 2] edgelist = [(0, 1), (1, 2), (0, 2)] sampler = EmbeddingComposite( dimod.StructureComposite(dimod.NullSampler(), nodelist, edgelist)) # temporarily change return_embedding_default EmbeddingComposite.return_embedding_default = True sampleset = sampler.sample_ising({'a': -1}, {'ac': 1}) self.assertIn('embedding', sampleset.info['embedding_context']) embedding = sampleset.info['embedding_context']['embedding'] self.assertEqual(set(embedding), {'a', 'c'}) self.assertIn('chain_break_method', sampleset.info['embedding_context']) self.assertEqual( sampleset.info['embedding_context']['chain_break_method'], 'majority_vote') # the default self.assertIn('embedding_parameters', sampleset.info['embedding_context']) self.assertEqual( sampleset.info['embedding_context']['embedding_parameters'], {}) # the default self.assertIn('chain_strength', sampleset.info['embedding_context']) self.assertEqual(sampleset.info['embedding_context']['chain_strength'], 1.414) # the default # restore the default EmbeddingComposite.return_embedding_default = False
def test_instantiation(self): for factory in [dimod.ExactSolver, dimod.RandomSampler, dimod.SimulatedAnnealingSampler]: sampler = dimod.StructureComposite(factory(), [0, 1, 2], [(0, 1), (0, 2), (1, 2)]) dit.assert_sampler_api(sampler) dit.assert_composite_api(sampler) dit.assert_structured_api(sampler)
def test_minimize_energy_chain_break_method(self): # bug report https://github.com/dwavesystems/dwave-system/issues/206 Q = { (0, 1): 1, (0, 2): 1, (0, 3): 1, (1, 2): 1, (1, 3): 1, (2, 3): 1, (0, 0): 1, (1, 1): 1, (2, 2): 1, (3, 3): 1 } S = {(0, 1): 1, (0, 2): 1, (0, 3): 1, (1, 2): 1, (1, 3): 1, (2, 3): 1} bqm = dimod.BinaryQuadraticModel.from_qubo(Q) G = dnx.chimera_graph(4) sampler = dimod.StructureComposite(dimod.ExactSolver(), G.nodes, G.edges) embedding = {0: [55], 1: [48], 2: [50, 53], 3: [52, 51]} composite = FixedEmbeddingComposite(sampler, embedding) cbm = chain_breaks.MinimizeEnergy(bqm, embedding) composite.sample(bqm, chain_break_method=cbm).resolve()
def test_smoke(self): nodelist = [0, 1, 2, 3] edgelist = [(0, 1), (1, 2), (2, 3), (0, 3)] child = dimod.StructureComposite(dimod.NullSampler(), nodelist, edgelist) sampler = AutoEmbeddingComposite(child) sampler.sample_ising({}, {(0, 1): -1}) sampler.sample_ising({}, {('a', 0): -1})
def test_find_embedding_kwarg(self): child = dimod.StructureComposite(dimod.NullSampler(), [0, 1], [(0, 1)]) def my_find_embedding(S, T): # does nothing return {v: [v] for v in set().union(*S)} sampler = EmbeddingComposite(child, find_embedding=my_find_embedding) # nothing breaks sampler.sample_ising({0: -1}, {})
def test_embedding_parameters_sample(self): child = dimod.StructureComposite(dimod.NullSampler(), [0, 1], [(0, 1)]) def my_find_embedding(S, T, a): assert a == -1 return {v: [v] for v in set().union(*S)} sampler = EmbeddingComposite(child, find_embedding=my_find_embedding) # nothing breaks sampler.sample_ising({0: -1}, {}, embedding_parameters={'a': -1})
def test_warnings(self): G = dnx.chimera_graph(12) sampler = EmbeddingComposite( dimod.StructureComposite(dimod.RandomSampler(), G.nodes, G.edges)) # this will need chains lengths > 7 J = {uv: -1 for uv in itertools.combinations(range(40), 2)} ss = sampler.sample_ising({}, J, warnings='SAVE') self.assertIn('warnings', ss.info)
def test_composed_sampler(self): nodelist = list(range(5)) edgelist = list(itertools.combinations(nodelist, 2)) structured_sampler = dimod.StructureComposite(dimod.NullSampler(), nodelist, edgelist) sampler = dimod.TrackingComposite(structured_sampler) structure = dimod.child_structure_dfs(sampler) self.assertEqual(structure.nodelist, nodelist) self.assertEqual(structure.edgelist, edgelist)
def test_instantiation(self): C = dnx.chimera_graph(2, 2, 4) for factory in [ dimod.ExactSolver, dimod.RandomSampler, dimod.SimulatedAnnealingSampler ]: structsampler = dimod.StructureComposite(factory(), nodelist=C.nodes(), edgelist=C.edges()) sampler = CheckerboardTransformComposite(structsampler, C) dit.assert_sampler_api(sampler) dit.assert_composite_api(sampler)
def test_broken_find_embedding(self): nodelist = [0, 1, 2, 3] edgelist = [(0, 1), (1, 2), (2, 3), (0, 3)] child = dimod.StructureComposite(dimod.NullSampler(), nodelist, edgelist) def find_embedding(*args, **kwargs): raise NotImplementedError sampler = AutoEmbeddingComposite(child, find_embedding=find_embedding) sampler.sample_ising({}, {(0, 1): -1}) with self.assertRaises(NotImplementedError): sampler.sample_ising({}, {('a', 0): -1})
def test_external_embedding_sampler(self): bqm = dimod.BinaryQuadraticModel.from_ising({'a': 1}, {}) init = State.from_subproblem(bqm, embedding={'a': [0]}) sampler = dimod.StructureComposite( SimulatedAnnealingSampler(), nodelist=[0], edgelist=[]) workflow = QPUSubproblemExternalEmbeddingSampler(qpu_sampler=sampler) # run mock sampling res = workflow.run(init).result() # verify mock sampler received custom kwargs self.assertEqual(res.subsamples.first.energy, -1)
def example_instance(): nP = 2 jobs = [1, 2, 3, 4] diff = [] embedding = { 0: [1760, 1632, 1764], 1: [1628, 1636, 1634], 2: [1638, 1630, 1627], 3: [1752, 1624, 1759, 1767], 4: [1763, 1635, 1765], 5: [1761, 1633], 6: [1754, 1626, 1758, 1766], 7: [1631, 1639, 1625], 8: [1637, 1629] } # base_sampler = neal.SimulatedAnnealingSampler() base_sampler = DWaveSampler() #base_sampler.properties['extended_j_range'] = [-10.0, 10.0] G = dnx.chimera_graph(16, 16, 4) nodelist = G.nodes() edgelist = G.edges() sampler = dimod.StructureComposite(base_sampler, nodelist, edgelist) new_sampler = FixedEmbeddingComposite(sampler, embedding) # print new_sampler.properties # print new_sampler.parameters #new_sampler = VirtualGraphComposite(sampler, embedding, chain_strength=-6.0) Q = create.createQUBO(jobs, nP, diff) solutions = { 'energy': [], "valid": [], 'max_time': [], 'chain_break_fraction': [] } for k in range(10): s = solve.solveQUBO(Q, jobs, nP, new_sampler, 0) solutions['energy'].append(s['energy']) solutions['valid'].append(s['valid']) solutions['max_time'].append(s['max_time']) solutions['chain_break_fraction'].append(s['chain_break_fraction']) solve.evaluate(solutions) print(solutions) print("Durchschnittliche Anteil gebrochender Ketten: ", sum(solutions['chain_break_fraction']) / 100)
def test_warning_chain_strength(self): G = dnx.chimera_graph(12) sampler = EmbeddingComposite( dimod.StructureComposite(dimod.RandomSampler(), G.nodes, G.edges)) J = {(0, 1): 100} ss = sampler.sample_ising({}, J, warnings='SAVE') self.assertIn('warnings', ss.info) count = 0 for warning in ss.info['warnings']: if issubclass(warning['type'], ChainStrengthWarning): count += 1 self.assertEqual(count, 1)
def test_chimera(self): C = dnx.chimera_graph(4) nodelist = list(C.nodes()) edgelist = list(C.edges()) structsampler = dimod.StructureComposite(dimod.RandomSampler(), nodelist=nodelist, edgelist=edgelist) Q = {(v, v): 0.1 for v in nodelist} Q.update({edge: -1.0 for edge in edgelist}) sampler = CheckerboardTransformComposite(structsampler, C) response = sampler.sample_qubo(Q, num_reads=1000) dit.assert_response_energies(response, dimod.BinaryQuadraticModel.from_qubo(Q))
def test_warnings_as_class_variable(self): G = dnx.chimera_graph(12) sampler = EmbeddingComposite( dimod.StructureComposite(dimod.RandomSampler(), G.nodes, G.edges)) # this will need chains lengths > 7 J = {uv: -1 for uv in itertools.combinations(range(40), 2)} EmbeddingComposite.warnings_default = 'SAVE' ss = sampler.sample_ising({}, J) self.assertIn('warnings', ss.info) EmbeddingComposite.warnings_default = 'IGNORE' # restore default
def test_keyer(self): C4 = dnx.chimera_graph(4) nodelist = sorted(C4.nodes) edgelist = sorted(sorted(edge) for edge in C4.edges) child = dimod.StructureComposite(dimod.NullSampler(), nodelist, edgelist) embedding = {0: [49, 53], 1: [52], 2: [50]} sampler = FixedEmbeddingComposite(child, embedding) with self.assertRaises(dwave.embedding.exceptions.MissingChainError): sampler.sample_qubo({(1, 4): 1}) sampler.sample_qubo({(1, 2): 1}).record # sample and resolve future sampler.sample_qubo({(1, 1): 1}).record # sample and resolve future
def test_keyerror(self): C16 = dnx.chimera_graph(16) child_sampler = dimod.RandomSampler() nodelist = sorted(C16.nodes) edgelist = sorted(sorted(edge) for edge in C16.edges) struc_sampler = dimod.StructureComposite(child_sampler, nodelist, edgelist) embedding = {0: [391, 386], 1: [390], 2: [385]} sampler = FixedEmbeddingComposite(struc_sampler, embedding) with self.assertRaises(ValueError): sampler.sample_qubo({(1, 4): 1}) sampler.sample_qubo({(1, 2): 1}).record # sample and resolve future sampler.sample_qubo({(1, 1): 1}).record # sample and resolve future
def test_return_embedding(self): nodelist = [0, 1, 2] edgelist = [(0, 1), (1, 2), (0, 2)] sampler = EmbeddingComposite( dimod.StructureComposite(dimod.NullSampler(), nodelist, edgelist)) sampleset = sampler.sample_ising({'a': -1}, {'ac': 1}, return_embedding=True) self.assertIn('embedding', sampleset.info) embedding = sampleset.info['embedding'] self.assertEqual(set(embedding), {'a', 'c'}) # default False sampleset = sampler.sample_ising({'a': -1}, {'ac': 1}) self.assertNotIn('embedding', sampleset.info)
def test_scale_aware_scale_composite(self): nodelist = [0, 1, 2] edgelist = [(0, 1), (1, 2), (0, 2)] embedding = {'a': [0], 'b': [1, 2]} sampler = FixedEmbeddingComposite(dimod.TrackingComposite( ScaleComposite( dimod.StructureComposite(dimod.NullSampler(), nodelist, edgelist))), embedding=embedding, scale_aware=True) _ = sampler.sample_ising({'a': 100, 'b': -100}, {'ab': 300}) self.assertIn('ignored_interactions', sampler.child.input) ignored = sampler.child.input['ignored_interactions'] self.assertTrue(ignored == [(1, 2)] or ignored == [(2, 1)])
def test_transforms_exact(self): C = dnx.chimera_graph(2, 2, 2) nodelist = list(C.nodes()) edgelist = list(C.edges()) structsampler = dimod.StructureComposite(dimod.ExactSolver(), nodelist=nodelist, edgelist=edgelist) sampler = CheckerboardTransformComposite(structsampler, C, aggregate=True) h = {v: 0.1 for v in nodelist} J = {edge: -1.0 for edge in edgelist} response = sampler.sample_ising(h, J) # All 4 gauges must return same samples for datum in response.data(): self.assertEqual(datum.num_occurrences, 4) dit.assert_response_energies( response, dimod.BinaryQuadraticModel.from_ising(h, J))
def test_bug60(self): for __ in range(100): # make a small K5 bqm bqm = dimod.BinaryQuadraticModel.empty(dimod.BINARY) for v in range(5): bqm.add_variable(v, random.uniform(-1, 1)) for u, v in itertools.combinations(range(5), 2): bqm.add_interaction(u, v, random.uniform(-1, 1)) sampler_exact = dimod.ExactSolver() # get the structure of a C1,2 chimera lattice C12 = dnx.chimera_graph(2, 2, 3) nodelist = sorted(C12) edgelist = sorted(sorted(edge) for edge in C12.edges) sampler_structured = dimod.StructureComposite(sampler_exact, nodelist=nodelist, edgelist=edgelist) sampler_embedding = EmbeddingComposite(sampler_structured) dtest.assert_sampler_api(sampler_embedding) resp_exact = sampler_exact.sample(bqm) resp_emb = sampler_embedding.sample(bqm) for sample, energy in resp_emb.data(['sample', 'energy']): self.assertEqual(bqm.energy(sample), energy) ground_exact = dict(next(iter(resp_exact))) ground_embed = dict(next(iter(resp_emb))) self.assertEqual(ground_embed, ground_exact)
def test_transform_embedded(self): C = dnx.chimera_graph(1) nodelist = list(C.nodes()) edgelist = list(C.edges()) structsampler = dimod.StructureComposite(dimod.ExactSolver(), nodelist=nodelist, edgelist=edgelist) gauges_sampler = CheckerboardTransformComposite(structsampler, C, aggregate=True) sampler = FixedEmbeddingComposite(gauges_sampler, { 'a': [0, 4], 'b': [1, 5], 'c': [2, 6] }) h = {'a': .5, 'c': 0} J = {('a', 'c'): -1} response = sampler.sample_ising(h, J) # All 4 gauges must return same samples for datum in response.data(): self.assertEqual(datum.num_occurrences, 4) dit.assert_response_energies( response, dimod.BinaryQuadraticModel.from_ising(h, J))
def composite_factory(sampler): return dimod.StructureComposite(sampler, [0, 1, 2], [(0, 1), (0, 2), (1, 2)])
def example_instance_delete_given(percentage): nP = 3 jobs = [5, 7, 3, 5, 9, 6, 3, 1, 3, 1] diff = [1, 2, 2, 5] base_sampler = DWaveSampler() nodelist = base_sampler.nodelist edgelist = base_sampler.edgelist # print new_sampler.properties # print new_sampler.parameters # new_sampler = VirtualGraphComposite(sampler, embedding, chain_strength=-6.0) Q, num_entries, max_entry = create.createQUBO(jobs, nP, diff) # nur noch nach absolutwerten sortiert, enthält nur die keys nicht die values sorted_q = sorted(Q, key=lambda dict_key: abs(Q[dict_key])) embedding = { 0: [ 622, 614, 606, 598, 726, 594, 722, 978, 630, 638, 850, 593, 465, 337, 602, 1106, 590 ], 1: [ 1090, 834, 1110, 578, 1102, 1094, 962, 1118, 706, 1134, 1126, 1142, 1150, 710, 836, 718, 1112, 702, 583 ], 2: [894, 830, 838, 846, 854, 886, 878, 870, 862, 851, 723, 595, 979], 3: [362, 106, 490, 234, 618, 749, 1002, 746, 874, 110, 118, 126, 1130], 4: [ 342, 334, 326, 318, 382, 374, 569, 953, 366, 358, 350, 313, 441, 697, 825, 1081 ], 5: [ 315, 325, 341, 333, 317, 365, 357, 349, 443, 571, 699, 381, 827, 955, 373 ], 6: [123, 635, 379, 1019, 763, 251, 891, 1147, 507, 1275, 1151], 7: [764, 756, 716, 748, 740, 732, 724, 708, 700], 8: [ 591, 599, 607, 345, 985, 601, 729, 857, 615, 473, 639, 1113, 623, 631 ], 9: [ 511, 455, 704, 448, 471, 463, 960, 576, 479, 487, 496, 495, 503, 320, 624, 832, 1088 ], 10: [ 709, 1141, 971, 1099, 1109, 843, 717, 844, 715, 701, 1101, 1117, 1125, 1133, 725, 733, 1149, 741 ], 11: [761, 1017, 633, 889, 505, 377, 249, 1145, 121, 1273, 127], 12: [893, 829, 885, 877, 837, 845, 853, 861, 869, 880, 752, 1008], 13: [1007, 999, 991, 959, 967, 975, 983, 1015, 1023, 977, 849, 721], 14: [ 1127, 961, 1119, 833, 1095, 1103, 1111, 1089, 705, 839, 1135, 831, 847, 1120, 1248, 1254, 1262, 1270, 1278, 855, 863 ], 15: [ 608, 589, 605, 1098, 597, 613, 480, 352, 586, 842, 714, 970, 224, 96, 101, 109, 117, 125 ], 16: [611, 355, 483, 227, 253, 245, 237, 229, 739, 867, 871, 995, 1123], 17: [491, 1003, 107, 363, 619, 747, 246, 238, 235, 875, 254, 1131], 18: [482, 758, 766, 750, 742, 994, 738, 610, 866, 1122, 354], 19: [ 988, 835, 1004, 996, 980, 707, 964, 972, 992, 864, 868, 876, 884, 892, 956, 963 ], 20: [632, 1144, 888, 504, 1016, 248, 376, 760, 1272, 120], 21: [ 982, 958, 966, 974, 950, 946, 818, 690, 990, 998, 1006, 1014, 1022, 692 ], 22: [ 442, 452, 468, 460, 1082, 444, 570, 698, 826, 954, 476, 484, 458, 492, 500, 508, 314 ], 23: [728, 216, 344, 856, 472, 236, 228, 220, 600, 244, 984, 734, 252], 24: [ 510, 502, 494, 865, 486, 481, 478, 353, 609, 993, 737, 1121, 470, 462 ], 25: [ 84, 72, 456, 200, 328, 92, 76, 100, 98, 226, 968, 108, 116, 584, 461, 469, 712, 124, 840, 1096 ], 26: [703, 751, 719, 727, 767, 759, 743, 735, 711, 695, 736], 27: [475, 493, 485, 477, 987, 603, 731, 859, 501, 509, 347, 1115], 28: [1011, 629, 371, 621, 627, 637, 755, 243, 499, 883, 1139, 115, 1267], 29: [ 1005, 997, 989, 1013, 965, 976, 973, 981, 957, 1021, 848, 720, 592, 596, 588 ], 30: [329, 343, 335, 585, 383, 359, 351, 457, 367, 375, 713, 841, 969, 338], 31: [ 340, 332, 324, 364, 348, 356, 316, 312, 440, 568, 696, 824, 952, 372, 380, 1080 ], 32: [ 1116, 1114, 1124, 1084, 1092, 1100, 1108, 986, 730, 1132, 858, 1140, 1148, 1097, 1083 ], 33: [1018, 762, 250, 634, 122, 506, 378, 890, 1020, 1146, 1274], 34: [757, 626, 1010, 754, 882, 498, 1138, 370, 242, 765, 114, 1266], 35: [1001, 617, 636, 628, 620, 873, 489, 745, 361, 233, 105, 1129, 612], 36: [1009, 625, 881, 497, 369, 241, 895, 753, 887, 1137, 113, 1265], 37: [ 255, 247, 872, 239, 488, 616, 232, 360, 744, 1000, 1128, 240, 112, 231 ] } sampler = dimod.StructureComposite(base_sampler, nodelist, edgelist) new_sampler = FixedEmbeddingComposite(sampler, embedding) alldata = [] n_deleted = [] v_deleted = [] for p in percentage: print("Percentage: ", p) Q_deleted, max, deleted = get_given_percentage_qubo( p, copy.deepcopy(Q), copy.deepcopy(sorted_q), num_entries) n_deleted.append(deleted) print("Größter gelöschter Wert: ", max) v_deleted.append(max) solution = [] kaputt = 0 for i in range(100): result = solve.solveQUBO(Q_deleted, jobs, nP, new_sampler) #print(result) solution.append(result) if result['valid'] == False: kaputt += 1 print(kaputt, " kaputte Lösungen für Threshold und ", p) alldata.append(solution) dist = [] for i in range(len(alldata)): dist.append( [alldata[i][j]['max_time'] for j in range(len(alldata[i]))]) print(dist) rc('font', **{'family': 'sans-serif', 'sans-serif': ['Helvetica']}) ## for Palatino and other serif fonts use: # rc('font',**{'family':'serif','serif':['Palatino']}) rc('text', usetex=True) plt.rc('text', usetex=True) plt.rc('font', family='serif') plt.boxplot(dist) plt.xticks([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 ], [ 0.0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.0 ]) plt.xlabel("Prozentanzahl entfernter Elemente") plt.ylabel("Umsteigedistanzen") plt.title("Jeweils 200 Aufrufe, beste Ergebnisse, Datensatz tai12, " + u'12 Flüge, 12 Gates, keine Kosten') tikz_save('mms_given.tikz', figureheight='\\figureheight', figurewidth='\\figurewidth') #plt.show() return alldata, n_deleted, v_deleted
def example_instance_big(): nP = 3 jobs = [5, 7, 3, 5, 9, 6, 3, 1, 3, 1] diff = [1, 2, 2, 5] base_sampler = DWaveSampler() nodelist = base_sampler.nodelist edgelist = base_sampler.edgelist Q, num_entries, max_value = create.createQUBO(jobs, nP, diff) embedding = { 0: [ 622, 614, 606, 598, 726, 594, 722, 978, 630, 638, 850, 593, 465, 337, 602, 1106, 590 ], 1: [ 1090, 834, 1110, 578, 1102, 1094, 962, 1118, 706, 1134, 1126, 1142, 1150, 710, 836, 718, 1112, 702, 583 ], 2: [894, 830, 838, 846, 854, 886, 878, 870, 862, 851, 723, 595, 979], 3: [362, 106, 490, 234, 618, 749, 1002, 746, 874, 110, 118, 126, 1130], 4: [ 342, 334, 326, 318, 382, 374, 569, 953, 366, 358, 350, 313, 441, 697, 825, 1081 ], 5: [ 315, 325, 341, 333, 317, 365, 357, 349, 443, 571, 699, 381, 827, 955, 373 ], 6: [123, 635, 379, 1019, 763, 251, 891, 1147, 507, 1275, 1151], 7: [764, 756, 716, 748, 740, 732, 724, 708, 700], 8: [ 591, 599, 607, 345, 985, 601, 729, 857, 615, 473, 639, 1113, 623, 631 ], 9: [ 511, 455, 704, 448, 471, 463, 960, 576, 479, 487, 496, 495, 503, 320, 624, 832, 1088 ], 10: [ 709, 1141, 971, 1099, 1109, 843, 717, 844, 715, 701, 1101, 1117, 1125, 1133, 725, 733, 1149, 741 ], 11: [761, 1017, 633, 889, 505, 377, 249, 1145, 121, 1273, 127], 12: [893, 829, 885, 877, 837, 845, 853, 861, 869, 880, 752, 1008], 13: [1007, 999, 991, 959, 967, 975, 983, 1015, 1023, 977, 849, 721], 14: [ 1127, 961, 1119, 833, 1095, 1103, 1111, 1089, 705, 839, 1135, 831, 847, 1120, 1248, 1254, 1262, 1270, 1278, 855, 863 ], 15: [ 608, 589, 605, 1098, 597, 613, 480, 352, 586, 842, 714, 970, 224, 96, 101, 109, 117, 125 ], 16: [611, 355, 483, 227, 253, 245, 237, 229, 739, 867, 871, 995, 1123], 17: [491, 1003, 107, 363, 619, 747, 246, 238, 235, 875, 254, 1131], 18: [482, 758, 766, 750, 742, 994, 738, 610, 866, 1122, 354], 19: [ 988, 835, 1004, 996, 980, 707, 964, 972, 992, 864, 868, 876, 884, 892, 956, 963 ], 20: [632, 1144, 888, 504, 1016, 248, 376, 760, 1272, 120], 21: [ 982, 958, 966, 974, 950, 946, 818, 690, 990, 998, 1006, 1014, 1022, 692 ], 22: [ 442, 452, 468, 460, 1082, 444, 570, 698, 826, 954, 476, 484, 458, 492, 500, 508, 314 ], 23: [728, 216, 344, 856, 472, 236, 228, 220, 600, 244, 984, 734, 252], 24: [ 510, 502, 494, 865, 486, 481, 478, 353, 609, 993, 737, 1121, 470, 462 ], 25: [ 84, 72, 456, 200, 328, 92, 76, 100, 98, 226, 968, 108, 116, 584, 461, 469, 712, 124, 840, 1096 ], 26: [703, 751, 719, 727, 767, 759, 743, 735, 711, 695, 736], 27: [475, 493, 485, 477, 987, 603, 731, 859, 501, 509, 347, 1115], 28: [1011, 629, 371, 621, 627, 637, 755, 243, 499, 883, 1139, 115, 1267], 29: [ 1005, 997, 989, 1013, 965, 976, 973, 981, 957, 1021, 848, 720, 592, 596, 588 ], 30: [329, 343, 335, 585, 383, 359, 351, 457, 367, 375, 713, 841, 969, 338], 31: [ 340, 332, 324, 364, 348, 356, 316, 312, 440, 568, 696, 824, 952, 372, 380, 1080 ], 32: [ 1116, 1114, 1124, 1084, 1092, 1100, 1108, 986, 730, 1132, 858, 1140, 1148, 1097, 1083 ], 33: [1018, 762, 250, 634, 122, 506, 378, 890, 1020, 1146, 1274], 34: [757, 626, 1010, 754, 882, 498, 1138, 370, 242, 765, 114, 1266], 35: [1001, 617, 636, 628, 620, 873, 489, 745, 361, 233, 105, 1129, 612], 36: [1009, 625, 881, 497, 369, 241, 895, 753, 887, 1137, 113, 1265], 37: [ 255, 247, 872, 239, 488, 616, 232, 360, 744, 1000, 1128, 240, 112, 231 ] } sampler = dimod.StructureComposite(base_sampler, nodelist, edgelist) new_sampler = FixedEmbeddingComposite(sampler, embedding) solutions = { 'energy': [], "valid": [], 'max_time': [], 'chain_break_fraction': [] } for k in range(10): s = solve.solveQUBO(Q, jobs, nP, new_sampler) solutions['energy'].append(s['energy']) solutions['valid'].append(s['valid']) solutions['max_time'].append(s['max_time']) solutions['chain_break_fraction'].append(s['chain_break_fraction']) solve.evaluate(solutions) print(solutions) print("Durchschnittliche Anteil gebrochender Ketten: ", sum(solutions['chain_break_fraction']) / 100)
qubo = {(i, i): 0.0 for i in range(len(qubomatrix)) } # necessary to keep the order of the sample columns consistent for index, value in np.ndenumerate(qubomatrix): if value != 0: qubo[index] = value print('Converted matrix into QUBO for D-Wave:\n', qubo, '\n') # create a pegasus graph and a simulation graph = dnx.pegasus_graph( 16 ) # stands for a pegasus P6 graph, see https://www.dwavesys.com/sites/default/files/mwj_dwave_qubits2018.pdf simulator = neal.SimulatedAnnealingSampler( ) # this makes it use a classical simulated annealing sampler composite = dimod.StructureComposite( simulator, graph.nodes, graph.edges) # necessary to make it use the pegasus graph sampler = LazyFixedEmbeddingComposite(composite) # similar as before response = sampler.sample_qubo(qubo, num_reads=10000, chain_strength=40) print('Response:\n', response, '\n') print('Embedding:\n', sampler.embedding, '\n') print('Nodelist:\n', sampler.nodelist, '\n') print('Edgelist:\n', sampler.edgelist, '\n') print('Adjacency:\n', sampler.adjacency, '\n') # save results in results.txt with open('results.txt', 'w') as file: file.write('energy\tnum_occurrences\tsample\n') for sample, energy, num_occurrences, cbf in response.data(): newsample = np.array([
# working with different topologies import neal import dimod import dwave_networkx as dnx import networkx as nx import dwave.embedding from dwave.system import DWaveSampler, EmbeddingComposite # Creating a Pegasus Sampler P6 = dnx.pegasus_graph(6) classical_sampler = neal.SimulatedAnnealingSampler() sampler = dimod.StructureComposite(classical_sampler, P6.nodes, P6.edges) # working with embeddings num_variables = 40 embedding = dwave.embedding.chimera.find_clique_embedding(num_variables, 16) toPrint1 = max(len(chain) for chain in embedding.values()) print(toPrint1) num_variables = 40 embedding = dwave.embedding.pegasus.find_clique_embedding(num_variables, 6) toPrint2 = max(len(chain) for chain in embedding.values()) print(toPrint2)
def test_intermediate_composites(self): child = dimod.StructureComposite(dimod.NullSampler(), [0, 1], [(0, 1)]) intermediate = dimod.TrackingComposite(child) sampler = EmbeddingComposite(intermediate) self.assertEqual(sampler.target_structure.nodelist, [0, 1])
def find_loop(unit_size=8, i0=0, j0=0, use_qpu=True): ''' dwaveの各セルを頂点として、頂点をつなげるループを 見つけようとするモデルを実行する。 結果は、エネルギー最小値にならないため 期待した結果にはならない。 今後のマシンの精度向上に期待。 または、どうにかして頑健なグラフ構造にすべきか。 ※注意:マシンは個体ごとにところどころ壊れているので、使用するセルを以下の引数で指定する。 Args: unit_size: セルの正方形の辺の長さ i0: セルの縦方向のオフセット j0: セルの横方向のオフセット use_qpu: qpuを使うか指定する ''' qpu_unit_size = 16 C16 = dnx.chimera_graph(qpu_unit_size) h, J = V(unit_size=unit_size, i0=i0, j0=j0) #print(h) #print(J) if use_qpu: sampler = DWaveSampler() samples = sampler.sample_ising(h, J, num_reads=10, annealing_time=20) if 0: count = 0 for s,e,o in samples.data(['sample', 'energy', 'num_occurrences']): print(e, o) print_chimera_simple(s) samples = sampler.sample_ising(h, J, num_reads=100, anneal_schedule = [[0.0,1.0],[20.0,0.0],[30.0,0.3],[35.0,0.3],[40.0,1.0]], reinitialize_state=False, initial_state=s) count += 1 if count >= 1 : break else: classical_sampler = neal.SimulatedAnnealingSampler() sampler = dimod.StructureComposite(classical_sampler, C16.nodes, C16.edges) samples = sampler.sample_ising(h, J, num_reads=10) count = 0 for s,e,o in samples.data(['sample', 'energy', 'num_occurrences']): print(e, o) print_chimera_simple(s) count += 1 if count >= 10 : break