Ejemplo n.º 1
0
    def test_minimization_problem(self):
        """Tests the optimizer with a minimization problem"""
        optimizer = GoemansWilliamsonOptimizer(num_cuts=10, seed=0)

        problem = Maxcut(self.graph).to_quadratic_program()

        # artificially convert to minimization
        max2min = MaximizeToMinimize()
        problem = max2min.convert(problem)

        results = optimizer.solve(problem)

        np.testing.assert_almost_equal([0, 1, 1, 0], results.x, 3)
        np.testing.assert_almost_equal(4, results.fval, 3)
    def test_converter_list(self):
        """Test converter list"""
        op = QuadraticProgram()
        op.integer_var(0, 3, "x")
        op.binary_var("y")

        op.maximize(linear={"x": 1, "y": 2})
        op.linear_constraint(linear={
            "x": 1,
            "y": 1
        },
                             sense="LE",
                             rhs=3,
                             name="xy_leq")
        min_eigen_solver = NumPyMinimumEigensolver()
        # a single converter
        qp2qubo = QuadraticProgramToQubo()
        min_eigen_optimizer = MinimumEigenOptimizer(min_eigen_solver,
                                                    converters=qp2qubo)
        result = min_eigen_optimizer.solve(op)
        self.assertEqual(result.fval, 4)
        # a list of converters
        ineq2eq = InequalityToEquality()
        int2bin = IntegerToBinary()
        penalize = LinearEqualityToPenalty()
        max2min = MaximizeToMinimize()
        converters = [ineq2eq, int2bin, penalize, max2min]
        min_eigen_optimizer = MinimumEigenOptimizer(min_eigen_solver,
                                                    converters=converters)
        result = min_eigen_optimizer.solve(op)
        self.assertEqual(result.fval, 4)
        with self.assertRaises(TypeError):
            invalid = [qp2qubo, "invalid converter"]
            MinimumEigenOptimizer(min_eigen_solver, converters=invalid)
Ejemplo n.º 3
0
 def test_maximize_to_minimize(self):
     """Test maximization to minimization conversion"""
     op_max = QuadraticProgram()
     op_min = QuadraticProgram()
     for i in range(2):
         op_max.binary_var(name="x{}".format(i))
         op_min.binary_var(name="x{}".format(i))
     op_max.integer_var(name="x{}".format(2), lowerbound=-3, upperbound=3)
     op_min.integer_var(name="x{}".format(2), lowerbound=-3, upperbound=3)
     op_max.maximize(constant=3,
                     linear={"x0": 1},
                     quadratic={("x1", "x2"): 2})
     op_min.minimize(constant=3,
                     linear={"x0": 1},
                     quadratic={("x1", "x2"): 2})
     # check conversion of maximization problem
     conv = MaximizeToMinimize()
     op_conv = conv.convert(op_max)
     self.assertEqual(op_conv.objective.sense,
                      op_conv.objective.Sense.MINIMIZE)
     x = [0, 1, 2]
     fval_min = op_conv.objective.evaluate(conv.interpret(x))
     self.assertAlmostEqual(fval_min, -7)
     self.assertAlmostEqual(op_max.objective.evaluate(x), -fval_min)
     # check conversion of minimization problem
     op_conv = conv.convert(op_min)
     self.assertEqual(op_conv.objective.sense, op_min.objective.sense)
     fval_min = op_conv.objective.evaluate(conv.interpret(x))
     self.assertAlmostEqual(op_min.objective.evaluate(x), fval_min)
Ejemplo n.º 4
0
 def test_empty_problem(self):
     """Test empty problem"""
     op = QuadraticProgram()
     conv = InequalityToEquality()
     op = conv.convert(op)
     conv = IntegerToBinary()
     op = conv.convert(op)
     conv = LinearEqualityToPenalty()
     op = conv.convert(op)
     conv = MaximizeToMinimize()
     op = conv.convert(op)
     _, shift = op.to_ising()
     self.assertEqual(shift, 0.0)
Ejemplo n.º 5
0
    def test_converter_list(self):
        """Test converters list"""
        # Input.

        model = Model()
        x_0 = model.binary_var(name="x0")
        x_1 = model.binary_var(name="x1")
        model.maximize(-x_0 + 2 * x_1)
        op = QuadraticProgram()
        op.from_docplex(model)

        # Get the optimum key and value.
        # a single converter.
        qp2qubo = QuadraticProgramToQubo()
        gmf = GroverOptimizer(
            4,
            num_iterations=self.n_iter,
            quantum_instance=self.sv_simulator,
            converters=qp2qubo,
        )
        results = gmf.solve(op)
        self.validate_results(op, results)

        # a list of converters
        ineq2eq = InequalityToEquality()
        int2bin = IntegerToBinary()
        penalize = LinearEqualityToPenalty()
        max2min = MaximizeToMinimize()
        converters = [ineq2eq, int2bin, penalize, max2min]
        gmf = GroverOptimizer(
            4,
            num_iterations=self.n_iter,
            quantum_instance=self.sv_simulator,
            converters=converters,
        )
        results = gmf.solve(op)
        self.validate_results(op, results)
        # invalid converters
        with self.assertRaises(TypeError):
            invalid = [qp2qubo, "invalid converter"]
            GroverOptimizer(
                4,
                num_iterations=self.n_iter,
                quantum_instance=self.sv_simulator,
                converters=invalid,
            )