def run(self, dag):
<EOL>final_op_types = ['<STR_LIT>', '<STR_LIT>']<EOL>final_ops = []<EOL>for candidate_node in dag.named_nodes(*final_op_types):<EOL><INDENT>is_final_op = True<EOL>for _, child_successors in dag.bfs_successors(candidate_node):<EOL><INDENT>if any(suc.type == '<STR_LIT>' and suc.name not in final_op_types<EOL>for suc in child_successors):<EOL><INDENT>is_final_op = False<EOL>break<EOL><DEDENT><DEDENT>if is_final_op:<EOL><INDENT>final_ops.append(candidate_node)<EOL><DEDENT><DEDENT>if not final_ops:<EOL><INDENT>return dag<EOL><DEDENT>barrier_layer = DAGCircuit()<EOL>for qreg in dag.qregs.values():<EOL><INDENT>barrier_layer.add_qreg(qreg)<EOL><DEDENT>for creg in dag.cregs.values():<EOL><INDENT>barrier_layer.add_creg(creg)<EOL><DEDENT>final_qubits = set(final_op.qargs[<NUM_LIT:0>] for final_op in final_ops)<EOL>barrier_layer.apply_operation_back(<EOL>Barrier(len(final_qubits)), list(final_qubits), [])<EOL>ordered_final_nodes = [node for node in dag.topological_op_nodes()<EOL>if node in set(final_ops)]<EOL>for final_node in ordered_final_nodes:<EOL><INDENT>barrier_layer.apply_operation_back(final_node.op,<EOL>final_node.qargs,<EOL>final_node.cargs)<EOL><DEDENT>for final_op in final_ops:<EOL><INDENT>dag.remove_op_node(final_op)<EOL><DEDENT>dag.extend_back(barrier_layer)<EOL>adjacent_pass = MergeAdjacentBarriers()<EOL>return adjacent_pass.run(dag)<EOL>
Return a circuit with a barrier before last measurements.