Repository: Qiskit/qiskit

Issue:
TwoQubitWeylDecomposition: failed to diagonalize M2 -- lack of normalization?
While writing [random test for 3586](https://github.com/Qiskit/qiskit-terra/pull/3586/files#diff-4dd440b6693c3194f4667ac2cdb99094), I noticed that a set this range wrongly (see https://github.com/Qiskit/qiskit-terra/pull/3586/commits/6e2b2709d8ffe702df8bce9a04089d8a9fb55245). However it seem that the previous range discover some crashes in `weyl_coordinates`.

### Steps to reproduce the problem

Currently, `two_qubit_decompose` is non-determinisic (see https://github.com/Qiskit/qiskit-terra/pull/3585), making reproduction complicated. Here are the situations I found:

```python
def test_cx_equivalence_1cx_random(rnd):
    qr = QuantumRegister(2, name='q')
    qc = QuantumCircuit(qr)

    qc.u3(rnd[0], rnd[1], rnd[2], qr[0])
    qc.u3(rnd[3], rnd[4], rnd[5], qr[1])

    qc.cx(qr[1], qr[0])

    qc.u3(rnd[6], rnd[7], rnd[8], qr[0])
    qc.u3(rnd[9], rnd[10], rnd[11], qr[1])

    sim = UnitarySimulatorPy()
    U = execute(qc, sim).result().get_unitary()
    two_qubit_cnot_decompose.num_basis_gates(U)


for _ in range(100):
    test_cx_equivalence_1cx_random(rnd=(0.0, 0.0, 0.0, 20895.999830149118, 4097.624991416554,
                                        0.00112247467014881, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0))
```

```python
def test_cx_equivalence_2cx_random(rnd):
    qr = QuantumRegister(2, name='q')
    qc = QuantumCircuit(qr)

    qc.u3(rnd[0], rnd[1], rnd[2], qr[0])
    qc.u3(rnd[3], rnd[4], rnd[5], qr[1])

    qc.cx(qr[1], qr[0])

    qc.u3(rnd[6], rnd[7], rnd[8], qr[0])
    qc.u3(rnd[9], rnd[10], rnd[11], qr[1])

    qc.cx(qr[0], qr[1])

    qc.u3(rnd[12], rnd[13], rnd[14], qr[0])
    qc.u3(rnd[15], rnd[16], rnd[17], qr[1])

    sim = UnitarySimulatorPy()
    U = execute(qc, sim).result().get_unitary()
    two_qubit_cnot_decompose.num_basis_gates(U)


for _ in range(100):
    test_cx_equivalence_2cx_random(rnd=(0.042351722707424384, 73735.43772411175,
                                        0.01757621764727491, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                                        0.004490852354911424, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                                        0.0))
```
```python
def test_cx_equivalence_3cx_random(rnd):
    qr = QuantumRegister(2, name='q')
    qc = QuantumCircuit(qr)

    qc.u3(rnd[0], rnd[1], rnd[2], qr[0])
    qc.u3(rnd[3], rnd[4], rnd[5], qr[1])

    qc.cx(qr[1], qr[0])

    qc.u3(rnd[6], rnd[7], rnd[8], qr[0])
    qc.u3(rnd[9], rnd[10], rnd[11], qr[1])

    qc.cx(qr[0], qr[1])

    qc.u3(rnd[12], rnd[13], rnd[14], qr[0])
    qc.u3(rnd[15], rnd[16], rnd[17], qr[1])

    qc.cx(qr[1], qr[0])

    qc.u3(rnd[18], rnd[19], rnd[20], qr[0])
    qc.u3(rnd[21], rnd[22], rnd[23], qr[1])

    sim = UnitarySimulatorPy()
    U = execute(qc, sim).result().get_unitary()
    two_qubit_cnot_decompose.num_basis_gates(U)

for _ in range(100):
    test_cx_equivalence_3cx_random(rnd=(0.17845153804438854, 8128.06054498254, 12287.99999713898,
                                        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                                        1983.8820662647765, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0))
```

All this cases trigger (eventually) `qiskit.exceptions.QiskitError: 'TwoQubitWeylDecomposition: failed to diagonalize M2'`.

Because they do not raise when the range is shorter (see https://github.com/Qiskit/qiskit-terra/pull/3586/commits/6e2b2709d8ffe702df8bce9a04089d8a9fb55245) I assume is lack of normalization.


Discussion:
these failures occur on master now too, or only after #3585 ?
Current master.

(also after #3585, but this issue is about current master)
I have been running into QiskitError: 'TwoQubitWeylDecomposition: failed to diagonalize M2 quite often when running a QAOA type application. What do we need to do to fix this?
Another example. Consider this minimal example:
```python
circuit = QuantumCircuit(2)
circuit.u2(-804.15,pi, 0)
circuit.u2(-6433.2,pi, 1)
circuit.cx(0, 1)
circuit.cx(1, 0)
circuit.draw()
```
```
     ┌────────────────┐     ┌───┐
q_0: ┤ U2(-804.15,pi) ├──■──┤ X ├
     ├────────────────┤┌─┴─┐└─┬─┘
q_1: ┤ U2(-6433.2,pi) ├┤ X ├──■──
     └────────────────┘└───┘     
```
The passes should not fail.
```python
pm = PassManager([Collect2qBlocks(), ConsolidateBlocks()])
pm.run(circuit)
```
```
QiskitError: 'TwoQubitWeylDecomposition: failed to diagonalize M2'
```
I think having a fallback for `ConsolidateBlocks`  when `TwoQubitWeylDecomposition` raises is a good starting.
because affects the default level 3, `priority: high`
Does mapping those phase values to be within 2pi work?  I am guessing that is the issue. 

Resolving patch:
diff --git a/qiskit/quantum_info/synthesis/weyl.py b/qiskit/quantum_info/synthesis/weyl.py
--- a/qiskit/quantum_info/synthesis/weyl.py
+++ b/qiskit/quantum_info/synthesis/weyl.py
@@ -54,10 +54,13 @@ def weyl_coordinates(U):
         M2real = np.random.randn()*M2.real + np.random.randn()*M2.imag
         _, P = la.eigh(M2real)
         D = P.T.dot(M2).dot(P).diagonal()
-        if np.allclose(P.dot(np.diag(D)).dot(P.T), M2, rtol=1.0e-13, atol=1.0e-13):
+        if np.allclose(P.dot(np.diag(D)).dot(P.T), M2, rtol=1.0e-10, atol=1.0e-10):
             break
     else:
-        raise QiskitError("TwoQubitWeylDecomposition: failed to diagonalize M2")
+        raise QiskitError("TwoQubitWeylDecomposition: failed to diagonalize M2. "
+                          "Please submit this output to "
+                          "https://github.com/Qiskit/qiskit-terra/issues/4159 "
+                          "Input %s" % U.tolist())
 
     d = -np.angle(D)/2
     d[3] = -d[0]-d[1]-d[2]


Test patch:
diff --git a/test/python/transpiler/test_consolidate_blocks.py b/test/python/transpiler/test_consolidate_blocks.py
--- a/test/python/transpiler/test_consolidate_blocks.py
+++ b/test/python/transpiler/test_consolidate_blocks.py
@@ -204,6 +204,29 @@ def test_node_added_before_block(self):
 
         self.assertEqual(qc, qc1)
 
+    def test_consolidate_blocks_big(self):
+        """Test ConsolidateBlocks with U2(<big numbers>)
+        https://github.com/Qiskit/qiskit-terra/issues/3637#issuecomment-612954865
+
+             ┌────────────────┐     ┌───┐
+        q_0: ┤ U2(-804.15,pi) ├──■──┤ X ├
+             ├────────────────┤┌─┴─┐└─┬─┘
+        q_1: ┤ U2(-6433.2,pi) ├┤ X ├──■──
+             └────────────────┘└───┘
+        """
+        circuit = QuantumCircuit(2)
+        circuit.u2(-804.15, np.pi, 0)
+        circuit.u2(-6433.2, np.pi, 1)
+        circuit.cx(0, 1)
+        circuit.cx(1, 0)
+
+        pass_manager = PassManager()
+        pass_manager.append(Collect2qBlocks())
+        pass_manager.append(ConsolidateBlocks())
+        result = pass_manager.run(circuit)
+
+        self.assertEqual(circuit, result)
+
     def test_node_added_after_block(self):
         """Test that a node after the block remains after the block