Return the sum of elements of the Pauli algebra

def pauli_sum(*elements: Pauli) -> Pauli:
    """Return the sum of elements of the Pauli algebra"""
    terms = []

    key = itemgetter(0)
    for term, grp in groupby(heapq.merge(*elements, key=key), key=key):
        coeff = sum(g[1] for g in grp)
        if not isclose(coeff, 0.0):
            terms.append((term, coeff))

    return Pauli(tuple(terms))