[WIP] Introduce a framework to convert a modeling library to a quadratic program and vice versa#116
[WIP] Introduce a framework to convert a modeling library to a quadratic program and vice versa#116t-imamichi wants to merge 46 commits intoqiskit-community:mainfrom
Conversation
Removed as per Stefan's comment
| def from_docplex(self, model: Model) -> None: | ||
| """Loads this quadratic program from a docplex model. |
There was a problem hiding this comment.
Should we deprecate from_docplex as well as to_docplex?
There was a problem hiding this comment.
Yes, I'm ready to add the deprecation warnings as follows.
https://github.com/Qiskit/qiskit-optimization/blob/27e2713873a8575e6b2136a499e29ffe6c3e3109/qiskit_optimization/problems/quadratic_program.py#L850-L852
https://github.com/Qiskit/qiskit-optimization/blob/27e2713873a8575e6b2136a499e29ffe6c3e3109/qiskit_optimization/problems/quadratic_program.py#L864-L866
| """ | ||
|
|
||
| def qp_to_model(self, quadratic_program: Any) -> GurobiModel: |
There was a problem hiding this comment.
Perhaps quadratic_program is an instance of QuadraticProgram, not Any?
There was a problem hiding this comment.
It is impossible to avoid cyclic import by pylint if quadratic_program: QuadraticProgram. If we are allowed to disable pylint cyclic import, I can do it.
There was a problem hiding this comment.
Oh, I see the problem now.
|
|
||
| return mdl | ||
|
|
||
| def model_to_qp(self, model: Model, quadratic_program: Any): |
There was a problem hiding this comment.
We could avoid having a quadratic_program as a method parameter if we made from_model static.
And should we have correct type hints?
There was a problem hiding this comment.
Yes, making from_model static is an option.
There was a problem hiding this comment.
should just return a QuatraticProgram, doesn't need to be static for that (and I'd keep it non-static, maybe there are cases where we would like to parametrize the translator). Same for GUROBI and interface.
There was a problem hiding this comment.
@stefan-woerner I mean QuadraticProgram.from_model() to be static, not the methods in the translators.
I think it should look like: qp = QuadraticProgram.from_model(another_model, translator)
| if translator is None: | ||
| from qiskit_optimization.translators.docplex import DocplexTranslator | ||
| translator = DocplexTranslator() | ||
| translator.model_to_qp(model, self) |
There was a problem hiding this comment.
Should we have a translator instance explicitly instead of defaulting to DOcplex?
| def from_docplex(self, model: Model) -> None: | ||
| """Loads this quadratic program from a docplex model. | ||
| def from_model(self, model: Model, translator: Optional[ModelTranslator] = None) -> None: | ||
| """Loads this quadratic program from an optimization model. |
There was a problem hiding this comment.
Is model: Model valid? I guess Model here comes from DOcplex? Should it be something more generic?
I suggest to make from_model a static method to be able to create a model like qp = QuadraticProgram.from_model(another_model, translator)
There was a problem hiding this comment.
You are right. model: Model is invalid here because Model is docplex.mp.model.Model. I agree to making from_model static.
| """ | ||
|
|
||
| def qp_to_model(self, quadratic_program: Any) -> Any: |
There was a problem hiding this comment.
Should we have correct type hints?
There was a problem hiding this comment.
It is impossible due to pylint outputs cyclic import errors. pylint does not seem to support typing.TYPE_CHECKING. So, I cannot find a way to avoid cyclic import errors except disabling it. Do you have any good idea?
| """ | ||
|
|
||
| @abstractmethod | ||
| def qp_to_model(self, quadratic_program: Any) -> Any: |
There was a problem hiding this comment.
Same as above, why quadratic_program: Any, but not quadratic_program: QuadraticProgram ?
| """ | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def model_to_qp(self, model: Any, quadratic_program: Any) -> None: |
# Conflicts: # qiskit_optimization/algorithms/__init__.py # qiskit_optimization/applications/max_cut.py # qiskit_optimization/problems/quadratic_program.py # test/problems/test_quadratic_program.py
# Conflicts: # qiskit_optimization/problems/quadratic_program.py
|
|
||
| return mdl | ||
|
|
||
| def model_to_qp(self, model: Model, quadratic_program: Any): |
There was a problem hiding this comment.
should just return a QuatraticProgram, doesn't need to be static for that (and I'd keep it non-static, maybe there are cases where we would like to parametrize the translator). Same for GUROBI and interface.
| class GurobipyTranslator(ModelTranslator): | ||
| """Translator between a gurobipy model and a quadratic program""" | ||
|
|
||
| def qp_to_model(self, quadratic_program: Any) -> GurobiModel: |
There was a problem hiding this comment.
Type hint: quadratic_program be of type QuadraticProgram (see @adekusar-drl's comments on interface and GUROBI as well)
|
Close it to continue discussion at #122 |
Summary
It introduces a model translator to convert a modeling libraries (e.g., docplex, gurobipy, etc.) to
QuadraticProgramand vice versa.It adds
QuadraticProgram.from_model(model, translator)andQuadraticProgram.to_model(translator)as a generic version offrom_docplexandto_docplex.I revised the design in #122 without gurobipy.
Fixes #80
TODO:
Details and comments