import torch


def metropolis_accept(
        current_energies,
        proposed_energies,
        proposal_delta_log_prob
):
    """Metropolis criterion.

    Parameters
    ----------
    current_energies : torch.Tensor
        Dimensionless energy of the current state x.
    proposed_energies : torch.Tensor
        Dimensionless energy of the proposed state x'.
    proposal_delta_log_prob : Union[torch.Tensor, float]
        The difference in log probabilities between the forward and backward proposal.
        This corresponds to    log g(x'|x) - log g(x|x'), where g is the proposal distribution.

    Returns
    -------
    accept : torch.Tensor
        A boolean tensor that is True for accepted proposals and False otherwise
    """
    # log p(x') - log p(x) - (log g(x'|x) - log g(x|x'))
    log_prob = -(proposed_energies - current_energies) - proposal_delta_log_prob
    log_acceptance_ratio = torch.min(
        torch.zeros_like(proposed_energies),
        log_prob,
    )
    log_random = torch.rand_like(log_acceptance_ratio).log()
    accept = log_acceptance_ratio >= log_random
    return accept