Skip to content

Commit 99649e7

Browse files
a-matsuowoodsp-ibmmanoelmarques
authored
* fixed qiskit-community/qiskit-aqua#750 for the DOcplex translator Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com> Co-authored-by: Manoel Marques <manoel@us.ibm.com>
1 parent 3ab3ec5 commit 99649e7

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

qiskit/optimization/ising/docplex.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,13 @@ def get_operator(mdl: Model, auto_penalty: bool = True,
160160

161161
# convert constraints into penalty terms.
162162
for constraint in mdl.iter_constraints():
163-
constant = constraint.right_expr.get_constant()
163+
constant = constraint.cplex_num_rhs()
164164

165165
# constant parts of penalty*(Constant-func)**2: penalty*(Constant**2)
166166
shift += penalty * constant ** 2
167167

168168
# linear parts of penalty*(Constant-func)**2: penalty*(-2*Constant*func)
169-
for __l in constraint.left_expr.iter_terms():
169+
for __l in constraint.iter_net_linear_coefs():
170170
z_p = np.zeros(num_nodes, dtype=np.bool)
171171
index = q_d[__l[0]]
172172
weight = __l[1]
@@ -176,8 +176,8 @@ def get_operator(mdl: Model, auto_penalty: bool = True,
176176
shift += -penalty * constant * weight
177177

178178
# quadratic parts of penalty*(Constant-func)**2: penalty*(func**2)
179-
for __l in constraint.left_expr.iter_terms():
180-
for l_2 in constraint.left_expr.iter_terms():
179+
for __l in constraint.iter_net_linear_coefs():
180+
for l_2 in constraint.iter_net_linear_coefs():
181181
index1 = q_d[__l[0]]
182182
index2 = q_d[l_2[0]]
183183
weight1 = __l[1]
@@ -258,8 +258,8 @@ def _auto_define_penalty(mdl: Model, default_penalty: float = 1e5) -> float:
258258
# if a constraint has float coefficient, return 1e5 for the penalty coefficient.
259259
terms = []
260260
for constraint in mdl.iter_constraints():
261-
terms.append(constraint.right_expr.get_constant())
262-
terms.extend(term[1] for term in constraint.left_expr.iter_terms())
261+
terms.append(constraint.cplex_num_rhs())
262+
terms.extend(term[1] for term in constraint.iter_net_linear_coefs())
263263
if any(isinstance(term, float) and not term.is_integer() for term in terms):
264264
logger.warning('Using %f for the penalty coefficient because a float coefficient exists '
265265
'in constraints. \nThe value could be too small. '

test/optimization/test_docplex.py

+18
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,21 @@ def test_docplex_constant_and_quadratic_terms_in_object_function(self):
320320

321321
# Compare objective
322322
self.assertEqual(result.eigenvalue.real + offset, expected_result)
323+
324+
def test_constants_in_left_side_and_variables_in_right_side(self):
325+
""" Test Constant values on the left-hand side of constraints and
326+
variables on the right-hand side of constraints for the DOcplex translator"""
327+
mdl = Model('left_constants_and_right_variables')
328+
x = mdl.binary_var(name='x')
329+
y = mdl.binary_var(name='y')
330+
mdl.maximize(mdl.sum(x + y))
331+
mdl.add_constraint(x == y)
332+
333+
qubit_op, offset = docplex.get_operator(mdl)
334+
print(qubit_op.print_details())
335+
e_e = NumPyMinimumEigensolver(qubit_op)
336+
result = e_e.run()
337+
338+
self.assertEqual(result['eigenvalue'] + offset, -2)
339+
actual_sol = result['eigenstate'].tolist()
340+
self.assertListEqual(actual_sol, [0, 0, 0, 1])

0 commit comments

Comments
 (0)