
    ,Vh              %          U d Z ddlZddlZddlZddlZddlZddlZddlZddlm	Z	m
Z
 ddlmZmZmZmZmZmZmZmZmZmZmZ ddlm	Z ddlmZmZ ddlmZmZmZmZ g dZ d	d e!d
      hZ" G d d      Z#dedefdZ$dedefdZ%deee&      dee&   dee'   de'de'dee&e'f   deee&   e'f   fdZ(deee&      dee   dedee&e'f   de'f
dZ)	 d|dee   dedee&e'f   dee'   def
dZ*de'de'de'de'de+f
d Z,de'de'de'de'de+f
d!Z-e,e-d"Z.d#e&dee'e'e'e'ge+f   fd$Z/d%e'd&e'd'e'd(e'd)e'd*e'de!fd+Z0d%e'd&e'd'e'd(e'd)e'd*e'de!fd,Z1e0e1d-Z2 G d. d/e#      Z3	 	 	 	 	 d}dee   dedee&e'f   dee'   d0ee'   d1e'd2e&d3e&defd4Z4 ejj                  e4d5      Z6 ejj                  e4d65      Z7 ejj                  e4d75      Z8ee'e'e'f   Z9ee9eeef   Z:ded8ee&e'f   deee'f   d9eee'f   d:ee'ee&   f   d)ed*ed3ede:fd;Z;ded8ee&ef   deee'f   d9eee'f   d:ee'ee&   f   d)ed<ee   d=ee:   d>e+d3eddfd?Z<d@ee&ee   f   d:ee'ee&   f   dAeddfdBZ=dC Z>	 	 d~dee   ded8ee&e'f   dDed3edefdEZ?	 	 	 ddee   dedee&e'f   dee'   dDed3e&defdFZ@dGeedHf   defdIZAdeee'      dee'   deee'      fdJZBdKe'dLeee'      deee'   ddf   fdMZCdN ZDdOe'dPe'dQee'   dee'   dRe'dSe'dTe'dUee'ef   dVe'dWe'deee'      dXee'   dee'   dYee'ee'   f   dZee'ee'   f   ddf d[ZEdOe'dPe'dQee'   dee'   dRe'dSe'dTe'dUee'ef   dVe'dWe'deee'      dXee'   dee'   dYee'ee'   f   dZee'ee'   f   ddf d\ZFdOe'dPe'dQee'   dee'   dRe'dSe'dTe'dUee'ef   dVe'dWe'deee'      dXee'   dee'   dYee'ee'   f   dZee'ee'   f   ddf d]ZGd^ZHeHeIfdOe'dPe'dQee'   dee'   dRe'dSe'dTe'dUee'ef   dVe'dWe'deee'      dXee'   dee'   dYee'ee'   f   dZee'ee'   f   d_ee'e!f   d`eddf$daZJ ej                  db      ZL ej                  dc      d2ee&ef   deeee'e!f   f   fdd       ZNdLeee'dHf      deedHf   fdeZOdeee'      dfee&dHf   dgee&   deeee'      eee'      eee'ee'   f      f   fdhZP G di dje#      ZQ	 d|dee   dedee&e'f   dee'   dkedefdlZRi ZS eTd7dm      D ]  ZUe*eSeU<   	  eTdmdn      D ]  ZUe6eSeU<   	  eTdndo      D ]  ZUe7eSeU<   	  eTdodp      D ]  ZUe8eSeU<   	 	 d|dee   dedee&e'f   dee'   def
dqZVi ZW eTd7dr      D ]  ZUe*eWeU<   	  eTdrds      D ]  ZUeReWeU<   	 	 d|dee   dedee&e'f   dee'   def
dtZXeVeXe*e6e7e8e@e@e@eReRduZYee&ef   eZdv<   dwe&dxeddfdyZ[dze&defd{Z\y)zSContains the path technology behind opt_einsum in addition to several path helpers.    N)Counterdefaultdict)AnyCallableDict	FrozenSet	GeneratorListOptionalSequenceSetTupleUnion)r   )compute_size_by_dict
flop_count)ArrayIndexTypePathSearchFunctionTypePathTypeTensorShapeType)	optimalBranchBoundbranchgreedyautoauto_hqget_path_fnDynamicProgrammingdynamic_programminginfc                   n    e Zd ZdZdee   dedeeef   ddfdZ		 d
dee   dedeeef   de
e   def
d	Zy)PathOptimizera  Base class for different path optimizers to inherit from.

    Subclassed optimizers should define a call method with signature:

    ```python
    def __call__(self, inputs: List[ArrayIndexType], output: ArrayIndexType, size_dict: dict[str, int], memory_limit: int | None = None) -> list[tuple[int, ...]]:
        \"\"\"
        Parameters:
            inputs: The indices of each input array.
            outputs: The output indices
            size_dict: The size of each index
            memory_limit: If given, the maximum allowed memory.
        \"\"\"
        # ... compute path here ...
        return path
    ```

    where `path` is a list of int-tuples specifying a contraction order.
    inputsoutput	size_dictreturnNc                 j    |||f}t        | d      s|| _        y|| j                  k7  rt        d      y)zUtility that stateful optimizers can use to ensure they are not
        called with different contractions across separate runs.
        _first_call_argszThe arguments specifying the contraction that this path optimizer instance was called with have changed - try creating a new instance.N)hasattrr(   
ValueError)selfr#   r$   r%   argss        @/home/dcms/DCMS/lib/python3.12/site-packages/opt_einsum/paths.py_check_args_against_first_callz,PathOptimizer._check_args_against_first_call5   sI     	*t/0$(D!T***W  +    memory_limitc                     t         N)NotImplementedError)r+   r#   r$   r%   r0   s        r-   __call__zPathOptimizer.__call__H   s
     "!r/   r2   )__name__
