def test_vartype_dimod_smoke(self):
        StructuredSolver.reformat_parameters('SPIN', {}, {})
        StructuredSolver.reformat_parameters('BINARY', {}, {})
        StructuredSolver.reformat_parameters(dimod.BINARY, {}, {})
        StructuredSolver.reformat_parameters(dimod.SPIN, {}, {})

        with self.assertRaises(ValueError):
            StructuredSolver.reformat_parameters("INTEGER", {}, {})
    def test_initial_states(self):
        doc = {'initial_state': {0: 0, 4: 1}}

        self.assertEqual(StructuredSolver.reformat_parameters('ising', doc, dict(num_qubits=9)),
                         dict(initial_state=[-1, 3, 3, 3, 1, 3, 3, 3, 3]))
        self.assertEqual(StructuredSolver.reformat_parameters('qubo', doc, dict(num_qubits=9)),
                         dict(initial_state=[0, 3, 3, 3, 1, 3, 3, 3, 3]))

        if dimod:
            self.assertEqual(StructuredSolver.reformat_parameters('SPIN', doc, dict(num_qubits=9)),
                             dict(initial_state=[-1, 3, 3, 3, 1, 3, 3, 3, 3]))
            self.assertEqual(StructuredSolver.reformat_parameters('BINARY', doc, dict(num_qubits=9)),
                             dict(initial_state=[0, 3, 3, 3, 1, 3, 3, 3, 3]))

        self.assertEqual(doc, {'initial_state': {0: 0, 4: 1}})
Exemplo n.º 3
0
    def setUp(self):
        # mock client
        self.client = Client(endpoint='e',
                             token='t',
                             solver=dict(name__contains='test'))
        self.client._fetch_solvers = lambda **kw: self.solvers
        self.client._submit = lambda *pa, **kw: None
        self.client.upload_problem_encoded = lambda *pa, **kw: Present(
            result=mock_problem_id)

        # mock solvers
        self.structured_solver = StructuredSolver(
            client=self.client,
            data={
                "properties": {
                    "supported_problem_types": ["qubo", "ising"],
                    "qubits": [0, 1, 2],
                    "couplers": [[0, 1], [0, 2], [1, 2]],
                    "num_qubits": 3,
                    "num_reads_range": [0, 100],
                    "parameters": {
                        "num_reads": "Number of samples to return.",
                        "postprocess": "either 'sampling' or 'optimization'"
                    },
                    "topology": {
                        "type": "chimera",
                        "shape": [16, 16, 4]
                    },
                    "category": "qpu",
                    "tags": ["lower_noise"]
                },
                "id": "test-qpu-solver",
                "description": "A test solver 1",
                "status": "online"
            })
        self.unstructured_solver = UnstructuredSolver(
            client=self.client,
            data={
                "properties": {
                    "supported_problem_types": ["bqm"],
                    "parameters": {
                        "num_reads": "Number of samples to return."
                    },
                    "category": "hybrid",
                },
                "id": "test-unstructured-solver",
                "description": "A test unstructured solver"
            })
        self.solvers = [self.structured_solver]
        # we can't use unstructured solvers without dimod installed,
        # so don't even try testing it
        if dimod:
            self.solvers.append(self.unstructured_solver)

        # reset all event handlers
        from dwave.cloud.events import _client_event_hooks_registry as reg
        reg.update({k: [] for k in reg})
Exemplo n.º 4
0
 def __init__(self, **kwargs):
     kwargs.setdefault(
         'solver',
         StructuredSolver(client=None, data=qpu_clique_solver_data(5)))
     kwargs.setdefault(
         'problem', generate_const_ising_problem(kwargs['solver'],
                                                 h=1,
                                                 j=-1))
     kwargs.setdefault('problem_type', 'ising')
     kwargs.setdefault('answer', self._problem_answer())
     super().__init__(**kwargs)
Exemplo n.º 5
0
def get_structured_solver():
    data = {
        "properties": {
            "supported_problem_types": ["qubo", "ising"],
            "qubits": [0, 1, 2, 3],
            "couplers": [(0, 1), (1, 2), (2, 3), (3, 0)],
            "num_qubits": 4,
            "parameters": {"num_reads": "Number of samples to return."}
        },
        "id": "test-structured-solver",
        "description": "A test structured solver"
    }
    return StructuredSolver(client=None, data=data)
