Exemplo n.º 1
0
    def test_max_cut(self):
        sampler = micro.EmbeddingComposite(micro.DWaveSampler())

        m = 2
        n = 2
        t = 2

        hoff = 2 * t
        voff = n * hoff
        mi = m * voff
        ni = n * hoff

        edges = []

        # tile edges
        edges.extend((k0, k1) for i in range(0, ni, hoff)
                     for j in range(i, mi, voff) for k0 in range(j, j + t)
                     for k1 in range(j + t, j + 2 * t))
        # horizontal edges
        edges.extend((k, k + hoff) for i in range(t, 2 * t)
                     for j in range(i, ni - hoff, hoff)
                     for k in range(j, mi, voff))
        # vertical edges
        edges.extend((k, k + voff) for i in range(t)
                     for j in range(i, ni, hoff)
                     for k in range(j, mi - voff, voff))

        J = {edge: 1 for edge in edges}
        h = {v: 0 for v in set().union(*J)}

        response = sampler.sample_ising(h, J)
    def test_max_cut(self):
        G = dnx.chimera_graph(2, 2, 4)

        sampler = micro.EmbeddingComposite(micro.DWaveSampler())

        cut = dnx.maximum_cut(G, sampler)

        for u, v in G.edges:
            self.assertTrue(u in cut or v in cut)
            self.assertFalse(u in cut and v in cut)
Exemplo n.º 3
0
    def test_sample_ising(self):

        mock_sampler = mock.MagicMock()
        mock_sampler.structure = (range(3), [(0, 1), (1, 2), (0, 2)], {
            0: {1, 2},
            1: {2, 0},
            2: {0, 1}
        })

        sampler = micro.EmbeddingComposite(mock_sampler)

        h = {'a': -1., 'b': 2}
        J = {('a', 'b'): 1.5}

        response = sampler.sample_ising(h, J)
Exemplo n.º 4
0
    def test_sample_qubo(self):
        sampler = micro.EmbeddingComposite(micro.DWaveSampler())

        Q = {(0, 0): .1, (0, 4): -.8, (4, 4): 1}

        response = sampler.sample_qubo(Q)

        # nothing failed and we got at least one response back
        self.assertGreaterEqual(len(response), 1)

        for sample in response.samples():
            for u, v in Q:
                self.assertIn(v, sample)
                self.assertIn(u, sample)

        for sample, energy in response.data(['sample', 'energy']):
            self.assertAlmostEqual(dimod.qubo_energy(sample, Q), energy)
Exemplo n.º 5
0
    def test_sample_ising_unstructured_not_integer_labelled(self):
        sampler = micro.EmbeddingComposite(micro.DWaveSampler())

        h = {'a': -1., 'b': 2}
        J = {('a', 'b'): 1.5}

        response = sampler.sample_ising(h, J)

        # nothing failed and we got at least one response back
        self.assertGreaterEqual(len(response), 1)

        for sample in response.samples():
            for v in h:
                self.assertIn(v, sample)

        for sample, energy in response.data(['sample', 'energy']):
            self.assertAlmostEqual(dimod.ising_energy(sample, h, J), energy)
Exemplo n.º 6
0
    def test_sample_ising(self):
        sampler = micro.EmbeddingComposite(micro.DWaveSampler())

        h = {0: -1., 4: 2}
        J = {(0, 4): 1.5}

        response = sampler.sample_ising(h, J)

        # nothing failed and we got at least one response back
        self.assertGreaterEqual(len(response), 1)

        for sample in response.samples():
            self.assertIsInstance(sample, dict)
            self.assertEqual(set(sample), set(h))

        for sample, energy in response.data(['sample', 'energy']):
            self.assertIsInstance(sample, dict)
            self.assertEqual(set(sample), set(h))
            self.assertAlmostEqual(dimod.ising_energy(sample, h, J), energy)
Exemplo n.º 7
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import networkx as nx
import dwave_networkx as dnx
import dwave_micro_client_dimod as micro
import dwave_qbsolv

urlc = 'https://cloud.dwavesys.com/sapi'
tokenc = 'SE-bb7f104b4a99cf9a10eeb9637f0806761c9fcedc'
solver_namec = 'DW_2000Q_1'

structured_samplerc = micro.DWaveSampler(solver_namec, urlc, tokenc)
samplerc = micro.EmbeddingComposite(structured_samplerc)
samplerq = dwave_qbsolv.QBSolv()
cloudsi = dnx.structural_imbalance(G, samplerc , num_reads=10000)
qbsolvsi = dnx.structural_imbalance (G, samplerq , solver= samplerc)

h = {v: node_values [v] for v in G.nodes }
J = {(u, v): eval for u, v in G.edges }

response = samplerc.sample_ising (h, J, num_reads=10000)
    fixed_variables = {
        var: 1 if x == '1' else -1
        for (var, x) in fixed_variables.items()
    }

    # fix variables
    for var, value in fixed_variables.items():
        bqm.fix_variable(var, value)
    # 'aux1' becomes disconnected, so needs to be fixed
    bqm.fix_variable('aux1', 1)  # don't care value

    if _qpu:
        # find embedding and put on system
        print("Running using QPU\n")
        sampler = system.EmbeddingComposite(system.DWaveSampler())
        response = sampler.sample_ising(bqm.linear,
                                        bqm.quadratic,
                                        num_reads=NUM_READS)
    else:
        # if no qpu access, use qbsolv's tabu
        print("Running using qbsolv's classical tabu search\n")
        sampler = qbsolv.QBSolv()
        response = sampler.sample_ising(bqm.linear, bqm.quadratic)

    ####################################################################################################
    # output results
    ####################################################################################################

    # responses are sorted in order of increasing energy, so the first energy is the minimum
    min_energy = next(response.energies())
Exemplo n.º 9
0
import dwave_micro_client_dimod as system
from dwave_circuit_fault_diagnosis_demo import *  # TODO

import pandas as pd
import pickle

original, labels = three_bit_multiplier()

DF = {}

sampler = system.EmbeddingComposite(system.DWaveSampler(permissive_ssl=True))

for i, config in enumerate(itertools.product((-1, 1), repeat=12)):
    bqm = original.copy()
    fixed_variables = dict(
        zip(('p5', 'p4', 'p3', 'p2', 'p1', 'p0', 'b2', 'b1', 'b0', 'a2', 'a1',
             'a0'), config))

    # fix variables
    for var, value in fixed_variables.items():
        bqm.fix_variable(var, value)
    # 'aux1' becomes disconnected, so needs to be fixed
    bqm.fix_variable('aux1', 1)  # don't care value

    # find embedding and put on system
    while True:
        try:
            response = sampler.sample_ising(bqm.linear,
                                            bqm.quadratic,
                                            num_reads=1000)
        except ValueError:
Exemplo n.º 10
0
 def test_instantiation_smoketest(self):
     sampler = micro.EmbeddingComposite(micro.DWaveSampler())