__module____qualname____doc__r
   r   r   strintr.   r   r   r4    r/   r-   r"   r"       s    (^$  S>	
 
0 '+"^$" " S>	"
 sm" 
"r/   r"   ssa_pathr&   c           
         t        t        t        |             t        |       z
  dz   }t        t	        |            }g }|}| D ]w  }t        |D cg c]  }t        j                  ||       c}      }t        |      D ]  }|j                  |        |j                  |       |j                  |       |dz  }y |D 	cg c]  }	t        |	       c}	S c c}w c c}	w )zConvert a path with static single assignment ids to a path with recycled
    linear ids.

    Example:
        ```python
        ssa_to_linear([(0, 3), (2, 4), (1, 5)])
        #> [(0, 3), (1, 2), (0, 1)]
        ```
       )summaplenlistrangesortedbisectbisect_leftreversedpopappendtuple)
r<   nidspathssasconsconjxs
             r-   ssa_to_linearrT   R   s    $ 	CX#h-/!3A
uQx.CD
C $?Qf((a0?@# 	AGGAJ	

3Cq ##E!H## @ $s   C
CrM   c                 `   t        t        t        |             t        |       z
  dz   }t        t	        |            t        j                  |      }g }| D ]T  }|j                  t        fd|D                     t        |d      D ]  }|=  j                  t        |             V |S )zConvert a path with recycled linear ids to a path with static single
    assignment ids.

    Exmaple:
        ```python
        linear_to_ssa([(0, 3), (1, 2), (0, 1)])
        #> [(0, 3), (2, 4), (1, 5)]
        ```
    r>   c              3   (   K   | ]	  }|     y wr2   r;   ).0id_linear_to_ssas     r-   	<genexpr>z linear_to_ssa.<locals>.<genexpr>   s     @SmC0@   T)reverse)r?   r@   rA   rB   rC   	itertoolscountrI   rJ   rD   next)rM   
num_inputsnew_idsr<   rL   rX   rY   s         @r-   rY   rY      s     Sd^$s4y014Jz*+Mooj)GH ,@C@@A#t, 	#Cc"	#T']+	,
 Or/   r#   r$   	remainingirR   r%   c           	          | |   | |   }}||z  }||z  }	t        j                  |gt        | j                  |||hz
         }
||
z  }t	        |t        |	|
z
        d|      }||fS )aE  Calculate the resulting indices and flops for a potential pairwise
    contraction - used in the recursive (optimal/branch) algorithms.

    Parameters:
        inputs: The indices of each tensor in this contraction, note this includes
            tensors unavailable to contract as static single assignment is used:>
            contracted tensors are not removed from the list.
        output: The set of output indices for the whole contraction.
        remaining: *The set of indices (corresponding to ``inputs``) of tensors still available to contract.
        i: Index of potential tensor to contract.
        j: Index of potential tensor to contract.
        size_dict: Size mapping of all the indices.

    Returns:
        k12: The resulting indices of the potential tensor.
        cost: Estimated flop count of operation.
       )	frozensetunionr@   __getitem__r   bool)r#   r$   rb   rc   rR   r%   k1k2eithersharedkeepk12costs                r-   calc_k12_flopsrq      s|    2 AYq	B"WF"WF??6PC(:(:IA<N$OPD
4-Cfd6D=11i@D9r/   c                     t        j                  t        | j                  |       }||z
  }t	        |      }t        |t        |      ||      S )zCompute the flop count for a contraction of all remaining arguments. This
    is used when a memory limit means that no pairwise contractions can be made.
    )rf   rg   r@   rh   rA   r   ri   )r#   rb   r$   r%   idx_contractioninner	num_termss          r-   _compute_oversize_flopsrv      sG      oos6+=+=y'IJOf$EIIotE{IyIIr/   r0   c                 >  	
 t        t        t        |             }t        |      dt        d      idt        t	        t        |                   fii 
i 		
fd d|t        t	        t        |                   d       t        d         S )a  Computes all possible pair contractions in a depth-first recursive manner,
    sieving results based on `memory_limit` and the best path found so far.

    Parameters:
        inputs: List of sets that represent the lhs side of the einsum subscript.
        output: Set that represents the rhs side of the overall einsum subscript.
        size_dict: Dictionary of index sizes.
        memory_limit: The maximum number of elements in a temporary array.

    Returns:
        path: The optimal contraction order within the memory limit constraint.

    Examples:
    ```python
    isets = [set('abd'), set('ac'), set('bdc')]
    oset = set('')
    idx_sizes = {'a': 1, 'b':2, 'c':3, 'd':4}
    optimal(isets, oset, idx_sizes, 5000)
    #> [(0, 2), (0, 1)]
    ```
    flopsr    r<   c           
      .   t        |      dk(  r|d<   | d<   y t        j                  |d      D ]  \  }}||kD  r||}}||   ||   f}	 |   \  }}||z   }	|	d   k\  r0t
        vr<	 |   }
|
kD  r1|t        ||      z   }	|	d   k  r|	d<   | t        |      fz   d<   t | ||ffz   ||fz   |||hz
  t        |      hz  |	        y # t        $ r t	        ||||      x\  }}|<   Y w xY w# t        $ r t        |      x}
|<   Y w xY w)Nr>   rx   r<   re   rM   r#   rb   rx   )	rA   r]   combinationsKeyErrorrq   _UNLIMITED_MEMr   rv   rJ   )rM   rb   r#   rx   rc   rR   keyro   flops12	new_flopssize12_optimal_iterate
best_flopsbest_ssa_pathr0   
output_setresult_cache
size_cacher%   s              r-   r   z!optimal.<locals>._optimal_iterate   s   y>Q"'Jw(,M*% **9a8 #	DAq1u!1!9fQi(Cr+C0W
 IJw// >1T'_F
 L( %(?	S]_h(i iI :g#66.7
7+48E)<L;N4Nj1 aVI%#q!f,F}<	=#	  r3A&*V_abdegp3qqW|C0r   T/CC/SSFZ_Ts$   C*C7!C43C47DDr;   r   rz   )rJ   r@   rf   floatrC   rA   setrT   )r#   r$   r%   r0   
inputs_setr   r   r   r   r   r   s     `` @@@@@@r-   r   r      s    6 s9f-.J6"J5<(J%c&k(:";!=>M,.J\^L+ +Z "Z3uS[?Q;RZ[\z233r/   rx   sizer   	best_sizec                     | |f||fk  S r2   r;   rx   r   r   r   s       r-   better_flops_firstr   !  s    4=J	222r/   c                     || f||fk  S r2   r;   r   s       r-   better_size_firstr   %  s    %=Iz222r/   rx   r   r~   c                     t         |    S r2   )_BETTER_FNSr~   s    r-   get_better_fnr   /  s    sr/   r   size1size2ro   rj   rk   c                     | |z
  |z
  S )zpThe default heuristic cost, corresponding to the total reduction in
    memory of performing a contraction.
    r;   r   r   r   ro   rj   rk   s         r-   cost_memory_removedr   6  s     E>E!!r/   c                 @    t        j                  dd      | |z
  |z
  z  S )zyLike memory-removed, but with a slight amount of noise that breaks ties
    and thus jumbles the contractions a bit.
    g      ?g{Gz?)randomgaussr   s         r-   cost_memory_removed_jitterr   =  s#     <<T"funu&<==r/   )memory-removedzmemory-removed-jitterc                       e Zd Z	 	 	 	 ddee   dededefdZedefd       Z		 dd	e
e   d
edeeef   dee   def
dZy)r   Nnbranchcutoff_flops_factorminimizecost_fnc                 
   ||dk  rt        d| d      || _        || _        || _        t        j                  ||      | _        t        |      | _        t        d      t        d      d| _
        t        d       | _        y)a  Explores possible pair contractions in a depth-first recursive manner like
        the `optimal` approach, but with extra heuristic early pruning of branches
        as well sieving by `memory_limit` and the best path found so far.


        Parameters:
            nbranch: How many branches to explore at each contraction step. If None, explore
                all possible branches. If an integer, branch into this many paths at
                each step. Defaults to None.
            cutoff_flops_factor: If at any point, a path is doing this much worse than the best path
                found so far was, terminate it. The larger this is made, the more paths
                will be fully explored and the slower the algorithm. Defaults to 4.
            minimize: Whether to optimize the path with regard primarily to the total
                estimated flop-count, or the size of the largest intermediate. The
                option not chosen will still be used as a secondary criterion.
            cost_fn: A function that returns a heuristic 'cost' of a potential contraction
                with which to sort candidates. Should have signature
                `cost_fn(size12, size1, size2, k12, k1, k2)`.
        Nr>   z6The number of branches must be at least one, `nbranch=z`.r    r   c                      t        d      S )Nr    )r   r;   r/   r-   <lambda>z&BranchBound.__init__.<locals>.<lambda>o  s
    5< r/   )r*   r   r   r   	_COST_FNSgetr   r   betterr   bestr   best_progress)r+   r   r   r   r   s        r-   __init__zBranchBound.__init__K  s~    4 Wq[UV]U^^`abb#6  %MM'7;#H-.3ElE%L$Q	/:;O/Pr/   r&   c                 2    t        | j                  d         S )Nr<   )rT   r   )r+   s    r-   rM   zBranchBound.pathq  s    TYYz233r/   inputs_output_r%   r0   c                 D   	
  j                  |       t        t        t        |            }t              |D ci c]  }|t	        |       c}
