Repository: Qiskit/qiskit

Issue:
commutation_checker.py tries to create identity operator with size 2^(2^num_qubits)
### Environment

- **Qiskit Terra version**: 0.22.2
- **Python version**: 3.10.8
- **Operating system**: Manjaro Linux 22.0.0

### What is happening?

I am trying to transpile a deep 10 qubit circuit with optimization_level=3. This results in
> ValueError: Maximum allowed dimension exceeded" in qiskit/circuit/commutation_checker.py, line 27, in _identity_op

### How can we reproduce the issue?
I broke my code down to this simple example:
``` 
import numpy as np
from qiskit import Aer, transpile, QuantumCircuit
from qiskit.circuit.library import XGate

n_qubits = 4
circ = QuantumCircuit(n_qubits + 1)
ctrl_str = format(np.random.randint(0, 2**n_qubits), "b").zfill(n_qubits)
mcx_gate =  XGate().control(num_ctrl_qubits=n_qubits, ctrl_state=ctrl_str).repeat(2)
circ.append(mcx_gate, list(range(n_qubits + 1)))
transpile(circ, backend=Aer.get_backend("aer_simulator"), optimization_level=3)
```
This fails with 
> numpy.core._exceptions._ArrayMemoryError: Unable to allocate 32.0 GiB for an array with shape (65536, 65536) and data type float64

on my system with 16GiB of memory.

### What should happen?

Transpilation should proceed without any issues.

### Any suggestions?

Simple fix: _identity_op() in commutation_checker.py takes as argument num_qubits:
https://github.com/Qiskit/qiskit-terra/blob/e775b4d19ad62558f30f39814b3d923ce03c8654/qiskit/circuit/commutation_checker.py#L24-L28
but is called with 2**num_qubits instead:
https://github.com/Qiskit/qiskit-terra/blob/e775b4d19ad62558f30f39814b3d923ce03c8654/qiskit/circuit/commutation_checker.py#L145-L147
So we simply need to replace the argument in line 147 with `extra_qarg2`. I don't know how to fix an issue unfortunately.


Discussion:
Thanks - your solution looks exactly correct to me.  Would you like to try fixing it?  If so, we have a [general contributing-to-Qiskit guide](https://qiskit.org/documentation/contributing_to_qiskit.html) and [a supplement specific to Terra](https://github.com/Qiskit/qiskit-terra/blob/main/CONTRIBUTING.md) that have instructions on how to do it, and you can feel free to ask here as well.

If not, no worries - just let us know here and we (or somebody else from the community) will take care of it.
Great, thanks for your quick response! If it's not too complicated I would like to try, might be a good way to learn more about git.
Sounds good to me - feel free to ask questions here if you need to, and either I or one of the community team will help out.  Don't worry if you get things wrong or forget something - there's nothing you can do to actually damage things on this repo.
I've assigned you to the issue so others know somebody's working on it, but don't worry if you need to drop out or anything - just let us know and I'll remove it again.  There's no time pressure or anything, it's just a tracker.

Resolving patch:
diff --git a/qiskit/circuit/commutation_checker.py b/qiskit/circuit/commutation_checker.py
--- a/qiskit/circuit/commutation_checker.py
+++ b/qiskit/circuit/commutation_checker.py
@@ -144,7 +144,7 @@ def commute(
             # being the lowest possible indices so the identity can be tensored before it.
             extra_qarg2 = num_qubits - len(qarg1)
             if extra_qarg2:
-                id_op = _identity_op(2**extra_qarg2)
+                id_op = _identity_op(extra_qarg2)
                 operator_1 = id_op.tensor(operator_1)
             op12 = operator_1.compose(operator_2, qargs=qarg2, front=False)
             op21 = operator_1.compose(operator_2, qargs=qarg2, front=True)


Test patch:
diff --git a/test/python/circuit/test_commutation_checker.py b/test/python/circuit/test_commutation_checker.py
--- a/test/python/circuit/test_commutation_checker.py
+++ b/test/python/circuit/test_commutation_checker.py
@@ -18,7 +18,7 @@
 from qiskit import ClassicalRegister
 from qiskit.test import QiskitTestCase
 
-from qiskit.circuit import QuantumRegister, Parameter
+from qiskit.circuit import QuantumRegister, Parameter, Qubit
 from qiskit.circuit import CommutationChecker
 from qiskit.circuit.library import (
     ZGate,
@@ -357,6 +357,12 @@ def test_complex_gates(self):
         res = comm_checker.commute(lf3, [0, 1, 2], [], lf4, [0, 1, 2], [])
         self.assertTrue(res)
 
+    def test_c7x_gate(self):
+        """Test wide gate works correctly."""
+        qargs = [Qubit() for _ in [None] * 8]
+        res = CommutationChecker().commute(XGate(), qargs[:1], [], XGate().control(7), qargs, [])
+        self.assertFalse(res)
+
 
 if __name__ == "__main__":
     unittest.main()