import torch


def orthogonal_random_matrix_(rows, columns, device):
    """Generate a random matrix whose columns are orthogonal to each
    other (in groups of size `rows`) and their norms is drawn from the
    chi-square distribution with `rows` degrees of freedom
    (namely the norm of a `rows`-dimensional vector distributed as N(0, I)).
    """
    w = torch.zeros([rows, columns], device=device)
    start = 0
    while start < columns:
        end = min(start+rows, columns)
        block = torch.randn(rows, rows, device=device)
        norms = torch.sqrt(torch.einsum("ab,ab->a", block, block))
        Q, _ = torch.qr(block)  # Q is orthonormal
        
        w[:, start:end] = (
            Q[:, :end-start] * norms[None, :end-start]
        )
        start += rows
    return w