i 		 
fd d|t        t        t        |                  dd        j                  S c c}w )a  Parameters:
            inputs_: List of sets that represent the lhs side of the einsum subscript
            output_: Set that represents the rhs side of the overall einsum subscript
            size_dict: Dictionary of index sizes
            memory_limit: The maximum number of elements in a temporary array.

        Returns:
            path: The contraction order within the memory limit constraint.

        Examples:
        ```python
        isets = [set('abd'), set('ac'), set('bdc')]
        oset = set('')
        idx_sizes = {'a': 1, 'b':2, 'c':3, 'd':4}
        optimal(isets, oset, idx_sizes, 5000)
        #> [(0, 2), (0, 1)]
        c                     t              dk(  r.j                  d<   j                  d<    j                  d<   y dt        t           dt        t           dt        dt        d	t
        f
 fd
}g }t        j                  d      D ]N  \  }}||kD  r||}}|   |   }
}	|	j                  |
      r+ ||	|
||      }|s9t        j                  ||       P |sUt        j                  d      D ]<  \  }}||kD  r||}}|   |   }
}	 ||	|
||      }|s't        j                  ||       > d}j                  |j                  k  rp|rmt        j                  |      \  }}}}\  }}}  ||ffz   |fz   ||hz
  t              hz  ||       |dz  }j                  |j                  k  r|rky y y y )Nr>   r   rx   r<   rj   rk   rc   rR   r&   c           	         	 | |f   \  }}	 |   }|z   }t        |      }j	                  ||j
                  d   j
                  d         sy |j                  t                 k  r|j                  t              <   n)|j                  j                  t                 z  kD  ry t        vrT|kD  rOt              z   }|j
                  d   k  r+|j
                  d<   t              fz   j
                  d<   y |      |      }
}	j                  ||	|
|| |      }||||||f|fS # t         $ r t        ||      x\  }}| |f<   Y Uw xY w# t         $ r t        |      x}|<   Y pw xY w)Nrx   r   r<   )r|   rq   r   maxr   r   r   rA   r   r}   rv   rJ   r   )rj   rk   rc   rR   ro   r   r   r   new_sizer   r   rp   rx   r#   r0   r$   r   rM   rb   r   r+   r   r   r%   s               r-   _assess_candidatezHBranchBound.__call__.<locals>._branch_iterate.<locals>._assess_candidate  s   u#/B#7LCT'_F "GO	tV, {{9h		'8JDIIV\L]^ t11#f+>>6?D&&s6{3!9!9D<N<NsSY{<[![[ !6Vl=R %(?	SZ\e(f fI 499W#55-6		'*04i8H7J0J		*-  *&)4j6Ku||FE5#r2FWiAq63FFG   u:HQWYbdeghjs:ttLC<B#7u
   T/CC/SSFZ_Ts"   
D< E& <#E#"E#&FFre   r   rM   r#   rb   rx   r   )rA   r   r   r9   r:   r   r]   r{   
isdisjointheapqheappushr   heappop)rM   r#   rb   rx   r   r   
candidatesrc   rR   rj   rk   	candidatebi_r   r   ro   _branch_iterater0   r$   r   r   r+   r   r%   s   `````            r-   r   z-BranchBound.__call__.<locals>._branch_iterate  s   9~"$(		&!%*		'"(,		*%'Gin 'G)C. 'GS 'GUX 'G]` 'G 'G 'GT J!..y!< :1q5aqAF1IB ==$-b"a;	NN:y9: %229a@ >DAq1u !1#AYq	B 1"b!Q ?I z9=> B<<'2+<*9>z9R61i6Aq3!Q	)!SF?(Aq61c&k]B#! a <<'2+<*+<*+<r/   r;   r   r   )	r.   rJ   r@   rf   r   r   rC   rA   rM   )r+   r   r   r%   r0   r#   kr   r$   r   r   s   ` ```  @@@@r-   r4   zBranchBound.__call__u  s    0 	++GWiH(-c)W.E(F!*7!3EKLa-a;;L
`bU	 U	n 	R#eCK>P:QYZabcyyy Ms   B)N   rx   r   r2   )r5   r6   r7   r   r:   r9   r   propertyr   rM   r
   r   r   r4   r;   r/   r-   r   r   J  s     "&#$'$Q#$Q !$Q 	$Q
 $QL 4h 4 4 '+yn%y  y S>	y
 smy 
yr/   r   r   r   r   r   c                 6    t        ||||      } || |||      S )N)r   r   r   r   )r   )	r#   r$   r%   r0   r   r   r   r   	optimizers	            r-   r   r     s,     -@8]dI VVY==r/   )r   re   r>   sizes
footprintsdim_ref_countsc                     ||z  }||z  }	||	z
  }
|| z  |	|d   z  z  |
|d   z  z  } |t        ||      ||   ||   |||      }||   }||   }||kD  r||||f\  }}}}|||f}||||fS )N   re   )r   )r$   r   rb   r   r   rj   rk   r   rl   twoonero   rp   id1id2s                  r-   _get_candidater   	  s     "WF
r'C
3,CF?s^A%66
73PQAR;R
SCS%(22

D B-C
B-C
SysB+CSc>DRr/   k2squeuepush_allc
                     	 	 fd|D        }
|r|
D ]  }t        j                  ||        y t        j                  |t        |
             y )Nc              3   B   K   | ]  }t        |        y wr2   )r   )	rW   rk   r   r   r   rj   r$   rb   r   s	     r-   rZ   z"_push_candidate.<locals>.<genexpr>3  s(     vln.	:~WY[]_fgvs   )r   r   min)r$   r   rb   r   r   rj   r   r   r   r   r   r   s   ``````   `  r-   _push_candidater   '  sH     wvruvJ# 	-INN5),	- 	uc*o.r/   dim_to_keysdimsc                 6   |D ]  }t        | |         }|dk  r)|d   j                  |       |d   j                  |       ?|dk(  r)|d   j                  |       |d   j                  |       m|d   j                  |       |d   j                  |        y )Nr>   re   r   )rA   discardadd)r   r   r   dimr^   s        r-   _update_ref_countsr   <  s    
  
