I am trying to use docplex to create a cost function for the longest path problem. As part of the formulation I would like to use the unit step function on a sum of variables contained in my model. Below is my code:
top_sort = list(range(0,5)) # topological sort of nodes
mdl = Model(name='lpp')
x = {i: mdl.binary_var(name='x_{0}'.format(i)) for i in top_sort}
H = mdl.piecewise(0, [(0, 0), (0, 1)], 0)
H(mdl.sum(x[k] for k in top_sort))
But, everytime I run this code I get this error:
The type of Variable x6 is continuous. It must be a binary variable.
The type of Variable _pwl6 is continuous. It must be a binary variable.
Traceback (most recent call last):
File "vqe.py", line 57, in <module>
qubitOp, offset = longest_path.get_operator(G)
File "C:\Users\vasnt\Documents\EDI2020\HONOURS\lpp\longest_path.py", line 109, in get_operator
return docplex.get_operator(mdl)
File "C:\Users\vasnt\Anaconda3\envs\qiskit\lib\site-packages\qiskit\optimization\applications\ising\docplex.py", line 90, in get_operator
_validate_input_model(mdl)
File "C:\Users\vasnt\Anaconda3\envs\qiskit\lib\site-packages\qiskit\optimization\applications\ising\docplex.py", line 232, in _validate_input_model
if not constraint.sense == ComparisonType.EQ:
AttributeError: 'PwlConstraint' object has no attribute 'sense'
I even tried applying H on just a single binary variable and it kept telling me that the variable is continuous and so will not work. Any idea what could be wrong? Thanks!