Skip to content
This repository was archived by the owner on Dec 7, 2021. It is now read-only.

Commit

Permalink
Fixed bugs of converters in the case of that the range of variable is…
Browse files Browse the repository at this point in the history
… 0 (#1259)

* fixed ineq2eq and int2bin converters

* removed the warning msg

* fix test

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Co-authored-by: Manoel Marques <Manoel.Marques@ibm.com>
  • Loading branch information
3 people authored Sep 28, 2020
1 parent c4cc2a5 commit 0132e08
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 28 deletions.
91 changes: 63 additions & 28 deletions qiskit/optimization/converters/inequality_to_equality.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,43 +186,60 @@ def _add_integer_slack_var_linear_constraint(self, linear, sense, rhs, name):

lhs_lb, lhs_ub = self._calc_linear_bounds(linear)

var_added = False

if sense == Constraint.Sense.LE:
sign = 1
self._dst.integer_var(
name=slack_name, lowerbound=0, upperbound=new_rhs - lhs_lb)
var_ub = new_rhs - lhs_lb
if var_ub > 0:
sign = 1
self._dst.integer_var(
name=slack_name, lowerbound=0, upperbound=var_ub)
var_added = True
elif sense == Constraint.Sense.GE:
sign = -1
self._dst.integer_var(
name=slack_name, lowerbound=0, upperbound=lhs_ub - new_rhs)
var_ub = lhs_ub - new_rhs
if var_ub > 0:
sign = -1
self._dst.integer_var(
name=slack_name, lowerbound=0, upperbound=var_ub)
var_added = True
else:
raise QiskitOptimizationError(
'The type of Sense in {} is not supported'.format(name))

# Add a new equality constraint.
new_linear = copy.deepcopy(linear)
new_linear[slack_name] = sign
if var_added:
new_linear[slack_name] = sign
self._dst.linear_constraint(new_linear, "==", new_rhs, name)

def _add_continuous_slack_var_linear_constraint(self, linear, sense, rhs, name):
slack_name = name + self._delimiter + 'continuous_slack'

lhs_lb, lhs_ub = self._calc_linear_bounds(linear)

var_added = False
if sense == Constraint.Sense.LE:
sign = 1
self._dst.continuous_var(
name=slack_name, lowerbound=0, upperbound=rhs - lhs_lb)
var_ub = rhs - lhs_lb
if var_ub > 0:
sign = 1
self._dst.continuous_var(
name=slack_name, lowerbound=0, upperbound=var_ub)
var_added = True
elif sense == Constraint.Sense.GE:
sign = -1
self._dst.continuous_var(
name=slack_name, lowerbound=0, upperbound=lhs_ub - rhs)
var_ub = lhs_ub - rhs
if var_ub > 0:
sign = -1
self._dst.continuous_var(
name=slack_name, lowerbound=0, upperbound=var_ub)
var_added = True
else:
raise QiskitOptimizationError(
'The type of Sense in {} is not supported'.format(name))

# Add a new equality constraint.
new_linear = copy.deepcopy(linear)
new_linear[slack_name] = sign
if var_added:
new_linear[slack_name] = sign
self._dst.linear_constraint(new_linear, "==", rhs, name)

def _add_auto_slack_var_linear_constraint(self, linear, sense, rhs, name):
Expand Down Expand Up @@ -255,21 +272,30 @@ def _add_integer_slack_var_quadratic_constraint(self, linear, quadratic, sense,

lhs_lb, lhs_ub = self._calc_quadratic_bounds(linear, quadratic)

var_added = False

if sense == Constraint.Sense.LE:
sign = 1
self._dst.integer_var(
name=slack_name, lowerbound=0, upperbound=new_rhs - lhs_lb)
var_ub = new_rhs - lhs_lb
if var_ub > 0:
sign = 1
self._dst.integer_var(
name=slack_name, lowerbound=0, upperbound=var_ub)
var_added = True
elif sense == Constraint.Sense.GE:
sign = -1
self._dst.integer_var(
name=slack_name, lowerbound=0, upperbound=lhs_ub - new_rhs)
var_ub = lhs_ub - new_rhs
if var_ub > 0:
sign = -1
self._dst.integer_var(
name=slack_name, lowerbound=0, upperbound=var_ub)
var_added = True
else:
raise QiskitOptimizationError(
'The type of Sense in {} is not supported'.format(name))

# Add a new equality constraint.
new_linear = copy.deepcopy(linear)
new_linear[slack_name] = sign
if var_added:
new_linear[slack_name] = sign
self._dst.quadratic_constraint(
new_linear, quadratic, "==", new_rhs, name)

Expand All @@ -279,21 +305,30 @@ def _add_continuous_slack_var_quadratic_constraint(self, linear, quadratic, sens

lhs_lb, lhs_ub = self._calc_quadratic_bounds(linear, quadratic)

var_added = False

if sense == Constraint.Sense.LE:
sign = 1
self._dst.continuous_var(
name=slack_name, lowerbound=0, upperbound=rhs - lhs_lb)
var_ub = rhs - lhs_lb
if var_ub > 0:
sign = 1
self._dst.continuous_var(
name=slack_name, lowerbound=0, upperbound=var_ub)
var_added = True
elif sense == Constraint.Sense.GE:
sign = -1
self._dst.continuous_var(
name=slack_name, lowerbound=0, upperbound=lhs_ub - rhs)
var_ub = lhs_ub - rhs
if var_ub > 0:
sign = -1
self._dst.continuous_var(
name=slack_name, lowerbound=0, upperbound=lhs_ub - rhs)
var_added = True
else:
raise QiskitOptimizationError(
'The type of Sense in {} is not supported'.format(name))

# Add a new equality constraint.
new_linear = copy.deepcopy(linear)
new_linear[slack_name] = sign
if var_added:
new_linear[slack_name] = sign
self._dst.quadratic_constraint(new_linear, quadratic, "==", rhs, name)

def _add_auto_slack_var_quadratic_constraint(self, linear, quadratic, sense, rhs, name):
Expand Down
17 changes: 17 additions & 0 deletions test/optimization/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,23 @@ def test_ising_to_quadraticprogram_quadratic_deprecated(self):
quadratic.objective.quadratic.coefficients.toarray(), quadratic_matrix
)

def test_0var_range_inequality(self):
""" Test InequalityToEquality converter when the var_rang of the slack variable is 0"""
op = QuadraticProgram()
op.binary_var('x')
op.binary_var('y')
op.linear_constraint(linear={'x': 1, 'y': 1}, sense='LE', rhs=0, name='xy_leq1')
op.linear_constraint(linear={'x': 1, 'y': 1}, sense='GE', rhs=2, name='xy_geq1')
op.quadratic_constraint(quadratic={('x', 'x'): 1}, sense='LE', rhs=0, name='xy_leq2')
op.quadratic_constraint(quadratic={('x', 'y'): 1}, sense='GE', rhs=1, name='xy_geq2')
ineq2eq = InequalityToEquality()
new_op = ineq2eq.convert(op)
self.assertEqual(new_op.get_num_vars(), 2)
self.assertTrue(all(l_const.sense == Constraint.Sense.EQ
for l_const in new_op.linear_constraints))
self.assertTrue(all(q_const.sense == Constraint.Sense.EQ
for q_const in new_op.quadratic_constraints))

def test_integer_to_binary2(self):
"""Test integer to binary variables 2"""
mod = QuadraticProgram()
Expand Down

0 comments on commit 0132e08

Please sign in to comment.