Skip to content

Commit

Permalink
Merge branch 'main' into akshayp2003-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
akshayp2003 committed Aug 1, 2022
2 parents 0f95e5a + cf95120 commit 9b04ab5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
10 changes: 10 additions & 0 deletions qiskit/algorithms/optimizers/scipy_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ def minimize(
if jac is not None and self._method == "l-bfgs-b":
jac = self._wrap_gradient(jac)

# Starting in scipy 1.9.0 maxiter is deprecated and maxfun (added in 1.5.0)
# should be used instead
swapped_deprecated_args = False
if self._method == "tnc" and "maxiter" in self._options:
swapped_deprecated_args = True
self._options["maxfun"] = self._options.pop("maxiter")

raw_result = minimize(
fun=fun,
x0=x0,
Expand All @@ -147,6 +154,9 @@ def minimize(
options=self._options,
**self._kwargs,
)
if swapped_deprecated_args:
self._options["maxiter"] = self._options.pop("maxfun")

result = OptimizerResult()
result.x = raw_result.x
result.fun = raw_result.fun
Expand Down
2 changes: 2 additions & 0 deletions qiskit/test/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ def setUpClass(cls):
# Internal deprecation warning emitted by jupyter client when
# calling nbconvert in python 3.10
r"There is no current event loop",
# Caused by internal scikit-learn scipy usage
r"The 'sym_pos' keyword is deprecated and should be replaced by using",
]
for msg in allow_DeprecationWarning_message:
warnings.filterwarnings("default", category=DeprecationWarning, message=msg)
Expand Down
14 changes: 9 additions & 5 deletions qiskit/transpiler/passmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,12 @@ class StagedPassManager(PassManager):
users as the relative positions of the stage are preserved so the behavior
will not change between releases.
These stages will be executed in order and any stage set to ``None`` will be skipped. If
a :class:`~qiskit.transpiler.PassManager` input is being used for more than 1 stage here
(for example in the case of a :class:`~.Pass` that covers both Layout and Routing) you will want
to set that to the earliest stage in sequence that it covers.
These stages will be executed in order and any stage set to ``None`` will be skipped.
If a stage is provided multiple times (i.e. at diferent relative positions), the
associated passes, including pre and post, will run once per declaration.
If a :class:`~qiskit.transpiler.PassManager` input is being used for more than 1 stage here
(for example in the case of a :class:`~.Pass` that covers both Layout and Routing) you will
want to set that to the earliest stage in sequence that it covers.
"""

invalid_stage_regex = re.compile(
Expand All @@ -380,6 +382,8 @@ def __init__(self, stages: Optional[Iterable[str]] = None, **kwargs) -> None:
instance. If this is not specified the default stages list
``['init', 'layout', 'routing', 'translation', 'optimization', 'scheduling']`` is
used. After instantiation, the final list will be immutable and stored as tuple.
If a stage is provided multiple times (i.e. at diferent relative positions), the
associated passes, including pre and post, will run once per declaration.
kwargs: The initial :class:`~.PassManager` values for any stages
defined in ``stages``. If a argument is not defined the
stages will default to ``None`` indicating an empty/undefined
Expand All @@ -403,7 +407,7 @@ def __init__(self, stages: Optional[Iterable[str]] = None, **kwargs) -> None:
super().__setattr__("_expanded_stages", tuple(self._generate_expanded_stages()))
super().__init__()
self._validate_init_kwargs(kwargs)
for stage in self.expanded_stages:
for stage in set(self.expanded_stages):
pm = kwargs.get(stage, None)
setattr(self, stage, pm)

Expand Down
24 changes: 23 additions & 1 deletion test/python/transpiler/test_staged_passmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from qiskit.transpiler import PassManager, StagedPassManager
from qiskit.transpiler.exceptions import TranspilerError
from qiskit.transpiler.passes import Optimize1qGates, Unroller, Depth
from qiskit.transpiler.passes import Optimize1qGates, Unroller, Depth, BasicSwap
from qiskit.test import QiskitTestCase


Expand Down Expand Up @@ -98,6 +98,26 @@ def test_invalid_stages(self):
for stage in invalid_stages:
self.assertIn(stage, message)

def test_repeated_stages(self):
stages = ["alpha", "omega", "alpha"]
pre_alpha = PassManager(Unroller(["u", "cx"]))
alpha = PassManager(Depth())
post_alpha = PassManager(BasicSwap([[0, 1], [1, 2]]))
omega = PassManager(Optimize1qGates())
spm = StagedPassManager(
stages, pre_alpha=pre_alpha, alpha=alpha, post_alpha=post_alpha, omega=omega
)
passes = [
*pre_alpha.passes(),
*alpha.passes(),
*post_alpha.passes(),
*omega.passes(),
*pre_alpha.passes(),
*alpha.passes(),
*post_alpha.passes(),
]
self.assertEqual(spm.passes(), passes)

def test_edit_stages(self):
spm = StagedPassManager()
with self.assertRaises(AttributeError):
Expand All @@ -120,6 +140,8 @@ def test_setattr(self):
spm.init = spm
mock_target = "qiskit.transpiler.passmanager.StagedPassManager._update_passmanager"
with patch(mock_target, spec=True) as mock:
spm.max_iteration = spm.max_iteration
mock.assert_not_called()
spm.init = None
mock.assert_called_once()

Expand Down

0 comments on commit 9b04ab5

Please sign in to comment.