dwavesystems / dimod

A shared API for QUBO/Ising samplers.
https://docs.ocean.dwavesys.com/en/stable/docs_dimod/
Apache License 2.0
122 stars 81 forks source link

TypeError: '<' not supported between instances of 'BinaryQuadraticModel' and 'int' #1211

Open ACE07-Sev opened 2 years ago

ACE07-Sev commented 2 years ago

Hi, I am trying to set an inequality constraint (I want the sum of some terms to be negative, less than 0 essentially), but get this error :

Traceback (most recent call last): File "C:\Users\elmm\Desktop\CQM\CQM Assignment JRP\CQM_JRP.py", line 182, in cqm.add_constraint(upper_bound_eq < 0, TypeError: '<' not supported between instances of 'BinaryQuadraticModel' and 'int'

I don't think there is a need for a minimal reproducible example, as any cqm with a < sense which has binary vars will get this error but mine is :

for i in range(M): for j in range(N): X.append(Binary('X' + str(i + 1) + "_" + str(j + 1))) if j == 0: lower_boundeq = quicksum(Y[y] list[0][y] X_[j] - D[i] k_i[i] k_val[k] * T for k in range(38) for y in range(3))

        cqm.add_constraint(lower_bound_eq <= 0,
                           label="Constraint lower-bound " + str(i + 1) + str(j + 1))
        upper_bound_eq = quicksum(D[i] * k_i[i] * k_val[k] * T - (Y_[y + 1] * list[0][y + 1] * X_[j] + (1 - X_[j]) * Max_capacity) for k in range(38) for y in range(3))
        cqm.add_constraint(upper_bound_eq < 0,
                           label="Constraint lower-bound " + str(i + 1) + str(j + 1))
    elif j == 1:
        lower_bound_eq = quicksum(Y_[y] * list[1][y] * X_[j] - D[i] * k_i[i] * k_val[k] * T for k in range(38) for y in range(3))
        cqm.add_constraint(lower_bound_eq <= 0,
                           label="Constraint lower-bound " + str(i + 1) + str(j + 1))
        upper_bound_eq = quicksum(D[i] * k_i[i] * k_val[k] * T - (Y_[y + 1] * list[0][y + 1] * X_[j] + (1 - X_[j]) * Max_capacity) for k in range(38) for y in range(3))
        cqm.add_constraint(upper_bound_eq < 0,
                           label="Constraint lower-bound " + str(i + 1) + str(j + 1))
    elif j == 2:
        lower_bound_eq = quicksum(Y_[y] * list[2][y] * X_[j] - D[i] * k_i[i] * k_val[k] * T for k in range(38) for y in range(3))
        cqm.add_constraint(lower_bound_eq <= 0,
                           label="Constraint lower-bound " + str(i + 1) + str(j + 1))
        upper_bound_eq = quicksum(D[i] * k_i[i] * k_val[k] * T - (Y_[y + 1] * list[0][y + 1] * X_[j] + (1 - X_[j]) * Max_capacity) for k in range(38) for y in range(3))
        cqm.add_constraint(upper_bound_eq < 0,
                           label="Constraint lower-bound " + str(i + 1) + str(j + 1))

X_.clear()
ACE07-Sev commented 2 years ago

In here, Y, X, K_i are binary variables, list is just a list of values

arcondello commented 2 years ago

Hi @ACE07-Sev , we do not support strict inequality. You need to use <=, possibly with a constant offset. E.g. sum(Binary(v) for v in range(10)) < 0 should be sum(Binary(v) for v in range(10)) <= -1

angystallone commented 2 years ago

Are you not going to support this either?

TypeError: '>=' not supported between instances of 'BinaryQuadraticModel' and 'BinaryQuadraticModel'

arcondello commented 2 years ago

Unfortunately that is also not supported at the moment, though you can do bqm0 - bqm1 >= 0.

ACE07-Sev commented 2 years ago

Can't this be added in the backend? For instance given your understanding of offsets which simply resolve the issue without affecting the result, you could add this offset interpretation given the expectation being risen, same case for @angystallone 's issue. Given the "TypeError: '>=' not supported between instances of 'BinaryQuadraticModel' and 'BinaryQuadraticModel' ", the function could automatically return the BQM - BQM>= or <= 0.

Simply an idea, you're absolutely much much more familiar with the backend process hence I am sure given more time D-wave will find more sophisticated resolves.

Thank you as always for the early replies.