'K$%A:1%%c*1%%c*aZ1!!#&1%%c*1!!#&1!!#&
'r/   c                 T    t        j                  |       \  }}}}||vs||vry||||fS )zFDefault contraction chooser that simply takes the minimum cost option.N)r   r   )r   rb   rp   rj   rk   ro   s         r-   _simple_chooserr   N  s:    e,D"b#	b	1Rr/   	choose_fnc                 ,   t        |       dk(  rdgS t        j                  ||      }|	t        }d}nd}| D cg c]  }t	        |       }}t	        |      t	        j
                  | z  }i }t        j                  t        |            }	g }
t        |      D ]3  \  }}||v r%|
j                  ||   |f       t        |	      ||<   /|||<   5 t        t              }|D ]   }||z
  D ]  }||   j                  |        " dD ci c]8  }||j                         D ch c]  \  }}t        |      |k\  s| c}}|z
  : }}}}|D ci c]  }|t        ||       }}g }|j                         D ]N  \  }}t!        ||j"                        }t        |dd       D ]!  \  }}|d|z   d }t%        ||||||||||
       # P |rG |||      }||\  }}}}|j'                  |      }|j'                  |      }||z
  D ]  }||   j)                  |        ||z
  D ]  }||   j)                  |        |
j                  ||f       ||v r |
j                  ||   t        |	      f       n||z
  D ]  }||   j                  |        t        |	      ||<   t+        |||||z
  z         t        ||      ||<   |}|D ch c]  }||   D ]  }|  }}}|j-                  |       |rt%        ||||||t/        |      |||
       |rG|j                         D cg c]  \  }}t        ||z  |      ||f } }}t1        j2                  |        t1        j4                  |       \  }!}}| rt1        j4                  |       \  }!}}|
j                  t7        ||      t9        ||      f       ||z  |z  }t        ||      }t        |	      }"t1        j:                  | ||"|f      \  }!}}| r|
S c c}w c c}}w c c}}}w c c}w c c}}w c c}}w )	zThis is the core function for :func:`greedy` but produces a path with
    static single assignment ids rather than recycled linear ids.
    SSA ids are cheaper to work with and easier to reason about.
    r>   )r   NFT)re   r   r   r   )rA   r   r   r   rf   intersectionr]   r^   	enumeraterI   r_   r   r   r   itemsr   rD   rh   r   rH   remover   r   rB   r   heapifyr   r   r   heappushpop)#r#   r$   r   r   r   r   rS   	fs_inputsrb   ssa_idsr<   ssa_idr~   r   r   r^   keysr   r   r   dim_keysdim_keys_listrc   rj   	k2s_guessrQ   rp   rk   ro   ssa_id1ssa_id2r   final_queuer   ssa_id12s#                                      r-   ssa_greedy_optimizer   V  s    6{av mmGW-G #	  (..!1.I.v!7!7!CCF ,.Iooc)n-G&(H + $)OOYs^V45!']IcN#IcN$ c"K &< 	&C  %	&& gm ]b[%6%6%8O	TCI<NORXXXN 
 DMMC#+C77MJM *,E$**, XxY-B-BC}Sb12 	EAr%a!eg.I	$ y);b"c--#--#; 	(C##B'	(; 	(C##B'	('*+)OOYs^T'];<V| *C $$S)*g	#;R&[8HI.sE:
