def _is_horizontal_edge(p, q, x_dim, y_dim, periodic):
    n_sites = x_dim*y_dim
    if p < n_sites and q >= n_sites or q < n_sites and p >= n_sites:
        return False
    if p >= n_sites and q >= n_sites:
        p -= n_sites
        q -= n_sites
    return (q == _right_neighbor(p, x_dim, y_dim, periodic)
            or p == _right_neighbor(q, x_dim, y_dim, periodic))