Repository: Qiskit/qiskit

Issue:
small improvement to DAGCircuit.replace_block_with_op()
### What should we add?

Short version: 
- Add the line `return new_node` to the end of `DAGCircuit.replace_block_with_op()`.

Details:
`DAGCircuit.replace_block_with_op(block, op, ...)` internally creates a `new_node` containing `op`, and replaces the `block` of DAG nodes by this `new_node`.

I think `replace_block_with_op()` should return `new_node`. (Currently there is no `return` statement). This change would be in line with the behavior of `DAGCircuit.substitute_node()`, which does return its internal `new_node`. Otherwise, the user does not know the node_id or even the name of the new node, so finding the new node for further manipulations is difficult. (All I can think of is to look along one of the wires for the one node in the new DAG that wasn't in the old DAG... obviously this is cumbersome.) Since the function already has `new_node` available internally, seems like we may as well `return` it.


Discussion:
This sounds very sensible to me. Would you be able to make the PR for it?
@aeddins-ibm and @jakelishman. I can take care of this if you like.
Yes please, Edwin - I'm guessing Andrew doesn't have time to do it himself.  I'll assign you - there's no particular rush because we won't merge any PR before Terra 0.22.0rc1 goes out (hopefully Thursday, but both Matthew and I are quite busy on other things as well).
Thanks @enavarro51 , that would be great! (Sorry @jakelishman , I kept intending to come back to this but never found the time)
No worries at all - there's no rush from our side!

Resolving patch:
diff --git a/qiskit/dagcircuit/dagcircuit.py b/qiskit/dagcircuit/dagcircuit.py
--- a/qiskit/dagcircuit/dagcircuit.py
+++ b/qiskit/dagcircuit/dagcircuit.py
@@ -1152,6 +1152,9 @@ def replace_block_with_op(self, node_block, op, wire_pos_map, cycle_check=True):
             DAGCircuitError: if ``cycle_check`` is set to ``True`` and replacing
                 the specified block introduces a cycle or if ``node_block`` is
                 empty.
+
+        Returns:
+            DAGOpNode: The op node that replaces the block.
         """
         block_qargs = set()
         block_cargs = set()
@@ -1187,6 +1190,8 @@ def replace_block_with_op(self, node_block, op, wire_pos_map, cycle_check=True):
         for nd in node_block:
             self._decrement_op(nd.op)
 
+        return new_node
+
     def substitute_node_with_dag(self, node, input_dag, wires=None, propagate_condition=True):
         """Replace one node with dag.
 


Test patch:
diff --git a/test/python/dagcircuit/test_dagcircuit.py b/test/python/dagcircuit/test_dagcircuit.py
--- a/test/python/dagcircuit/test_dagcircuit.py
+++ b/test/python/dagcircuit/test_dagcircuit.py
@@ -1868,7 +1868,7 @@ def test_single_node_block(self):
         dag = DAGCircuit()
         dag.add_qreg(qr)
         node = dag.apply_operation_back(HGate(), [qr[0]])
-        dag.replace_block_with_op(
+        new_node = dag.replace_block_with_op(
             [node], XGate(), {bit: idx for (idx, bit) in enumerate(dag.qubits)}
         )
 
@@ -1878,6 +1878,7 @@ def test_single_node_block(self):
 
         self.assertEqual(expected_dag, dag)
         self.assertEqual(expected_dag.count_ops(), dag.count_ops())
+        self.assertIsInstance(new_node.op, XGate)
 
 
 class TestDagProperties(QiskitTestCase):