3 ;c+c*:;Br;r;;BS	7 R `i_n_n_pqP[PSU[(vu=vsKqKq	MM+]];/NAw
{37BWg.GW0EFGBw& #C/=**;x8MN7B  OM /0 P
 N\ <" rs5   O3O>!O88O8<	O>PP
#P8O>c                 j    |t         vrt        | |||d|      S t        | ||||      }t        |      S )a9  Finds the path by a three stage algorithm:

    1. Eagerly compute Hadamard products.
    2. Greedily compute contractions to maximize `removed_size`
    3. Greedily compute outer products.

    This algorithm scales quadratically with respect to the
    maximum number of elements sharing a common dim.

    Parameters:
        inputs: List of sets that represent the lhs side of the einsum subscript
        output: Set that represents the rhs side of the overall einsum subscript
        size_dict: Dictionary of index sizes
        memory_limit: The maximum number of elements in a temporary array
        choose_fn: A function that chooses which contraction to perform from the queue
        cost_fn: A function that assigns a potential contraction a cost.

    Returns:
        path: The contraction order (a list of tuples of ints).

    Examples:
        ```python
        isets = [set('abd'), set('ac'), set('bdc')]
        oset = set('')
        idx_sizes = {'a': 1, 'b':2, 'c':3, 'd':4}
        greedy(isets, oset, idx_sizes)
        #> [(0, 2), (0, 1)]
        ```
    r>   )r   r   )r   r   )r}   r   r   rT   )r#   r$   r%   r0   r   r   r<   s          r-   r   r     sA    J >)ffiqRYZZ"669gYbcH""r/   tree.c                 \   t        |       t        k(  rg S | g}g }g }t        |      dkD  r|j                  d      }|j	                  dd       t        |D cg c]  }t        |      t        k(  s| c}      D ]:  |dxx   t        fd|D              fz  cc<   |j	                  |d   d          < |D cg c]  }t        |      t        k7  s| c}D ]6  }|dxx   t        |      t        |      z   fz  cc<   |j                  |       8 t        |      dkD  r|S c c}w c c}w )a{  Converts a contraction tree to a contraction path as it has to be
    returned by path optimizers. A contraction tree can either be an int
    (=no contraction) or a tuple containing the terms to be contracted. An
    arbitrary number (>= 1) of terms can be contracted at once. Note that
    contractions are commutative, e.g. (j, k, l) = (k, l, j). Note that in
    general, solutions are not unique.

    Parameters:
        c: Contraction tree

    Returns:
        path: Contraction path

    Examples:
        ```python
        _tree_to_sequence(((1,2),(0,(4,5,3))))
        #> [(1, 2), (1, 2, 3), (0, 2), (0, 1)]
        ```
    r   r   r;   c              3   .   K   | ]  }|k  s	d   ywr>   Nr;   )rW   qrc   s     r-   rZ   z$_tree_to_sequence.<locals>.<genexpr>4  s     /qQ/s   
)typer:   rA   rH   insertrD   r?   rI   )r   ctrP   rR   rc   i_tups        ` r-   _tree_to_sequencer   	  s   > DzS	 $vAA!A
a&1*EE"I	BA8qaC89 	"AaDS/A//11DHHQqT"Xq!	" *+Ad5kS.@eA 	EaDSVc!f_&&DHHUO	 a&1* H 9 Bs   D$/D$;D)D)c                 Z   g }t        t        t        |                   }t        j                  |  |z
  }t        |      dkD  rt               }|j                         g}t        |      dkD  r~|j                         }|j                  |       || |   z  }|D 	ch c]  }	t        || |	   z        dkD  s|	 }
}	|j                  |
       |j                  |
       t        |      dkD  r~|j                  |       t        |      dkD  r|D cg c]  }t        |       c}S c c}	w c c}w )a  Finds disconnected subgraphs in the given list of inputs. Inputs are
    connected if they share summation indices. Note: Disconnected subgraphs
    can be contracted independently before forming outer products.

    Parameters:
        inputs: List of sets that represent the lhs side of the einsum subscript
        output: Set that represents the rhs side of the overall einsum subscript

    Returns:
        subgraphs: List containing sets of indices for each subgraph

    Examples:
        ```python
        _find_disconnected_subgraphs([set("ab"), set("c"), set("ad")], set("bd"))
        #> [{0, 2}, {1}]

        _find_disconnected_subgraphs([set("ab"), set("c"), set("ad")], set("abd"))
        #> [{0}, {1}, {2}]
        ```
    r   )
r   rC   rA   rf   rg   rH   r   extenddifference_updaterI   )r#   r$   	subgraphsunused_inputsi_sumgr   rR   i_tmpr   rK   rS   s               r-   _find_disconnected_subgraphsr	  >  s   * Ic&k*+MOOV$v-E
m
q
 E !!fqjAEE!HF1I%E)HqS1B-Ca-GHAHHHQK++A. !fqj 	 m
q
  #,,QIaL,, I -s   D#3D#D(rP   seqc                 F    d t        |t        |       ddd         D        S )zSelect elements of ``seq`` which are marked by the bitmap set ``s``.

    E.g.:

        >>> list(_bitmap_select(0b11010, ['A', 'B', 'C', 'D', 'E']))
        ['B', 'D', 'E']
    c              3   2   K   | ]  \  }}|d k(  s|  yw)1Nr;   )rW   rS   bs      r-   rZ   z!_bitmap_select.<locals>.<genexpr>p  s     >$!QQ#XA>s   Nr>   r   )zipbin)rP   r
  s     r-   _bitmap_selectr  h  s$     ?#c3q6%1R%=1>>r/   c                 x    | ||z  z  }|rt        j                  t        ||       }n
t               }||z
  }||z
  S )zoCalculates the effective outer indices of the intermediate tensor
    corresponding to the subgraph ``s``.
    )rf   rg   r  )	r  all_tensorsrP   r#   i1_cut_i2_wo_outputi1_union_i2ri_r
i_contracts	            r-   _dp_calc_legsr  s  sF    
 	
[1_Aoo~a89k$s*J##r/   cost1cost2r  cost_caps1s2xnr  r  r  	contract1	contract2c                     | |z   t        ||      z   }||k  rD||z  }||vs|||   d   k  r/t        ||	||
||      }t        ||      }|||k  r||||ff||<   yyyy)a  Performs the inner comparison of whether the two subgraphs (the bitmaps
    `s1` and `s2`) should be merged and added to the dynamic programming
    search. Will skip for a number of reasons:

    1. If the number of operations to form `s = s1 | s2` including previous
       contractions is above the cost-cap.
    2. If we've already found a better way of making `s`.
    3. If the intermediate tensor corresponding to `s` is going to break the
       memory limit.
    r>   N)r   r  )r  r  r  r%   r  r  r  r  r  r  r#   r  r0   r   r!  rp   rP   rc   mems                      r-   _dp_compare_flopsr$    s    8 5=/YGGDxGB;$Aq/aa9LkZA&q)4C#sl':D9i"891 (; * r/   c                     ||z  }t        ||	||
||      }t        ||      }t        | ||      }||k  r#||vs|||   d   k  r|||k  r||||ff||<   yyyy)zLike `_dp_compare_flops` but sieves the potential contraction based
    on the size of the intermediate tensor created, rather than the number of
    operations, and so calculates that first.
    r>   N)r  r   r   r  r  r  r%   r  r  r  r  r  r  r#   r  r0   r   r!  rP   rc   r#  rp   s                      r-   _dp_compare_sizer'    s    * 	RAaa1DkRA
q)
,CueS!DxB;$Aq/#sl':D9i"891 (; * r/   c                     ||z  }t        ||	||
||      }t        ||      }| |z   |z   }||k  r#||vs|||   d   k  r|||k  r||||ff||<   yyyy)zLike ``_dp_compare_flops`` but sieves the potential contraction based
    on the total size of memory created, rather than the number of
    operations, and so calculates that first.
    r>   Nr  r   r&  s                      r-   _dp_compare_writer*    s    * 	RAaa1DkRA
q)
,C5=3DxB;$Aq/#sl':D9i"891 (; * r/   @   factorcombinec                     ||z  }t        ||	||
||      }t        ||      }t        ||      }| |z    ||||z  f      z   }||k  r#||vs|||   d   k  r|||k  r||||ff||<   yyyy)zLike ``_dp_compare_flops`` but sieves the potential contraction based
    on some combination of both the flops and size,.
    r>   Nr)  )r  r  r  r%   r  r  r  r  r  r  r#   r  r0   r   r!  r,  r-  rP   rc   r#  frp   s                         r-   _dp_compare_combor0    s    , 	RAaa1DkRA
q)
,C[)4A5=7Av|#455DxB;$Aq/#sl':D9i"891 (; * r/   z%(flops|size|write|combo|limit)-*(\d*)   c                    | dk(  rt         dfS | dk(  rt        dfS | dk(  rt        dfS t        |       r| t	        d      fS t
        j                  |       }|t        d|  d      |j                         \  } }|rt	        |      nt        }| dk(  r+t        j                  t        |t        	      t	        d      fS | d
k(  r+t        j                  t        |t        	      t	        d      fS t        d|  d      )zThis works out what local scoring function to use for the dp algorithm
    as well as a `naive_scale` to account for the memory_limit checks.
    rx   r>   r   writer    z!Couldn't parse `minimize` value: .combo)r,  r-  limit)r$  r'  r*  callabler   minimize_finder	fullmatchr*   groupsDEFAULT_COMBO_FACTOR	functoolspartialr0  r?   r   )r   matchcustom_factorr,  s       r-   _parse_minimizer@    s
   
 7 !##	V	""	W	 !##	(	 u%% %%h/E}<XJaHII#llnHm%2U=!8LF7  !263OQVW\Q]]]	W	  !263OQVW\Q]]]<XJaHIIr/   c                 0    t        j                  d |       S )zMake a simple left to right binary tree out of iterable `seq`.

    ```python
    tuple_nest([1, 2, 3, 4])
    #> (((1, 2), 3), 4)
    ```

    c                 
    | |fS r2   r;   rS   ys     r-   r   z#simple_tree_tuple.<locals>.<lambda>6  s
    !Q r/   )r<  reduce)r
  s    r-   simple_tree_tuplerF  -  s     /55r/   all_inds
ind_countsc                    t        fdt        |      D              }g }g }g }t        |       D ]W  \  }}||z
  }	|	s!t        |      dkD  r|j                  |f       .|j                  |	       |j                  |	|k7  r|fn|       Y |||fS )aQ  Take `inputs` and parse for single term index operations, i.e. where
    an index appears on one tensor and nowhere else.

    If a term is completely reduced to a scalar in this way it can be removed
    to `inputs_done`. If only some indices can be summed then add a 'single
    term contraction' that will perform this summation.
    c              3   :   K   | ]  \  }}|   d k(  s|  ywr   r;   )rW   rc   r   rH  s      r-   rZ   z0_dp_parse_out_single_term_ops.<locals>.<genexpr>C  s      Qtq!jmq>PQs   r   )rf   r   rA   rI   )
