code: def test_cp_to_tensor_with_weights():
      A = tl.reshape(tl.arange(1, 5, dtype=float), (2, 2))
      B = tl.reshape(tl.arange(5, 9, dtype=float), (2, 2))
      weigths = tl.tensor([2, -1], **tl.context(A))
  
      out = cp_to_tensor((weigths, [A, B]))
      expected = tl.tensor([[-2, -2], [6, 10]])  # computed by hand
      assert_array_equal(out, expected)
  
      (weigths, factors) = random_cp(
          (5, 5, 5), rank=5, normalise_factors=True, full=False
      )
      true_res = tl.dot(
          tl.dot(factors[0], tl.diag(weigths)),
          tl.transpose(tl.tenalg.khatri_rao(factors[1:])),
      )
      true_res = tl.fold(true_res, 0, (5, 5, 5))
      res = cp_to_tensor((weigths, factors))
      assert_array_almost_equal(
          true_res,
          res,
          err_msg="weights incorrectly incorporated in cp_to_tensor",
      )
  
imports: import numpy
  import tensorly
  from ..tenalg import khatri_rao, mode_dot
  from ..cp_tensor import cp_to_tensor, cp_to_unfolded, cp_to_vec, _validate_cp_tensor, cp_normalize, CPTensor, cp_mode_dot, cp_norm, cp_flip_sign, _cp_n_param, validate_cp_rank, cp_lstsq_grad, cp_permute_factors
  from ..base import unfold, tensor_to_vec
  from tensorly.random import random_cp
  from tensorly.testing import assert_equal, assert_raises, assert_, assert_array_equal, assert_array_almost_equal
  import pytest
label_desc: Use image node_id 5 for calling a global function with example usage: test_cp_to_tensor_with_weights() without return types