Example #1
0
    def test_conflicting_vartype(self):
        bqm = dimod.BinaryQuadraticModel({0: 1.}, {
            (0, 1): 2,
            (2, 3): .4
        }, 0.0, dimod.SPIN)

        s = coo.dumps(bqm, vartype_header=True)
        with self.assertRaises(ValueError):
            coo.loads(s, dimod.BinaryQuadraticModel, dimod.BINARY)
Example #2
0
    def test_no_vartype(self):
        bqm = dimod.BinaryQuadraticModel({0: 1.}, {
            (0, 1): 2,
            (2, 3): .4
        }, 0.0, dimod.SPIN)

        s = coo.dumps(bqm)
        with self.assertRaises(ValueError):
            coo.loads(s, dimod.BinaryQuadraticModel)
Example #3
0
    def test_functional_string_empty_SPIN(self):

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

        s = coo.dumps(bqm)
        new_bqm = coo.loads(s, dimod.BinaryQuadraticModel, dimod.SPIN)

        self.assertEqual(bqm, new_bqm)
Example #4
0
 def test_loads(self):
     contents = "0 0 1.000000\n0 1 2.000000\n2 3 0.400000"
     bqm = coo.loads(contents, dimod.BinaryQuadraticModel, dimod.SPIN)
     self.assertEqual(
         bqm,
         dimod.BinaryQuadraticModel.from_ising({0: 1.}, {
             (0, 1): 2,
             (2, 3): .4
         }))
Example #5
0
    def test_functional_SPIN_vartypeheader(self):
        bqm = dimod.BinaryQuadraticModel({0: 1.}, {
            (0, 1): 2,
            (2, 3): .4
        }, 0.0, dimod.SPIN)

        s = coo.dumps(bqm, vartype_header=True)
        new_bqm = coo.loads(s, dimod.BinaryQuadraticModel)

        self.assertEqual(bqm, new_bqm)
Example #6
0
    def test_functional_string_SPIN(self):

        bqm = dimod.BinaryQuadraticModel({0: 1.}, {
            (0, 1): 2,
            (2, 3): .4
        }, 0.0, dimod.SPIN)

        s = coo.dumps(bqm)
        new_bqm = coo.loads(s, dimod.BinaryQuadraticModel, dimod.SPIN)

        self.assertEqual(bqm, new_bqm)
Example #7
0
    def from_coo(cls, obj, vartype=None):
        """Deserialize a binary quadratic model from a COOrdinate format encoding.

        COOrdinate_ is a sparse encoding for binary quadratic models.

        .. _COOrdinate: https://en.wikipedia.org/wiki/Sparse_matrix#Coordinate_list_(COO)

        Args:
            obj: (str/file):
                Either a string or a `.read()`-supporting `file object`_ that represents
                linear and quadratic biases for a binary quadratic model. This data
                is stored as a list of 3-tuples, (i, j, bias), where :math:`i=j`
                for linear biases.

            vartype (:class:`.Vartype`/str/set, optional):
                Variable type for the binary quadratic model. Accepted input values:

                * :class:`.Vartype.SPIN`, ``'SPIN'``, ``{-1, 1}``
                * :class:`.Vartype.BINARY`, ``'BINARY'``, ``{0, 1}``

                If not provided, the vartype must be specified with a header in the
                file.

        .. _file object: https://docs.python.org/3/glossary.html#term-file-object

        .. note:: Variables must use index lables (numeric lables). Binary quadratic
            models created from COOrdinate format encoding have offsets set to
            zero.

        .. note:: This method will be deprecated in the future. The preferred
            pattern is to use :func:`~dimod.serialization.coo.load` or
            :func:`~dimod.serialization.coo.loads` directly.

        """
        import dimod.serialization.coo as coo

        if isinstance(obj, str):
            return coo.loads(obj, cls=cls, vartype=vartype)

        return coo.load(obj, cls=cls, vartype=vartype)