I prepared a quadratic optimization task with binary and integer variables and linear and quadratic constraints. I fed it into QuadraticProgram
in Qiskit. After that, I tried to convert the program to binary optimization task with constraints in form of equality only. I used InequalityToEquality
and IntegerToBinary
converters to do this. However, an error was returned. After some trial and error, I realized that the problem is caused by the quadratic constraint, so I removed it and everything is fine.
My questions are:
- Is the problem really in the quadratic constraint? Or in other words, are such constraints forbidden when I want to use converters?
- If so, is there any other way how to convert
QuadraticProgram
to QUBO in Qiskit? (note thatQuadraticProgramToQubo
also does not work sinceIntegerToBinary
is part of it)
Here is my code:
%matplotlib inline
from qiskit.optimization import QuadraticProgram
from qiskit.optimization.converters import InequalityToEquality, IntegerToBinary, QuadraticProgramToQubo
#create empty optimization task (model)
mod = QuadraticProgram('Quadratic optimization problem') #string in brackets - user defined name of the task
#adding variables
mod.binary_var(name = 'x') #bin
mod.integer_var(name = 'y', lowerbound = 0, upperbound = 5)
mod.integer_var(name = 'z', lowerbound = 0, upperbound = 5)
#setting objective function
mod.minimize(constant = 3, linear = [1,0,1], quadratic = [[1, 0, 2],[0,1,0],[2,0,1]])
#adding constraints
mod.linear_constraint(linear = {'x': 1, 'y': 1, 'z':1}, sense = '==', rhs = 1, name = 'L1')
mod.linear_constraint(linear = {'x': -1, 'y': -1, 'z':2}, sense = '>=', rhs = 0, name = 'L2')
mod.linear_constraint(linear = {'x': 10, 'y': 20, 'z':30}, sense = '<=', rhs = 100, name = 'L3')
#PROBLEMATIC QUADRATIC CONSTAINT
mod.quadratic_constraint(linear = {'x':1, 'y':1}, quadratic = {('x','y'):1, ('x','z'):-1}, sense = '<=', rhs = 2, name = 'Q1')
modConverted = InequalityToEquality().convert(mod)
modConverted = IntegerToBinary().convert(modConverted)
print(modConverted.export_as_lp_string())
And here is the returned error:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-13-f345029f5fa8> in <module>
20
21 modConverted = InequalityToEquality().convert(mod)
---> 22 modConverted = IntegerToBinary().convert(modConverted)
23 print(modConverted.export_as_lp_string())
/opt/conda/lib/python3.7/site-packages/qiskit/optimization/converters/integer_to_binary.py in convert(self, problem)
96 )
97
---> 98 self._substitute_int_var()
99
100 else:
/opt/conda/lib/python3.7/site-packages/qiskit/optimization/converters/integer_to_binary.py in _substitute_int_var(self)
199 )
200 quadratic, q_linear, q_constant = self._convert_quadratic_coefficients_dict(
--> 201 constraint.quadratic.to_dict()
202 )
203
/opt/conda/lib/python3.7/site-packages/qiskit/optimization/converters/integer_to_binary.py in _convert_quadratic_coefficients_dict(self, coefficients)
137 quadratic = {}
138 for (name_i, name_j), v in coefficients.items():
--> 139 x = self._src.get_variable(name_i)
140 y = self._src.get_variable(name_j)
141
/opt/conda/lib/python3.7/site-packages/qiskit/optimization/problems/quadratic_program.py in get_variable(self, i)
243 return self.variables[i]
244 else:
--> 245 return self.variables[self._variables_index[i]]
246
247 def get_num_vars(self, vartype: Optional[VarType] = None) -> int:
KeyError: 0
```