
    ,Vh3                     v    d Z ddlmZ ddlmZ g dZdad Z ed      dd	       Zd
 Z	d Z
d Zd Zd Zd Zy)zORequired functions for optimized contractions of numpy arrays using tensorflow.    )has_array_interface)to_backend_cache_wrap)to_tensorflowbuild_expressionevaluate_constantsNc                     t         8dd l} 	 | j                         }| j                  j                         }|sd}| ||fa t         S # t        $ r: 	 | j                  j
                  j                         }n# t        $ r d}Y nw xY wY kw xY w)Nr   Fcpu)	_CACHED_TF_DEVICE
tensorflowexecuting_eagerlyAttributeErrorcontribeagerin_eager_modetestgpu_device_name)tfr   devices      N/home/dcms/DCMS/lib/python3.12/site-packages/opt_einsum/backends/tensorflow.py_get_tensorflow_and_devicer      s      	((*E ((*F-  	

((668! 	s5   A 	B$A43B4B?BBBBT)	constantsc                 :   t               \  }}}|r9t        |       r,|j                  |      5  |j                  |       cddd       S | S t        |       r9|r|j                  |       S |j	                  | j
                  | j                        S | S # 1 sw Y   | S xY w)z?Convert a numpy array to a ``tensorflow.placeholder`` instance.N)r   r   r   convert_to_tensorplaceholderdtypeshape)arrayconstantr   r   r   s        r   r   r   "   s     34Bu%6" 3++E23 3 5!''..~~ekk5;;77L3 s   BBc                     t               \  }}| D cg c]  }t        |       c}|j                  d      fd}|S c c}w )z=Build a tensorflow function based on ``arrays`` and ``expr``.r   backendc                      j                         }t        |       D ci c]"  \  }}|j                  j                  dk(  s ||$ }}}|j	                  |      S c c}}w )NPlaceholder)	feed_dict)get_default_sessionzipoptyperun)arrayssessionpar$   graphplaceholdersr   s        r   tensorflow_contractz3build_expression_graph.<locals>.tensorflow_contractA   s\    ((*&),&?^da14499P]C]QT^	^{{5I{66 _s   !AA)r   r   	_contract)r*   expr_r   r0   r.   r/   r   s        @@@r   build_expression_graphr4   :   sJ    )+HB16<=UM%(=LNN<N>E7  >s   Ac                 T   t               \  }}}| D cg c]  }t        |d       } } || ddd\  }}|j                         }t        |j	                  |D cg c]  }||	 c}            }|D cg c]  }|dnt        t        |      d       }}||fS c c}w c c}w c c}w )zConvert constant arguments to tensorflow constants, and perform any
    possible constant contractions. Requires evaluating a tensorflow graph.
    T)r   r   r!   r   N)r   r   r%   iterr)   next)	const_arraysr2   r   r3   xnew_opsnew_contraction_listr+   
new_constss	            r   evaluate_constants_graphr>   J   s     *+HB1 >JJM!d3JLJ$(,ae$f!G! $$&Ggkkg"G1"GHIJ^efYZqytmD4Dt&TTfGf((( K
 #Hfs   BB !B 4!B%c                     fd}|S )zCBuild a eager tensorflow function based on ``arrays`` and ``expr``.c                  ~    j                  | D cg c]  }t        |       c}d      j                         S c c}w )Nr   r    )r1   r   numpy)r*   r:   r2   s     r   tensorflow_eager_contractz9build_expression_eager.<locals>.tensorflow_eager_contractb   s2    ~~@A}Q/@,~W]]__@s   : )r3   r2   rB   s    ` r   build_expression_eagerrD   _   s    ` %$    c                 L     || D cg c]  }t        |       c}dddS c c}w )zoConvert constant arguments to tensorflow_eager arrays, and perform any
    possible constant contractions.
    r   Tr6   )r   )r9   r2   r:   s      r   evaluate_constants_eagerrG   h   s'     L9q-"9<dhii9s   !c                 L    t               \  }}}|rt        nt        } || |      S N)r   rD   r4   )r*   r2   r3   r   fns        r   r   r   r   s(    ,.KAq%#(	.DBfdrE   c                 L    t               \  }}}|rt        nt        } || |      S rI   )r   rG   r>   )r9   r2   r3   r   rJ   s        r   r   r   x   s(    ,.KAq%%*	!0HBlD!!rE   )F)__doc__opt_einsum.helpersr   opt_einsum.sharingr   __all__r
   r   r   r4   r>   rD   rG   r   r   rC   rE   r   <module>rP      sV    U 2 4
E . & '. )*%j"rE   