Exemplo n.º 1
0
    def test_empty_name(self):
        """Test empty names"""

        with self.subTest("problem name"):
            q_p = QuadraticProgram("")
            self.assertEqual(q_p.name, "")

        with self.subTest("variable name"):
            q_p = QuadraticProgram()
            x = q_p.binary_var(name="")
            y = q_p.integer_var(name="")
            z = q_p.continuous_var(name="")
            self.assertEqual(x.name, "x0")
            self.assertEqual(y.name, "x1")
            self.assertEqual(z.name, "x2")

        with self.subTest("variable name 2"):
            q_p = QuadraticProgram()
            w = q_p.binary_var(name="w")
            x = q_p.binary_var(name="")
            y = q_p.integer_var(name="")
            z = q_p.continuous_var(name="")
            self.assertEqual(w.name, "w")
            self.assertEqual(x.name, "x1")
            self.assertEqual(y.name, "x2")
            self.assertEqual(z.name, "x3")

        with self.subTest("variable name list"):
            q_p = QuadraticProgram()
            x = q_p.binary_var_list(2, name="")
            y = q_p.integer_var_list(2, name="")
            z = q_p.continuous_var_list(2, name="")
            self.assertListEqual([v.name for v in x], ["x0", "x1"])
            self.assertListEqual([v.name for v in y], ["x2", "x3"])
            self.assertListEqual([v.name for v in z], ["x4", "x5"])

        with self.subTest("variable name dict"):
            q_p = QuadraticProgram()
            x = q_p.binary_var_dict(2, name="")
            y = q_p.integer_var_dict(2, name="")
            z = q_p.continuous_var_dict(2, name="")
            self.assertDictEqual({k: v.name for k, v in x.items()}, {"x0": "x0", "x1": "x1"})
            self.assertDictEqual({k: v.name for k, v in y.items()}, {"x2": "x2", "x3": "x3"})
            self.assertDictEqual({k: v.name for k, v in z.items()}, {"x4": "x4", "x5": "x5"})

        with self.subTest("linear constraint name"):
            q_p = QuadraticProgram()
            x = q_p.linear_constraint(name="")
            y = q_p.linear_constraint(name="")
            self.assertEqual(x.name, "c0")
            self.assertEqual(y.name, "c1")

        with self.subTest("quadratic constraint name"):
            q_p = QuadraticProgram()
            x = q_p.quadratic_constraint(name="")
            y = q_p.quadratic_constraint(name="")
            self.assertEqual(x.name, "q0")
            self.assertEqual(y.name, "q1")
Exemplo n.º 2
0
    def test_printable_name(self):
        """Test non-printable names"""
        name = "\n"

        with self.assertWarns(UserWarning):
            _ = QuadraticProgram(name)

        q_p = QuadraticProgram()

        with self.assertWarns(UserWarning):
            q_p.binary_var(name + "bin")

        with self.assertWarns(UserWarning):
            q_p.binary_var_list(10, name)

        with self.assertWarns(UserWarning):
            q_p.binary_var_dict(10, name)

        with self.assertWarns(UserWarning):
            q_p.integer_var(0, 1, name + "int")

        with self.assertWarns(UserWarning):
            q_p.integer_var_list(10, 0, 1, name)

        with self.assertWarns(UserWarning):
            q_p.integer_var_dict(10, 0, 1, name)

        with self.assertWarns(UserWarning):
            q_p.continuous_var(0, 1, name + "cont")

        with self.assertWarns(UserWarning):
            q_p.continuous_var_list(10, 0, 1, name)

        with self.assertWarns(UserWarning):
            q_p.continuous_var_dict(10, 0, 1, name)

        with self.assertWarns(UserWarning):
            q_p.linear_constraint(name=name)

        with self.assertWarns(UserWarning):
            q_p.quadratic_constraint(name=name)
    def test_var_dict(self):
        """test {binary,integer,continuous}_var_dict"""
        q_p = QuadraticProgram()

        c_count = 0
        b_count = 0
        i_count = 0

        def verify_counts():
            self.assertEqual(q_p.get_num_vars(), c_count + b_count + i_count)
            self.assertEqual(q_p.get_num_continuous_vars(), c_count)
            self.assertEqual(q_p.get_num_binary_vars(), b_count)
            self.assertEqual(q_p.get_num_integer_vars(), i_count)

        def check_dict(var_dict, offset):
            verify_counts()
            variables = var_dict.values()
            for i, x in enumerate(variables):
                y = q_p.get_variable(i + offset)
                z = q_p.get_variable(x.name)
                self.assert_equal(x, y)
                self.assert_equal(x, z)

        d_0 = q_p.continuous_var_dict(name='a', key_format='_{}', keys=3)
        c_count += 3
        check_dict(d_0, 0)

        d_1 = q_p.binary_var_dict(name='b', keys=5)
        b_count += 5
        check_dict(d_1, len(d_0))

        d_2 = q_p.integer_var_dict(key_format='_{}',
                                   keys=7,
                                   lowerbound=-4,
                                   upperbound=10)
        i_count += 7
        check_dict(d_2, len(d_0) + len(d_1))

        d_3 = q_p.continuous_var_dict(name='a', key_format='_{}', keys=3)
        c_count += 3
        check_dict(d_3, len(d_0) + len(d_1) + len(d_2))

        d_4 = q_p.continuous_var_dict(name='c', keys=range(3))
        c_count += 3
        check_dict(d_4, len(d_0) + len(d_1) + len(d_2) + len(d_3))

        with self.assertRaises(QiskitOptimizationError):
            q_p.binary_var_dict(name='c0')

        with self.assertRaises(QiskitOptimizationError):
            q_p.binary_var_dict(name='c', keys=range(3))

        with self.assertRaises(QiskitOptimizationError):
            q_p = QuadraticProgram()
            q_p.binary_var_dict(key_format='{}{}')

        with self.assertRaises(QiskitOptimizationError):
            q_p = QuadraticProgram()
            q_p.binary_var_dict(keys=0)

        with self.assertRaises(QiskitOptimizationError):
            q_p = QuadraticProgram()
            q_p.binary_var_dict(name='a')
            q_p.binary_var_dict(name='a')

        with self.assertRaises(QiskitOptimizationError):
            q_p = QuadraticProgram()
            q_p.binary_var_dict(key_format='_{{}}')
