How to use the dimod.BinaryQuadraticModel.empty function in dimod

To help you get started, we’ve selected a few dimod examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dwavesystems / dimod / tests / test_serialization_coo.py View on Github external
def test_dumps_empty_SPIN(self):
        bqm = dimod.BinaryQuadraticModel.empty(dimod.BINARY)
        s = coo.dumps(bqm)
        self.assertEqual(s, '')
github dwavesystems / dimod / tests / test_binary_quadratic_model.py View on Github external
def test_to_json_file_empty(self):
        bqm = dimod.BinaryQuadraticModel.empty(dimod.BINARY)

        tmpdir = tempfile.mkdtemp()
        filename = path.join(tmpdir, 'test.txt')

        with open(filename, 'w') as file:
            bqm.to_json(fp=file)

        with open(filename, 'r') as file:
            jsonschema.validate(json.load(file), bqm_json_schema)

        shutil.rmtree(tmpdir)
github dwavesystems / dimod / tests / test_serialization_coo.py View on Github external
def test_functional_file_empty_BINARY(self):

        bqm = dimod.BinaryQuadraticModel.empty(dimod.BINARY)

        tmpdir = tempfile.mkdtemp()
        filename = os.path.join(tmpdir, 'test.qubo')

        with open(filename, 'w') as file:
            coo.dump(bqm, fp=file)

        with open(filename, 'r') as file:
            new_bqm = coo.load(file, dimod.BinaryQuadraticModel, dimod.BINARY)

        shutil.rmtree(tmpdir)

        self.assertEqual(bqm, new_bqm)
github dwavesystems / dimod / tests / test_binary_quadratic_model.py View on Github external
def test_functional_bytes_empty(self):
        bqm = dimod.BinaryQuadraticModel.empty(dimod.SPIN)

        new = dimod.BinaryQuadraticModel.from_serializable(bqm.to_serializable(use_bytes=True))

        self.assertEqual(bqm, new)
github dwavesystems / dimod / tests / test_binary_quadratic_model.py View on Github external
def test_consistency_after_retrieve(self):
        bqm = dimod.BinaryQuadraticModel.empty(dimod.SPIN)
        retrieved = pickle.loads(pickle.dumps(bqm))
        # make sure the retrieved one was reconstructed properly
        retrieved.add_variable('a', -1)
        bqm.add_variable('a', -1)
        retrieved.add_interaction(0, 1, -.5)
        bqm.add_interaction(0, 1, -.5)
        self.assertEqual(bqm, retrieved)
        self.assertConsistentBQM(retrieved)
github dwavesystems / dimod / tests / test_binary_quadratic_model.py View on Github external
def test_from_serializable_empty_v3(self):
        bqm = dimod.BinaryQuadraticModel.empty(dimod.SPIN)

        s = {'bias_type': 'float32',
             'index_type': 'uint16',
             'info': {},
             'linear_biases': [],
             'num_interactions': 0,
             'num_variables': 0,
             'offset': 0.0,
             'quadratic_biases': [],
             'quadratic_head': [],
             'quadratic_tail': [],
             'type': 'BinaryQuadraticModel',
             'use_bytes': False,
             'variable_labels': [],
             'variable_type': 'SPIN',
             'version': {'bqm_schema': '3.0.0'}}
github dwavesystems / dimod / tests / test_higherorder.py View on Github external
def test_several_terms(self):
        poly = {(0, 1, 2): -1, (1, 2, 3): 1, (0, 2, 3): .5,
                (0,): .4,
                (): .5}

        bqm = make_quadratic(poly, 5.0,
                             bqm=dimod.BinaryQuadraticModel.empty(dimod.SPIN))

        variables = set().union(*poly)
        aux_variables = tuple(set(bqm.linear) - variables)
        variables = tuple(variables)
        self.assertTrue(aux_variables)
        for config in itertools.product((-1, 1), repeat=len(variables)):
            sample = dict(zip(variables, config))

            energy = poly_energy(sample, poly)

            reduced_energies = []
            for aux_config in itertools.product((-1, 1),
                                                repeat=len(aux_variables)):
                aux_sample = dict(zip(aux_variables, aux_config))
                aux_sample.update(sample)
                reduced_energies.append(bqm.energy(aux_sample))
github dwavesystems / dimod / tests / test_binary_quadratic_model.py View on Github external
def test_functional_empty(self):
        # round trip
        bqm = dimod.BinaryQuadraticModel.empty(dimod.SPIN)

        new = dimod.BinaryQuadraticModel.from_serializable(bqm.to_serializable())

        self.assertEqual(bqm, new)
github dwavesystems / dwave_networkx / dwave_networkx / algorithms / markov.py View on Github external
"""Construct a binary quadratic model for a markov network.


    Parameters
    ----------
    G : NetworkX graph
        A Markov Network as returned by :func:`.markov_network`

    Returns
    -------
    bqm : :obj:`dimod.BinaryQuadraticModel`
        A binary quadratic model.

    """

    bqm = dimod.BinaryQuadraticModel.empty(dimod.BINARY)

    # the variable potentials
    for v, ddict in MN.nodes(data=True, default=None):
        potential = ddict.get('potential', None)

        if potential is None:
            continue

        # for single nodes we don't need to worry about order

        phi0 = potential[(0,)]
        phi1 = potential[(1,)]

        bqm.add_variable(v, phi1 - phi0)
        bqm.add_offset(phi0)
github dwavesystems / dwavebinarycsp / dwavebinarycsp / compilers / stitcher.py View on Github external
def _bqm_from_1sat(constraint):
    """create a bqm for a constraint with only one variable

    bqm will have exactly classical gap 2.
    """
    configurations = constraint.configurations
    num_configurations = len(configurations)

    bqm = dimod.BinaryQuadraticModel.empty(dimod.SPIN)

    if num_configurations == 1:
        val, = next(iter(configurations))
        v, = constraint.variables
        bqm.add_variable(v, -1 if val > 0 else +1)
    else:
        bqm.add_variables_from((v, 0.0) for v in constraint.variables)

    return bqm.change_vartype(constraint.vartype)