def build_knapsack_cqm(costs, weights, max_weight): """Construct a CQM for the knapsack problem. Args: costs (array-like): Array of costs for the items. weights (array-like): Array of weights for the items. max_weight (int): Maximum allowable weight for the knapsack. Returns: Constrained quadratic model instance that represents the knapsack problem. """ num_items = len(costs) print("\nBuilding a CQM for {} items.".format(str(num_items))) cqm = ConstrainedQuadraticModel() obj = BinaryQuadraticModel(vartype='BINARY') constraint = QuadraticModel() for i in range(num_items): # Objective is to maximize the total costs obj.add_variable(i) obj.set_linear(i, -costs[i]) # Constraint is to keep the sum of items' weights under or equal capacity constraint.add_variable('BINARY', i) constraint.set_linear(i, weights[i]) cqm.set_objective(obj) cqm.add_constraint(constraint, sense="<=", rhs=max_weight, label='capacity') return cqm
def anti_crossing_loops(num_variables): """Anti crossing problems with two loops. These instances are copies of the instance studied in [DJA]_. Args: num_variables (int): Number of variables used to generate the problem. Must be an even number greater than 8. Returns: :obj:`.BinaryQuadraticModel`. .. [DJA] Dickson, N., Johnson, M., Amin, M. et al. Thermally assisted quantum annealing of a 16-qubit problem. Nat Commun 4, 1903 (2013). https://doi.org/10.1038/ncomms2920 """ bqm = BinaryQuadraticModel({}, {}, 0, 'SPIN') if num_variables % 2 != 0 or num_variables < 8: raise ValueError('num_variables must be an even number > 8') hf = int(num_variables / 4) for n in range(hf): if n % 2 == 1: bqm.set_quadratic(n, n + hf, -1) bqm.set_quadratic(n, (n + 1) % hf, -1) bqm.set_quadratic(n + hf, (n + 1) % hf + hf, -1) bqm.set_quadratic(n, n + 2 * hf, -1) bqm.set_quadratic(n + hf, n + 3 * hf, -1) bqm.add_variable(n, 1) bqm.add_variable(n + hf, 1) bqm.add_variable(n + 2 * hf, -1) bqm.add_variable(n + 3 * hf, -1) bqm.set_linear(0, 0) bqm.set_linear(hf, 0) return bqm
def anti_crossing_clique(num_variables): """Anti crossing problems with a single clique. Given the number of variables, the code will generate a clique of size num_variables/2, each variable ferromagnetically interacting with a partner variable with opposite bias. A single variable in the cluster will have no bias applied. Args: num_variables (int): Number of variables used to generate the problem. Must be an even number greater than 6. Returns: :obj:`.BinaryQuadraticModel`. """ if num_variables % 2 != 0 or num_variables < 6: raise ValueError('num_variables must be an even number > 6') bqm = BinaryQuadraticModel({}, {}, 0, 'SPIN') hf = int(num_variables / 2) for n in range(hf): for m in range(n + 1, hf): bqm.add_interaction(n, m, -1) bqm.add_interaction(n, n + hf, -1) bqm.add_variable(n, 1) bqm.add_variable(n + hf, -1) bqm.set_linear(1, 0) return bqm