Пример #1
0
    def test_empty(self, dtype):
        qm = QM()

        with tempfile.TemporaryFile() as tf:
            with qm.to_file() as qmf:
                shutil.copyfileobj(qmf, tf)
            tf.seek(0)
            new = QM.from_file(tf)

        self.assertTrue(qm.is_equal(new))
        self.assertEqual(qm.dtype, new.dtype)
Пример #2
0
 def test_simple(self):
     qm = QM()
     a = qm.add_variable('SPIN', 'a')
     qm.set_linear(a, 1.5)
     qm.change_vartype('BINARY', a)
     self.assertEqual(qm.energy({a: 1}), 1.5)
     self.assertEqual(qm.energy({a: 0}), -1.5)
     self.assertIs(qm.vartype(a), dimod.BINARY)
Пример #3
0
 def test_self_loop_spin(self):
     qm = QM(vartypes={'i': 'INTEGER', 's': 'SPIN', 'x': 'BINARY'})
     with self.subTest('BINARY'):
         with self.assertRaises(ValueError):
             qm.add_quadratic('x', 'x', 1)
     with self.subTest('INTEGER'):
         qm.add_quadratic('i', 'i', 1)
         self.assertEqual(qm.get_quadratic('i', 'i'), 1)
         self.assertEqual(qm.quadratic, {('i', 'i'): 1})
     with self.subTest('SPIN'):
         with self.assertRaises(ValueError):
             qm.add_quadratic('s', 's', 1)
Пример #4
0
    def test_linear(self, dtype):
        qm = QM(dtype=dtype)
        qm.add_variable('INTEGER', 'a')
        qm.add_variable('SPIN', 'b')
        qm.add_variable('BINARY', 'c')

        self.assertEqual(qm.linear, {'a': 0, 'b': 0, 'c': 0})
        self.assertEqual(qm.quadratic, {})
        self.assertEqual(qm.adj, {'a': {}, 'b': {}, 'c': {}})
Пример #5
0
    def test_expressions(self):
        i = Integer('i')
        j = Integer('j')

        self.assertTrue((i * j).is_equal(
            QM({}, {'ij': 1}, 0, {
                'i': 'INTEGER',
                'j': 'INTEGER'
            })))
        self.assertTrue((i * i).is_equal(QM({}, {'ii': 1}, 0,
                                            {'i': 'INTEGER'})))
        self.assertTrue(
            ((2 * i) * (3 * i)).is_equal(QM({}, {'ii': 6}, 0,
                                            {'i': 'INTEGER'})))
        self.assertTrue((i + j).is_equal(
            QM({
                'i': 1,
                'j': 1
            }, {}, 0, {
                'i': 'INTEGER',
                'j': 'INTEGER'
            })))
        self.assertTrue((i + 2 * j).is_equal(
            QM({
                'i': 1,
                'j': 2
            }, {}, 0, {
                'i': 'INTEGER',
                'j': 'INTEGER'
            })))
        self.assertTrue((i - 2 * j).is_equal(
            QM({
                'i': 1,
                'j': -2
            }, {}, 0, {
                'i': 'INTEGER',
                'j': 'INTEGER'
            })))
        self.assertTrue((-i).is_equal(QM({'i': -1}, {}, 0, {'i': 'INTEGER'})))
        self.assertTrue((1 - i).is_equal(QM({'i': -1}, {}, 1,
                                            {'i': 'INTEGER'})))
        self.assertTrue((i - 1).is_equal(QM({'i': 1}, {}, -1,
                                            {'i': 'INTEGER'})))
        self.assertTrue(((i - j)**2).is_equal((i - j) * (i - j)))
        self.assertTrue(
            ((2 * i + 4 * i * j + 6) / 2.).is_equal(i + 2 * i * j + 3))
Пример #6
0
 def test_isub(self):
     qm = Integer('i')
     qm -= Integer('j')
     qm -= 5
     self.assertTrue(
         qm.is_equal(
             QM({
                 'i': 1,
                 'j': -1
             }, {}, -5, {
                 'i': 'INTEGER',
                 'j': 'INTEGER'
             })))
