(*operands_: Any, **kwargs: Any)
return _core_contract(operands, contraction_list, backend=backend, **einsum_kwargs)
Evaluates the Einstein summation convention on the operands. A drop in
    replacement for NumPy's einsum function that optimizes the order of contraction
    to reduce overall scaling at the cost of several intermediate arrays.

    **Parameters:**

    - **subscripts** - *(str)* Specifies the subscripts for summation.
    - **\\*operands** - *(list of array_like)* hese are the arrays for the operation.
    - **out** - *(array_like)* A output array in which set the sresulting output.
    - **dtype** - *(str)* The dtype of the given contraction, see np.einsum.
    - **order** - *(str)* The order of the resulting contraction, see np.einsum.
    - **casting** - *(str)* The casting procedure for operations of different dtype, see np.einsum.
    - **use_blas** - *(bool)* Do you use BLAS for valid operations, may use extra memory for more intermediates.
    - **optimize** - *(str, list or bool, optional (default: ``auto``))* Choose the type of path.

        - if a list is given uses this as the path.
        - `'optimal'` An algorithm that explores all possible ways of
          contracting the listed tensors. Scales factorially with the number of
          terms in the contraction.
        - `'dp'` A faster (but essentially optimal) algorithm that uses
          dynamic programming to exhaustively search all contraction paths
          without outer-products.
        - `'greedy'` An cheap algorithm that heuristically chooses the best
          pairwise contraction at each step. Scales linearly in the number of
          terms in the contraction.
        - `'random-greedy'` Run a randomized version of the greedy algorithm
          32 times and pick the best path.
        - `'random-greedy-128'` Run a randomized version of the greedy
          algorithm 128 times and pick the best path.
        - `'branch-all'` An algorithm like optimal but that restricts itself
          to searching 'likely' paths. Still scales factorially.
        - `'branch-2'` An even more restricted version of 'branch-all' that
          only searches the best two options at each step. Scales exponentially
          with the number of terms in the contraction.
        - `'auto'` Choose the best of the above algorithms whilst aiming to
          keep the path finding time below 1ms.
        - `'auto-hq'` Aim for a high quality contraction, choosing the best
          of the above algorithms whilst aiming to keep the path finding time
          below 1sec.

    - **memory_limit** - *({None, int, 'max_input'} (default: `None`))* - Give the upper bound of the largest intermediate tensor contract will build.
        - None or -1 means there is no limit
        - `max_input` means the limit is set as largest input tensor
        - a positive integer is taken as an explicit limit on the number of elements

        The default is None. Note that imposing a limit can make contractions
        exponentially slower to perform.

    - **backend** - *(str, optional (default: ``auto``))* Which library to use to perform the required ``tensordot``, ``transpose``
        and ``einsum`` calls. Should match the types of arrays supplied, See
        :func:`contract_expression` for generating expressions which convert
        numpy arrays to and from the backend library automatically.

    **Returns:**

    - **out** - *(array_like)* The result of the einsum expression.

    **Notes:**

    This function should produce a result identical to that of NumPy's einsum
    function. The primary difference is ``contract`` will attempt to form
    intermediates which reduce the overall scaling of the given einsum contraction.
    By default the worst intermediate formed will be equal to that of the largest
    input array. For large einsum expressions with many input arrays this can
    provide arbitrarily large (1000 fold+) speed improvements.

    For contractions with just two tensors this function will attempt to use
    NumPy's built-in BLAS functionality to ensure that the given operation is
    performed optimally. When NumPy is linked to a threaded BLAS, potential
    speedups are on the order of 20-100 for a six core machine.
Evaluates the Einstein summation convention on the operands. A drop in
    replacement for NumPy's einsum function that optimizes the order of contraction
    to reduce overall scaling at the cost of several intermediate arrays.
