Repository: Qiskit/qiskit

Issue:
Transpiling 1q circuit at optimization level 3 breaks in 1q basis
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Information

- **Qiskit Terra version**: 5bdd12db84b2ef13292d26cd6750fa8fabd61bb3
- **Python version**: 3.7.5
- **Operating system**: MacOs Catalina

### What is the current behavior?

The following code breaks if `basis_gates=['u3']` and `optimization_level=3`. 
It works for `basis_gates=['u3','cx']` or if `optimization_level<=2`.

```python
qc = QuantumCircuit(1)
qc.x(0)
transpile(qc, basis_gates=['u3'], optimization_level=3)
```

Edit: changed to a simpler example.

### Traceback

```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jul/Work/Qiskit/qiskit-terra/qiskit/compiler/transpile.py", line 214, in transpile
    circuits = parallel_map(_transpile_circuit, list(zip(circuits, transpile_args)))
  File "/Users/jul/Work/Qiskit/qiskit-terra/qiskit/tools/parallel.py", line 108, in parallel_map
    return [task(values[0], *task_args, **task_kwargs)]
  File "/Users/jul/Work/Qiskit/qiskit-terra/qiskit/compiler/transpile.py", line 304, in _transpile_circuit
    pass_manager = level_3_pass_manager(pass_manager_config)
  File "/Users/jul/Work/Qiskit/qiskit-terra/qiskit/transpiler/preset_passmanagers/level3.py", line 176, in level_3_pass_manager
    ConsolidateBlocks(basis_gates=basis_gates),
  File "/Users/jul/Work/Qiskit/qiskit-terra/qiskit/transpiler/basepasses.py", line 31, in __call__
    pass_instance = type.__call__(cls, *args, **kwargs)
  File "/Users/jul/Work/Qiskit/qiskit-terra/qiskit/transpiler/passes/optimization/consolidate_blocks.py", line 63, in __init__
    self.decomposer = TwoQubitBasisDecomposer(kak_basis_gate)
  File "/Users/jul/Work/Qiskit/qiskit-terra/qiskit/quantum_info/synthesis/two_qubit_decompose.py", line 293, in __init__
    basis = self.basis = TwoQubitWeylDecomposition(Operator(gate).data)
  File "/Users/jul/Work/Qiskit/qiskit-terra/qiskit/quantum_info/operators/operator.py", line 103, in __init__
    raise QiskitError("Invalid input data format for Operator")
qiskit.exceptions.QiskitError: 'Invalid input data format for Operator'
```


### What is the expected behavior?

Transpile with only a single qubit gate, if possible (or throw a meaningful error).




Discussion:
i got expected behaviour , try updating or moving to another low version of terra or other
On stable it still works, right, but we should fix it on the master version (ideally before the release). Which versions did you try for transpiling? I'm running on 5bdd12db84b2ef13292d26cd6750fa8fabd61bb3 (version of July 30th).


Resolving patch:
diff --git a/qiskit/transpiler/passes/optimization/consolidate_blocks.py b/qiskit/transpiler/passes/optimization/consolidate_blocks.py
--- a/qiskit/transpiler/passes/optimization/consolidate_blocks.py
+++ b/qiskit/transpiler/passes/optimization/consolidate_blocks.py
@@ -60,7 +60,10 @@ def __init__(self,
             self.decomposer = TwoQubitBasisDecomposer(kak_basis_gate)
         elif basis_gates is not None:
             kak_basis_gate = unitary_synthesis._choose_kak_gate(basis_gates)
-            self.decomposer = TwoQubitBasisDecomposer(kak_basis_gate)
+            if kak_basis_gate is not None:
+                self.decomposer = TwoQubitBasisDecomposer(kak_basis_gate)
+            else:
+                self.decomposer = None
         else:
             self.decomposer = TwoQubitBasisDecomposer(CXGate())
 


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
@@ -310,6 +310,15 @@ def test_classical_conditions_maintained(self):
 
         self.assertEqual(qc, qc1)
 
+    def test_no_kak_in_basis(self):
+        """Test that pass just returns the input dag without a KAK gate."""
+        qc = QuantumCircuit(1)
+        qc.h(0)
+        dag = circuit_to_dag(qc)
+        consolidate_blocks_pass = ConsolidateBlocks(basis_gates=['u3'])
+        res = consolidate_blocks_pass.run(dag)
+        self.assertEqual(res, dag)
+
 
 if __name__ == '__main__':
     unittest.main()