nodes: 0, module; 1, function_definition; 2, function_name:to_numpy_vectors; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, expression_statement; 17, expression_statement; 18, if_statement; 19, if_statement; 20, return_statement; 21, identifier:variable_order; 22, None; 23, identifier:dtype; 24, attribute; 25, identifier:index_dtype; 26, attribute; 27, identifier:sort_indices; 28, False; 29, comment:"""Convert a binary quadratic model to numpy arrays.
  
          Args:
              variable_order (iterable, optional):
                  If provided, labels the variables; otherwise, row/column indices are used.
  
              dtype (:class:`numpy.dtype`, optional):
                  Data-type of the biases. By default, the data-type is inferred from the biases.
  
              index_dtype (:class:`numpy.dtype`, optional):
                  Data-type of the indices. By default, the data-type is inferred from the labels.
  
              sort_indices (bool, optional, default=False):
                  If True, the indices are sorted, first by row then by column. Otherwise they
                  match :attr:`~.BinaryQuadraticModel.quadratic`.
  
          Returns:
              :obj:`~numpy.ndarray`: A numpy array of the linear biases.
  
              tuple: The quadratic biases in COOrdinate format.
  
                  :obj:`~numpy.ndarray`: A numpy array of the row indices of the quadratic matrix
                  entries
  
                  :obj:`~numpy.ndarray`: A numpy array of the column indices of the quadratic matrix
                  entries
  
                  :obj:`~numpy.ndarray`: A numpy array of the values of the quadratic matrix
                  entries
  
              The offset
  
          Examples:
              >>> bqm = dimod.BinaryQuadraticModel({}, {(0, 1): .5, (3, 2): -1, (0, 3): 1.5}, 0.0, dimod.SPIN)
              >>> lin, (i, j, vals), off = bqm.to_numpy_vectors(sort_indices=True)
              >>> lin
              array([0., 0., 0., 0.])
              >>> i
              array([0, 0, 2])
              >>> j
              array([1, 3, 3])
              >>> vals
              array([ 0.5,  1.5, -1. ])
  
          """; 30, assignment; 31, assignment; 32, assignment; 33, assignment; 34, assignment; 35, assignment; 36, assignment; 37, comparison_operator:variable_order is None; 38, block; 39, else_clause; 40, identifier:sort_indices; 41, comment:# row index should be less than col index, this handles upper-triangular vs lower-triangular; 42, block; 43, expression_list; 44, identifier:np; 45, identifier:float; 46, identifier:np; 47, identifier:int64; 48, identifier:linear; 49, attribute; 50, identifier:quadratic; 51, attribute; 52, identifier:num_variables; 53, call; 54, identifier:num_interactions; 55, call; 56, identifier:irow; 57, call; 58, identifier:icol; 59, call; 60, identifier:qdata; 61, call; 62, identifier:variable_order; 63, None; 64, try_statement; 65, comment:# we could speed this up a lot with cython; 66, for_statement; 67, block; 68, expression_statement; 69, if_statement; 70, comment:# sort lexigraphically; 71, expression_statement; 72, if_statement; 73, identifier:ldata; 74, tuple; 75, call; 76, identifier:self; 77, identifier:linear; 78, identifier:self; 79, identifier:quadratic; 80, identifier:len; 81, argument_list; 82, identifier:len; 83, argument_list; 84, attribute; 85, argument_list; 86, attribute; 87, argument_list; 88, attribute; 89, argument_list; 90, block; 91, except_clause; 92, pattern_list; 93, call; 94, block; 95, try_statement; 96, expression_statement; 97, comment:# we could speed this up a lot with cython; 98, for_statement; 99, assignment; 100, call; 101, comment:# in-place; 102, block; 103, assignment; 104, not_operator; 105, comment:# copy; 106, block; 107, identifier:irow; 108, identifier:icol; 109, identifier:qdata; 110, attribute; 111, argument_list; 112, identifier:linear; 113, identifier:quadratic; 114, identifier:np; 115, identifier:empty; 116, identifier:num_interactions; 117, keyword_argument; 118, identifier:np; 119, identifier:empty; 120, identifier:num_interactions; 121, keyword_argument; 122, identifier:np; 123, identifier:empty; 124, identifier:num_interactions; 125, keyword_argument; 126, expression_statement; 127, identifier:KeyError; 128, block; 129, identifier:idx; 130, tuple_pattern; 131, identifier:enumerate; 132, argument_list; 133, expression_statement; 134, expression_statement; 135, expression_statement; 136, block; 137, except_clause; 138, assignment; 139, pattern_list; 140, call; 141, block; 142, identifier:swaps; 143, comparison_operator:irow > icol; 144, attribute; 145, argument_list; 146, expression_statement; 147, identifier:order; 148, call; 149, call; 150, expression_statement; 151, expression_statement; 152, expression_statement; 153, attribute; 154, identifier:type; 155, attribute; 156, identifier:dtype; 157, identifier:index_dtype; 158, identifier:dtype; 159, identifier:index_dtype; 160, identifier:dtype; 161, identifier:dtype; 162, assignment; 163, raise_statement; 164, tuple_pattern; 165, identifier:bias; 166, call; 167, assignment; 168, assignment; 169, assignment; 170, expression_statement; 171, identifier:KeyError; 172, block; 173, identifier:label_to_idx; 174, dictionary_comprehension; 175, identifier:idx; 176, tuple_pattern; 177, identifier:enumerate; 178, argument_list; 179, expression_statement; 180, expression_statement; 181, expression_statement; 182, identifier:irow; 183, identifier:icol; 184, identifier:swaps; 185, identifier:any; 186, assignment; 187, attribute; 188, argument_list; 189, attribute; 190, argument_list; 191, assignment; 192, assignment; 193, assignment; 194, identifier:ldata; 195, identifier:dtype; 196, identifier:self; 197, identifier:offset; 198, identifier:ldata; 199, call; 200, call; 201, identifier:u; 202, identifier:v; 203, attribute; 204, argument_list; 205, subscript; 206, identifier:u; 207, subscript; 208, identifier:v; 209, subscript; 210, identifier:bias; 211, assignment; 212, raise_statement; 213, pair; 214, for_in_clause; 215, tuple_pattern; 216, identifier:bias; 217, call; 218, assignment; 219, assignment; 220, assignment; 221, pattern_list; 222, expression_list; 223, identifier:np; 224, identifier:lexsort; 225, tuple; 226, parenthesized_expression; 227, identifier:all; 228, identifier:irow; 229, subscript; 230, identifier:icol; 231, subscript; 232, identifier:qdata; 233, subscript; 234, attribute; 235, argument_list; 236, identifier:ValueError; 237, argument_list; 238, identifier:quadratic; 239, identifier:items; 240, identifier:irow; 241, identifier:idx; 242, identifier:icol; 243, identifier:idx; 244, identifier:qdata; 245, identifier:idx; 246, identifier:ldata; 247, call; 248, call; 249, identifier:v; 250, identifier:idx; 251, pattern_list; 252, call; 253, identifier:u; 254, identifier:v; 255, attribute; 256, argument_list; 257, subscript; 258, subscript; 259, subscript; 260, subscript; 261, subscript; 262, identifier:bias; 263, subscript; 264, subscript; 265, subscript; 266, subscript; 267, identifier:irow; 268, identifier:icol; 269, comparison_operator:order == range(len(order)); 270, identifier:irow; 271, identifier:order; 272, identifier:icol; 273, identifier:order; 274, identifier:qdata; 275, identifier:order; 276, identifier:np; 277, identifier:fromiter; 278, generator_expression; 279, keyword_argument; 280, keyword_argument; 281, parenthesized_expression; 282, attribute; 283, argument_list; 284, identifier:ValueError; 285, argument_list; 286, identifier:idx; 287, identifier:v; 288, identifier:enumerate; 289, argument_list; 290, identifier:quadratic; 291, identifier:items; 292, identifier:irow; 293, identifier:idx; 294, identifier:label_to_idx; 295, identifier:u; 296, identifier:icol; 297, identifier:idx; 298, identifier:label_to_idx; 299, identifier:v; 300, identifier:qdata; 301, identifier:idx; 302, identifier:irow; 303, identifier:swaps; 304, identifier:icol; 305, identifier:swaps; 306, identifier:icol; 307, identifier:swaps; 308, identifier:irow; 309, identifier:swaps; 310, identifier:order; 311, call; 312, subscript; 313, for_in_clause; 314, identifier:count; 315, identifier:num_variables; 316, identifier:dtype; 317, identifier:dtype; 318, concatenated_string; 319, identifier:np; 320, identifier:fromiter; 321, generator_expression; 322, keyword_argument; 323, keyword_argument; 324, string:"provided 'variable_order' does not match binary quadratic model"; 325, identifier:variable_order; 326, identifier:range; 327, argument_list; 328, identifier:linear; 329, identifier:v; 330, identifier:v; 331, call; 332, string:"if 'variable_order' is not provided, binary quadratic model must be "; 333, string:"index labeled [0, ..., N-1]"; 334, subscript; 335, for_in_clause; 336, identifier:count; 337, identifier:num_variables; 338, identifier:dtype; 339, identifier:dtype; 340, call; 341, identifier:range; 342, argument_list; 343, identifier:linear; 344, identifier:v; 345, identifier:v; 346, identifier:variable_order; 347, identifier:len; 348, argument_list; 349, identifier:num_variables; 350, identifier:order
