# Buggy code
def pi_check(inpt, eps=1e-6, output="text", ndigits=5):
    """Computes if a number is close to an integer
    fraction or multiple of PI and returns the
    corresponding string.

    Args:
        inpt (float): Number to check.
        eps (float): EPS to check against.
        output (str): Options are 'text' (default),
                      'latex', 'mpl', and 'qasm'.
        ndigits (int): Number of digits to print
                       if returning raw inpt.

    Returns:
        str: string representation of output.

    Raises:
        QiskitError: if output is not a valid option.
    """
    if isinstance(inpt, ParameterExpression):
        try:
            return pi_check(float(inpt), eps=eps, output=output, ndigits=ndigits)
        except (ValueError, TypeError):
            return str(inpt)
    elif isinstance(inpt, str):
        return inpt

    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":
            pi = "\\pi"
        elif output == "mpl":
            pi = "$\\pi$"
        else:
            raise QiskitError(
                "pi_check parameter output should be text, latex, mpl, or qasm."
            )
        if abs(val) >= 1 - eps:
            if abs(abs(val) - abs(round(val))) < eps:
                val = int(round(val))
                if val == 1:
                    str_out = "{}".format(pi)
                elif val == -1:
                    str_out = "-{}".format(pi)
                else:
                    str_out = "{}{}".format(val, pi)
                return str_out

        val = np.pi / single_inpt
        if abs(abs(val) - abs(round(val))) < eps:
            val = int(round(val))
            if val > 0:
                if output == "latex":
                    str_out = "\\frac{%s}{%s}" % (pi, abs(val))
                else:
                    str_out = "{}/{}".format(pi, val)
            else:
                if output == "latex":
                    str_out = "\\frac{-%s}{%s}" % (pi, abs(val))
                else:
                    str_out = "-{}/{}".format(pi, abs(val))
            return str_out

        # Look for all fracs in 8
        abs_val = abs(single_inpt)
        frac = np.where(np.abs(abs_val - FRAC_MESH) < 1e-8)
        if frac[0].shape[0]:
            numer = int(frac[1][0]) + 1
            denom = int(frac[0][0]) + 1
            if single_inpt < 0:
                numer *= -1

            if numer == 1 and denom == 1:
                str_out = "{}".format(pi)
            elif numer == -1 and denom == 1:
                str_out = "-{}".format(pi)
            elif numer == 1:
                if output == "latex":
                    str_out = "\\frac{%s}{%s}" % (pi, denom)
                else:
                    str_out = "{}/{}".format(pi, denom)
            elif numer == -1:
                if output == "latex":
                    str_out = "\\frac{-%s}{%s}" % (pi, denom)
                else:
                    str_out = "-{}/{}".format(pi, denom)
            elif denom == 1:
                if output == "latex":
                    str_out = "\\frac{%s}{%s}" % (numer, pi)
                else:
                    str_out = "{}/{}".format(numer, pi)
            else:
                if output == "latex":
                    str_out = "\\frac{%s%s}{%s}" % (numer, pi, denom)
                elif output == "qasm":
                    str_out = "{}*{}/{}".format(numer, pi, denom)
                else:
                    str_out = "{}{}/{}".format(numer, pi, denom)

            return str_out
        # nothing found
        str_out = "%.{}g".format(ndigits) % single_inpt
        return str_out

    complex_inpt = complex(inpt)
    real, imag = map(normalize, [complex_inpt.real, complex_inpt.imag])

    if real == "0" and imag != "0":
        return imag + "j"
    elif real != 0 and imag != "0":
        return "{}+{}j".format(real, imag)
    return real


# Traceback (qiskit.qasm.exceptions.QasmError)
import qiskit as qk
from math import pi
qc = qk.QuantumCircuit(1)
qc.u1(5*pi, 0)
print(qc.qasm())
OPENQASM 2.0;
include "qelib1.inc";
qreg q[1];
u1(5pi) q[0];

