We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
OptimizeSwapBeforeMeasure needs multiple invocations
The `OptimizeSwapBeforeMeasure` pass is supposed to commute quantum SWAP gates that immediately precede measurements, past the measurements, and instead convert them to classical swap (i.e.just retarget the measurement clbit).

However this should be generalized to work with any permutation that happens before the measurements, not just one swap layer. I made this work by invoking it many times for the example below, but really one invocation should be smart enough to remove all the swaps (that whole swap network should be modeled as a permutation).

```py
from qiskit import QuantumCircuit
from qiskit.transpiler.passes import OptimizeSwapBeforeMeasure, Depth, FixedPoint
from qiskit.transpiler import PassManager

circ = QuantumCircuit(8, 8)
circ.swap(3, 4)
circ.swap(6, 7)
circ.swap(0, 1)
circ.swap(6, 7)
circ.swap(4, 5)
circ.swap(0, 1)
circ.swap(5, 6)
circ.swap(3, 4)
circ.swap(1, 2)
circ.swap(6, 7)
circ.swap(4, 5)
circ.swap(2, 3)
circ.swap(0, 1)
circ.swap(5, 6)
circ.swap(1, 2)
circ.swap(6, 7)
circ.swap(4, 5)
circ.swap(2, 3)
circ.swap(3, 4)
circ.swap(3, 4)
circ.swap(5, 6)
circ.swap(1, 2)
circ.swap(4, 5)
circ.swap(2, 3)
circ.swap(5, 6)
circ.measure(range(8), range(8))
print(circ)

_depth_check = [Depth(), FixedPoint('depth')]
_opt = [OptimizeSwapBeforeMeasure()]
def _opt_control(property_set):
    return not property_set['depth_fixed_point']

pm = PassManager()
pm.append(_depth_check + _opt, do_while=_opt_control)  # should not need this do_while
print(pm.run(circ))
```