Пример #7
0
    def test_quadratic(self, dtype):
        qm = QM(dtype=dtype)
        qm.add_variable('INTEGER', 'a')
        qm.add_variable('SPIN', 'b')
        qm.add_variable('BINARY', 'c')

        qm.set_quadratic('a', 'b', 1)
        qm.set_quadratic('b', 'c', 2)

        self.assertEqual(qm.linear, {'a': 0, 'b': 0, 'c': 0})
        self.assertEqual(qm.quadratic, {'ab': 1, 'bc': 2})
        self.assertEqual(qm.adj, {
            'a': {
                'b': 1
            },
            'b': {
                'a': 1,
                'c': 2
            },
            'c': {
                'b': 2
            }
        })
Пример #8
0
 def test_empty(self, dtype):
     qm = QM(dtype=dtype)
     self.assertEqual(qm.linear, {})
     self.assertEqual(qm.quadratic, {})
     self.assertEqual(qm.adj, {})
Пример #9
0
 def test_cross_vartype(self, vartype0, vartype1):
     qm = QM()
     u = qm.add_variable(vartype0)
     with self.assertRaises(TypeError):
         qm.add_variable(vartype1, u)
Пример #10
0
    def test_degree(self):
        qm = QM()
        i = qm.add_variable('INTEGER')
        j = qm.add_variable('INTEGER')
        x = qm.add_variable('BINARY')

        qm.set_linear(i, 1.5)
        qm.set_linear(x, -2)

        qm.set_quadratic(i, j, 1)
        qm.set_quadratic(j, j, 5)
        qm.set_quadratic(x, i, 7)

        self.assertEqual(qm.degree(i), 2)
        self.assertEqual(qm.degree(j), 2)
        self.assertEqual(qm.degree(x), 1)
Пример #11
0
    def test_add_integer_with_contradicting_ub_lb(self):
        qm = QM()
        qm.add_variable('INTEGER', 'i', lower_bound=-1, upper_bound=103)

        with self.assertRaises(ValueError):
            qm.add_variable('INTEGER', 'i', lower_bound=-2)

        with self.assertRaises(ValueError):
            qm.add_variable('INTEGER', 'i', upper_bound=10)

        qm.add_variable('INTEGER', 'i', lower_bound=-1)
        qm.add_variable('INTEGER', 'i', upper_bound=103, lower_bound=-1)
Пример #12
0
 def test_invalid(self):
     qm = QM()
     a = qm.add_variable('INTEGER', 'a')
     with self.assertRaises(TypeError):
         qm.change_vartype('SPIN', a)
Пример #13
0
 def test_add_one(self, vartype_str, vartype):
     qm = QM()
     v = qm.add_variable(vartype_str)
     self.assertEqual(v, 0)
     self.assertIs(qm.vartype(v), vartype)
Пример #14
0
 def test_setting(self):
     qm = QM()
     qm.offset = 5
     self.assertEqual(qm.offset, 5)
     qm.offset -= 2
     self.assertEqual(qm.offset, 3)
Пример #15
0
def qmSpin(label):
    qm = QM()
    qm.set_linear(qm.add_variable('SPIN', label), 1)
    return qm
Пример #16
0
    def test_3var(self, dtype):
        qm = QM()
        qm.add_variable('INTEGER', 'i')
        qm.add_variable('BINARY', 'x')
        qm.add_variable('SPIN', 's')

        qm.set_linear('i', 3)
        qm.set_quadratic('s', 'i', 2)
        qm.set_quadratic('x', 's', -2)
        qm.set_quadratic('i', 'i', 5)
        qm.offset = 7

        with tempfile.TemporaryFile() as tf:
            with qm.to_file() as qmf:
                shutil.copyfileobj(qmf, tf)
            tf.seek(0)
            new = QM.from_file(tf)

        self.assertTrue(qm.is_equal(new))
        self.assertEqual(qm.dtype, new.dtype)
