Repository: Qiskit/qiskit

Issue:
text drawer should capitalize gate names
The text drawer currently changes gate name capitalizations to capitalize the first letter. I think it should just rely on the gate name and not intervene. 



Discussion:
The name of `HGate` is "h". Is that fine? 
Hm. How about we capitalize all then? 
`X, Y, Z, S, T, RX, RY, RZ, RXX, RYY, RZZ, MS, ...`
I think that looks nicer than just the first letter capitalized, and is consistent with the recent class name upgrades (https://github.com/Qiskit/qiskit-terra/pull/3695)
so.. all the names in all capital?

I agree that, if there is `label`, it should be as it is.
Ok then what if we assign default `label` to all those standard gates?
`RZGate.name = 'rz'` and `RZGate.label = 'RZ'`
and so on...

Witht the understanding that "label" is how i want my gates displayed, and name is what backends (or QASM) expect. Then we can capitalize as we wish. For example I want to display "iSWAP" not ISWAP or iswap or iSwap or ISwap (#3992)
This will break aer noise models unless you build it using the new labels instead of gate names.

In particular this will break the device noise models which only have the names stored in their backend configuration
ok then nevermind. can we just capitalize everything then in the drawers?

Resolving patch:
diff --git a/qiskit/visualization/text.py b/qiskit/visualization/text.py
--- a/qiskit/visualization/text.py
+++ b/qiskit/visualization/text.py
@@ -20,7 +20,12 @@
 import sys
 from numpy import ndarray
 
-from qiskit.circuit import ControlledGate
+from qiskit.circuit import ControlledGate, Gate, Instruction
+from qiskit.circuit import Reset as ResetInstruction
+from qiskit.circuit import Measure as MeasureInstruction
+from qiskit.extensions import IGate, UnitaryGate, HamiltonianGate, RZZGate, SwapGate, Snapshot
+from qiskit.extensions import Barrier as BarrierInstruction
+from qiskit.extensions.quantum_initializer.initializer import Initialize
 from .tools.pi_check import pi_check
 from .exceptions import VisualizationError
 
@@ -715,22 +720,31 @@ def params_for_label(instruction):
                 ret.append('%s' % param)
         return ret
 
+    @staticmethod
+    def special_label(instruction):
+        """Some instructions have special labels"""
+        labels = {IGate: 'I',
+                  Initialize: 'initialize',
+                  UnitaryGate: 'unitary',
+                  HamiltonianGate: 'Hamiltonian'}
+        instruction_type = type(instruction)
+        if instruction_type in {Gate, Instruction}:
+            return instruction.name
+        return labels.get(instruction_type, None)
+
     @staticmethod
     def label_for_box(instruction, controlled=False):
         """ Creates the label for a box."""
         if getattr(instruction.op, 'label', None) is not None:
             return instruction.op.label
+
         if controlled:
-            label = instruction.op.base_gate.name
+            label = TextDrawing.special_label(instruction.op.base_gate) or \
+                    instruction.op.base_gate.name.upper()
         else:
-            label = instruction.name
+            label = TextDrawing.special_label(instruction.op) or instruction.name.upper()
         params = TextDrawing.params_for_label(instruction)
 
-        # generate correct label for the box
-        if label == 'id':
-            label = 'i'
-        label = label.capitalize()
-
         if params:
             label += "(%s)" % ','.join(params)
         return label
@@ -882,12 +896,12 @@ def add_connected_gate(instruction, gates, layer, current_cons):
             layer._set_multibox(instruction.op.label, qubits=instruction.qargs,
                                 conditional=conditional)
 
-        elif instruction.name == 'measure':
+        elif isinstance(instruction.op, MeasureInstruction):
             gate = MeasureFrom()
             layer.set_qubit(instruction.qargs[0], gate)
             layer.set_clbit(instruction.cargs[0], MeasureTo())
 
-        elif instruction.name in ['barrier', 'snapshot', 'save', 'load', 'noise']:
+        elif isinstance(instruction.op, (BarrierInstruction, Snapshot)):
             # barrier
             if not self.plotbarriers:
                 return layer, current_cons, connection_label
@@ -895,15 +909,16 @@ def add_connected_gate(instruction, gates, layer, current_cons):
             for qubit in instruction.qargs:
                 layer.set_qubit(qubit, Barrier())
 
-        elif instruction.name == 'swap':
+        elif isinstance(instruction.op, SwapGate):
             # swap
             gates = [Ex(conditional=conditional) for _ in range(len(instruction.qargs))]
             add_connected_gate(instruction, gates, layer, current_cons)
 
-        elif instruction.name == 'reset':
+        elif isinstance(instruction.op, ResetInstruction):
+            # reset
             layer.set_qubit(instruction.qargs[0], Reset(conditional=conditional))
 
-        elif instruction.name == 'rzz':
+        elif isinstance(instruction.op, RZZGate):
             # rzz
             connection_label = "zz(%s)" % TextDrawing.params_for_label(instruction)[0]
             gates = [Bullet(conditional=conditional), Bullet(conditional=conditional)]


Test patch:
diff --git a/test/python/visualization/references/circuit_text_ref.txt b/test/python/visualization/references/circuit_text_ref.txt
--- a/test/python/visualization/references/circuit_text_ref.txt
+++ b/test/python/visualization/references/circuit_text_ref.txt
@@ -1,7 +1,7 @@
         ┌───┐┌───┐┌───┐ ░ ┌───┐┌───┐┌─────┐┌───┐┌─────┐┌───┐     ┌────────┐┌────────┐┌────────┐┌────────┐┌───────────┐┌──────────────┐                                                                                ┌─┐      
-q_0: |0>┤ X ├┤ Y ├┤ Z ├─░─┤ H ├┤ S ├┤ Sdg ├┤ T ├┤ Tdg ├┤ I ├─|0>─┤ Rx(pi) ├┤ Ry(pi) ├┤ Rz(pi) ├┤ U1(pi) ├┤ U2(pi,pi) ├┤ U3(pi,pi,pi) ├─X───■────■───■───■───■──────────■────────────■─────────■─────────■───────■───■─┤M├──────
+q_0: |0>┤ X ├┤ Y ├┤ Z ├─░─┤ H ├┤ S ├┤ SDG ├┤ T ├┤ TDG ├┤ I ├─|0>─┤ RX(pi) ├┤ RY(pi) ├┤ RZ(pi) ├┤ U1(pi) ├┤ U2(pi,pi) ├┤ U3(pi,pi,pi) ├─X───■────■───■───■───■──────────■────────────■─────────■─────────■───────■───■─┤M├──────
         └─░─┘└───┘└───┘ ░ └───┘└───┘└─────┘└───┘└─────┘└───┘     └────────┘└────────┘└────────┘└────────┘└───────────┘└──────────────┘ │ ┌─┴─┐┌─┴─┐ │ ┌─┴─┐ │pi ┌──────┴───────┐┌───┴────┐┌───┴────┐┌───┴────┐  │   │ └╥┘┌─┐   
-q_1: |0>──░────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────X─┤ X ├┤ Y ├─■─┤ H ├─■───┤ U3(pi,pi,pi) ├┤ Rz(pi) ├┤ Ry(pi) ├┤ Rx(pi) ├──■───X──╫─┤M├───
+q_1: |0>──░────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────X─┤ X ├┤ Y ├─■─┤ H ├─■───┤ U3(pi,pi,pi) ├┤ RZ(pi) ├┤ RY(pi) ├┤ RX(pi) ├──■───X──╫─┤M├───
           ░                                                                                                                              └───┘└───┘   └───┘     └──────────────┘└────────┘└────────┘└────────┘┌─┴─┐ │  ║ └╥┘┌─┐
 q_2: |0>──░───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤ X ├─X──╫──╫─┤M├
           ░                                                                                                                                                                                                   └───┘    ║  ║ └╥┘
diff --git a/test/python/visualization/test_circuit_text_drawer.py b/test/python/visualization/test_circuit_text_drawer.py
--- a/test/python/visualization/test_circuit_text_drawer.py
+++ b/test/python/visualization/test_circuit_text_drawer.py
@@ -329,9 +329,9 @@ def test_text_cu3_reverse_bits(self):
     def test_text_crz(self):
         """ crz drawing. """
         expected = '\n'.join(["                    ┌──────────┐",
-                              "q_0: |0>─────■──────┤ Rz(pi/2) ├",
+                              "q_0: |0>─────■──────┤ RZ(pi/2) ├",
                               "        ┌────┴─────┐└────┬─────┘",
-                              "q_1: |0>┤ Rz(pi/2) ├─────┼──────",
+                              "q_1: |0>┤ RZ(pi/2) ├─────┼──────",
                               "        └──────────┘     │      ",
                               "q_2: |0>─────────────────■──────",
                               "                                "])
@@ -344,9 +344,9 @@ def test_text_crz(self):
     def test_text_cry(self):
         """ cry drawing. """
         expected = '\n'.join(["                    ┌──────────┐",
-                              "q_0: |0>─────■──────┤ Ry(pi/2) ├",
+                              "q_0: |0>─────■──────┤ RY(pi/2) ├",
                               "        ┌────┴─────┐└────┬─────┘",
-                              "q_1: |0>┤ Ry(pi/2) ├─────┼──────",
+                              "q_1: |0>┤ RY(pi/2) ├─────┼──────",
                               "        └──────────┘     │      ",
                               "q_2: |0>─────────────────■──────",
                               "                                "])
@@ -359,9 +359,9 @@ def test_text_cry(self):
     def test_text_crx(self):
         """ crx drawing. """
         expected = '\n'.join(["                    ┌──────────┐",
-                              "q_0: |0>─────■──────┤ Rx(pi/2) ├",
+                              "q_0: |0>─────■──────┤ RX(pi/2) ├",
                               "        ┌────┴─────┐└────┬─────┘",
-                              "q_1: |0>┤ Rx(pi/2) ├─────┼──────",
+                              "q_1: |0>┤ RX(pi/2) ├─────┼──────",
                               "        └──────────┘     │      ",
                               "q_2: |0>─────────────────■──────",
                               "                                "])
@@ -530,6 +530,25 @@ def test_text_single_gate(self):
         circuit.h(qr2[1])
         self.assertEqual(str(_text_circuit_drawer(circuit)), expected)
 
+    def test_text_id(self):
+        """Id drawing."""
+        expected = '\n'.join(["         ┌───┐",
+                              "q1_0: |0>┤ I ├",
+                              "         ├───┤",
+                              "q1_1: |0>┤ I ├",
+                              "         └───┘",
+                              "q2_0: |0>─────",
+                              "         ┌───┐",
+                              "q2_1: |0>┤ I ├",
+                              "         └───┘"])
+
+        qr1 = QuantumRegister(2, 'q1')
+        qr2 = QuantumRegister(2, 'q2')
+        circuit = QuantumCircuit(qr1, qr2)
+        circuit.id(qr1)
+        circuit.id(qr2[1])
+        self.assertEqual(str(_text_circuit_drawer(circuit)), expected)
+
     def test_text_barrier(self):
         """ Barrier drawing. """
         expected = '\n'.join(['          ░ ',
@@ -791,7 +810,7 @@ def test_text_spacing_2378(self):
                               "              │      ",
                               "q_1: |0>──────X──────",
                               "        ┌───────────┐",
-                              "q_2: |0>┤ Rz(11111) ├",
+                              "q_2: |0>┤ RZ(11111) ├",
                               "        └───────────┘"])
         qr = QuantumRegister(3, 'q')
         circuit = QuantumCircuit(qr)
@@ -891,12 +910,12 @@ def test_2Qgate_nottogether_across_4(self):
         self.assertEqual(str(_text_circuit_drawer(circuit, reverse_bits=True)), expected)
 
     def test_unitary_nottogether_across_4(self):
-        """ Unitary that are 2 bits apart"""
+        """ unitary that are 2 bits apart"""
         expected = '\n'.join(["        ┌──────────┐",
                               "q_0: |0>┤0         ├",
                               "        │          │",
                               "q_1: |0>┤          ├",
-                              "        │  Unitary │",
+                              "        │  unitary │",
                               "q_2: |0>┤          ├",
                               "        │          │",
                               "q_3: |0>┤1         ├",
@@ -913,7 +932,7 @@ def test_kraus(self):
         """ Test Kraus.
         See https://github.com/Qiskit/qiskit-terra/pull/2238#issuecomment-487630014"""
         expected = '\n'.join(["        ┌───────┐",
-                              "q_0: |0>┤ Kraus ├",
+                              "q_0: |0>┤ kraus ├",
                               "        └───────┘"])
 
         error = SuperOp(0.75 * numpy.eye(4) + 0.25 * numpy.diag([1, -1, -1, 1]))
@@ -928,7 +947,7 @@ def test_multiplexer(self):
         See https://github.com/Qiskit/qiskit-terra/pull/2238#issuecomment-487630014"""
         expected = '\n'.join(["        ┌──────────────┐",
                               "q_0: |0>┤0             ├",
-                              "        │  Multiplexer │",
+                              "        │  multiplexer │",
                               "q_1: |0>┤1             ├",
                               "        └──────────────┘"])
 
@@ -993,7 +1012,7 @@ def test_text_bound_parameters(self):
         """Bound parameters
         See: https://github.com/Qiskit/qiskit-terra/pull/3876 """
         expected = '\n'.join(["         ┌──────────────┐",
-                              "qr_0: |0>┤ My_u2(pi,pi) ├",
+                              "qr_0: |0>┤ my_u2(pi,pi) ├",
                               "         └──────────────┘"])
 
         my_u2_circuit = QuantumCircuit(1, name='my_u2')
@@ -1506,7 +1525,7 @@ def test_conditional_multiplexer(self):
 
         expected = '\n'.join(["         ┌──────────────┐",
                               "qr_0: |0>┤0             ├",
-                              "         │  Multiplexer │",
+                              "         │  multiplexer │",
                               "qr_1: |0>┤1             ├",
                               "         └──────┬───────┘",
                               "qr_2: |0>───────┼────────",
@@ -1575,7 +1594,7 @@ def test_text_complex(self):
         See https://github.com/Qiskit/qiskit-terra/issues/3640 """
         expected = '\n'.join(["     ┌────────────────────────────────────┐",
                               "q_0: ┤0                                   ├",
-                              "     │  Initialize(0.5+0.1j,0,0,0.86023j) │",
+                              "     │  initialize(0.5+0.1j,0,0,0.86023j) │",
                               "q_1: ┤1                                   ├",
                               "     └────────────────────────────────────┘"])
         ket = numpy.array([0.5 + 0.1 * 1j, 0, 0, 0.8602325267042626 * 1j])
@@ -1588,7 +1607,7 @@ def test_text_complex_pireal(self):
         See https://github.com/Qiskit/qiskit-terra/issues/3640 """
         expected = '\n'.join(["        ┌─────────────────────────────────┐",
                               "q_0: |0>┤0                                ├",
-                              "        │  Initialize(pi/10,0,0,0.94937j) │",
+                              "        │  initialize(pi/10,0,0,0.94937j) │",
                               "q_1: |0>┤1                                ├",
                               "        └─────────────────────────────────┘"
                               ])
@@ -1602,7 +1621,7 @@ def test_text_complex_piimaginary(self):
         See https://github.com/Qiskit/qiskit-terra/issues/3640 """
         expected = '\n'.join(["        ┌─────────────────────────────────┐",
                               "q_0: |0>┤0                                ├",
-                              "        │  Initialize(0.94937,0,0,pi/10j) │",
+                              "        │  initialize(0.94937,0,0,pi/10j) │",
                               "q_1: |0>┤1                                ├",
                               "        └─────────────────────────────────┘"
                               ])
@@ -1620,7 +1639,7 @@ def test_text_all_1q_1c(self):
         """ Test q0-c0 in q0-c0"""
         expected = '\n'.join(["         ┌───────┐",
                               "qr_0: |0>┤0      ├",
-                              "         │  Name │",
+                              "         │  name │",
                               " cr_0: 0 ╡0      ╞",
                               "         └───────┘"])
 
@@ -1638,7 +1657,7 @@ def test_text_all_2q_2c(self):
                               "qr_0: |0>┤0      ├",
                               "         │       │",
                               "qr_1: |0>┤1      ├",
-                              "         │  Name │",
+                              "         │  name │",
                               " cr_0: 0 ╡0      ╞",
                               "         │       │",
                               " cr_1: 0 ╡1      ╞",
@@ -1664,7 +1683,7 @@ def test_text_4q_2c(self):
                               "q_3: |0>┤2      ├",
                               "        │       │",
                               "q_4: |0>┤3      ├",
-                              "        │  Name │",
+                              "        │  name │",
                               "q_5: |0>┤       ├",
                               "        │       │",
                               " c_0: 0 ╡       ╞",
@@ -1885,7 +1904,7 @@ def test_controlled_composite_gate_edge(self):
                               "q_0: |0>┤0     ├",
                               "        │      │",
                               "q_1: |0>■      ├",
-                              "        │  Ghz │",
+                              "        │  ghz │",
                               "q_2: |0>┤1     ├",
                               "        │      │",
                               "q_3: |0>┤2     ├",
@@ -1908,7 +1927,7 @@ def test_controlled_composite_gate_top(self):
                               "        ┌──┴───┐",
                               "q_1: |0>┤0     ├",
                               "        │      │",
-                              "q_2: |0>┤2 Ghz ├",
+                              "q_2: |0>┤2 ghz ├",
                               "        │      │",
                               "q_3: |0>┤1     ├",
                               "        └──────┘"])
@@ -1928,7 +1947,7 @@ def test_controlled_composite_gate_bot(self):
         expected = '\n'.join(["        ┌──────┐",
                               "q_0: |0>┤1     ├",
                               "        │      │",
-                              "q_1: |0>┤0 Ghz ├",
+                              "q_1: |0>┤0 ghz ├",
                               "        │      │",
                               "q_2: |0>┤2     ├",
                               "        └──┬───┘",
@@ -1952,7 +1971,7 @@ def test_controlled_composite_gate_top_bot(self):
                               "        ┌──┴───┐",
                               "q_1: |0>┤0     ├",
                               "        │      │",
-                              "q_2: |0>┤1 Ghz ├",
+                              "q_2: |0>┤1 ghz ├",
                               "        │      │",
                               "q_3: |0>┤2     ├",
                               "        └──┬───┘",
@@ -1977,7 +1996,7 @@ def test_controlled_composite_gate_all(self):
                               "q_1: |0>┤0     ├",
                               "        │      │",
                               "q_2: |0>■      ├",
-                              "        │  Ghz │",
+                              "        │  ghz │",
                               "q_3: |0>┤1     ├",
                               "        │      │",
                               "q_4: |0>┤2     ├",
@@ -2156,7 +2175,7 @@ def test_controlled_composite_gate_edge(self):
                               "q_0: |0>┤0     ├",
                               "        │      │",
                               "q_1: |0>o      ├",
-                              "        │  Ghz │",
+                              "        │  ghz │",
                               "q_2: |0>┤1     ├",
                               "        │      │",
                               "q_3: |0>┤2     ├",
@@ -2179,7 +2198,7 @@ def test_controlled_composite_gate_top(self):
                               "        ┌──┴───┐",
                               "q_1: |0>┤0     ├",
                               "        │      │",
-                              "q_2: |0>┤2 Ghz ├",
+                              "q_2: |0>┤2 ghz ├",
                               "        │      │",
                               "q_3: |0>┤1     ├",
                               "        └──────┘"])
@@ -2199,7 +2218,7 @@ def test_controlled_composite_gate_bot(self):
         expected = '\n'.join(["        ┌──────┐",
                               "q_0: |0>┤1     ├",
                               "        │      │",
-                              "q_1: |0>┤0 Ghz ├",
+                              "q_1: |0>┤0 ghz ├",
                               "        │      │",
                               "q_2: |0>┤2     ├",
                               "        └──┬───┘",
@@ -2223,7 +2242,7 @@ def test_controlled_composite_gate_top_bot(self):
                               "        ┌──┴───┐",
                               "q_1: |0>┤0     ├",
                               "        │      │",
-                              "q_2: |0>┤1 Ghz ├",
+                              "q_2: |0>┤1 ghz ├",
                               "        │      │",
                               "q_3: |0>┤2     ├",
                               "        └──┬───┘",
@@ -2248,7 +2267,7 @@ def test_controlled_composite_gate_all(self):
                               "q_1: |0>┤0     ├",
                               "        │      │",
                               "q_2: |0>o      ├",
-                              "        │  Ghz │",
+                              "        │  ghz │",
                               "q_3: |0>┤1     ├",
                               "        │      │",
                               "q_4: |0>┤2     ├",