forked from qiskit-community/qiskit-aqua
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly deprecate quadratic program ising converter classes (qiskit-…
…community#1178) * Properly deprecate quadratic program ising converters In qiskit-community#1061 the ising converter classes were removed without a deprecation period and then backported to the stable branch. This is a violation of both the Qiskit deprecation policy [1] and the Qiskit stable branch policy [2] and should not have been merged like that. This is preventing the Qiskit metapackage 0.20.0 from being released because the tutorials as written today do not work with aqua 0.7.4 because of this breaking change. It should have been deprecated first for an appropriate period to give users a chance to adjust their code and then removed. During this period the tutorial could also be updated. This also should never have been backported to stable since users expect a stable point release to just contain bugfixes removals and deprecations should not be part of stable releases. This commit adds back the removed classes and deprecates them, this will need to be backported and released as 0.7.5. While normally this deprecation would not be allowed under the backport policy it is necessary here because we already released a breaking change. [1] https://qiskit.org/documentation/contributing_to_qiskit.html#deprecation-policy [2] https://qiskit.org/documentation/contributing_to_qiskit.html#stable-branch-policy * Fix typo * Remove broken loop * Add missing print methods Running through the tutorials there are more methods that have been removed that needed to go through a proper deprecation, this commit starts adding them back. There is still print_details() that needs to be added back. * add unit test * Add print_details to SummedOp This is needed for backwards compat with the optimziation code which used to return a legacy WeightedPauliOp object. 'print_details()' is used in the tutorials on the output from conversion so a deprecated method is added to add this funcionality to gracefully move users over to the new workflow. * fix lint * fix docstring Co-authored-by: Manoel Marques <manoel.marques@ibm.com>
- Loading branch information
1 parent
baeb4ae
commit 374cef9
Showing
7 changed files
with
304 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
qiskit/optimization/converters/ising_to_quadratic_program.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 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. | ||
|
||
|
||
"""The converter from a ```Operator``` to ``QuadraticProgram``.""" | ||
|
||
from typing import Optional, Union | ||
import copy | ||
import warnings | ||
import numpy as np # pylint: disable=unused-import | ||
|
||
from qiskit.aqua.operators import OperatorBase, WeightedPauliOperator | ||
from ..problems.quadratic_program import QuadraticProgram | ||
|
||
|
||
class IsingToQuadraticProgram: | ||
"""Convert a qubit operator into a quadratic program""" | ||
|
||
def __init__(self, linear: bool = False) -> None: | ||
r""" | ||
Args: | ||
linear: If linear is True, :math:`x^2` is treated as a linear term | ||
since :math:`x^2 = x` for :math:`x \in \{0,1\}`. | ||
Else, :math:`x^2` is treat as a quadratic term. | ||
The default value is False. | ||
""" | ||
self._qubit_op = None | ||
self._offset = 0.0 | ||
self._num_qubits = 0 | ||
self._qubo_matrix = None # type: Optional[np.ndarray] | ||
self._qp = None # type: Optional[QuadraticProgram] | ||
self._linear = linear | ||
warnings.warn("The IsingToQuadraticProgram class is deprecated and " | ||
"will be removed in a future release. Use the " | ||
".from_ising() method on the QuadraticProgram class " | ||
"instead.", DeprecationWarning) | ||
|
||
def encode(self, qubit_op: Union[OperatorBase, WeightedPauliOperator], offset: float = 0.0 | ||
) -> QuadraticProgram: | ||
"""Convert a qubit operator and a shift value into a quadratic program | ||
Args: | ||
qubit_op: The qubit operator to be converted into a | ||
:class:`~qiskit.optimization.problems.quadratic_program.QuadraticProgram` | ||
offset: The shift value of the qubit operator | ||
Returns: | ||
QuadraticProgram converted from the input qubit operator and the shift value | ||
Raises: | ||
QiskitOptimizationError: If there are Pauli Xs in any Pauli term | ||
QiskitOptimizationError: If there are more than 2 Pauli Zs in any Pauli term | ||
NotImplementedError: If the input operator is a ListOp | ||
""" | ||
self._qubit_op = qubit_op | ||
self._offset = copy.deepcopy(offset) | ||
self._num_qubits = qubit_op.num_qubits | ||
self._qp = QuadraticProgram() | ||
self._qp.from_ising(qubit_op, offset, | ||
linear=self._linear) | ||
return self._qp |
49 changes: 49 additions & 0 deletions
49
qiskit/optimization/converters/quadratic_program_to_ising.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 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. | ||
|
||
"""The converter from an ```QuadraticProgram``` to ``Operator``.""" | ||
|
||
from typing import Tuple, Optional | ||
import warnings | ||
|
||
from qiskit.aqua.operators import OperatorBase | ||
from ..problems.quadratic_program import QuadraticProgram | ||
|
||
|
||
class QuadraticProgramToIsing: | ||
"""Convert an optimization problem into a qubit operator.""" | ||
|
||
def __init__(self) -> None: | ||
"""Initialize the internal data structure.""" | ||
self._src = None # type: Optional[QuadraticProgram] | ||
warnings.warn("The QuadraticProgramToIsing class is deprecated and " | ||
"will be removed in a future release. Use the " | ||
".to_ising() method on a QuadraticProgram object " | ||
"instead.", DeprecationWarning) | ||
|
||
def encode(self, op: QuadraticProgram) -> Tuple[OperatorBase, float]: | ||
"""Convert a problem into a qubit operator | ||
Args: | ||
op: The optimization problem to be converted. Must be an unconstrained problem with | ||
binary variables only. | ||
Returns: | ||
The qubit operator of the problem and the shift value. | ||
Raises: | ||
QiskitOptimizationError: If a variable type is not binary. | ||
QiskitOptimizationError: If constraints exist in the problem. | ||
""" | ||
|
||
self._src = op | ||
return self._src.to_ising() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
releasenotes/notes/deprecate-ising-converter-classes-11749cdb6ac1bfaa.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
deprecations: | ||
- | | ||
The ising convert classes | ||
:class:`qiskit.optimization.converters.QuadraticProgramToIsing` and | ||
:class:`qiskit.optimization.converters.IsingToQuadraticProgram` have | ||
been deprecated and will be removed in a future release. Instead the | ||
:class:`qiskit.optimization.QuadraticProgram` methods | ||
:meth:`~qiskit.optimization.QuadraticProgram.to_ising` and | ||
:meth:`~qiskit.optimization.QuadraticPrgraom.from_ising` should be used | ||
instead. | ||
- | | ||
The ``pprint_as_string`` method for | ||
:class:`qiskit.optimization.QuadraticProgram` has been deprecated and will | ||
be removed in a future release. Instead you should just run | ||
``.pprint_as_string()`` on the output from | ||
:meth:`~qiskit.optimization.QuadraticProgram.to_docplex` | ||
- | | ||
The ``prettyprint`` method for | ||
:class:`qiskit.optimization.QuadraticProgram` has been deprecated and will | ||
be removed in a future release. Instead you should just run | ||
``.prettyprint()`` on the output from | ||
:meth:`~qiskit.optimization.QuadraticProgram.to_docplex` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters