import base64
import struct


def _decode_quadratic_biases(quadratic_string, edgelist):
    """Inverse of _serialize_quadratic_biases

    Args:
        quadratic_string (str) : base 64 encoded string of little
            endian 8 byte floats, one for each of the edges.
        edgelist (list): a list of edges of the form [(node1, node2), ...].

    Returns:
        dict: J. A dict of the form {edge1: bias1, ...} where each
            edge is of the form (node1, node2).

    Example:
        >>> _decode_quadratic_biases('AAAAAAAA8L8AAAAAAADwP5qZmZmZmdk/',
        ...                          [(0, 1), (1, 2), (0, 2)])
        {(0, 1): -1.0, (0, 2): 0.4, (1, 2): 1.0}

    """
    quadratic_bytes = base64.b64decode(quadratic_string)
    return {tuple(edge): bias for edge, bias in zip(edgelist,
            struct.unpack('<' + 'd' * (len(quadratic_bytes) // 8), quadratic_bytes))}