Evaluates the Einstein summation convention on the operands . A drop in replacement for NumPy s einsum function that optimizes the order of contraction to reduce overall scaling at the cost of several intermediate arrays .
def contract(*operands_: Any, **kwargs: Any) -> ArrayType:
    """
    Evaluates the Einstein summation convention on the operands. A drop in
    replacement for NumPy's einsum function that optimizes the order of contraction
    to reduce overall scaling at the cost of several intermediate arrays.

    **Parameters:**

    - **subscripts** - *(str)* Specifies the subscripts for summation.
    - **\\*operands** - *(list of array_like)* hese are the arrays for the operation.
    - **out** - *(array_like)* A output array in which set the sresulting output.
    - **dtype** - *(str)* The dtype of the given contraction, see np.einsum.
    - **order** - *(str)* The order of the resulting contraction, see np.einsum.
    - **casting** - *(str)* The casting procedure for operations of different dtype, see np.einsum.
    - **use_blas** - *(bool)* Do you use BLAS for valid operations, may use extra memory for more intermediates.
    - **optimize** - *(str, list or bool, optional (default: ``auto``))* Choose the type of path.

        - if a list is given uses this as the path.
        - `'optimal'` An algorithm that explores all possible ways of
          contracting the listed tensors. Scales factorially with the number of
          terms in the contraction.
        - `'dp'` A faster (but essentially optimal) algorithm that uses
          dynamic programming to exhaustively search all contraction paths
          without outer-products.
        - `'greedy'` An cheap algorithm that heuristically chooses the best
          pairwise contraction at each step. Scales linearly in the number of
          terms in the contraction.
        - `'random-greedy'` Run a randomized version of the greedy algorithm
          32 times and pick the best path.
        - `'random-greedy-128'` Run a randomized version of the greedy
          algorithm 128 times and pick the best path.
        - `'branch-all'` An algorithm like optimal but that restricts itself
          to searching 'likely' paths. Still scales factorially.
        - `'branch-2'` An even more restricted version of 'branch-all' that
          only searches the best two options at each step. Scales exponentially
          with the number of terms in the contraction.
        - `'auto'` Choose the best of the above algorithms whilst aiming to
          keep the path finding time below 1ms.
        - `'auto-hq'` Aim for a high quality contraction, choosing the best
          of the above algorithms whilst aiming to keep the path finding time
          below 1sec.

    - **memory_limit** - *({None, int, 'max_input'} (default: `None`))* - Give the upper bound of the largest intermediate tensor contract will build.
        - None or -1 means there is no limit
        - `max_input` means the limit is set as largest input tensor
        - a positive integer is taken as an explicit limit on the number of elements

        The default is None. Note that imposing a limit can make contractions
        exponentially slower to perform.

    - **backend** - *(str, optional (default: ``auto``))* Which library to use to perform the required ``tensordot``, ``transpose``
        and ``einsum`` calls. Should match the types of arrays supplied, See
        :func:`contract_expression` for generating expressions which convert
        numpy arrays to and from the backend library automatically.

    **Returns:**

    - **out** - *(array_like)* The result of the einsum expression.

    **Notes:**

    This function should produce a result identical to that of NumPy's einsum
    function. The primary difference is ``contract`` will attempt to form
    intermediates which reduce the overall scaling of the given einsum contraction.
    By default the worst intermediate formed will be equal to that of the largest
    input array. For large einsum expressions with many input arrays this can
    provide arbitrarily large (1000 fold+) speed improvements.

    For contractions with just two tensors this function will attempt to use
    NumPy's built-in BLAS functionality to ensure that the given operation is
    performed optimally. When NumPy is linked to a threaded BLAS, potential
    speedups are on the order of 20-100 for a six core machine.
    """
    optimize_arg = kwargs.pop("optimize", True)
    if optimize_arg is True:
        optimize_arg = "auto"

    valid_einsum_kwargs = ["out", "dtype", "order", "casting"]
    einsum_kwargs = {k: v for (k, v) in kwargs.items() if k in valid_einsum_kwargs}

    # If no optimization, run pure einsum
    if optimize_arg is False:
        return _einsum(*operands_, **einsum_kwargs)

    # Grab non-einsum kwargs
    use_blas = kwargs.pop("use_blas", True)
    memory_limit = kwargs.pop("memory_limit", None)
    backend = kwargs.pop("backend", "auto")
    gen_expression = kwargs.pop("_gen_expression", False)
    constants_dict = kwargs.pop("_constants_dict", {})

    # Make sure remaining keywords are valid for einsum
    unknown_kwargs = [k for (k, v) in kwargs.items() if k not in valid_einsum_kwargs]
    if len(unknown_kwargs):
        raise TypeError("Did not understand the following kwargs: {}".format(unknown_kwargs))

    if gen_expression:
        full_str = operands_[0]

    # Build the contraction list and operand
    operands: Sequence[ArrayType]
    contraction_list: ContractionListType
    operands, contraction_list = contract_path(  # type: ignore
        *operands_, optimize=optimize_arg, memory_limit=memory_limit, einsum_call=True, use_blas=use_blas
    )

    # check if performing contraction or just building expression
    if gen_expression:
        return ContractExpression(full_str, contraction_list, constants_dict, **einsum_kwargs)

    return _core_contract(operands, contraction_list, backend=backend, **einsum_kwargs)
def contract ( * operands_ : Any , * * kwargs : Any ) -> ArrayType : optimize_arg = kwargs . pop ( "optimize" , True ) if optimize_arg is True : optimize_arg = "auto" valid_einsum_kwargs = [ "out" , "dtype" , "order" , "casting" ] einsum_kwargs = { k : v for ( k , v ) in kwargs . items ( ) if k in valid_einsum_kwargs } # If no optimization, run pure einsum if optimize_arg is False : return _einsum ( * operands_ , * * einsum_kwargs ) # Grab non-einsum kwargs use_blas = kwargs . pop ( "use_blas" , True ) memory_limit = kwargs . pop ( "memory_limit" , None ) backend = kwargs . pop ( "backend" , "auto" ) gen_expression = kwargs . pop ( "_gen_expression" , False ) constants_dict = kwargs . pop ( "_constants_dict" , { } ) # Make sure remaining keywords are valid for einsum unknown_kwargs = [ k for ( k , v ) in kwargs . items ( ) if k not in valid_einsum_kwargs ] if len ( unknown_kwargs ) : raise TypeError ( "Did not understand the following kwargs: {}" . format ( unknown_kwargs ) ) if gen_expression : full_str = operands_ [ 0 ] # Build the contraction list and operand operands : Sequence [ ArrayType ] contraction_list : ContractionListType operands , contraction_list = contract_path ( # type: ignore * operands_ , optimize = optimize_arg , memory_limit = memory_limit , einsum_call = True , use_blas = use_blas ) # check if performing contraction or just building expression if gen_expression : return ContractExpression ( full_str , contraction_list , constants_dict , * * einsum_kwargs ) return _core_contract ( operands , contraction_list , backend = backend , * * einsum_kwargs )