forked from qiskit-community/qiskit-aqua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docplex.py
277 lines (216 loc) · 9.67 KB
/
docplex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# -*- coding: utf-8 -*-
# This code is part of Qiskit.
#
# (C) Copyright IBM 2019, 2020
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
Automatically generate Ising Hamiltonians from general models of optimization problems.
This program converts general models of optimization problems into Ising Hamiltonian.
To write models of optimization problems, DOcplex (Python library for optimization problems)
is used in the program.
(https://cdn.rawgit.com/IBMDecisionOptimization/docplex-doc/master/docs/index.html)
It supports models that consist of the following elements now.
- Binary variables.
- Linear or quadratic object function.
- Equality constraints.
- Symbols in constraints have to be equal (==).
- Inequality constraints (e.g. x+y <= 5) are not allowed.
The following is an example of use.
.. code-block:: python
# Create an instance of a model and variables with DOcplex.
mdl = Model(name='tsp')
x = {(i,p): mdl.binary_var(name='x_{0}_{1}'.format(i,p)) for i in range(num_node)
for p in range(num_node)}
# Object function
tsp_func = mdl.sum(ins.w[i,j] * x[(i,p)] * x[(j,(p+1)%num_node)] for i in range(num_node)
for j in range(num_node) for p in range(num_node))
mdl.minimize(tsp_func)
# Constraints
for i in range(num_node):
mdl.add_constraint(mdl.sum(x[(i,p)] for p in range(num_node)) == 1)
for p in range(num_node):
mdl.add_constraint(mdl.sum(x[(i,p)] for i in range(num_node)) == 1)
# Call the method to convert the model into Ising Hamiltonian.
qubitOp, offset = get_operator(mdl)
# Calculate with the generated Ising Hamiltonian.
ee = NumPyMinimumEigensolver(qubitOp)
result = ee.run()
print('get_operator')
print('tsp objective:', result['energy'] + offset)
"""
import logging
from math import fsum
from typing import Tuple
import numpy as np
from docplex.mp.constants import ComparisonType
from docplex.mp.model import Model
from qiskit.quantum_info import Pauli
from qiskit.aqua import AquaError
from qiskit.aqua.operators import WeightedPauliOperator
logger = logging.getLogger(__name__)
def get_operator(mdl: Model, auto_penalty: bool = True,
default_penalty: float = 1e5) -> Tuple[WeightedPauliOperator, float]:
"""
Generate Ising Hamiltonian from a model of DOcplex.
Args:
mdl : A model of DOcplex for a optimization problem.
auto_penalty : If true, the penalty coefficient is automatically defined
by "_auto_define_penalty()".
default_penalty : The default value of the penalty coefficient for the constraints.
This value is used if "auto_penalty" is False.
Returns:
A WeightedPauliOperator for the Hamiltonian and a constant shift for the obj function.
"""
_validate_input_model(mdl)
# set the penalty coefficient by _auto_define_penalty() or manually.
if auto_penalty:
penalty = _auto_define_penalty(mdl, default_penalty)
else:
penalty = default_penalty
# set a sign corresponding to a maximized or minimized problem.
# sign == 1 is for minimized problem. sign == -1 is for maximized problem.
sign = 1
if mdl.is_maximized():
sign = -1
# assign variables of the model to qubits.
q_d = {}
index = 0
for i in mdl.iter_variables():
if i in q_d:
continue
q_d[i] = index
index += 1
# initialize Hamiltonian.
num_nodes = len(q_d)
pauli_list = []
shift = 0
zero = np.zeros(num_nodes, dtype=np.bool)
# convert a constant part of the object function into Hamiltonian.
shift += mdl.get_objective_expr().get_constant() * sign
# convert linear parts of the object function into Hamiltonian.
l_itr = mdl.get_objective_expr().iter_terms()
for j in l_itr:
z_p = np.zeros(num_nodes, dtype=np.bool)
index = q_d[j[0]]
weight = j[1] * sign / 2
z_p[index] = True
pauli_list.append([-weight, Pauli(z_p, zero)])
shift += weight
# convert quadratic parts of the object function into Hamiltonian.
q_itr = mdl.get_objective_expr().iter_quads()
for i in q_itr:
index1 = q_d[i[0][0]]
index2 = q_d[i[0][1]]
weight = i[1] * sign / 4
if index1 == index2:
shift += weight
else:
z_p = np.zeros(num_nodes, dtype=np.bool)
z_p[index1] = True
z_p[index2] = True
pauli_list.append([weight, Pauli(z_p, zero)])
z_p = np.zeros(num_nodes, dtype=np.bool)
z_p[index1] = True
pauli_list.append([-weight, Pauli(z_p, zero)])
z_p = np.zeros(num_nodes, dtype=np.bool)
z_p[index2] = True
pauli_list.append([-weight, Pauli(z_p, zero)])
shift += weight
# convert constraints into penalty terms.
for constraint in mdl.iter_constraints():
constant = constraint.right_expr.get_constant()
# constant parts of penalty*(Constant-func)**2: penalty*(Constant**2)
shift += penalty * constant ** 2
# linear parts of penalty*(Constant-func)**2: penalty*(-2*Constant*func)
for __l in constraint.left_expr.iter_terms():
z_p = np.zeros(num_nodes, dtype=np.bool)
index = q_d[__l[0]]
weight = __l[1]
z_p[index] = True
pauli_list.append([penalty * constant * weight, Pauli(z_p, zero)])
shift += -penalty * constant * weight
# quadratic parts of penalty*(Constant-func)**2: penalty*(func**2)
for __l in constraint.left_expr.iter_terms():
for l_2 in constraint.left_expr.iter_terms():
index1 = q_d[__l[0]]
index2 = q_d[l_2[0]]
weight1 = __l[1]
weight2 = l_2[1]
penalty_weight1_weight2 = penalty * weight1 * weight2 / 4
if index1 == index2:
shift += penalty_weight1_weight2
else:
z_p = np.zeros(num_nodes, dtype=np.bool)
z_p[index1] = True
z_p[index2] = True
pauli_list.append([penalty_weight1_weight2, Pauli(z_p, zero)])
z_p = np.zeros(num_nodes, dtype=np.bool)
z_p[index1] = True
pauli_list.append([-penalty_weight1_weight2, Pauli(z_p, zero)])
z_p = np.zeros(num_nodes, dtype=np.bool)
z_p[index2] = True
pauli_list.append([-penalty_weight1_weight2, Pauli(z_p, zero)])
shift += penalty_weight1_weight2
# Remove paulis whose coefficients are zeros.
qubit_op = WeightedPauliOperator(paulis=pauli_list)
return qubit_op, shift
def _validate_input_model(mdl: Model):
"""
Check whether an input model is valid. If not, raise an AquaError
Args:
mdl : A model of DOcplex for a optimization problem.
Raises:
AquaError: Unsupported input model
"""
valid = True
# validate an object type of the input.
if not isinstance(mdl, Model):
raise AquaError('An input model must be docplex.mp.model.Model.')
# raise an error if the type of the variable is not a binary type.
for var in mdl.iter_variables():
if not var.is_binary():
logger.warning('The type of Variable %s is %s. It must be a binary variable. ',
var, var.vartype.short_name)
valid = False
# raise an error if the constraint type is not an equality constraint.
for constraint in mdl.iter_constraints():
if not constraint.sense == ComparisonType.EQ:
logger.warning('Constraint %s is not an equality constraint.', constraint)
valid = False
if not valid:
raise AquaError('The input model has unsupported elements.')
def _auto_define_penalty(mdl: Model, default_penalty: float = 1e5) -> float:
"""
Automatically define the penalty coefficient.
This returns object function's (upper bound - lower bound + 1).
Args:
mdl : A model of DOcplex for a optimization problem.
default_penalty : The default value of the penalty coefficient for the constraints.
Returns:
The penalty coefficient for the Hamiltonian.
"""
# if a constraint has float coefficient, return 1e5 for the penalty coefficient.
terms = []
for constraint in mdl.iter_constraints():
terms.append(constraint.right_expr.get_constant())
terms.extend(term[1] for term in constraint.left_expr.iter_terms())
if any(isinstance(term, float) and not term.is_integer() for term in terms):
logger.warning('Using %f for the penalty coefficient because a float coefficient exists '
'in constraints. \nThe value could be too small. '
'If so, set the penalty coefficient manually.', default_penalty)
return default_penalty
# (upper bound - lower bound) can be calculate as the sum of absolute value of coefficients
# Firstly, add 1 to guarantee that infeasible answers will be greater than upper bound.
penalties = [1]
# add linear terms of the object function.
penalties.extend(abs(i[1]) for i in mdl.get_objective_expr().iter_terms())
# add quadratic terms of the object function.
penalties.extend(abs(i[1]) for i in mdl.get_objective_expr().iter_quads())
return fsum(penalties)