def test_duplicate(self): cqm = CQM() bqm = BQM({'a': -1}, {'ab': 1}, 1.5, 'SPIN') cqm.add_constraint(bqm <= 5, label='hello') with self.assertRaises(ValueError): cqm.add_constraint(bqm <= 5, label='hello')
def test_functional(self): cqm = CQM() bqm = BQM({'a': -1}, {'ab': 1}, 1.5, 'SPIN') cqm.add_constraint(bqm, '<=') cqm.add_constraint(bqm, '>=') cqm.set_objective(BQM({'c': -1}, {}, 'SPIN')) cqm.add_constraint(Spin('a')*Integer('d')*5 <= 3) new = CQM.from_file(cqm.to_file()) self.assertTrue(cqm.objective.is_equal(new.objective)) self.assertEqual(set(cqm.constraints), set(new.constraints)) for label, constraint in cqm.constraints.items(): self.assertTrue(constraint.lhs.is_equal(new.constraints[label].lhs)) self.assertEqual(constraint.rhs, new.constraints[label].rhs) self.assertEqual(constraint.sense, new.constraints[label].sense)
def bqm_from_coo(coo: Union[PathLike, TextIO], vartype="BINARY") -> BQM: """Read bqm from coordinate format. This is intended as a replacement for dimod.serialization.coo.load, which sometimes handles integral coefficients poorly. """ data_frame = pd.read_csv(coo, names=["i", "j", "coefficient"], sep=" ") quadratic = {} linear = {} for i, j, coefficient in data_frame.itertuples(index=False): if i == j: linear[i] = coefficient else: quadratic[(i, j)] = coefficient return BQM(linear, quadratic, vartype=vartype)
def to_bqm(self, model): """Given a pysmt model, return a bqm. Adds the values of the biases as determined by the SMT solver to a bqm. Args: model: A pysmt model. Returns: :obj:`dimod.BinaryQuadraticModel` """ linear = ((v, float(model.get_py_value(bias))) for v, bias in self.linear.items()) quadratic = ((u, v, float(model.get_py_value(bias))) for (u, v), bias in self.quadratic.items()) offset = float(model.get_py_value(self.offset)) return BQM(linear, quadratic, offset, dimod.SPIN)
if __name__ == '__main__': parser = ArgumentParser() parser.add_argument("input_file", help="File to be converted.", type=FileType("r")) parser.add_argument("output_file", help="Path to the output file", type=FileType("w")) args = parser.parse_args() print(args.input_file) print(args.output_file) ising = BQM.from_coo(args.input_file, vartype="SPIN") qubo_bqm = ising.change_vartype("BINARY", inplace=False) qubo_bqm.relabel_variables({i: i - 1 for i in qubo_bqm.variables}, inplace=True) num_variables = len(qubo_bqm.variables) num_couplers = len(qubo_bqm.quadratic) header = f"p qubo 0 {num_variables} {num_variables} {num_couplers}\n" qubo_dict, _ = qubo_bqm.to_qubo() args.output_file.write(header) for (i, j), coef in sorted(qubo_dict.items()):
def test_from_qm(self): qm = BQM({'a': -1}, {'ab': 1}, 1.5, 'SPIN') + Integer('i') cqm = CQM.from_quadratic_model(qm) self.assertTrue(cqm.objective.is_equal(qm))
def test_from_bqm(self): bqm = BQM({'a': -1}, {'ab': 1}, 1.5, 'SPIN') cqm = CQM.from_bqm(bqm) self.assertTrue(cqm.objective.is_equal(dimod.QM.from_bqm(bqm)))
def test_bqm(self): cqm = CQM() bqm = BQM({'a': -1}, {'ab': 1}, 1.5, 'SPIN') cqm.add_constraint(bqm, '<=') cqm.add_constraint(bqm, '>=') # add it again