```
                    ┌─┐                                 
q_0: ─X──X─────X────┤M├─────────────────────────────────
      │  │     │    └╥┘         ┌─┐                     
q_1: ─X──X──X──X──X──╫─────X────┤M├─────────────────────
            │     │  ║     │    └╥┘      ┌─┐            
q_2: ───────X──X──X──╫──X──X─────╫──X────┤M├────────────
               │     ║  │        ║  │    └╥┘┌─┐         
q_3: ─X─────X──X─────╫──X──X──X──╫──X─────╫─┤M├─────────
      │     │        ║     │  │  ║        ║ └╥┘┌─┐      
q_4: ─X──X──X──X─────╫──X──X──X──╫──X─────╫──╫─┤M├──────
         │     │     ║  │        ║  │     ║  ║ └╥┘┌─┐   
q_5: ────X──X──X──X──╫──X──X─────╫──X──X──╫──╫──╫─┤M├───
            │     │  ║     │     ║     │  ║  ║  ║ └╥┘┌─┐
q_6: ─X──X──X──X──X──╫──X──X─────╫─────X──╫──╫──╫──╫─┤M├
      │  │     │     ║  │ ┌─┐    ║        ║  ║  ║  ║ └╥┘
q_7: ─X──X─────X─────╫──X─┤M├────╫────────╫──╫──╫──╫──╫─
                     ║    └╥┘    ║        ║  ║  ║  ║  ║ 
c: 8/════════════════╩═════╩═════╩════════╩══╩══╩══╩══╩═
                     0     7     1        2  3  4  5  6 
           ┌─┐               
q_0: ──────┤M├───────────────
        ┌─┐└╥┘               
q_1: ───┤M├─╫────────────────
     ┌─┐└╥┘ ║                
q_2: ┤M├─╫──╫────────────────
     └╥┘ ║  ║    ┌─┐         
q_3: ─╫──╫──╫────┤M├─────────
      ║  ║  ║    └╥┘      ┌─┐
q_4: ─╫──╫──╫─────╫───────┤M├
      ║  ║  ║ ┌─┐ ║       └╥┘
q_5: ─╫──╫──╫─┤M├─╫────────╫─
      ║  ║  ║ └╥┘ ║ ┌─┐    ║ 
q_6: ─╫──╫──╫──╫──╫─┤M├────╫─
      ║  ║  ║  ║  ║ └╥┘┌─┐ ║ 
q_7: ─╫──╫──╫──╫──╫──╫─┤M├─╫─
      ║  ║  ║  ║  ║  ║ └╥┘ ║ 
c: 8/═╩══╩══╩══╩══╩══╩══╩══╩═
      0  1  2  3  4  5  6  7 
```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `qiskit/transpiler/passes/optimization/optimize_swap_before_measure.py`
Content:
```
1 # This code is part of Qiskit.
2 #
3 # (C) Copyright IBM 2017, 2019.
4 #
5 # This code is licensed under the Apache License, Version 2.0. You may
6 # obtain a copy of this license in the LICENSE.txt file in the root directory
7 # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
8 #
9 # Any modifications or derivative works of this code must retain this
10 # copyright notice, and modified files need to carry a notice indicating
11 # that they have been altered from the originals.
12 
13 
14 """Remove the swaps followed by measurement (and adapt the measurement)."""
15 
16 from qiskit.circuit import Measure
17 from qiskit.circuit.library.standard_gates import SwapGate
18 from qiskit.transpiler.basepasses import TransformationPass
19 from qiskit.dagcircuit import DAGCircuit
20 
21 
22 class OptimizeSwapBeforeMeasure(TransformationPass):
23     """Remove the swaps followed by measurement (and adapt the measurement).
24 
25     Transpiler pass to remove swaps in front of measurements by re-targeting
26     the classical bit of the measure instruction.
27     """
28 
29     def run(self, dag):
30         """Run the OptimizeSwapBeforeMeasure pass on `dag`.
31 
32         Args:
33             dag (DAGCircuit): the DAG to be optimized.
34 
35         Returns:
36             DAGCircuit: the optimized DAG.
37         """
38         swaps = dag.op_nodes(SwapGate)
39         for swap in swaps:
40             final_successor = []
41             for successor in dag.successors(swap):
42                 final_successor.append(successor.type == 'out' or (successor.type == 'op' and
43                                                                    successor.op.name == 'measure'))
44             if all(final_successor):
45                 # the node swap needs to be removed and, if a measure follows, needs to be adapted
46                 swap_qargs = swap.qargs
47                 measure_layer = DAGCircuit()
48                 for qreg in dag.qregs.values():
49                     measure_layer.add_qreg(qreg)
50                 for creg in dag.cregs.values():
51                     measure_layer.add_creg(creg)
52                 for successor in list(dag.successors(swap)):
53                     if successor.type == 'op' and successor.op.name == 'measure':
54                         # replace measure node with a new one, where qargs is set with the "other"
55                         # swap qarg.
56                         dag.remove_op_node(successor)
57                         old_measure_qarg = successor.qargs[0]
58                         new_measure_qarg = swap_qargs[swap_qargs.index(old_measure_qarg) - 1]
59                         measure_layer.apply_operation_back(Measure(), [new_measure_qarg],
60                                                            [successor.cargs[0]])
61                 dag.compose(measure_layer)
62                 dag.remove_op_node(swap)
63         return dag
64
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/qiskit/transpiler/passes/optimization/optimize_swap_before_measure.py b/qiskit/transpiler/passes/optimization/optimize_swap_before_measure.py
--- a/qiskit/transpiler/passes/optimization/optimize_swap_before_measure.py
+++ b/qiskit/transpiler/passes/optimization/optimize_swap_before_measure.py
@@ -36,7 +36,7 @@
             DAGCircuit: the optimized DAG.
         """
         swaps = dag.op_nodes(SwapGate)
-        for swap in swaps:
+        for swap in swaps[::-1]:
             final_successor = []
             for successor in dag.successors(swap):
                 final_successor.append(successor.type == 'out' or (successor.type == 'op' and

{"golden_diff": "diff --git a/qiskit/transpiler/passes/optimization/optimize_swap_before_measure.py b/qiskit/transpiler/passes/optimization/optimize_swap_before_measure.py\n--- a/qiskit/transpiler/passes/optimization/optimize_swap_before_measure.py\n+++ b/qiskit/transpiler/passes/optimization/optimize_swap_before_measure.py\n@@ -36,7 +36,7 @@\n             DAGCircuit: the optimized DAG.\n         \"\"\"\n         swaps = dag.op_nodes(SwapGate)\n-        for swap in swaps:\n+        for swap in swaps[::-1]:\n             final_successor = []\n             for successor in dag.successors(swap):\n                 final_successor.append(successor.type == 'out' or (successor.type == 'op' and\n", "issue": "OptimizeSwapBeforeMeasure needs multiple invocations\nThe `OptimizeSwapBeforeMeasure` pass is supposed to commute quantum SWAP gates that immediately precede measurements, past the measurements, and instead convert them to classical swap (i.e.just retarget the measurement clbit).\r\n\r\nHowever this should be generalized to work with any permutation that happens before the measurements, not just one swap layer. I made this work by invoking it many times for the example below, but really one invocation should be smart enough to remove all the swaps (that whole swap network should be modeled as a permutation).\r\n\r\n```py\r\nfrom qiskit import QuantumCircuit\r\nfrom qiskit.transpiler.passes import OptimizeSwapBeforeMeasure, Depth, FixedPoint\r\nfrom qiskit.transpiler import PassManager\r\n\r\ncirc = QuantumCircuit(8, 8)\r\ncirc.swap(3, 4)\r\ncirc.swap(6, 7)\r\ncirc.swap(0, 1)\r\ncirc.swap(6, 7)\r\ncirc.swap(4, 5)\r\ncirc.swap(0, 1)\r\ncirc.swap(5, 6)\r\ncirc.swap(3, 4)\r\ncirc.swap(1, 2)\r\ncirc.swap(6, 7)\r\ncirc.swap(4, 5)\r\ncirc.swap(2, 3)\r\ncirc.swap(0, 1)\r\ncirc.swap(5, 6)\r\ncirc.swap(1, 2)\r\ncirc.swap(6, 7)\r\ncirc.swap(4, 5)\r\ncirc.swap(2, 3)\r\ncirc.swap(3, 4)\r\ncirc.swap(3, 4)\r\ncirc.swap(5, 6)\r\ncirc.swap(1, 2)\r\ncirc.swap(4, 5)\r\ncirc.swap(2, 3)\r\ncirc.swap(5, 6)\r\ncirc.measure(range(8), range(8))\r\nprint(circ)\r\n\r\n_depth_check = [Depth(), FixedPoint('depth')]\r\n_opt = [OptimizeSwapBeforeMeasure()]\r\ndef _opt_control(property_set):\r\n    return not property_set['depth_fixed_point']\r\n\r\npm = PassManager()\r\npm.append(_depth_check + _opt, do_while=_opt_control)  # should not need this do_while\r\nprint(pm.run(circ))\r\n```\r\n\r\n```\r\n                    \u250c\u2500\u2510                                 \r\nq_0: \u2500X\u2500\u2500X\u2500\u2500\u2500\u2500\u2500X\u2500\u2500\u2500\u2500\u2524M\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\r\n      \u2502  \u2502     \u2502    \u2514\u2565\u2518         \u250c\u2500\u2510                     \r\nq_1: \u2500X\u2500\u2500X\u2500\u2500X\u2500\u2500X\u2500\u2500X\u2500\u2500\u256b\u2500\u2500\u2500\u2500\u2500X\u2500\u2500\u2500\u2500\u2524M\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\r\n            \u2502     \u2502  \u2551     \u2502    \u2514\u2565\u2518      \u250c\u2500\u2510            \r\nq_2: \u2500\u2500\u2500\u2500\u2500\u2500\u2500X\u2500\u2500X\u2500\u2500X\u2500\u2500\u256b\u2500\u2500X\u2500\u2500X\u2500\u2500\u2500\u2500\u2500\u256b\u2500\u2500X\u2500\u2500\u2500\u2500\u2524M\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\r\n               \u2502     \u2551  \u2502        \u2551  \u2502    \u2514\u2565\u2518\u250c\u2500\u2510         \r\nq_3: \u2500X\u2500\u2500\u2500\u2500\u2500X\u2500\u2500X\u2500\u2500\u2500\u2500\u2500\u256b\u2500\u2500X\u2500\u2500X\u2500\u2500X\u2500\u2500\u256b\u2500\u2500X\u2500\u2500\u2500\u2500\u2500\u256b\u2500\u2524M\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\r\n      \u2502     \u2502        \u2551     \u2502  \u2502  \u2551        \u2551 \u2514\u2565\u2518\u250c\u2500\u2510      \r\nq_4: \u2500X\u2500\u2500X\u2500\u2500X\u2500\u2500X\u2500\u2500\u2500\u2500\u2500\u256b\u2500\u2500X\u2500\u2500X\u2500\u2500X\u2500\u2500\u256b\u2500\u2500X\u2500\u2500\u2500\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2524M\u251c\u2500\u2500\u2500\u2500\u2500\u2500\r\n         \u2502     \u2502     \u2551  \u2502        \u2551  \u2502     \u2551  \u2551 \u2514\u2565\u2518\u250c\u2500\u2510   \r\nq_5: \u2500\u2500\u2500\u2500X\u2500\u2500X\u2500\u2500X\u2500\u2500X\u2500\u2500\u256b\u2500\u2500X\u2500\u2500X\u2500\u2500\u2500\u2500\u2500\u256b\u2500\u2500X\u2500\u2500X\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2524M\u251c\u2500\u2500\u2500\r\n            \u2502     \u2502  \u2551     \u2502     \u2551     \u2502  \u2551  \u2551  \u2551 \u2514\u2565\u2518\u250c\u2500\u2510\r\nq_6: \u2500X\u2500\u2500X\u2500\u2500X\u2500\u2500X\u2500\u2500X\u2500\u2500\u256b\u2500\u2500X\u2500\u2500X\u2500\u2500\u2500\u2500\u2500\u256b\u2500\u2500\u2500\u2500\u2500X\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2524M\u251c\r\n      \u2502  \u2502     \u2502     \u2551  \u2502 \u250c\u2500\u2510    \u2551        \u2551  \u2551  \u2551  \u2551 \u2514\u2565\u2518\r\nq_7: \u2500X\u2500\u2500X\u2500\u2500\u2500\u2500\u2500X\u2500\u2500\u2500\u2500\u2500\u256b\u2500\u2500X\u2500\u2524M\u251c\u2500\u2500\u2500\u2500\u256b\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2500\u256b\u2500\r\n                     \u2551    \u2514\u2565\u2518    \u2551        \u2551  \u2551  \u2551  \u2551  \u2551 \r\nc: 8/\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2569\u2550\u2550\u2550\u2550\u2550\u2569\u2550\u2550\u2550\u2550\u2550\u2569\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2569\u2550\u2550\u2569\u2550\u2550\u2569\u2550\u2550\u2569\u2550\u2550\u2569\u2550\r\n                     0     7     1        2  3  4  5  6 \r\n           \u250c\u2500\u2510               \r\nq_0: \u2500\u2500\u2500\u2500\u2500\u2500\u2524M\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\r\n        \u250c\u2500\u2510\u2514\u2565\u2518               \r\nq_1: \u2500\u2500\u2500\u2524M\u251c\u2500\u256b\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\r\n     \u250c\u2500\u2510\u2514\u2565\u2518 \u2551                \r\nq_2: \u2524M\u251c\u2500\u256b\u2500\u2500\u256b\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\r\n     \u2514\u2565\u2518 \u2551  \u2551    \u250c\u2500\u2510         \r\nq_3: \u2500\u256b\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2500\u2500\u2500\u2524M\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\r\n      \u2551  \u2551  \u2551    \u2514\u2565\u2518      \u250c\u2500\u2510\r\nq_4: \u2500\u256b\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2500\u2500\u2500\u2500\u256b\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524M\u251c\r\n      \u2551  \u2551  \u2551 \u250c\u2500\u2510 \u2551       \u2514\u2565\u2518\r\nq_5: \u2500\u256b\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2524M\u251c\u2500\u256b\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256b\u2500\r\n      \u2551  \u2551  \u2551 \u2514\u2565\u2518 \u2551 \u250c\u2500\u2510    \u2551 \r\nq_6: \u2500\u256b\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2524M\u251c\u2500\u2500\u2500\u2500\u256b\u2500\r\n      \u2551  \u2551  \u2551  \u2551  \u2551 \u2514\u2565\u2518\u250c\u2500\u2510 \u2551 \r\nq_7: \u2500\u256b\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2500\u256b\u2500\u2524M\u251c\u2500\u256b\u2500\r\n      \u2551  \u2551  \u2551  \u2551  \u2551  \u2551 \u2514\u2565\u2518 \u2551 \r\nc: 8/\u2550\u2569\u2550\u2550\u2569\u2550\u2550\u2569\u2550\u2550\u2569\u2550\u2550\u2569\u2550\u2550\u2569\u2550\u2550\u2569\u2550\u2550\u2569\u2550\r\n      0  1  2  3  4  5  6  7 \r\n```\n", "before_files": [{"content": "# This code is part of Qiskit.\n#\n# (C) Copyright IBM 2017, 2019.\n#\n# This code is licensed under the Apache License, Version 2.0. You may\n# obtain a copy of this license in the LICENSE.txt file in the root directory\n# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.\n#\n# Any modifications or derivative works of this code must retain this\n# copyright notice, and modified files need to carry a notice indicating\n# that they have been altered from the originals.\n\n\n\"\"\"Remove the swaps followed by measurement (and adapt the measurement).\"\"\"\n\nfrom qiskit.circuit import Measure\nfrom qiskit.circuit.library.standard_gates import SwapGate\nfrom qiskit.transpiler.basepasses import TransformationPass\nfrom qiskit.dagcircuit import DAGCircuit\n\n\nclass OptimizeSwapBeforeMeasure(TransformationPass):\n    \"\"\"Remove the swaps followed by measurement (and adapt the measurement).\n\n    Transpiler pass to remove swaps in front of measurements by re-targeting\n    the classical bit of the measure instruction.\n    \"\"\"\n\n    def run(self, dag):\n        \"\"\"Run the OptimizeSwapBeforeMeasure pass on `dag`.\n\n        Args:\n            dag (DAGCircuit): the DAG to be optimized.\n\n        Returns:\n            DAGCircuit: the optimized DAG.\n        \"\"\"\n        swaps = dag.op_nodes(SwapGate)\n        for swap in swaps:\n            final_successor = []\n            for successor in dag.successors(swap):\n                final_successor.append(successor.type == 'out' or (successor.type == 'op' and\n                                                                   successor.op.name == 'measure'))\n            if all(final_successor):\n                # the node swap needs to be removed and, if a measure follows, needs to be adapted\n                swap_qargs = swap.qargs\n                measure_layer = DAGCircuit()\n                for qreg in dag.qregs.values():\n                    measure_layer.add_qreg(qreg)\n                for creg in dag.cregs.values():\n                    measure_layer.add_creg(creg)\n                for successor in list(dag.successors(swap)):\n                    if successor.type == 'op' and successor.op.name == 'measure':\n                        # replace measure node with a new one, where qargs is set with the \"other\"\n                        # swap qarg.\n                        dag.remove_op_node(successor)\n                        old_measure_qarg = successor.qargs[0]\n                        new_measure_qarg = swap_qargs[swap_qargs.index(old_measure_qarg) - 1]\n                        measure_layer.apply_operation_back(Measure(), [new_measure_qarg],\n                                                           [successor.cargs[0]])\n                dag.compose(measure_layer)\n                dag.remove_op_node(swap)\n        return dag\n", "path": "qiskit/transpiler/passes/optimization/optimize_swap_before_measure.py"}], "after_files": [{"content": "# This code is part of Qiskit.\n#\n# (C) Copyright IBM 2017, 2019.\n#\n# This code is licensed under the Apache License, Version 2.0. You may\n# obtain a copy of this license in the LICENSE.txt file in the root directory\n# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.\n#\n# Any modifications or derivative works of this code must retain this\n# copyright notice, and modified files need to carry a notice indicating\n# that they have been altered from the originals.\n\n\n\"\"\"Remove the swaps followed by measurement (and adapt the measurement).\"\"\"\n\nfrom qiskit.circuit import Measure\nfrom qiskit.circuit.library.standard_gates import SwapGate\nfrom qiskit.transpiler.basepasses import TransformationPass\nfrom qiskit.dagcircuit import DAGCircuit\n\n\nclass OptimizeSwapBeforeMeasure(TransformationPass):\n    \"\"\"Remove the swaps followed by measurement (and adapt the measurement).\n\n    Transpiler pass to remove swaps in front of measurements by re-targeting\n    the classical bit of the measure instruction.\n    \"\"\"\n\n    def run(self, dag):\n        \"\"\"Run the OptimizeSwapBeforeMeasure pass on `dag`.\n\n        Args:\n            dag (DAGCircuit): the DAG to be optimized.\n\n        Returns:\n            DAGCircuit: the optimized DAG.\n        \"\"\"\n        swaps = dag.op_nodes(SwapGate)\n        for swap in swaps[::-1]:\n            final_successor = []\n            for successor in dag.successors(swap):\n                final_successor.append(successor.type == 'out' or (successor.type == 'op' and\n                                                                   successor.op.name == 'measure'))\n            if all(final_successor):\n                # the node swap needs to be removed and, if a measure follows, needs to be adapted\n                swap_qargs = swap.qargs\n                measure_layer = DAGCircuit()\n                for qreg in dag.qregs.values():\n                    measure_layer.add_qreg(qreg)\n                for creg in dag.cregs.values():\n                    measure_layer.add_creg(creg)\n                for successor in list(dag.successors(swap)):\n                    if successor.type == 'op' and successor.op.name == 'measure':\n                        # replace measure node with a new one, where qargs is set with the \"other\"\n                        # swap qarg.\n                        dag.remove_op_node(successor)\n                        old_measure_qarg = successor.qargs[0]\n                        new_measure_qarg = swap_qargs[swap_qargs.index(old_measure_qarg) - 1]\n                        measure_layer.apply_operation_back(Measure(), [new_measure_qarg],\n                                                           [successor.cargs[0]])\n                dag.compose(measure_layer)\n                dag.remove_op_node(swap)\n        return dag\n", "path": "qiskit/transpiler/passes/optimization/optimize_swap_before_measure.py"}]}