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 next(self, state, **runopts): num_reads = runopts.get('num_reads', self.num_reads) sampling_params = runopts.get('sampling_params', self.sampling_params) params = sampling_params.copy() params.update(num_reads=num_reads) sampler = FixedEmbeddingComposite(self.sampler, embedding=state.embedding) response = sampler.sample(state.subproblem, **params) return state.updated(subsamples=response)
def solve_dwave_mineng(bqm: dimod.binary_quadratic_model, solver_type="Advantage", num_reads=1000) -> pd.DataFrame: """Dwaveでquboを解く Args: bqm (dimod.binary_quadratic_model): QUBOを入力します solver_type (str, optional): solverのタイプを選ぶ. Defaults to "Advantage". num_reads (int, optional): Dwaveでの計算回数をしています. Defaults to 1000. Returns: pd.DataFrame: 結果を返します """ sampler: DWaveSampler = _get_dwave_sampler(solver_type=solver_type) embedding = minorminer.find_embedding(bqm.quadratic, sampler.edgelist) cbm = dwave.embedding.MinimizeEnergy(bqm, embedding) composite = FixedEmbeddingComposite(sampler, embedding) result = composite.sample(bqm, chain_strength=0.5, num_reads=num_reads, auto_scale=True, chain_break_method=cbm) df_result = result.to_pandas_dataframe() return df_result
bqm = dwavebinarycsp.stitch(csp) chimera_cell = [(i, j + 4) for j in range(4) for i in range(4)] embeddings = [ minorminer.find_embedding(bqm.to_qubo()[0].keys(), chimera_cell) for i in range(100) ] min_emb = min(embeddings, key=lambda x: len(sum(x.values(), []))) print("Minimum embedding configuration:", min_emb) print("Minimum embedding length:", len(sum(min_emb.values(), []))) # Verification of the found embedding print("Verification of the found embedding") sampler = FixedEmbeddingComposite(DWaveSampler(), min_emb) response = sampler.sample(bqm, num_reads=5000) for sample, energy, occurences in response.data( ['sample', 'energy', 'num_occurrences']): print(list(sample.values()), 'Occurrences:', occurences, 'Energy:', energy) # Bonus question: running in parallel print("Bonus question: running in parallel") sampler2 = FixedEmbeddingComposite(TilingComposite(DWaveSampler(), 1, 1, 4), min_emb) response = sampler.sample(bqm, num_reads=5000) for sample, energy, occurences in response.data( ['sample', 'energy', 'num_occurrences']): print(list(sample.values()), 'Occurrences:', occurences, 'Energy:', energy) print("Adjacency:", sampler2.adjacency)
# Graph partitioning on clique N = int(sys.argv[1]) gamma = 3 linear = (N - 1) * (1 - gamma) quad = (2 * gamma) - 2 Q = {} for i in range(N): Q[i, i] = linear for j in range(i + 1, N): Q[i, j] = quad chainstrength = 20 offset = gamma * N**2 / 4 bqm = dimod.BinaryQuadraticModel.from_qubo(Q, offset=offset) dwave_sampler = DWaveSampler() embedding = find_embedding(Q, dwave_sampler.edgelist) chain_lengths = [len(embedding[k]) for k in embedding] print("Total qubits ", sum(chain_lengths)) print("Longest chain ", max(chain_lengths)) sampler = FixedEmbeddingComposite(DWaveSampler(), embedding) response = sampler.sample(bqm, chain_strength=chainstrength, num_reads=1000) for sample, energy, num, cbf in response.data( ['sample', 'energy', 'num_occurrences', 'chain_break_fraction']): print(sample, energy, num, cbf)
def nCr(n, r): f = math.factorial return f(n) / f(r) / f(n - r) N = 16 chainstrength = 40 numruns = 1000 # Ising model is 0.5 x_i x_j - 0.5 # There is a constant offset of -1/2 per pair in the clique # The number of pairs is (N 2) h = {} J = {(i, j): 0.5 for i in range(N) for j in range(i + 1, N)} offset = -nCr(N, 2) / 2 bqm = dimod.BinaryQuadraticModel.from_ising(h, J, offset=offset) # Do the embedding dwave_sampler = DWaveSampler() A = dwave_sampler.edgelist embedding = find_embedding(J, A) sampler = FixedEmbeddingComposite(DWaveSampler(), embedding) sampleset = sampler.sample(bqm, chain_strength=chainstrength, num_reads=numruns) dwave.inspector.show(bqm, sampleset, sampler)
import dwavebinarycsp as dbc import dwavebinarycsp.factories.constraint.gates as gates from dwave.system.composites import FixedEmbeddingComposite, EmbeddingComposite from dwave.system.samplers import DWaveSampler # Making two different BQMs (think: energy functions or optimization functions) csp1 = dbc.ConstraintSatisfactionProblem(dbc.BINARY) csp1.add_constraint(gates.and_gate(['a', 'b', 'c'])) bqm1 = dbc.stitch(csp1) csp2 = dbc.ConstraintSatisfactionProblem(dbc.BINARY) csp2.add_constraint(gates.or_gate(['a', 'b', 'c'])) bqm2 = dbc.stitch(csp2) # Using Embedding Composite sampler = EmbeddingComposite(DWaveSampler()) sampler.sample(bqm1) # Gets a new embedding for bqm1 sampler.sample(bqm2) # Gets a new embedding for bqm2 # Using Fixed Embedding Composite # Note: bqm1 and bqm2 can both be represented by the same graph - triangle graph. embedding = { 'a': [0, 4], 'b': [1], 'c': [5] } # Embedding the triangle graph using QPU indices fixedSampler = FixedEmbeddingComposite(DWaveSampler(), embedding) fixedSampler.sample(bqm1) fixedSampler.sample(bqm2) # in both samples, the SAME embedding gets used
def next(self, state): sampler = FixedEmbeddingComposite(self.sampler, embedding=state.embedding) response = sampler.sample(state.subproblem, num_reads=self.num_reads) return state.updated(subsamples=response)
if useDwave: client = Client.from_config( token='DEV-ee477a6974fcc6a5c059c5275e3fbeb40a227db7') #print(client.get_solvers()) #print("sampler properties: ") #print(DWaveSampler().properties) # create a minor embedding of the source graph representing "bqm" onto the dWave's Chimera graph embedding = find_embedding(bqm.to_networkx_graph(), DWaveSampler().edgelist) print("embedding created") print(embedding) sampler = FixedEmbeddingComposite(DWaveSampler(), embedding) print('+++ starting sampling on QPU') sampleSet = sampler.sample(bqm, num_reads=50) # returns SampleSet print(sampleSet.info) print('lowest energy sample') print(sampleSet.first) print('all samples') for datum in sampleSet.data( fields=['sample', 'energy', 'num_occurrences']): print(datum) else: sampler = SimulatedAnnealingSampler() response = sampler.sample(bqm, num_reads=5, num_sweeps=1000) # returns SampleSet for datum in response.data(['sample', 'energy']): print(datum.sample, datum.energy)
return False else: return True csp = dwavebinarycsp.ConstraintSatisfactionProblem(dwavebinarycsp.SPIN) csp.add_constraint(NEA3SAT, ['a', 'b', 'c']) csp.add_constraint(NEA3SAT, ['c', 'd', 'e']) my_bqm = dwavebinarycsp.stitch(csp, min_classical_gap=1) my_embedding = {'a': {0}, 'b': {7}, 'c': {1, 4}, 'd': {2}, 'e': {6}} # Method 1 print('************ Methord 1 (Embedded in one unit cell) ***************') sampler1 = FixedEmbeddingComposite(DWaveSampler(), my_embedding) response = sampler1.sample(my_bqm, num_reads=100) print('Neighbourhood check : \n', sampler1.adjacency) print(my_bqm) for sample, energy, occurrences in response.data( ['sample', 'energy', 'num_occurrences']): print(list(sample.values()), 'Occurrences :', occurrences, 'Energey :', energy) # Method 2 print( '************ Methord 2 (Tiled across a Chimera-structured sampler in multiple unit cell) ***************' ) sampler2 = FixedEmbeddingComposite(TilingComposite(DWaveSampler(), 1, 1, 4), my_embedding) response = sampler2.sample(my_bqm, num_reads=5)