def to_coo(self, fp=None, vartype_header=False): """Serialize the binary quadratic model to 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: fp (file, optional): `.write()`-supporting `file object`_ to save the linear and quadratic biases of a binary quadratic model to. The model is stored as a list of 3-tuples, (i, j, bias), where :math:`i=j` for linear biases. If not provided, returns a string. vartype_header (bool, optional, default=False): If true, the binary quadratic model's variable type as prepended to the string or file as a header. .. _file object: https://docs.python.org/3/glossary.html#term-file-object .. note:: Variables must use index lables (numeric lables). Binary quadratic models saved to COOrdinate format encoding do not preserve offsets. .. note:: This method will be deprecated in the future. The preferred pattern is to use :func:`~dimod.serialization.coo.dump` or :func:`~dimod.serialization.coo.dumps` directly. """ import dimod.serialization.coo as coo if fp is None: return coo.dumps(self, vartype_header) else: coo.dump(self, fp, vartype_header)
def test_dumps_sortable_SPIN_with_header(self): bqm = dimod.BinaryQuadraticModel.from_ising({0: 1.}, { (0, 1): 2, (2, 3): .4 }) s = coo.dumps(bqm, vartype_header=True) contents = "# vartype=SPIN\n0 0 1.000000\n0 1 2.000000\n2 3 0.400000" self.assertEqual(s, contents)
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)
def test_dumps_sortable_SPIN(self): bqm = dimod.BinaryQuadraticModel.from_ising({0: 1.}, { (0, 1): 2, (2, 3): .4 }) s = coo.dumps(bqm) contents = "0 0 1.000000\n0 1 2.000000\n2 3 0.400000" self.assertEqual(s, contents)
def test_dumps_sortable_BINARY_with_header(self): bqm = dimod.BinaryQuadraticModel.from_qubo({ (0, 0): 1., (0, 1): 2, (2, 3): .4 }) s = coo.dumps(bqm, vartype_header=True) contents = "# vartype=BINARY\n0 0 1.000000\n0 1 2.000000\n2 3 0.400000" self.assertEqual(s, contents)
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)
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)
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)
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)
def test_dumps_empty_SPIN(self): bqm = dimod.BinaryQuadraticModel.empty(dimod.BINARY) s = coo.dumps(bqm) self.assertEqual(s, '')