Repository: Qiskit/qiskit

Issue:
indicate global phase in circuit drawer
If a circuit has the global_phase property (introduced in #4565 ) set to something non-zero, then the circuit drawer should show that somewhere. I don't know where, somewhere that looks good (bottom or upper-right maybe).



Discussion:

Resolving patch:
diff --git a/qiskit/visualization/circuit_visualization.py b/qiskit/visualization/circuit_visualization.py
--- a/qiskit/visualization/circuit_visualization.py
+++ b/qiskit/visualization/circuit_visualization.py
@@ -441,8 +441,9 @@ def _text_circuit_drawer(circuit, filename=None, reverse_bits=False,
         layout = circuit._layout
     else:
         layout = None
+    phase = circuit.global_phase if hasattr(circuit, 'global_phase') else None
     text_drawing = _text.TextDrawing(qregs, cregs, ops, layout=layout, initial_state=initial_state,
-                                     cregbundle=cregbundle)
+                                     cregbundle=cregbundle, phase=phase)
     text_drawing.plotbarriers = plot_barriers
     text_drawing.line_length = fold
     text_drawing.vertical_compression = vertical_compression
diff --git a/qiskit/visualization/text.py b/qiskit/visualization/text.py
--- a/qiskit/visualization/text.py
+++ b/qiskit/visualization/text.py
@@ -519,7 +519,7 @@ class TextDrawing():
 
     def __init__(self, qregs, cregs, instructions, plotbarriers=True,
                  line_length=None, vertical_compression='high', layout=None, initial_state=True,
-                 cregbundle=False):
+                 cregbundle=False, phase=None):
         self.qregs = qregs
         self.cregs = cregs
         self.instructions = instructions
@@ -527,6 +527,7 @@ def __init__(self, qregs, cregs, instructions, plotbarriers=True,
         self.initial_state = initial_state
 
         self.cregbundle = cregbundle
+        self.phase = phase
         self.plotbarriers = plotbarriers
         self.line_length = line_length
         if vertical_compression not in ['high', 'medium', 'low']:
@@ -630,6 +631,10 @@ def lines(self, line_length=None):
                 rest_of_the_line -= layer_groups[-1][-1][0].length
 
         lines = []
+
+        if self.phase:
+            lines.append('global phase: %s' % pi_check(self.phase, ndigits=5))
+
         for layer_group in layer_groups:
             wires = list(zip(*layer_group))
             lines += self.draw_wires(wires)


Test patch:
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
@@ -2981,5 +2981,50 @@ def test_draw_hamiltonian_multi(self):
         self.assertEqual(circuit.draw(output='text').single_string(), expected)
 
 
+class TestTextPhase(QiskitTestCase):
+    """Testing the draweing a circuit with phase"""
+
+    def test_bell(self):
+        """Text Bell state with phase."""
+        expected = '\n'.join(["global phase: pi/2",
+                              "     ┌───┐     ",
+                              "q_0: ┤ H ├──■──",
+                              "     └───┘┌─┴─┐",
+                              "q_1: ─────┤ X ├",
+                              "          └───┘"])
+
+        qr = QuantumRegister(2, 'q')
+        circuit = QuantumCircuit(qr)
+        circuit.global_phase = 3.141592653589793 / 2
+
+        circuit.h(0)
+        circuit.cx(0, 1)
+        self.assertEqual(circuit.draw(output='text').single_string(), expected)
+
+    def test_empty(self):
+        """Text empty circuit (two registers) with phase."""
+        expected = '\n'.join(["global phase: 3",
+                              "     ",
+                              "q_0: ",
+                              "     ",
+                              "q_1: ",
+                              "     "])
+
+        qr = QuantumRegister(2, 'q')
+        circuit = QuantumCircuit(qr)
+        circuit.global_phase = 3
+
+        self.assertEqual(circuit.draw(output='text').single_string(), expected)
+
+    def test_empty_noregs(self):
+        """Text empty circuit (no registers) with phase."""
+        expected = '\n'.join(["global phase: 4.21"])
+
+        circuit = QuantumCircuit()
+        circuit.global_phase = 4.21
+
+        self.assertEqual(circuit.draw(output='text').single_string(), expected)
+
+
 if __name__ == '__main__':
     unittest.main()