qk.QuantumCircuit.from_qasm_str(qc.qasm())
Error near line 4 Column 10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/circuit/quantumcircuit.py", line 1614, in from_qasm_str
return _circuit_from_qasm(qasm)
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/circuit/quantumcircuit.py", line 2158, in _circuit_from_qasm
ast = qasm.parse()
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/qasm/qasm.py", line 56, in parse
return qasm_p.parse(self._data)
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/qasm/qasmparser.py", line 1072, in parse
self.parser.parse(data, lexer=self.lexer, debug=self.parse_deb)
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/lib/python3.5/site-packages/ply/yacc.py", line 333, in parse
return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/lib/python3.5/site-packages/ply/yacc.py", line 1120, in parseopt_notrack
p.callable(pslice)
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/qasm/qasmparser.py", line 341, in p_id_e
+ str(program[1].value) + "'")
qiskit.qasm.exceptions.QasmError: "Expected an ID, received 'pi'"
from qiskit.circuit.library import *
U1Gate(5*pi).qasm()
'u1(5pi)'

# Fixed code
def pi_check(inpt, eps=1e-6, output="text", ndigits=5):
    """Computes if a number is close to an integer
    fraction or multiple of PI and returns the
    corresponding string.

    Args:
        inpt (float): Number to check.
        eps (float): EPS to check against.
        output (str): Options are 'text' (default),
                      'latex', 'mpl', and 'qasm'.
        ndigits (int): Number of digits to print
                       if returning raw inpt.

    Returns:
        str: string representation of output.

    Raises:
        QiskitError: if output is not a valid option.
    """
    if isinstance(inpt, ParameterExpression):
        try:
            return pi_check(float(inpt), eps=eps, output=output, ndigits=ndigits)
        except (ValueError, TypeError):
            return str(inpt)
    elif isinstance(inpt, str):
        return inpt

    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":
            pi = "\\pi"
        elif output == "mpl":
            pi = "$\\pi$"
        else:
            raise QiskitError(
                "pi_check parameter output should be text, latex, mpl, or qasm."
            )
        if abs(val) >= 1 - eps:
            if abs(abs(val) - abs(round(val))) < eps:
                val = int(round(val))
                if val == 1:
                    str_out = "{}".format(pi)
                elif val == -1:
                    str_out = "-{}".format(pi)
                else:
                    if output == "qasm":
                        str_out = "{}*{}".format(val, pi)
                    else:
                        str_out = "{}{}".format(val, pi)
                return str_out

        val = np.pi / single_inpt
        if abs(abs(val) - abs(round(val))) < eps:
            val = int(round(val))
            if val > 0:
                if output == "latex":
                    str_out = "\\frac{%s}{%s}" % (pi, abs(val))
                else:
                    str_out = "{}/{}".format(pi, val)
            else:
                if output == "latex":
                    str_out = "\\frac{-%s}{%s}" % (pi, abs(val))
                else:
                    str_out = "-{}/{}".format(pi, abs(val))
            return str_out

        # Look for all fracs in 8
        abs_val = abs(single_inpt)
        frac = np.where(np.abs(abs_val - FRAC_MESH) < 1e-8)
        if frac[0].shape[0]:
            numer = int(frac[1][0]) + 1
            denom = int(frac[0][0]) + 1
            if single_inpt < 0:
                numer *= -1

            if numer == 1 and denom == 1:
                str_out = "{}".format(pi)
            elif numer == -1 and denom == 1:
                str_out = "-{}".format(pi)
            elif numer == 1:
                if output == "latex":
                    str_out = "\\frac{%s}{%s}" % (pi, denom)
                else:
                    str_out = "{}/{}".format(pi, denom)
            elif numer == -1:
                if output == "latex":
                    str_out = "\\frac{-%s}{%s}" % (pi, denom)
                else:
                    str_out = "-{}/{}".format(pi, denom)
            elif denom == 1:
                if output == "latex":
                    str_out = "\\frac{%s}{%s}" % (numer, pi)
                else:
                    str_out = "{}/{}".format(numer, pi)
            else:
                if output == "latex":
                    str_out = "\\frac{%s%s}{%s}" % (numer, pi, denom)
                elif output == "qasm":
                    str_out = "{}*{}/{}".format(numer, pi, denom)
                else:
                    str_out = "{}{}/{}".format(numer, pi, denom)

            return str_out
        # nothing found
        str_out = "%.{}g".format(ndigits) % single_inpt
        return str_out

    complex_inpt = complex(inpt)
    real, imag = map(normalize, [complex_inpt.real, complex_inpt.imag])

    if real == "0" and imag != "0":
        return imag + "j"
    elif real != 0 and imag != "0":
        return "{}+{}j".format(real, imag)
    return real