# Buggy code
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


# 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 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