code: def to_numpy_vectors(self, variable_order=None, dtype=np.float, index_dtype=np.int64, sort_indices=False):
          """Convert a binary quadratic model to numpy arrays.
  
          Args:
              variable_order (iterable, optional):
                  If provided, labels the variables; otherwise, row/column indices are used.
  
              dtype (:class:`numpy.dtype`, optional):
                  Data-type of the biases. By default, the data-type is inferred from the biases.
  
              index_dtype (:class:`numpy.dtype`, optional):
                  Data-type of the indices. By default, the data-type is inferred from the labels.
  
              sort_indices (bool, optional, default=False):
                  If True, the indices are sorted, first by row then by column. Otherwise they
                  match :attr:`~.BinaryQuadraticModel.quadratic`.
  
          Returns:
              :obj:`~numpy.ndarray`: A numpy array of the linear biases.
  
              tuple: The quadratic biases in COOrdinate format.
  
                  :obj:`~numpy.ndarray`: A numpy array of the row indices of the quadratic matrix
                  entries
  
                  :obj:`~numpy.ndarray`: A numpy array of the column indices of the quadratic matrix
                  entries
  
                  :obj:`~numpy.ndarray`: A numpy array of the values of the quadratic matrix
                  entries
  
              The offset
  
          Examples:
              >>> bqm = dimod.BinaryQuadraticModel({}, {(0, 1): .5, (3, 2): -1, (0, 3): 1.5}, 0.0, dimod.SPIN)
              >>> lin, (i, j, vals), off = bqm.to_numpy_vectors(sort_indices=True)
              >>> lin
              array([0., 0., 0., 0.])
              >>> i
              array([0, 0, 2])
              >>> j
              array([1, 3, 3])
              >>> vals
              array([ 0.5,  1.5, -1. ])
  
          """
          linear = self.linear
          quadratic = self.quadratic
  
          num_variables = len(linear)
          num_interactions = len(quadratic)
  
          irow = np.empty(num_interactions, dtype=index_dtype)
          icol = np.empty(num_interactions, dtype=index_dtype)
          qdata = np.empty(num_interactions, dtype=dtype)
  
          if variable_order is None:
              try:
                  ldata = np.fromiter((linear[v] for v in range(num_variables)), count=num_variables, dtype=dtype)
              except KeyError:
                  raise ValueError(("if 'variable_order' is not provided, binary quadratic model must be "
                                    "index labeled [0, ..., N-1]"))
  
              # we could speed this up a lot with cython
              for idx, ((u, v), bias) in enumerate(quadratic.items()):
                  irow[idx] = u
                  icol[idx] = v
                  qdata[idx] = bias
  
          else:
              try:
                  ldata = np.fromiter((linear[v] for v in variable_order), count=num_variables, dtype=dtype)
              except KeyError:
                  raise ValueError("provided 'variable_order' does not match binary quadratic model")
  
              label_to_idx = {v: idx for idx, v in enumerate(variable_order)}
  
              # we could speed this up a lot with cython
              for idx, ((u, v), bias) in enumerate(quadratic.items()):
                  irow[idx] = label_to_idx[u]
                  icol[idx] = label_to_idx[v]
                  qdata[idx] = bias
  
          if sort_indices:
              # row index should be less than col index, this handles upper-triangular vs lower-triangular
              swaps = irow > icol
              if swaps.any():
                  # in-place
                  irow[swaps], icol[swaps] = icol[swaps], irow[swaps]
  
              # sort lexigraphically
              order = np.lexsort((irow, icol))
              if not (order == range(len(order))).all():
                  # copy
                  irow = irow[order]
                  icol = icol[order]
                  qdata = qdata[order]
  
          return ldata, (irow, icol, qdata), ldata.dtype.type(self.offset)