Repository: Qiskit/qiskit

Issue:
 pi_check cannot handle large values
<!-- ⚠️ 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**: master
- **Python version**: 3.7
- **Operating system**:

### What is the current behavior?

When we bind a large value to a custom gate, the value appears as `-pi/0` in the drawer output.

### Steps to reproduce the problem

```python
from qiskit.circuit import QuantumCircuit, Gate

def my_gate(*args):
    return Gate('my_gate', num_qubits=1, params=list(args))
    
qc = QuantumCircuit(1)
qc.append(my_gate(1e9), [0])
qc.draw()
```

output
```
     ┌────────────────┐
q_0: ┤ my_gate(-pi/0) ├
     └────────────────┘
```

### What is the expected behavior?

The value will be shown in scientific notation such as `%.2e`.
This is important to represent a custom pulse gate which has a parametrized modulation frequency.

### Suggested solutions





Discussion:
Ok I should be able to fix this

But one question, how does the %.2e represent 1e9? And may I ask why you would want to add a list of a billion values to a gate?
It seems like a `pi_check` problem:
```
from qiskit.circuit.tools import pi_check
pi_check(1e9)
```
```
'-pi/0'
```
Hi @metacubestormerxii12 , thank you very much for taking this issue. The motivation of adding such large value comes from pulse module. Currently we are trying to represent a pulse schedule by a gate and this will enables us to seamlessly integrate pulse program with existing circuit library.
As you may know, some parameters of pulse, such as frequency in GHz, can take very large value, thus we need to show this value correctly. Scientific notation is convenient to compactly show values to fit in with the gate symbol in the circuit drawer. Perhaps we can add this formatter, i.e. `%.2e`, to stylesheet so that user can customize.
OK thank you @nkanazawa1989 and @1ucian0 . I will take a look into the pi_check function.
@metacubestormerxii12 Are you working on this?

Resolving patch:
diff --git a/qiskit/circuit/tools/pi_check.py b/qiskit/circuit/tools/pi_check.py
--- a/qiskit/circuit/tools/pi_check.py
+++ b/qiskit/circuit/tools/pi_check.py
@@ -18,7 +18,8 @@
 from qiskit.circuit.parameterexpression import ParameterExpression
 from qiskit.exceptions import QiskitError
 
-N, D = np.meshgrid(np.arange(1, 17), np.arange(1, 17))
+MAX_FRAC = 16
+N, D = np.meshgrid(np.arange(1, MAX_FRAC+1), np.arange(1, MAX_FRAC+1))
 FRAC_MESH = N / D * np.pi
 
 
@@ -52,7 +53,7 @@ def pi_check(inpt, eps=1e-6, output='text', ndigits=5):
     def normalize(single_inpt):
         if abs(single_inpt) < 1e-14:
             return '0'
-        val = single_inpt / np.pi
+
         if output in ['text', 'qasm']:
             pi = 'pi'
         elif output == 'latex':
@@ -62,6 +63,9 @@ def normalize(single_inpt):
         else:
             raise QiskitError('pi_check parameter output should be text, '
                               'latex, mpl, or qasm.')
+
+        # First check is for whole multiples of pi
+        val = single_inpt / np.pi
         if abs(val) >= 1 - eps:
             if abs(abs(val) - abs(round(val))) < eps:
                 val = int(round(val))
@@ -76,6 +80,14 @@ def normalize(single_inpt):
                         str_out = '{}{}'.format(val, pi)
                 return str_out
 
+        # Second is a check for a number larger than MAX_FRAC*np.pi, since no
+        # fractions will exceed that value
+        if abs(single_inpt) >= MAX_FRAC*np.pi:
+            str_out = '%.{}g'.format(ndigits) % single_inpt
+            return str_out
+
+        # Third check is for fractions for 1*np.pi in the numer and any
+        # number in the denom.
         val = np.pi / single_inpt
         if abs(abs(val) - abs(round(val))) < eps:
             val = int(round(val))
@@ -91,7 +103,9 @@ def normalize(single_inpt):
                     str_out = '-{}/{}'.format(pi, abs(val))
             return str_out
 
-        # Look for all fracs in 8
+        # Fourth check for fractions where the numer > 1*np.pi and numer
+        # is up to MAX_FRAC*np.pi and denom is up to MAX_FRAC and all
+        # fractions are reduced. Ex. 15pi/16, 2pi/5, 15pi/2, 8pi/3, 16pi/9.
         abs_val = abs(single_inpt)
         frac = np.where(np.abs(abs_val - FRAC_MESH) < 1e-8)
         if frac[0].shape[0]:


Test patch:
diff --git a/test/python/circuit/test_tools.py b/test/python/circuit/test_tools.py
--- a/test/python/circuit/test_tools.py
+++ b/test/python/circuit/test_tools.py
@@ -31,12 +31,14 @@ class TestPiCheck(QiskitTestCase):
                    (2.999999999999999, '3'),
                    (0.99, '0.99'),
                    (0.999999999999999, '1'),
+                   (1e9, '1e+09'),
+                   (1e-9, '1e-09'),
                    (6*pi/5+1j*3*pi/7, '6pi/5+3pi/7j'),
                    (-6*pi/5+1j*3*pi/7, '-6pi/5+3pi/7j'),
                    (6*pi/5-1j*3*pi/7, '6pi/5-3pi/7j'),
                    (-6*pi/5-1j*3*pi/7, '-6pi/5-3pi/7j'),
                    (-382578.0+.0234567j, '-3.8258e+05+0.023457j'),
-                   (-382578.0-.0234567j, '-3.8258e+05-0.023457j'),
+                   (-382578.0-.0234567j, '-3.8258e+05-0.023457j')
                    ])
     def test_default(self, case):
         """Default pi_check({case[0]})='{case[1]}'"""