def spherical_tensor(Ji, Jj, K, Q):
    ur"""Return a matrix representation of the spherical tensor with quantum
    numbers $J_i, J_j, K, Q$.

    >>> from sympy import pprint
    >>> pprint(spherical_tensor(1, 1, 1, 0))
    ⎡-√2        ⎤
    ⎢────  0  0 ⎥
    ⎢ 2         ⎥
    ⎢           ⎥
    ⎢ 0    0  0 ⎥
    ⎢           ⎥
    ⎢         √2⎥
    ⎢ 0    0  ──⎥
    ⎣         2 ⎦

    >>> pprint(spherical_tensor(1, 2, 1, -1))
    ⎡      √10          ⎤
    ⎢0  0  ───   0    0 ⎥
    ⎢       10          ⎥
    ⎢                   ⎥
    ⎢           √30     ⎥
    ⎢0  0   0   ───   0 ⎥
    ⎢            10     ⎥
    ⎢                   ⎥
    ⎢                √15⎥
    ⎢0  0   0    0   ───⎥
    ⎣                 5 ⎦

    """
    keti = {(Ji, Mi): Matrix([KroneckerDelta(i, j)
            for j in range(2*Ji+1)])
            for i, Mi in enumerate(perm_m(Ji))}

    braj = {(Jj, Mj): Matrix([KroneckerDelta(i, j)
            for j in range(2*Jj+1)]).adjoint()
            for i, Mj in enumerate(perm_m(Jj))}

    if K not in perm_j(Ji, Jj):
        raise ValueError("K value is not allowed.")
    if Q not in perm_m(K):
        raise ValueError("Q value is not allowed.")

    Ni = 2*Ji+1
    Nj = 2*Jj+1
    T = zeros(Ni, Nj)
    for i, Mi in enumerate(perm_m(Ji)):
        for j, Mj in enumerate(perm_m(Jj)):
            T += (-1)**(Jj-Mj)*clebsch_gordan(Ji, Jj, K, Mi, -Mj, Q) * \
                keti[(Ji, Mi)]*braj[(Jj, Mj)]

    return T