r#   rG  rH  i_singleinputs_parsedinputs_doneinputs_contractionsrR   rc   	i_reduceds
     `       r-   _dp_parse_out_single_term_opsrP  9  s     Qy':QQH*,M$&K8:&! F1L	A
t$   +&&yA~t1EF +':::r/   c                   j    e Zd ZdZddedeeef   deddfdZ	 dde	e
   d	e
d
eeef   dee   def
dZy)r   aO  Finds the optimal path of pairwise contractions without intermediate outer
    products based a dynamic programming approach presented in
    Phys. Rev. E 90, 033315 (2014) (the corresponding preprint is publicly
    available at https://arxiv.org/abs/1304.6112). This method is especially
    well-suited in the area of tensor network states, where it usually
    outperforms all the other optimization strategies.

    This algorithm shows exponential scaling with the number of inputs
    in the worst case scenario (see example below). If the graph to be
    contracted consists of disconnected subgraphs, the algorithm scales
    linearly in the number of disconnected subgraphs and only exponentially
    with the number of inputs per subgraph.

    Parameters:
        minimize: What to minimize:
            - 'flops' - minimize the number of flops
            - 'size' - minimize the size of the largest intermediate
            - 'write' - minimize the size of all intermediate tensors
            - 'combo' - minimize `flops + alpha * write` summed over intermediates, a default ratio of alpha=64
            is used, or it can be customized with `f'combo-{alpha}'`
            - 'limit' - minimize `max(flops, alpha * write)` summed over intermediates, a default ratio of alpha=64
            is used, or it can be customized with `f'limit-{alpha}'`
            - callable - a custom local cost function

        cost_cap: How to implement cost-capping:
            - True - iteratively increase the cost-cap
            - False - implement no cost-cap at all
            - int - use explicit cost cap

        search_outer: In rare circumstances the optimal contraction may involve an outer
            product, this option allows searching such contractions but may well
            slow down the path finding considerably on all but very small graphs.
    r   r  search_outerr&   Nc                 .    || _         || _        || _        y r2   )r   rR  r  )r+   r   r  rR  s       r-   r   zDynamicProgramming.__init__w  s     ( r/   r   r   
size_dict_memory_limit_c                 	  / t        | j                        \  }}| j                  rd nd }t        t	        j
                  g ||       }t        |      }	t        |	      D 
ci c]  \  }
}||

 c}}
/|D cg c]  }t        /fd|D               }}t        /fd|D              }|j                         D ci c]  \  }}|/v s/|   | }}}t        t        |            D 
cg c]  }
||
   	 }}
|t        |      z  t        j                  t        j                  |d      z  }t!        ||	|      \  }}}|st#        t%        |            S |}dgt        |      z  }| j                  rt        t        t        |                  g}nt'        ||      }dt        |      z  dz
  }|D ]\  }dgdz  t        t        |      dz
        D 
cg c]  }
i  c}
z   }|D 
ci c]  }
d|
z  ||
   d||
   f c}
|d<   t        j                  d	 d
 |D              }t        j(                  t+        ||       }| j,                  du rt/        ||z  |      }n&| j,                  du rt1        d      }n| j,                  }t        |      dk(  rd}n)t3        t5        t7        |j8                  |            d      }t        |d         dk(  rt        dt        |d         dz         D ]  }||   } t        d|dz  dz         D ]  }!||!   j                         D ]k  \  }"\  }#}$}%|||!z
     j                         D ]I  \  }&\  }'}(})|"|&z  r|!||!z
  k7  s|"|&k  s|#|'z  |z
  }* ||*      s/|#|'z  }+ ||$|(|+|||"|&| ||||*||%|)       K m   ||kD  rt        |d         dk(  rt;        d      ||z  }t        |d         dk(  rt=        |d   j?                               d   \  }},}-|jA                  |-       |jA                  t/        ||             _ tC        t        t        |            |j8                        D 
cg c]  }
||
   	 }}
t%        |      }.t#        |.      S c c}}
w c c}w c c}}w c c}
w c c}
w c c}
w c c}
w )a2  Parameters:
            inputs_: List of sets that represent the lhs side of the einsum subscript
            output_: Set that represents the rhs side of the overall einsum subscript
            size_dict_: Dictionary of index sizes
            memory_limit_: The maximum number of elements in a temporary array.

        Returns:
            path: The contraction order (a list of tuples of ints).

        Examples:
            ```python
            n_in = 3  # exponential scaling
            n_out = 2 # linear scaling
            s = dict()
            i_all = []
            for _ in range(n_out):
                i = [set() for _ in range(n_in)]
                for j in range(n_in):
                    for k in range(j+1, n_in):
                        c = oe.get_symbol(len(s))
                        i[j].add(c)
                        i[k].add(c)
                        s[c] = 2
                i_all.extend(i)
            o = DynamicProgramming()
            o(i_all, set(), s)
            #> [(1, 2), (0, 4), (1, 2), (0, 2), (0, 1)]
            ```
        c                      y)NTr;   rS   s    r-   r   z-DynamicProgramming.__call__.<locals>.<lambda>  s    r/   c                     | S r2   r;   rX  s    r-   r   z-DynamicProgramming.__call__.<locals>.<lambda>  s    Q r/   c              3   (   K   | ]	  }|     y wr2   r;   rW   r   
symbol2ints     r-   rZ   z.DynamicProgramming.__call__.<locals>.<genexpr>  s     5aJqM5r[   c              3   (   K   | ]	  }|     y wr2   r;   r[  s     r-   rZ   z.DynamicProgramming.__call__.<locals>.<genexpr>  s     :Q:a=:r[   r>   Nre   r   c                     | |z  S r2   r;   rC  s     r-   r   z-DynamicProgramming.__call__.<locals>.<lambda>  s
    QU r/   c              3   &   K   | ]	  }d |z    ywr   r;   )rW   rR   s     r-   rZ   z.DynamicProgramming.__call__.<locals>.<genexpr>  s     <OQ!V<Os   TFr    r   z.No contraction found for given `memory_limit`.r   )"r@  r   rR  r   r]   chainrJ   r   rf   r   rC   rA   r<  rE  operatormulrP  r   rF  r	  rg   r  r  r   r   r   r   r@   rh   RuntimeErrorrB   valuesrI   rD   )0r+   r   r   rT  rU  _check_contractionnaive_scale_check_outerrH  rG  rR   r   rc   r#   r$   vsize_dict_canonicalr%   
naive_costrM  rN  subgraph_contractionssubgraph_contractions_sizer  r  r  rS   bitmap_gsubgraph_indsr  cost_incrementrK   r  mr  i1r  r   r  i2r  r!  r  r  rp   contractionr   r\  s0                                                  @r-   r4   zDynamicProgramming.__call__|  s   H +:$--*H'K+/+<+<;Y__?g?w?@
$ (1':;tq!ad;
?FG!)5155GG:'::<F<L<L<NbDAqRSWaRaz!}a/bb5:3?R;S5TU(+U	U 3v;.1A1A(,,PY[\1]]
3PQWYacm3n00$%6{%CDD !,&'S3{+;%;""5V#567I4VVDI
 CK'1, G	RA !6A:U3q6A:5F(G(GGALMNqAFVAY+>q+ABBNAaD !''(:<OQ<OPH &OO^Hf-MNM}}$/0F	R%' <===!Q&!"!$SY-B-BM)R%SUV!Wae*/q#ad)a-0 *A1B #1a1fqj1 *:;A$**, *6B 6UI>?Ahnn>N * :$:R	 )+Ra1q5jBG;=7f:L$7 (44G'H682g(:,1,1,7,5,4,.,.,.,4,7,2,?,9,5,5)****	*B z)AbE
a&'WXX *H4M ae*/P $("#7#: At[!((5&--.B1i.PQOG	R\ c456.::!
 "!$!
 !
 !!67 &&g <GbU> )HNJ!
s0   %Q:Q%>Q*Q*,Q0	Q5$Q:;Q?)rx   TFr2   )r5   r6   r7   r8   r9   r   ri   r:   r   r
   r   r   r   r   r4   r;   r/   r-   r   r   T  s     D! !%c	:J !ae !rv ! (,^'n%^'  ^' cN	^'
  }^' 
^'r/   r   kwargsc                 .    t        di |} || |||      S )Nr;   )r   )r#   r$   r%   r0   rt  r   s         r-   r   r     s"     #,V,IVVY==r/         	      c                 X    t        j                  t        |       t              | |||      S )zuFinds the contraction path by automatically choosing the method based on
    how many input arguments there are.
    )_AUTO_CHOICESr   rA   r   )r#   r$   r%   r0   s       r-   r   r   3  s&     S[&1&&)\ZZr/         c                 \    ddl m} t        j                  t	        |       |      | |||      S )zFinds the contraction path by automatically choosing the method based on
    how many input arguments there are, but targeting a more generous
    amount of search time than ``'auto'``.
    r   )random_greedy_128)opt_einsum.path_randomr  _AUTO_HQ_CHOICESr   rA   )r#   r$   r%   r0   r  s        r-   r   r   F  s,     9F->?PY[ghhr/   )r   zauto-hqr   z
branch-allzbranch-2zbranch-1r   eageropportunisticdpzdynamic-programming_PATH_OPTIONSnamefnc                 `    | t         v rt        d|  d      |t         | j                         <   y)z<Add path finding function ``fn`` as an option with ``name``.Path optimizer 'z' already exists.N)r  r|   lower)r  r  s     r-   register_path_fnr  d  s0    })$/@ABB"$M$**,r/   	path_typec           	          | j                         } | t        vr-t        d|  dt        t        j	                                d      t        |    S )z=Get the correct path finding function from str ``path_type``.r  z' not found, valid options are r4  )r  r  r|   r   r   )r  s    r-   r   r   l  sO    !I%))4STWXeXjXjXlTmSnnopqq##r/   r2   )NNr   rx   r   )Nr   )NNr   )]r8   rE   r<  r   r]   ra  r   recollectionsr   r   typingr   r   r   r   r	   r
   r   r   r   r   r   CounterTypeopt_einsum.helpersr   r   opt_einsum.typingr   r   r   r   __all__r   r}   r"   rT   rY   r9   r:   rq   rv   r   ri   r   r   r   r   r   r   r   r   r   r=  
