Skip to content

Commit 6b64a88

Browse files
adekusar-drlmanoelmarques
authored andcommitted
Remove dependency on internal DOCplex methods. (qiskit-community#1441)
* docplex corrections * more on docplex methods. * fixes in docplex.py
1 parent 233fa67 commit 6b64a88

File tree

2 files changed

+53
-15
lines changed

2 files changed

+53
-15
lines changed

qiskit/optimization/applications/ising/docplex.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
5858
"""
5959

60-
from typing import Tuple
60+
from typing import Tuple, Union, List
6161
import logging
6262
from math import fsum
6363

@@ -159,7 +159,9 @@ def get_operator(mdl: Model, auto_penalty: bool = True,
159159

160160
# convert constraints into penalty terms.
161161
for constraint in mdl.iter_constraints():
162-
constant = constraint.cplex_num_rhs()
162+
right_cst = constraint.get_right_expr().get_constant()
163+
left_cst = constraint.get_left_expr().get_constant()
164+
constant = float(right_cst - left_cst)
163165

164166
# constant parts of penalty*(Constant-func)**2: penalty*(Constant**2)
165167
shift += penalty * constant ** 2
@@ -175,8 +177,8 @@ def get_operator(mdl: Model, auto_penalty: bool = True,
175177
shift += -penalty * constant * weight
176178

177179
# quadratic parts of penalty*(Constant-func)**2: penalty*(func**2)
178-
for __l in constraint.iter_net_linear_coefs():
179-
for l_2 in constraint.iter_net_linear_coefs():
180+
for __l in _iter_net_linear_coeffs(constraint):
181+
for l_2 in _iter_net_linear_coeffs(constraint):
180182
index1 = q_d[__l[0]]
181183
index2 = q_d[l_2[0]]
182184
weight1 = __l[1]
@@ -207,6 +209,35 @@ def get_operator(mdl: Model, auto_penalty: bool = True,
207209
return qubit_op, shift
208210

209211

212+
def _iter_net_linear_coeffs(constraint: Union[LinearConstraint, QuadraticConstraint]) \
213+
-> List[Tuple[Var, float]]:
214+
"""
215+
Builds a list of tuples, where each tuple contains a decision variable and a
216+
corresponding coefficient of this variable in the constraint.
217+
218+
Args:
219+
constraint: A constraint to analyze.
220+
221+
Returns:
222+
A list of tuples of variables and coefficients.
223+
"""
224+
left_expr = constraint.get_left_expr()
225+
right_expr = constraint.get_right_expr()
226+
# for linear constraints we may get an instance of Var instead of expression,
227+
# e.g. x + y = z
228+
if isinstance(left_expr, Var):
229+
left_expr = left_expr + 0
230+
if isinstance(right_expr, Var):
231+
right_expr = right_expr + 0
232+
233+
variables = {}
234+
for var in left_expr.iter_variables():
235+
variables[var] = left_expr.get_coef(var)
236+
for var in right_expr.iter_variables():
237+
variables[var] = variables.get(var, 0.0) - right_expr.get_coef(var)
238+
return list(variables.items())
239+
240+
210241
def _validate_input_model(mdl: Model) -> None:
211242
"""Check whether an input model is valid. If not, raise an AquaError.
212243

qiskit/optimization/problems/quadratic_program.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -563,11 +563,11 @@ def from_docplex(self, model: Model) -> None:
563563
# keep track of names separately, since docplex allows to have None names.
564564
var_names = {}
565565
for x in model.iter_variables():
566-
if isinstance(x.get_vartype(), ContinuousVarType):
566+
if isinstance(x.vartype, ContinuousVarType):
567567
x_new = self.continuous_var(x.lb, x.ub, x.name)
568-
elif isinstance(x.get_vartype(), BinaryVarType):
568+
elif isinstance(x.vartype, BinaryVarType):
569569
x_new = self.binary_var(x.name)
570-
elif isinstance(x.get_vartype(), IntegerVarType):
570+
elif isinstance(x.vartype, IntegerVarType):
571571
x_new = self.integer_var(x.lb, x.ub, x.name)
572572
else:
573573
raise QiskitOptimizationError(
@@ -593,7 +593,7 @@ def from_docplex(self, model: Model) -> None:
593593
# get quadratic part of objective
594594
quadratic = {}
595595
if isinstance(model.objective_expr, QuadExpr):
596-
for quad_triplet in model.objective_expr.generate_quad_triplets():
596+
for quad_triplet in model.objective_expr.iter_quad_triplets():
597597
i = var_names[quad_triplet[0]]
598598
j = var_names[quad_triplet[1]]
599599
v = quad_triplet[2]
@@ -620,15 +620,22 @@ def from_docplex(self, model: Model) -> None:
620620
name = constraint.name
621621
sense = constraint.sense
622622

623-
rhs = 0
624-
if not isinstance(constraint.lhs, Var):
625-
rhs -= constraint.lhs.constant
626-
if not isinstance(constraint.rhs, Var):
627-
rhs += constraint.rhs.constant
623+
left_expr = constraint.get_left_expr()
624+
right_expr = constraint.get_right_expr()
625+
# for linear constraints we may get an instance of Var instead of expression,
626+
# e.g. x + y = z
627+
if isinstance(left_expr, Var):
628+
left_expr = left_expr + 0
629+
if isinstance(right_expr, Var):
630+
right_expr = right_expr + 0
631+
632+
rhs = right_expr.constant - left_expr.constant
628633

629634
lhs = {}
630-
for x in constraint.iter_net_linear_coefs():
631-
lhs[var_names[x[0]]] = x[1]
635+
for x in left_expr.iter_variables():
636+
lhs[var_names[x]] = left_expr.get_coef(x)
637+
for x in right_expr.iter_variables():
638+
lhs[var_names[x]] = lhs.get(var_names[x], 0.0) - right_expr.get_coef(x)
632639

633640
if sense == sense.EQ:
634641
self.linear_constraint(lhs, '==', rhs, name)

0 commit comments

Comments
 (0)