Пример #17
0
 def test_dtype(self):
     self.assertEqual(QM().dtype, np.float64)  # default
     self.assertEqual(QM(dtype=np.float32).dtype, np.float32)
     self.assertEqual(QM(dtype=np.float64).dtype, np.float64)
Пример #18
0
def qmBinary(label):
    qm = QM()
    qm.set_linear(qm.add_variable('BINARY', label), 1)
    return qm
Пример #19
0
    def test_min_max_sum(self):
        qm = QM()
        i = qm.add_variable('INTEGER')
        j = qm.add_variable('INTEGER')
        x = qm.add_variable('BINARY')

        qm.set_linear(i, 1.5)
        qm.set_linear(x, -2)

        qm.set_quadratic(i, j, 1)
        qm.set_quadratic(j, j, 5)
        qm.set_quadratic(x, i, 7)

        self.assertEqual(qm.linear.max(), 1.5)
        self.assertEqual(qm.linear.min(), -2)
        self.assertEqual(qm.linear.sum(), -.5)

        self.assertEqual(qm.quadratic.max(), 7)
        self.assertEqual(qm.quadratic.min(), 1)
        self.assertEqual(qm.quadratic.sum(), 13)

        self.assertEqual(qm.adj[j].max(), 5)
        self.assertEqual(qm.adj[j].min(), 1)
        self.assertEqual(qm.adj[j].sum(), 6)
Пример #20
0
    def test_add_integer_with_ub_lb(self):
        qm = QM()

        h = qm.add_variable('INTEGER', 'h')
        i = qm.add_variable('INTEGER', 'i', lower_bound=-1)
        j = qm.add_variable('INTEGER', 'j', upper_bound=103)
        k = qm.add_variable('INTEGER', 'k', lower_bound=-50, upper_bound=50)

        self.assertEqual(qm.lower_bound(i), -1)
        self.assertEqual(qm.upper_bound(i), qm.upper_bound(h))

        self.assertEqual(qm.lower_bound(j), qm.lower_bound(h))
        self.assertEqual(qm.upper_bound(j), 103)

        self.assertEqual(qm.lower_bound(k), -50)
        self.assertEqual(qm.upper_bound(k), 50)
Пример #21
0
 def test_existing_same_type(self, vartype_str, vartype):
     qm = QM()
     u = qm.add_variable(vartype_str)
     v = qm.add_variable(vartype_str, label=u)
     self.assertEqual(u, v)
Пример #22
0
    def test_triangle(self):
        qm = QM()
        qm.add_variables_from('SPIN', 'rstu')
        qm.add_variable('BINARY', 'x')
        qm.add_variable('INTEGER', 'i')

        qm.quadratic['sx'] = 5
        qm.quadratic['si'] = -3
        qm.quadratic['xi'] = 23
        qm.quadratic['ti'] = -7
        qm.quadratic['xu'] = 3
        qm.quadratic['rs'] = -12

        qm.offset = 1.5
        qm.linear['r'] = 5
        qm.linear['x'] = -4
        qm.linear['i'] = .25

        new = qm.spin_to_binary(inplace=False)

        rng = np.random.default_rng(42)

        for _ in range(10):
            sample = {}
            for v in qm.variables:
                if qm.vartype(v) == dimod.BINARY:
                    sample[v] = rng.choice((0, 1))
                elif qm.vartype(v) == dimod.SPIN:
                    sample[v] = rng.choice((-1, 1))
                elif qm.vartype(v) == dimod.INTEGER:
                    sample[v] = rng.choice(10)

            energy = qm.energy(sample)

            for v in qm.variables:
                if qm.vartype(v) == dimod.SPIN:
                    sample[v] = (sample[v] + 1) // 2

            self.assertEqual(energy, new.energy(sample))

        self.assertIs(qm.vartype('r'), dimod.SPIN)
        self.assertIs(new.vartype('r'), dimod.BINARY)
Пример #23
0
 def test_empty_offset(self):
     self.assertEqual(QM().offset, 0)
     self.assertEqual(QM(dtype=np.float64).offset.dtype, np.float64)