def test_smoke(self): child_sampler = MockDWaveSampler() 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
def test_simple_complete_graph_sample_ising(self): """sample_ising on a K4.""" K4 = VirtualGraphComposite(self.sampler, embedding={ 0: {0, 4}, 1: {1, 5}, 2: {2, 6}, 3: {3, 7} }, flux_biases=False) K4.sample_ising({0: .1, 1: .2}, {(0, 1): 1.5})
def test_embedding_parameter(self): """Given embedding should be saved as a parameter""" sampler = self.sampler __, __, adj = sampler.structure embedding = {v: (v, ) for v in adj} sampler = VirtualGraphComposite(sampler, embedding=embedding, flux_biases=False) self.assertEqual(sampler.embedding, embedding)
def test_reverse_annealing(self): child_sampler = DWaveSampler() # get an embedding K10_edges = list(itertools.combinations(range(10), 2)) embedding = minorminer.find_embedding(K10_edges, child_sampler.edgelist) sampler = VirtualGraphComposite(child_sampler, embedding) h = {} J = {edge: -1 for edge in K10_edges} kwargs = { 'initial_state': {v: 0 for v in set().union(*J)}, 'anneal_schedule': [(0, 1), (55.0, 0.45), (155.0, 0.45), (210.0, 1)] } # sample and resolve sampler.sample_ising(h, J, **kwargs).samples()
def test_structure_keyword_setting(self): sampler = VirtualGraphComposite(self.sampler, embedding={ 'a': set(range(8)), 'b': set(range(8, 16)), 'c': set(range(16, 24)) }, flux_biases=False) nodelist, edgelist, adj = sampler.structure self.assertEqual(nodelist, ['a', 'b', 'c']) self.assertEqual(edgelist, [('a', 'b'), ('b', 'c')]) self.assertEqual(adj, {'a': {'b'}, 'b': {'a', 'c'}, 'c': {'b'}}) # unlike variable names sampler = VirtualGraphComposite(self.sampler, embedding={ 'a': set(range(8)), 1: set(range(8, 16)), 'c': set(range(16, 24)) }, flux_biases=False) nodelist, edgelist, adj = sampler.structure self.assertEqual(set(nodelist), {'a', 1, 'c'}) self.assertEqual(adj, {'a': {1}, 1: {'a', 'c'}, 'c': {1}}) self.assertIsInstance(edgelist, list) self.assertIsInstance(nodelist, list) # edges should still be unique for u in adj: for v in adj[u]: if (u, v) in edgelist: assert (v, u) not in edgelist if (v, u) in edgelist: assert (u, v) not in edgelist assert (u, v) in edgelist or (v, u) in edgelist
def test_construction(self): child_sampler = DWaveSampler() # get an embedding K10_edges = list(itertools.combinations(range(10), 2)) embedding = minorminer.find_embedding(K10_edges, child_sampler.edgelist) sampler = VirtualGraphComposite(child_sampler, embedding) dtest.assert_sampler_api(sampler) h = {} J = {edge: -1 for edge in K10_edges} sampler.sample_ising(h, J) sampler.sample_ising(h, J, apply_flux_bias_offsets=False)
def test_construction(self): child_sampler = self.qpu # get an embedding K10_edges = list(itertools.combinations(range(10), 2)) embedding = minorminer.find_embedding(K10_edges, child_sampler.edgelist) sampler = VirtualGraphComposite(child_sampler, embedding, flux_bias_num_reads=10) dtest.assert_sampler_api(sampler) h = {} J = {edge: -1 for edge in K10_edges} # run with fbo sampler.sample_ising(h, J).resolve() # and again without sampler.sample_ising(h, J, apply_flux_bias_offsets=False).resolve()
for datum in response.data(): print(datum) for sample, energy in response.data(fields=['sample', 'energy'], sorted_by='energy'): print(sample, energy) print('################################################################################') from dwave.system.samplers import DWaveSampler from dwave.system.composites import EmbeddingComposite sampler = EmbeddingComposite(DWaveSampler()) # EmbeddingComposite() composite that maps unstructured problems to the graph structure of the selected sampler, a process known as minor-embedding. response = sampler.sample(bqm, num_reads=1000) # Because of the sampler's probabilistic nature, you typically request multiple samples for a problem; this example sets num_reads to 1000. print(response) for sample, energy, num_occurrences in response.data(fields=['sample', 'energy', 'num_occurrences'], sorted_by='num_occurrences'): print(sample, energy, "Occurrences: ", num_occurrences) print('################################################################################') from dwave.system.samplers import DWaveSampler from dwave.system.composites import VirtualGraphComposite # simplify minor-embedding print(DWaveSampler().properties['extended_j_range']) # maps a problem's variables x, y to qubits 1, 5 and variable z to two qubits 0 and 4 embedding = {'x': {1}, 'y': {5}, 'z': {0, 4}} # checks some features supported on the D-Wave system used as a sampler sampler = VirtualGraphComposite(DWaveSampler(), embedding) print(sampler.parameters)
# Running with `VirtualGraphComposite` # Next we will run our same problem using the `VirtualGraphComposite` tool. # This tool may take longer to run than `FixedEmbeddingComposite` as it may need to calculate flux bias offsets (discussed further below). # When we use `VirtualGraphComposite` we specify the parameter `chain_strength`. # The maximum value for this parameter is `chain_strength=2.0`. # The range of coupling strengths available is finite. # Chaining is typically accomplished by setting the coupling strength to the largest allowed negative value and scaling down the input couplings relative to that. # Yet a reduced energy scale for the input couplings may make it harder for the QPU to find global optima for a problem. from dwave.system.composites import VirtualGraphComposite vg_response = VirtualGraphComposite(dwave_sampler, embedding, chain_strength=2.0).sample_ising( h, J, num_reads=reads) energies = vg_response.record.energy print("QPU call complete using", vg_response.info['timing']['qpu_access_time'] / 1000000.0, "seconds of QPU time.") print("Energy values in range", [energies.min(), energies.max()]) # Visualize these results. vg_x_vals = range(reads) vg_x_pos = np.arange(reads) vg_y_vals = [] for datum in vg_response.data(): for _ in range(datum[2]): vg_y_vals.append(datum[3])