<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def to_numpy_matrix(self, variable_order=None):
 """Convert a binary quadratic model to NumPy 2D array. Args: variable_order (list, optional):
 If provided, indexes the rows/columns of the NumPy array. If `variable_order` includes any variables not in the binary quadratic model, these are added to the NumPy array. Returns: :class:`numpy.ndarray`: The binary quadratic model as a NumPy 2D array. Note that the binary quadratic model is converted to :class:`~.Vartype.BINARY` vartype. Notes: The matrix representation of a binary quadratic model only makes sense for binary models. For a binary sample x, the energy of the model is given by: .. math:: E(x) = x^T Q x The offset is dropped when converting to a NumPy array. Examples: This example converts a binary quadratic model to NumPy array format while ordering variables and adding one ('d'). array([[ 0. , 0. , 0. , 0. ], [ 0. , 0.5, 1.5, 0. ], [ 0. , 0. , -1. , 0.5], [ 0. , 0. , 0. , 1. ]]) """

        import numpy as np

        if variable_order is None:
            # just use the existing variable labels, assuming that they are [0, N)
            num_variables = len(self)
            mat = np.zeros((num_variables, num_variables), dtype=float)

            try:
                for v, bias in iteritems(self.binary.linear):
                    mat[v, v] = bias
            except IndexError:
                raise ValueError(("if 'variable_order' is not provided, binary quadratic model must be "
                                  "index labeled [0, ..., N-1]"))

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

        else:
            num_variables = len(variable_order)
            idx = {v: i for i, v in enumerate(variable_order)}

            mat = np.zeros((num_variables, num_variables), dtype=float)

            try:
                for v, bias in iteritems(self.binary.linear):
                    mat[idx[v], idx[v]] = bias
            except KeyError as e:
                raise ValueError(("variable {} is missing from variable_order".format(e)))

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

        return mat