Exemplo n.º 4
0
    def test_var_dict(self):
        """test {binary,integer,continuous}_var_dict"""
        q_p = QuadraticProgram()

        d_0 = q_p.continuous_var_dict(name="a", key_format="_{}", keys=3)
        self.assertSetEqual(set(d_0.keys()), {"a_0", "a_1", "a_2"})
        self.assertSetEqual({var.name
                             for var in q_p.variables}, {"a_0", "a_1", "a_2"})
        for var in q_p.variables:
            self.assertAlmostEqual(var.lowerbound, 0)
            self.assertAlmostEqual(var.upperbound, INFINITY)
            self.assertEqual(var.vartype, VarType.CONTINUOUS)
            self.assertTupleEqual(var.as_tuple(), d_0[var.name].as_tuple())

        d_1 = q_p.binary_var_dict(name="b", keys=5)
        self.assertSetEqual(set(d_1.keys()), {"b3", "b4", "b5", "b6", "b7"})
        self.assertSetEqual(
            {var.name
             for var in q_p.variables},
            {"a_0", "a_1", "a_2", "b3", "b4", "b5", "b6", "b7"},
        )
        for var in q_p.variables[-5:]:
            self.assertAlmostEqual(var.lowerbound, 0)
            self.assertAlmostEqual(var.upperbound, 1)
            self.assertEqual(var.vartype, VarType.BINARY)
            self.assertTupleEqual(var.as_tuple(), d_1[var.name].as_tuple())

        d_2 = q_p.integer_var_dict(keys=1,
                                   key_format="-{}",
                                   lowerbound=-4,
                                   upperbound=10)
        self.assertSetEqual(set(d_2.keys()), {"x-8"})
        self.assertSetEqual(
            {var.name
             for var in q_p.variables},
            {"a_0", "a_1", "a_2", "b3", "b4", "b5", "b6", "b7", "x-8"},
        )
        for var in q_p.variables[-1:]:
            self.assertAlmostEqual(var.lowerbound, -4)
            self.assertAlmostEqual(var.upperbound, 10)
            self.assertEqual(var.vartype, VarType.INTEGER)
            self.assertTupleEqual(var.as_tuple(), d_2[var.name].as_tuple())

        d_3 = q_p.binary_var_dict(name="c", keys=range(3))
        self.assertSetEqual(set(d_3.keys()), {"c0", "c1", "c2"})
        self.assertSetEqual(
            {var.name
             for var in q_p.variables},
            {
                "a_0",
                "a_1",
                "a_2",
                "b3",
                "b4",
                "b5",
                "b6",
                "b7",
                "x-8",
                "c0",
                "c1",
                "c2",
            },
        )
        for var in q_p.variables[-3:]:
            self.assertAlmostEqual(var.lowerbound, 0)
            self.assertAlmostEqual(var.upperbound, 1)
            self.assertEqual(var.vartype, VarType.BINARY)
            self.assertTupleEqual(var.as_tuple(), d_3[var.name].as_tuple())

        with self.assertRaises(QiskitOptimizationError):
            q_p.binary_var_dict(name="c", keys=range(3))

        d_4 = q_p.binary_var_dict(1, "x", "_")
        self.assertSetEqual(set(d_4.keys()), {"x_"})
        self.assertSetEqual(
            {var.name
             for var in q_p.variables},
            {
                "a_0",
                "a_1",
                "a_2",
                "b3",
                "b4",
                "b5",
                "b6",
                "b7",
                "x-8",
                "c0",
                "c1",
                "c2",
                "x_",
            },
        )
        for var in q_p.variables[-1:]:
            self.assertAlmostEqual(var.lowerbound, 0)
            self.assertAlmostEqual(var.upperbound, 1)
            self.assertEqual(var.vartype, VarType.BINARY)
            self.assertTupleEqual(var.as_tuple(), d_4[var.name].as_tuple())

        with self.assertRaises(QiskitOptimizationError):
            q_p.binary_var_dict(1, "x", "_")

        with self.assertRaises(QiskitOptimizationError):
            q_p.binary_var("x_")

        d_5 = q_p.continuous_var_dict(1, -1, 2, "", "")
        self.assertSetEqual(set(d_5.keys()), {""})
        self.assertSetEqual(
            {var.name
             for var in q_p.variables},
            {
                "a_0",
                "a_1",
                "a_2",
                "b3",
                "b4",
                "b5",
                "b6",
                "b7",
                "x-8",
                "c0",
                "c1",
                "c2",
                "x_",
                "",
            },
        )
        for var in q_p.variables[-1:]:
            self.assertAlmostEqual(var.lowerbound, -1)
            self.assertAlmostEqual(var.upperbound, 2)
            self.assertEqual(var.vartype, VarType.CONTINUOUS)
            self.assertTupleEqual(var.as_tuple(), d_5[var.name].as_tuple())

        with self.assertRaises(QiskitOptimizationError):
            q_p.binary_var_dict(1, "", "")

        with self.assertRaises(QiskitOptimizationError):
            q_p.integer_var(0, 1, "")

        with self.assertRaises(QiskitOptimizationError):
            q_p = QuadraticProgram()
            q_p.binary_var_dict(keys=1, key_format="{}{}")

        with self.assertRaises(QiskitOptimizationError):
            q_p = QuadraticProgram()
            q_p.binary_var_dict(keys=0)

        with self.assertRaises(QiskitOptimizationError):
            q_p = QuadraticProgram()
            q_p.binary_var_dict(keys=1, key_format="_{{}}")

        with self.assertRaises(QiskitOptimizationError):
            q_p = QuadraticProgram()
            q_p.binary_var_dict(keys=2, key_format="")

        with self.assertRaises(QiskitOptimizationError):
            q_p = QuadraticProgram()
            q_p.binary_var_dict(keys=range(2), key_format="")
    def test_var_dict(self):
        """test {binary,integer,continuous}_var_dict"""
        q_p = QuadraticProgram()

        d_0 = q_p.continuous_var_dict(name='a', key_format='_{}', keys=3)
        self.assertSetEqual(set(d_0.keys()), {'a_0', 'a_1', 'a_2'})
        self.assertSetEqual({var.name
                             for var in q_p.variables}, {'a_0', 'a_1', 'a_2'})
        for var in q_p.variables:
            self.assertAlmostEqual(var.lowerbound, 0)
            self.assertAlmostEqual(var.upperbound, INFINITY)
            self.assertEqual(var.vartype, VarType.CONTINUOUS)
            self.assertTupleEqual(var.as_tuple(), d_0[var.name].as_tuple())

        d_1 = q_p.binary_var_dict(name='b', keys=5)
        self.assertSetEqual(set(d_1.keys()), {'b3', 'b4', 'b5', 'b6', 'b7'})
        self.assertSetEqual(
            {var.name
             for var in q_p.variables},
            {'a_0', 'a_1', 'a_2', 'b3', 'b4', 'b5', 'b6', 'b7'})
        for var in q_p.variables[-5:]:
            self.assertAlmostEqual(var.lowerbound, 0)
            self.assertAlmostEqual(var.upperbound, 1)
            self.assertEqual(var.vartype, VarType.BINARY)
            self.assertTupleEqual(var.as_tuple(), d_1[var.name].as_tuple())

        d_2 = q_p.integer_var_dict(keys=1,
                                   key_format='-{}',
                                   lowerbound=-4,
                                   upperbound=10)
        self.assertSetEqual(set(d_2.keys()), {'x-8'})
        self.assertSetEqual(
            {var.name
             for var in q_p.variables},
            {'a_0', 'a_1', 'a_2', 'b3', 'b4', 'b5', 'b6', 'b7', 'x-8'})
        for var in q_p.variables[-1:]:
            self.assertAlmostEqual(var.lowerbound, -4)
            self.assertAlmostEqual(var.upperbound, 10)
            self.assertEqual(var.vartype, VarType.INTEGER)
            self.assertTupleEqual(var.as_tuple(), d_2[var.name].as_tuple())

        d_3 = q_p.binary_var_dict(name='c', keys=range(3))
        self.assertSetEqual(set(d_3.keys()), {'c0', 'c1', 'c2'})
        self.assertSetEqual({var.name
                             for var in q_p.variables}, {
                                 'a_0', 'a_1', 'a_2', 'b3', 'b4', 'b5', 'b6',
                                 'b7', 'x-8', 'c0', 'c1', 'c2'
                             })
        for var in q_p.variables[-3:]:
            self.assertAlmostEqual(var.lowerbound, 0)
            self.assertAlmostEqual(var.upperbound, 1)
            self.assertEqual(var.vartype, VarType.BINARY)
            self.assertTupleEqual(var.as_tuple(), d_3[var.name].as_tuple())

        with self.assertRaises(QiskitOptimizationError):
            q_p.binary_var_dict(name='c', keys=range(3))

        d_4 = q_p.binary_var_dict(1, 'x', '_')
        self.assertSetEqual(set(d_4.keys()), {'x_'})
        self.assertSetEqual({var.name
                             for var in q_p.variables}, {
                                 'a_0', 'a_1', 'a_2', 'b3', 'b4', 'b5', 'b6',
                                 'b7', 'x-8', 'c0', 'c1', 'c2', 'x_'
                             })
        for var in q_p.variables[-1:]:
            self.assertAlmostEqual(var.lowerbound, 0)
            self.assertAlmostEqual(var.upperbound, 1)
            self.assertEqual(var.vartype, VarType.BINARY)
            self.assertTupleEqual(var.as_tuple(), d_4[var.name].as_tuple())

        with self.assertRaises(QiskitOptimizationError):
            q_p.binary_var_dict(1, 'x', '_')

        with self.assertRaises(QiskitOptimizationError):
            q_p.binary_var('x_')

        d_5 = q_p.continuous_var_dict(1, -1, 2, '', '')
        self.assertSetEqual(set(d_5.keys()), {''})
        self.assertSetEqual({var.name
                             for var in q_p.variables}, {
                                 'a_0', 'a_1', 'a_2', 'b3', 'b4', 'b5', 'b6',
                                 'b7', 'x-8', 'c0', 'c1', 'c2', 'x_', ''
                             })
        for var in q_p.variables[-1:]:
            self.assertAlmostEqual(var.lowerbound, -1)
            self.assertAlmostEqual(var.upperbound, 2)
            self.assertEqual(var.vartype, VarType.CONTINUOUS)
            self.assertTupleEqual(var.as_tuple(), d_5[var.name].as_tuple())

        with self.assertRaises(QiskitOptimizationError):
            q_p.binary_var_dict(1, '', '')

        with self.assertRaises(QiskitOptimizationError):
            q_p.integer_var(0, 1, '')

        with self.assertRaises(QiskitOptimizationError):
            q_p = QuadraticProgram()
            q_p.binary_var_dict(keys=1, key_format='{}{}')

        with self.assertRaises(QiskitOptimizationError):
            q_p = QuadraticProgram()
            q_p.binary_var_dict(keys=0)

        with self.assertRaises(QiskitOptimizationError):
            q_p = QuadraticProgram()
            q_p.binary_var_dict(keys=1, key_format='_{{}}')

        with self.assertRaises(QiskitOptimizationError):
            q_p = QuadraticProgram()
            q_p.binary_var_dict(keys=2, key_format='')

        with self.assertRaises(QiskitOptimizationError):
            q_p = QuadraticProgram()
            q_p.binary_var_dict(keys=range(2), key_format='')