Repository: Qiskit/qiskit-terra (python)

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.

Pull request:
Return `DAGOpNode` from `dagcircuit.replace_block_with_op`
<!--
⚠️ If you do not respect this template, your pull request will be closed.
⚠️ Your pull request title should be short detailed and understandable for all.
⚠️ Also, please add a release note file using reno if the change needs to be
  documented in the release notes.
⚠️ If your pull request fixes an open issue, please link to the issue.

- [ ] I have added the tests to cover my changes.
- [ ] I have updated the documentation accordingly.
- [ ] I have read the CONTRIBUTING document.
-->

### Summary

Fixes #8724

### Details and comments

This PR now returns the `DAGOpNode` that replaces the block from `dagcircuit.replace_block_with_op`.


Patch:
diff --git a/qiskit/dagcircuit/dagcircuit.py b/qiskit/dagcircuit/dagcircuit.py
index 721497775..223467347 100644
--- a/qiskit/dagcircuit/dagcircuit.py
+++ b/qiskit/dagcircuit/dagcircuit.py
@@ -1152,6 +1152,9 @@ class DAGCircuit:
             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 @@ class DAGCircuit:
         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.
 
diff --git a/releasenotes/notes/add-new-node-return-f2574c1593cbb57b.yaml b/releasenotes/notes/add-new-node-return-f2574c1593cbb57b.yaml
new file mode 100644
index 000000000..e1dca9f0c
--- /dev/null
+++ b/releasenotes/notes/add-new-node-return-f2574c1593cbb57b.yaml
@@ -0,0 +1,6 @@
+---
+upgrade:
+  - |
+    Updated :meth:`~qiskit.dagcircuit.DAGCircuit.replace_block_with_op` to now
+    return the new `~qiskit.dagcircuit.DAGOpNode` that is created when the block
+    is replaced.


Test patch:
diff --git a/test/python/dagcircuit/test_dagcircuit.py b/test/python/dagcircuit/test_dagcircuit.py
index bfdf0f77f..8024e13ad 100644
--- a/test/python/dagcircuit/test_dagcircuit.py
+++ b/test/python/dagcircuit/test_dagcircuit.py
@@ -1868,7 +1868,7 @@ class TestReplaceBlock(QiskitTestCase):
         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 @@ class TestReplaceBlock(QiskitTestCase):
 
         self.assertEqual(expected_dag, dag)
         self.assertEqual(expected_dag.count_ops(), dag.count_ops())
+        self.assertIsInstance(new_node.op, XGate)
 
 
 class TestDagProperties(QiskitTestCase):