branch_allbranch_2branch_1GreedyCostTypeGreedyContractionTyper   r   r   r   r   r   r   r	  r  r  r$  r'  r*  r;  r?   r0  compiler8  	lru_cacher@  rF  rP  r   r   r{  rC   rc   r   r  r   r  __annotations__r  r   r;   r/   r-   <module>r     s   Y       	 , i i i i ) ? _ _
 dE%L)/" /"d$H $ $\ X ,!)C.!!cN! ~! 	!
 ! CH~! 9S>3!HJ)C.!JN#J J CH~	J
 	J& #'	R4 R4R4 CH~R4 3-	R4
 R4p3c 3 3# 3# 3RV 33S 3 3 3 3QU 3
  s xc3(<d(BC " "C " "# "3 "TW "\a ">s >3 >s > >RU >[^ >ch > *7	d- dV #'! #> >> CH~> 3-	>
 c]> > > > >  Yvt4
9VQ/9VQ/sC}%nnnn\] S> NC'( ^S()	
 c#h' 	 	  <//S>/ NC'(/ ^S()	/
 c#h'/ 	/ 
n	/ %&/ / / 
/*'c3~../'c#h'' ' 
	'$ #D DD S>D 	D
 D DV #'#)# )#)# CH~)# 3-	)#
 )# )# )#X2E#s(O 2 2j'-in)= '-yQT~ '-Z^_hil_mZn '-T?c ?Ys^ 4 ?9S>SWY]C]9^ ?$ #:#:#: S#: Cy	#:
 #: 	#: 	#: 	S#X#: #: #: 3 #: S#: 3-#: S%*_%#: S%*_%#:  
!#:L::: S: Cy	:
 : 	: 	: 	S#X: : : 3 : S: 3-: S%*_%: S%*_%:  
!:>::: S: Cy	:
 : 	: 	: 	S#X: : : 3 : S: 3-: S%*_%: S%*_%:  
!:>  & !5#::: S: Cy	:
 : 	: 	: 	S#X: : : 3 : S: 3-: S%*_%: S%*_%:  #u*!:" #:$ 
%:B "**EF SJeCM2 JuXuSRWZGX=X7Y J J<	68E#s(O4 	6sCx 	6;3 ;,1#s(O;ITUXIY;
4	#eCj!14c5:o8N3OOP;6F' F'Z #'	> >> CH~> 3-	>
 > > 	q! AM!	q! "A!M!"	q!  AM! 	q"  AM!  #'		[ 	[	[ CH~	[ 3-		[
 	[  	q! "A!Q"	q" .A-Q. #'	i ii CH~i 3-	i
 i  
.4tC//0 %3 %$: %t %$3 $#9 $r/   