Exemplo n.º 6
0
    def setUp(self):
        # mock solvers
        self.qpu1 = StructuredSolver(
            client=None,
            data={
                "properties": {
                    "supported_problem_types": ["qubo", "ising"],
                    "qubits": [0, 1, 2],
                    "couplers": [[0, 1], [0, 2], [1, 2]],
                    "num_qubits": 3,
                    "num_reads_range": [0, 100],
                    "parameters": {
                        "num_reads": "Number of samples to return.",
                        "postprocess": "either 'sampling' or 'optimization'"
                    },
                    "topology": {
                        "type": "chimera",
                        "shape": [16, 16, 4]
                    },
                    "category": "qpu",
                    "tags": ["lower_noise"]
                },
                "id": "qpu1",
                "description": "QPU Chimera solver",
                "status": "online",
                "avg_load": 0.1
            })
        self.qpu2 = StructuredSolver(
            client=None,
            data={
                "properties": {
                    "supported_problem_types": ["qubo", "ising"],
                    "qubits": [0, 1, 2, 3, 4],
                    "couplers": [[0, 1], [0, 2], [1, 2], [2, 3], [3, 4]],
                    "num_qubits": 5,
                    "num_reads_range": [0, 200],
                    "parameters": {
                        "num_reads": "Number of samples to return.",
                        "flux_biases": "Supported ...",
                        "anneal_schedule": "Supported ..."
                    },
                    "topology": {
                        "type": "pegasus",
                        "shape": [6, 6, 12]
                    },
                    "category": "qpu",
                    "vfyc": True
                },
                "id": "qpu2",
                "description": "QPU Pegasus solver",
                "avg_load": 0.2
            })
        self.software = StructuredSolver(
            client=None,
            data={
                "properties": {
                    "supported_problem_types": ["qubo", "ising"],
                    "qubits": [0, 1],
                    "couplers": [[0, 1]],
                    "num_qubits": 7,
                    "num_reads_range": [0, 1000],
                    "parameters": {
                        "num_reads": "Number of samples to return."
                    },
                    "vfyc": False,
                    "topology": {
                        "type": "chimera",
                        "shape": [4, 4, 4]
                    },
                    "category": "software",
                    # the following are only present in this solver
                    "some_set": [1, 2],
                    "some_range": [1, 2],
                    "some_string": "x",
                    "tags": ["tag"]
                },
                "id": "sw_solver1",
                "description": "Software solver",
                "avg_load": 0.7
            })
        self.hybrid = UnstructuredSolver(client=None,
                                         data={
                                             "properties": {
                                                 "supported_problem_types":
                                                 ["bqm"],
                                                 "maximum_number_of_variables":
                                                 10000,
                                                 "maximum_time_limit_hrs":
                                                 24.0,
                                                 "minimum_time_limit":
                                                 [[1, 3.0], [1024, 3.0],
                                                  [4096, 10.0], [10000, 40.0]],
                                                 "quota_conversion_rate":
                                                 20,
                                                 "parameters": {
                                                     "time_limit": ""
                                                 },
                                                 "category":
                                                 "hybrid",
                                             },
                                             "id": "hybrid_v1",
                                             "description": "Hybrid solver"
                                         })

        self.qpu_solvers = [self.qpu1, self.qpu2]
        self.software_solvers = [self.software]
        self.hybrid_solvers = [self.hybrid]

        self.structured_solvers = self.qpu_solvers + self.software_solvers
        self.unstructured_solvers = self.hybrid_solvers

        self.solvers = self.structured_solvers + self.unstructured_solvers

        # mock client
        self.client = Client('endpoint', 'token')
        self.client._fetch_solvers = lambda **kw: self.solvers
 def test_initial_states_sequence(self):
     doc = {'initial_state': [-1, 3, 3, 3, 1, 3, 3, 3, 3]}
     self.assertEqual(StructuredSolver.reformat_parameters('ising', doc, dict(num_qubits=9)),
                      dict(initial_state=[-1, 3, 3, 3, 1, 3, 3, 3, 3]))
 def test_initial_states_inplace(self):
     doc = {'initial_state': {0: 0, 4: 1}}
     StructuredSolver.reformat_parameters('ising', doc, dict(num_qubits=9), inplace=True)
     self.assertEqual(doc, dict(initial_state=[-1, 3, 3, 3, 1, 3, 3, 3, 3]))
 def test_empty(self):
     self.assertEqual(StructuredSolver.reformat_parameters('ising', {}, {}), {})
 def setUpClass(cls):
     try:
         cls.solver_mock = StructuredSolver(data=mocks.qpu_pegasus_solver_data(16,
             problem_timing_data=mocks.qpu_problem_timing_data(qpu='advantage')), client=None)
     except RuntimeError:
         raise cls.skipTest(cls, "structured solver mock not available")
    def test_vartype_smoke(self):
        for vt in StructuredSolver._handled_problem_types:
            StructuredSolver.reformat_parameters(vt, {}, {})

        with self.assertRaises(ValueError):
            StructuredSolver.reformat_parameters('not a type', {}, {})
import copy
import networkx as nx
from parameterized import parameterized, parameterized_class

try:
    import dimod
except ImportError:
    dimod = None

from dwave.cloud.client import Client
from dwave.cloud.solver import StructuredSolver
from dwave.cloud.testing import mocks

try:
    C16 = StructuredSolver(data=mocks.qpu_chimera_solver_data(16), client=None)
    P16 = StructuredSolver(data=mocks.qpu_pegasus_solver_data(16), client=None)
    solvers = [(C16,), (P16,)]
except RuntimeError:
    solvers = []

from tests import config

@parameterized_class(("solver", ), solvers)
@unittest.skipUnless(dimod, "dimod not installed")
class TestCheckProblem(unittest.TestCase):

    def test_identity(self):
        # NOTE: cast to list can be removed once we drop support for dimod 0.8.x
        bqm = dimod.generators.ran_r(1, list(self.solver.edges))
        self.assertTrue(self.solver.check_problem(bqm.linear, bqm.quadratic))