qiskit/transpiler/passes/optimize_swap_before_measure.py
def run(self, dag):
        """Return a new circuit that has been optimized."""
        swaps = dag.op_nodes(SwapGate)
        for swap in swaps:
            final_successor = []
            for successor in dag.successors(swap):
                final_successor.append(successor.type == 'out' or (successor.type == 'op' and
                                                                   successor.op.name == 'measure'))
            if all(final_successor):
                # the node swap needs to be removed and, if a measure follows, needs to be adapted
                swap_qargs = swap.qargs
                measure_layer = DAGCircuit()
                for qreg in dag.qregs.values():
                    measure_layer.add_qreg(qreg)
                for creg in dag.cregs.values():
                    measure_layer.add_creg(creg)
                for successor in dag.successors(swap):
                    if successor.type == 'op' and successor.op.name == 'measure':
                        # replace measure node with a new one, where qargs is set with the "other"
                        # swap qarg.
                        dag.remove_op_node(successor)
                        old_measure_qarg = successor.qargs[0]
                        new_measure_qarg = swap_qargs[swap_qargs.index(old_measure_qarg) - 1]
                        measure_layer.apply_operation_back(Measure(), [new_measure_qarg],
                                                           [successor.cargs[0]])
                dag.extend_back(measure_layer)
                dag.remove_op_node(swap)
        return dag
def run(self, dag):
        """Return a new circuit that has been optimized."""
        swaps = dag.op_nodes(SwapGate)
        for swap in swaps:
            final_successor = []
            for successor in dag.successors(swap):
                final_successor.append(successor.type == 'out' or (successor.type == 'op' and
                                                                   successor.op.name == 'measure'))
            if all(final_successor):
                # the node swap needs to be removed and, if a measure follows, needs to be adapted
                swap_qargs = swap.qargs
                measure_layer = DAGCircuit()
                for qreg in dag.qregs.values():
                    measure_layer.add_qreg(qreg)
                for creg in dag.cregs.values():
                    measure_layer.add_creg(creg)
                for successor in dag.successors(swap):
                    if successor.type == 'op' and successor.op.name == 'measure':
                        # replace measure node with a new one, where qargs is set with the "other"
                        # swap qarg.
                        dag.remove_op_node(successor)
                        old_measure_qarg = successor.qargs[0]
                        new_measure_qarg = swap_qargs[swap_qargs.index(old_measure_qarg) - 1]
                        measure_layer.apply_operation_back(Measure(), [new_measure_qarg],
                                                           [successor.cargs[0]])
                dag.extend_back(measure_layer)
                dag.remove_op_node(swap)
        return dag
Return a new circuit that has been optimized.