<commit_before>from dimod import _PY2
from dimod.vartypes import Vartype

if _PY2:
    def iteritems(d):
        return d.iteritems()

    def itervalues(d):
        return d.itervalues()

else:
    def iteritems(d):
        return d.items()

    def itervalues(d):
        return d.values()


def to_networkx_graph(model, node_attribute_name='bias', edge_attribute_name='bias'):
    """Return the BinaryQuadraticModel as a NetworkX graph.

    Args:
        node_attribute_name (hashable): The attribute name for the
            linear biases.
        edge_attribute_name (hashable): The attribute name for the
            quadratic biases.

    Returns:
        :class:`networkx.Graph`: A NetworkX with the biases stored as
        node/edge attributes.

    Examples:
        >>> import networkx as nx
        >>> model = pm.BinaryQuadraticModel({0: 1, 1: -1, 2: .5},
        ...                                 {(0, 1): .5, (1, 2): 1.5},
        ...                                 1.4,
        ...                                 pm.SPIN)
        >>> BQM = model.to_networkx_graph()
        >>> BQM[0][1]['bias']
        0.5


    """
    import networkx as nx

    BQM = nx.Graph()

    # add the linear biases
    BQM.add_nodes_from(((v, {node_attribute_name: bias, 'vartype': model.vartype})
                        for v, bias in iteritems(model.linear)))

    # add the quadratic biases
    BQM.add_edges_from(((u, v, {edge_attribute_name: bias}) for (u, v), bias in iteritems(model.quadratic)))

    # set the offset
    BQM.offset = model.offset

    return BQM


def to_ising(model):
    """Converts the model into the (h, J, offset) Ising format.

    If the model type is not spin, it is converted.

    Returns:
        tuple: A 3-tuple:

            dict: The linear biases.

            dict: The quadratic biases.

            number: The offset.

    """
    if model.vartype == model.SPIN:
        # can just return the model as-is.
        return model.linear, model.quadratic, model.offset

    if model.vartype != model.BINARY:
        raise RuntimeError('converting from unknown vartype')

    return model.binary_to_spin(model.linear, model.quadratic, model.offset)


def to_qubo(model):
    """Converts the model into the (Q, offset) QUBO format.

    If the model type is not binary, it is converted.

    Returns:
        tuple: A 2-tuple:

            dict: The qubo biases.

            number: The offset.

    """
    if model.vartype == model.BINARY:
        # need to dump the linear biases into quadratic
        qubo = {}
        for v, bias in iteritems(model.linear):
            qubo[(v, v)] = bias
        for edge, bias in iteritems(model.quadratic):
            qubo[edge] = bias
        return qubo, model.offset

    if model.vartype != model.SPIN:
        raise RuntimeError('converting from unknown vartype')

    linear, quadratic, offset = model.spin_to_binary(model.linear, model.quadratic, model.offset)

    quadratic.update({(v, v): bias for v, bias in iteritems(linear)})

    return quadratic, offset<commit_msg>Add to_numpy_array function<commit_after>from dimod import _PY2
from dimod.vartypes import Vartype

if _PY2:
    def iteritems(d):
        return d.iteritems()

    def itervalues(d):
        return d.itervalues()

else:
    def iteritems(d):
        return d.items()

    def itervalues(d):
        return d.values()


def to_networkx_graph(model, node_attribute_name='bias', edge_attribute_name='bias'):
    """Return the BinaryQuadraticModel as a NetworkX graph.

    Args:
        node_attribute_name (hashable): The attribute name for the
            linear biases.
        edge_attribute_name (hashable): The attribute name for the
            quadratic biases.

    Returns:
        :class:`networkx.Graph`: A NetworkX with the biases stored as
        node/edge attributes.

    Examples:
        >>> import networkx as nx
        >>> model = pm.BinaryQuadraticModel({0: 1, 1: -1, 2: .5},
        ...                                 {(0, 1): .5, (1, 2): 1.5},
        ...                                 1.4,
        ...                                 pm.SPIN)
        >>> BQM = model.to_networkx_graph()
        >>> BQM[0][1]['bias']
        0.5


    """
    import networkx as nx

    BQM = nx.Graph()

    # add the linear biases
    BQM.add_nodes_from(((v, {node_attribute_name: bias, 'vartype': model.vartype})
                        for v, bias in iteritems(model.linear)))

    # add the quadratic biases
    BQM.add_edges_from(((u, v, {edge_attribute_name: bias}) for (u, v), bias in iteritems(model.quadratic)))

    # set the offset
    BQM.offset = model.offset

    return BQM


def to_ising(model):
    """Converts the model into the (h, J, offset) Ising format.

    If the model type is not spin, it is converted.

    Returns:
        tuple: A 3-tuple:

            dict: The linear biases.

            dict: The quadratic biases.

            number: The offset.

    """
    if model.vartype == model.SPIN:
        # can just return the model as-is.
        return model.linear, model.quadratic, model.offset

    if model.vartype != model.BINARY:
        raise RuntimeError('converting from unknown vartype')

    return model.binary_to_spin(model.linear, model.quadratic, model.offset)


def to_qubo(model):
    """Converts the model into the (Q, offset) QUBO format.

    If the model type is not binary, it is converted.

    Returns:
        tuple: A 2-tuple:

            dict: The qubo biases.

            number: The offset.

    """
    if model.vartype == model.BINARY:
        # need to dump the linear biases into quadratic
        qubo = {}
        for v, bias in iteritems(model.linear):
            qubo[(v, v)] = bias
        for edge, bias in iteritems(model.quadratic):
            qubo[edge] = bias
        return qubo, model.offset

    if model.vartype != model.SPIN:
        raise RuntimeError('converting from unknown vartype')

    linear, quadratic, offset = model.spin_to_binary(model.linear, model.quadratic, model.offset)

    quadratic.update({(v, v): bias for v, bias in iteritems(linear)})

    return quadratic, offset


def to_numpy_array(model):
    """todo"""
    import numpy as np

    mat = np.zeros((len(model), len(model)), dtype=float)

    for v, bias in iteritems(model.binary.linear):
        mat[v, v] = bias

    for (u, v), bias in iteritems(model.binary.quadratic):
        if u < v:
            mat[u, v] = bias
        else:
            mat[v, u] = bias

    return mat
{'fraction_non_alphanumeric': 0.10710449593371182, 'fraction_numerical': 0.007058462482737456, 'mean_word_length': 2.06585136406397, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3855319', 'n_tokens_mistral': 2088, 'n_tokens_neox': 1962, 'n_words': 602}