def test_quadratic_program_to_qubo_inequality_to_penalty(self): """Test QuadraticProgramToQubo, passing inequality pattern""" op = QuadraticProgram() conv = QuadraticProgramToQubo() op.binary_var(name="x") op.binary_var(name="y") # Linear constraints linear_constraint = {"x": 1, "y": 1} op.linear_constraint(linear_constraint, Constraint.Sense.GE, 1, "P(1-x-y+xy)") conv.penalty = 1 constant = 1 linear = {"x": -conv.penalty, "y": -conv.penalty} quadratic = {("x", "y"): conv.penalty} op2 = conv.convert(op) cnst = op2.objective.constant ldct = op2.objective.linear.to_dict(use_name=True) qdct = op2.objective.quadratic.to_dict(use_name=True) self.assertEqual(cnst, constant) self.assertEqual(ldct, linear) self.assertEqual(qdct, quadratic) self.assertEqual(op2.get_num_linear_constraints(), 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. 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 convert_qubo(qubo, linear, quadratic, penalty_multiplier=1): max_coeff = max(max(linear.values()), max(quadratic.values())) penalty = np.floor(max_coeff * penalty_multiplier) converter = QuadraticProgramToQubo(penalty=penalty) qubo = converter.convert(qubo) obj = qubo.objective linear = obj.linear.to_array() max_linear = np.amax(linear) quadratic = obj.quadratic.to_array() max_quadratic = np.amax(quadratic) max_coeff = max(max_linear, max_quadratic) qubo.objective.quadratic._coefficients /= max_coeff qubo.objective.linear._coefficients /= max_coeff qubo_constant = qubo.objective._constant qubo.objective._constant = 0 return qubo, max_coeff, qubo_constant
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)