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. n_iter = 8 # a single converter. qp2qubo = QuadraticProgramToQubo() gmf = GroverOptimizer(4, num_iterations=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() converters = [ineq2eq, int2bin, penalize] gmf = GroverOptimizer(4, num_iterations=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=n_iter, quantum_instance=self.sv_simulator, converters=invalid)
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() converters = [ineq2eq, int2bin, penalize] 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)
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) _, shift = op.to_ising() self.assertEqual(shift, 0.0)
def test_0var_range_inequality(self): """ Test InequalityToEquality converter when the var_rang of the slack variable is 0""" op = QuadraticProgram() op.binary_var('x') op.binary_var('y') op.linear_constraint(linear={'x': 1, 'y': 1}, sense='LE', rhs=0, name='xy_leq1') op.linear_constraint(linear={'x': 1, 'y': 1}, sense='GE', rhs=2, name='xy_geq1') op.quadratic_constraint(quadratic={('x', 'x'): 1}, sense='LE', rhs=0, name='xy_leq2') op.quadratic_constraint(quadratic={('x', 'y'): 1}, sense='GE', rhs=1, name='xy_geq2') ineq2eq = InequalityToEquality() new_op = ineq2eq.convert(op) self.assertEqual(new_op.get_num_vars(), 2) self.assertTrue(all(l_const.sense == Constraint.Sense.EQ for l_const in new_op.linear_constraints)) self.assertTrue(all(q_const.sense == Constraint.Sense.EQ for q_const in new_op.quadratic_constraints))
def test_inequality_mode_auto(self): """ Test auto mode of InequalityToEqualityConverter() """ op = QuadraticProgram() for i in range(3): op.binary_var(name='x{}'.format(i)) # Linear constraints linear_constraint = {'x0': 1, 'x1': 1} op.linear_constraint(linear_constraint, Constraint.Sense.EQ, 1, 'x0x1') linear_constraint = {'x1': 1, 'x2': -1} op.linear_constraint(linear_constraint, Constraint.Sense.LE, 2, 'x1x2') linear_constraint = {'x0': 1.1, 'x2': 2.2} op.linear_constraint(linear_constraint, Constraint.Sense.GE, 3.3, 'x0x2') conv = InequalityToEquality(mode='auto') op2 = conv.convert(op) lst = [op2.variables[3].vartype, op2.variables[4].vartype] self.assertListEqual(lst, [Variable.Type.INTEGER, Variable.Type.CONTINUOUS])
def test_inequality_mode_continuous(self): """Test continuous mode of InequalityToEqualityConverter()""" op = QuadraticProgram() for i in range(3): op.binary_var(name="x{}".format(i)) # Linear constraints linear_constraint = {"x0": 1, "x1": 1} op.linear_constraint(linear_constraint, Constraint.Sense.EQ, 1, "x0x1") linear_constraint = {"x1": 1, "x2": -1} op.linear_constraint(linear_constraint, Constraint.Sense.LE, 2, "x1x2") linear_constraint = {"x0": 1, "x2": 3} op.linear_constraint(linear_constraint, Constraint.Sense.GE, 2, "x0x2") conv = InequalityToEquality(mode="continuous") op2 = conv.convert(op) lst = [op2.variables[3].vartype, op2.variables[4].vartype] self.assertListEqual( lst, [Variable.Type.CONTINUOUS, Variable.Type.CONTINUOUS])
def test_inequality_mode_integer(self): """Test integer mode of InequalityToEqualityConverter()""" op = QuadraticProgram() for i in range(3): op.binary_var(name=f"x{i}") # Linear constraints linear_constraint = {"x0": 1, "x1": 1} op.linear_constraint(linear_constraint, Constraint.Sense.EQ, 1, "x0x1") linear_constraint = {"x1": 1, "x2": -1} op.linear_constraint(linear_constraint, Constraint.Sense.LE, 2, "x1x2") linear_constraint = {"x0": 1, "x2": 3} op.linear_constraint(linear_constraint, Constraint.Sense.GE, 2, "x0x2") conv = InequalityToEquality(mode="integer") op2 = conv.convert(op) lst = [op2.variables[3].vartype, op2.variables[4].vartype] self.assertListEqual(lst, [Variable.Type.INTEGER, Variable.Type.INTEGER])
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={ "y": 1, "x": 1 }, sense="LE", rhs=3, name="xy_leq") # construct minimum eigen optimizer min_eigen_solver = NumPyMinimumEigensolver() min_eigen_optimizer = MinimumEigenOptimizer(min_eigen_solver) # a single converter qp2qubo = QuadraticProgramToQubo() recursive_min_eigen_optimizer = RecursiveMinimumEigenOptimizer( min_eigen_optimizer, min_num_vars=2, converters=qp2qubo) result = recursive_min_eigen_optimizer.solve(op) self.assertEqual(result.fval, 4) # a list of converters ineq2eq = InequalityToEquality() int2bin = IntegerToBinary() penalize = LinearEqualityToPenalty() converters = [ineq2eq, int2bin, penalize] recursive_min_eigen_optimizer = RecursiveMinimumEigenOptimizer( min_eigen_optimizer, min_num_vars=2, converters=converters) result = recursive_min_eigen_optimizer.solve(op) self.assertEqual(result.fval, 4) # invalid converters with self.assertRaises(TypeError): invalid = [qp2qubo, "invalid converter"] RecursiveMinimumEigenOptimizer(min_eigen_optimizer, min_num_vars=2, converters=invalid)
def test_0var_range_inequality(self): """Test InequalityToEquality converter when the var_rang of the slack variable is 0""" op = QuadraticProgram() op.binary_var("x") op.binary_var("y") op.linear_constraint(linear={ "x": 1, "y": 1 }, sense="LE", rhs=0, name="xy_leq1") op.linear_constraint(linear={ "x": 1, "y": 1 }, sense="GE", rhs=2, name="xy_geq1") op.quadratic_constraint(quadratic={("x", "x"): 1}, sense="LE", rhs=0, name="xy_leq2") op.quadratic_constraint(quadratic={("x", "y"): 1}, sense="GE", rhs=1, name="xy_geq2") ineq2eq = InequalityToEquality() new_op = ineq2eq.convert(op) self.assertEqual(new_op.get_num_vars(), 2) self.assertTrue( all(l_const.sense == Constraint.Sense.EQ for l_const in new_op.linear_constraints)) self.assertTrue( all(q_const.sense == Constraint.Sense.EQ for q_const in new_op.quadratic_constraints))
def test_inequality_binary(self): """Test InequalityToEqualityConverter with binary variables""" op = QuadraticProgram() for i in range(3): op.binary_var(name="x{}".format(i)) # Linear constraints linear_constraint = {"x0": 1, "x1": 1} op.linear_constraint(linear_constraint, Constraint.Sense.EQ, 1, "x0x1") linear_constraint = {"x1": 1, "x2": -1} op.linear_constraint(linear_constraint, Constraint.Sense.LE, 2, "x1x2") linear_constraint = {"x0": 1, "x2": 3} op.linear_constraint(linear_constraint, Constraint.Sense.GE, 2, "x0x2") # Quadratic constraints quadratic = {("x0", "x1"): 1, ("x1", "x2"): 2} op.quadratic_constraint({}, quadratic, Constraint.Sense.LE, 3, "x0x1_x1x2LE") quadratic = {("x0", "x1"): 3, ("x1", "x2"): 4} op.quadratic_constraint({}, quadratic, Constraint.Sense.GE, 3, "x0x1_x1x2GE") # Convert inequality constraints into equality constraints conv = InequalityToEquality() op2 = conv.convert(op) self.assertListEqual( [v.name for v in op2.variables], [ "x0", "x1", "x2", "x1x2@int_slack", "x0x2@int_slack", "x0x1_x1x2LE@int_slack", "x0x1_x1x2GE@int_slack", ], ) # Check names and objective senses self.assertEqual(op.name, op2.name) self.assertEqual(op.objective.sense, op2.objective.sense) # For linear constraints lst = [ op2.linear_constraints[0].linear.to_dict()[0], op2.linear_constraints[0].linear.to_dict()[1], ] self.assertListEqual(lst, [1, 1]) self.assertEqual(op2.linear_constraints[0].sense, Constraint.Sense.EQ) lst = [ op2.linear_constraints[1].linear.to_dict()[1], op2.linear_constraints[1].linear.to_dict()[2], op2.linear_constraints[1].linear.to_dict()[3], ] self.assertListEqual(lst, [1, -1, 1]) lst = [op2.variables[3].lowerbound, op2.variables[3].upperbound] self.assertListEqual(lst, [0, 3]) self.assertEqual(op2.linear_constraints[1].sense, Constraint.Sense.EQ) lst = [ op2.linear_constraints[2].linear.to_dict()[0], op2.linear_constraints[2].linear.to_dict()[2], op2.linear_constraints[2].linear.to_dict()[4], ] self.assertListEqual(lst, [1, 3, -1]) lst = [op2.variables[4].lowerbound, op2.variables[4].upperbound] self.assertListEqual(lst, [0, 2]) self.assertEqual(op2.linear_constraints[2].sense, Constraint.Sense.EQ) # For quadratic constraints lst = [ op2.quadratic_constraints[0].quadratic.to_dict()[(0, 1)], op2.quadratic_constraints[0].quadratic.to_dict()[(1, 2)], op2.quadratic_constraints[0].linear.to_dict()[5], ] self.assertListEqual(lst, [1, 2, 1]) lst = [op2.variables[5].lowerbound, op2.variables[5].upperbound] self.assertListEqual(lst, [0, 3]) lst = [ op2.quadratic_constraints[1].quadratic.to_dict()[(0, 1)], op2.quadratic_constraints[1].quadratic.to_dict()[(1, 2)], op2.quadratic_constraints[1].linear.to_dict()[6], ] self.assertListEqual(lst, [3, 4, -1]) lst = [op2.variables[6].lowerbound, op2.variables[6].upperbound] self.assertListEqual(lst, [0, 4]) new_x = conv.interpret(np.arange(7)) np.testing.assert_array_almost_equal(new_x, np.arange(3))
def test_inequality_integer(self): """ Test InequalityToEqualityConverter with integer variables """ op = QuadraticProgram() for i in range(3): op.integer_var(name='x{}'.format(i), lowerbound=-3, upperbound=3) # Linear constraints linear_constraint = {'x0': 1, 'x1': 1} op.linear_constraint(linear_constraint, Constraint.Sense.EQ, 1, 'x0x1') linear_constraint = {'x1': 1, 'x2': -1} op.linear_constraint(linear_constraint, Constraint.Sense.LE, 2, 'x1x2') linear_constraint = {'x0': 1, 'x2': 3} op.linear_constraint(linear_constraint, Constraint.Sense.GE, 2, 'x0x2') # Quadratic constraints quadratic = {('x0', 'x1'): 1, ('x1', 'x2'): 2} op.quadratic_constraint({}, quadratic, Constraint.Sense.LE, 3, 'x0x1_x1x2LE') quadratic = {('x0', 'x1'): 3, ('x1', 'x2'): 4} op.quadratic_constraint({}, quadratic, Constraint.Sense.GE, 3, 'x0x1_x1x2GE') conv = InequalityToEquality() op2 = conv.convert(op) self.assertListEqual([v.name for v in op2.variables], ['x0', 'x1', 'x2', 'x1x2@int_slack', 'x0x2@int_slack', 'x0x1_x1x2LE@int_slack', 'x0x1_x1x2GE@int_slack']) # For linear constraints lst = [ op2.linear_constraints[0].linear.to_dict()[0], op2.linear_constraints[0].linear.to_dict()[1], ] self.assertListEqual(lst, [1, 1]) self.assertEqual(op2.linear_constraints[0].sense, Constraint.Sense.EQ) lst = [ op2.linear_constraints[1].linear.to_dict()[1], op2.linear_constraints[1].linear.to_dict()[2], op2.linear_constraints[1].linear.to_dict()[3], ] self.assertListEqual(lst, [1, -1, 1]) lst = [op2.variables[3].lowerbound, op2.variables[3].upperbound] self.assertListEqual(lst, [0, 8]) self.assertEqual(op2.linear_constraints[1].sense, Constraint.Sense.EQ) lst = [ op2.linear_constraints[2].linear.to_dict()[0], op2.linear_constraints[2].linear.to_dict()[2], op2.linear_constraints[2].linear.to_dict()[4], ] self.assertListEqual(lst, [1, 3, -1]) lst = [op2.variables[4].lowerbound, op2.variables[4].upperbound] self.assertListEqual(lst, [0, 10]) self.assertEqual(op2.linear_constraints[2].sense, Constraint.Sense.EQ) # For quadratic constraints lst = [ op2.quadratic_constraints[0].quadratic.to_dict()[(0, 1)], op2.quadratic_constraints[0].quadratic.to_dict()[(1, 2)], op2.quadratic_constraints[0].linear.to_dict()[5], ] self.assertListEqual(lst, [1, 2, 1]) lst = [op2.variables[5].lowerbound, op2.variables[5].upperbound] self.assertListEqual(lst, [0, 30]) lst = [ op2.quadratic_constraints[1].quadratic.to_dict()[(0, 1)], op2.quadratic_constraints[1].quadratic.to_dict()[(1, 2)], op2.quadratic_constraints[1].linear.to_dict()[6], ] self.assertListEqual(lst, [3, 4, -1]) lst = [op2.variables[6].lowerbound, op2.variables[6].upperbound] self.assertListEqual(lst, [0, 60]) new_x = conv.interpret(np.arange(7)) np.testing.assert_array_almost_equal(new_x, np.arange(3))