import math


def get_chain_stats(embedding):
    """ Embedded chains account for the number of qubits used per
    variable in the source.

    Arg:
        embedding (dict):
            Mapping from source graph to target graph as a dict of form
            {s: {t, ...}, ...}, where s is a source-model variable and t
            is a target-model variable.
    Returns:
        *stats:
            Max, min, tota, average, standard deviation

    """
    total = 0
    max_chain = 0
    N = len(embedding)
    min_chain = float('inf')

    # Get max min and total
    for chain in embedding.values():
        chain_len = len(chain)
        total += chain_len
        if chain_len > max_chain:
            max_chain = chain_len
        if chain_len < min_chain:
            min_chain =  chain_len

    # Get avg and standard deviation
    avg_chain = total/N
    sum_deviations = 0
    for chain in embedding.values():
        chain_len = len(chain)
        deviation = (chain_len - avg_chain)**2
        sum_deviations += deviation
    std_dev = math.sqrt(sum_deviations/N)

    return max_chain, min_chain, total, avg_chain, std_dev