diff --git a/qiskit/transpiler/passmanager.py b/qiskit/transpiler/passmanager.py index def19d829a69..ea118c359655 100644 --- a/qiskit/transpiler/passmanager.py +++ b/qiskit/transpiler/passmanager.py @@ -169,8 +169,12 @@ def _normalize_passes( passes = [passes] for pass_ in passes: if isinstance(pass_, FlowController): - # Normalize passes in nested FlowController - PassManager._normalize_passes(pass_.passes) + # Normalize passes in nested FlowController. + # TODO: Internal renormalisation should be the responsibility of the + # `FlowController`, but the separation between `FlowController`, + # `RunningPassManager` and `PassManager` is so muddled right now, it would be better + # to do this as part of more top-down refactoring. ---Jake, 2022-10-03. + pass_.passes = PassManager._normalize_passes(pass_.passes) elif not isinstance(pass_, BasePass): raise TranspilerError( "%s is not a BasePass or FlowController instance " % pass_.__class__ diff --git a/qiskit/transpiler/runningpassmanager.py b/qiskit/transpiler/runningpassmanager.py index 6cff16cb2c03..036a6d57bc0a 100644 --- a/qiskit/transpiler/runningpassmanager.py +++ b/qiskit/transpiler/runningpassmanager.py @@ -181,7 +181,7 @@ def _do_pass(self, pass_, dag, options): pass_.do_while = partial(pass_.do_while, self.fenced_property_set) for _pass in pass_: - self._do_pass(_pass, dag, pass_.options) + dag = self._do_pass(_pass, dag, pass_.options) else: raise TranspilerError( "Expecting type BasePass or FlowController, got %s." % type(pass_) diff --git a/releasenotes/notes/fix-nested-flow-controllers-a2a5f03eed482fa2.yaml b/releasenotes/notes/fix-nested-flow-controllers-a2a5f03eed482fa2.yaml new file mode 100644 index 000000000000..b5c719e7382a --- /dev/null +++ b/releasenotes/notes/fix-nested-flow-controllers-a2a5f03eed482fa2.yaml @@ -0,0 +1,8 @@ +--- +fixes: + - | + Nesting a :class:`.FlowController` inside another in a :class:`.PassManager` + could previously cause some transpiler passes to become "forgotten" during + transpilation, if the passes returned a new :class:`.DAGCircuit` rather than + mutating their input. Nested :class:`.FlowController`\ s will now affect + the transpilation correctly.