def onsite(site, p):
    x, y = site.pos
    E = 0.15    # the magnitude of electrical field in y direction
    return tau_z * (p.mu - 2 * p.t) + sigma_z * p.B + tau_x * p.Delta + E* y* s_0

s_0 = numpy.identity(2). However, the program crashed to 
UserCodeError: Error occurred in user-supplied value function "onsite".

An error occurred in the onsite function that you provided. Kwant can't do anything about this, so it raises an error. If you look at the traceback it will show you what error was raised by your function, and you can fix it.

Happy Kwanting,