Repository: Qiskit/qiskit

Issue:
fix text circuit drawer for unitary gates
<!-- ⚠️ 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**:
- **Python version**:
- **Operating system**:

### What is the current behavior?
exception is raised (TypeError) when attempting to draw circuit with unitary matrix gate.


### Steps to reproduce the problem
print circuit with unitary gate


### What is the expected behavior?
Draw a box with "unitary". Don't need to try displaying matrix.


### Suggested solutions





Discussion:

Resolving patch:
diff --git a/qiskit/tools/visualization/_text.py b/qiskit/tools/visualization/_text.py
--- a/qiskit/tools/visualization/_text.py
+++ b/qiskit/tools/visualization/_text.py
@@ -12,6 +12,7 @@
 from shutil import get_terminal_size
 import sys
 import numpy
+import sympy
 
 from .exceptions import VisualizationError
 
@@ -595,7 +596,7 @@ def params_for_label(instruction):
         """Get the params and format them to add them to a label. None if there are no params."""
         if 'op' in instruction and hasattr(instruction['op'], 'params'):
             return ['%.5g' % i for i in instruction['op'].params
-                    if not isinstance(i, numpy.ndarray)]
+                    if not isinstance(i, (numpy.ndarray, sympy.Matrix))]
         return None
 
     @staticmethod


Test patch:
diff --git a/test/python/tools/visualization/test_circuit_text_drawer.py b/test/python/tools/visualization/test_circuit_text_drawer.py
--- a/test/python/tools/visualization/test_circuit_text_drawer.py
+++ b/test/python/tools/visualization/test_circuit_text_drawer.py
@@ -843,6 +843,38 @@ def test_text_justify_right_measure_resize(self):
         circuit.measure(qr1[1], cr1[1])
         self.assertEqual(str(_text_circuit_drawer(circuit, justify='right')), expected)
 
+    def test_text_unitary_1q(self):
+        """Test unitary matrix gate on 1 qubit"""
+        qr = QuantumRegister(1, 'q')
+        cr = ClassicalRegister(1, 'c')
+        circuit = QuantumCircuit(qr, cr)
+        circuit.unitary([[1, 0], [0, 1]], qr[0])
+        actual = str(_text_circuit_drawer(circuit))
+        expected = '\n'.join(['        ┌─────────┐',
+                              'q_0: |0>┤ Unitary ├',
+                              '        └─────────┘',
+                              ' c_0: 0 ═══════════',
+                              '                   '])
+        self.assertEqual(actual, expected)
+
+    def test_text_unitary_2q(self):
+        """Test unitary matrix gate on 1 qubit"""
+        qr = QuantumRegister(3, 'q')
+        circuit = QuantumCircuit(qr)
+        circuit.unitary([[1, 0, 0, 0],
+                         [0, 1, 0, 0],
+                         [0, 0, 0, 1],
+                         [0, 0, 1, 0]], qr[1], qr[2])
+        actual = str(_text_circuit_drawer(circuit))
+        expected = '\n'.join(['                   ',
+                              'q_0: |0>───────────',
+                              '        ┌─────────┐',
+                              'q_1: |0>┤         ├',
+                              '        │ Unitary │',
+                              'q_2: |0>┤         ├',
+                              '        └─────────┘'])
+        self.assertEqual(actual, expected)
+
 
 if __name__ == '__main__':
     unittest.main()