§
    ¹³ÙgÓ8  ã                   óþ   — 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Zddlm	Z	m
Z
mZmZmZmZ ddlmZmZ ddlmZ  G d„ d¦  «        Z G d	„ d
ej        ¦  «        Z G d„ de¦  «        Z G d„ de¦  «        ZdS )z#Lazy testers for optional features.é    N)ÚUnionÚIterableÚDictÚOptionalÚCallableÚType)ÚMissingOptionalLibraryErrorÚOptionalDependencyImportWarningé   )Úwrap_methodc                   ó"   — e Zd ZdZdZd„ Zd„ ZdS )Ú_RequireNowzùHelper callable that accepts all function signatures and simply calls
    :meth:`.LazyDependencyManager.require_now`.  This helpful when used with :func:`.wrap_method`,
    as the callable needs to be compatible with all signatures and be picklable.©Ú_testerÚ_featurec                 ó"   — || _         || _        d S ©Nr   )ÚselfÚtesterÚfeatures      úN/granary/edev/quantum/lib/python3.11/site-packages/qiskit/utils/lazy_tester.pyÚ__init__z_RequireNow.__init__$   s   € ØˆŒØˆŒˆˆó    c                 óD   — | j                              | j        ¦  «         d S r   )r   Úrequire_nowr   )r   Ú_argsÚ_kwargss      r   Ú__call__z_RequireNow.__call__(   s    € ØŒ× Ò  ¤Ñ/Ô/Ð/Ð/Ð/r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r   © r   r   r   r      sE   € € € € € ðTð Tð (€Ið ð  ð  ð0ð 0ð 0ð 0ð 0r   r   c                   óX  — e Zd ZdZdZdddddœd„Zej        defd„¦   «         Z	d„ Z
ej        d	edefd
„¦   «         Zej        d	edeegef         fd„¦   «         Zd„ Zej        dedefd„¦   «         Zej        dedeegef         fd„¦   «         Zd„ Zdefd„Zej        d„ ¦   «         ZdS )ÚLazyDependencyManagerao  A mananger for some optional features that are expensive to import, or to verify the
    existence of.

    These objects can be used as Booleans, such as ``if x``, and will evaluate ``True`` if the
    dependency they test for is available, and ``False`` if not.  The presence of the dependency
    will only be tested when the Boolean is evaluated, so it can be used as a runtime test in
    functions and methods without requiring an import-time test.

    These objects also encapsulate the error handling if their dependency is not present, so you can
    do things such as::

        from qiskit.utils import LazyImportManager
        HAS_MATPLOTLIB = LazyImportManager("matplotlib")

        @HAS_MATPLOTLIB.require_in_call
        def my_visualisation():
            ...

        def my_other_visualisation():
            # ... some setup ...
            HAS_MATPLOTLIB.require_now("my_other_visualisation")
            ...

        def my_third_visualisation():
            if HAS_MATPLOTLIB:
                from matplotlib import pyplot
            else:
                ...

    In all of these cases, ``matplotlib`` is not imported until the functions are entered.  In the
    case of the decorator, ``matplotlib`` is tested for import when the function is called for
    the first time.  In the second and third cases, the loader attempts to import ``matplotlib``
    when the :meth:`require_now` method is called, or when the Boolean context is evaluated.  For
    the ``require`` methods, an error is raised if the library is not available.

    This is the base class, which provides the Boolean context checking and error management.  The
    concrete classes :class:`LazyImportTester` and :class:`LazySubprocessTester` provide convenient
    entry points for testing that certain symbols are importable from modules, or certain
    command-line tools are available, respectively.
    ©Ú_boolÚ	_callbackÚ_nameÚ_installÚ_msgN©ÚnameÚcallbackÚinstallÚmsgc                óL   — d| _         || _        || _        || _        || _        dS )aó  
        Args:
            name: the name of this optional dependency.
            callback: a callback that is called immediately after the availability of the library is
                tested with the result.  This will only be called once.
            install: how to install this optional dependency.  Passed to
                :class:`.MissingOptionalLibraryError` as the ``pip_install`` parameter.
            msg: an extra message to include in the error raised if this is required.
        Nr'   )r   r.   r/   r0   r1   s        r   r   zLazyDependencyManager.__init__X   s+   € ð ˆŒ
Ø!ˆŒØˆŒ
ØˆŒØˆŒ	ˆ	ˆ	r   Úreturnc                 ó   — dS )a4  Subclasses of :class:`LazyDependencyManager` should override this method to implement the
        actual test of availability.  This method should return a Boolean, where ``True`` indicates
        that the dependency was available.  This method will only ever be called once.

        :meta public:
        Fr$   ©r   s    r   Ú_is_availablez#LazyDependencyManager._is_availableh   s	   € ð ˆur   c                 ó’   — | j         €:|                      ¦   «         | _         | j        �|                      | j         ¦  «         | j         S r   )r(   r6   r)   r5   s    r   Ú__bool__zLazyDependencyManager.__bool__r   sA   € ØŒ:ÐØ×+Ò+Ñ-Ô-ˆDŒJØŒ~Ð)Ø—’˜tœzÑ*Ô*Ð*ØŒzÐr   Úfeature_or_callablec                 ó   — d S r   r$   ©r   r9   s     r   Úrequire_in_callz%LazyDependencyManager.require_in_cally   s   € ØJMÈ#r   c                 ó   — d S r   r$   r;   s     r   r<   z%LazyDependencyManager.require_in_call|   s   € Ø[^Ð[^r   c                 óò   ‡ ‡‡— t          |t          ¦  «        r
|Šˆˆ fd„}|S |Št          ‰dd¦  «        pt          ‰dd¦  «        pt          ‰¦  «        Št          j        ‰¦  «        ˆˆˆ fd„¦   «         }|S )aÑ  Create a decorator for callables that requires that the dependency is available when the
        decorated function or method is called.

        Args:
            feature_or_callable (str or Callable): the name of the feature that requires these
                dependencies.  If this function is called directly as a decorator (for example
                ``@HAS_X.require_in_call`` as opposed to
                ``@HAS_X.require_in_call("my feature")``), then the feature name will be taken to be
                the function name, or class and method name as appropriate.

        Returns:
            Callable: a decorator that will make its argument require this dependency before it is
            called.
        c                 óL   •‡ — t          j        ‰ ¦  «        ˆˆ ˆfd„¦   «         }|S )Nc                  ó>   •— ‰                      ‰¦  «          ‰| i |¤ŽS r   ©r   ©ÚargsÚkwargsr   Úfunctionr   s     €€€r   ÚoutzELazyDependencyManager.require_in_call.<locals>.decorator.<locals>.out’   s,   ø€ à×$Ò$ WÑ-Ô-Ð-Ø#˜8 TÐ4¨VÐ4Ð4Ð4r   )Ú	functoolsÚwraps)rE   rF   r   r   s   ` €€r   Ú	decoratorz8LazyDependencyManager.require_in_call.<locals>.decorator‘   sC   øø€ Ý” Ñ*Ô*ð5ð 5ð 5ð 5ð 5ð 5ñ +Ô*ð5ð �
r   r!   Nr   c                  ó>   •— ‰                      ‰¦  «          ‰| i |¤ŽS r   rA   rB   s     €€€r   rF   z2LazyDependencyManager.require_in_call.<locals>.out¢   s,   ø€ à×Ò˜WÑ%Ô%Ð%Ø�8˜TÐ, VÐ,Ð,Ð,r   )Ú
isinstanceÚstrÚgetattrrG   rH   )r   r9   rI   rF   r   rE   s   `   @@r   r<   z%LazyDependencyManager.require_in_call   sÁ   øøø€ õ Ð)­3Ñ/Ô/ð 	Ø)ˆGðð ð ð ð ð ð Ðà&ˆå�H˜n¨dÑ3Ô3ð Ý�x ¨TÑ2Ô2ðå�8‰}Œ}ð 	õ 
Œ˜Ñ	"Ô	"ð	-ð 	-ð 	-ð 	-ð 	-ð 	-ñ 
#Ô	"ð	-ð ˆ
r   Úfeature_or_classc                 ó   — d S r   r$   ©r   rN   s     r   Úrequire_in_instancez)LazyDependencyManager.require_in_instance©   s   € ØCFÀ3r   c                 ó   — d S r   r$   rP   s     r   rQ   z)LazyDependencyManager.require_in_instance¬   s   € ØTWÐTWr   c                 óî   ‡ ‡— t          |t          ¦  «        r
|Šˆˆ fd„}|S |}t          |dd¦  «        pt          |dd¦  «        pt          |¦  «        Št          |dt	          ‰ ‰¦  «        ¬¦  «         |S )aí  A class decorator that requires the dependency is available when the class is
        initialized.  This decorator can be used even if the class does not define an ``__init__``
        method.

        Args:
            feature_or_class (str or Type): the name of the feature that requires these
                dependencies.  If this function is called directly as a decorator (for example
                ``@HAS_X.require_in_instance`` as opposed to
                ``@HAS_X.require_in_instance("my feature")``), then the feature name will be taken
                as the name of the class.

        Returns:
            Callable: a class decorator that ensures that the wrapped feature is present if the
            class is initialized.
        c                 óH   •— t          | dt          ‰‰¦  «        ¬¦  «         | S )Nr   ©Úbefore)r   r   )Úclass_r   r   s    €€r   rI   z<LazyDependencyManager.require_in_instance.<locals>.decoratorÂ   s'   ø€ Ý˜F Jµ{À4ÈÑ7QÔ7QÐRÑRÔRÐRØ�r   r!   Nr   r   rU   )rK   rL   rM   r   r   )r   rN   rI   rW   r   s   `   @r   rQ   z)LazyDependencyManager.require_in_instance¯   s¥   øø€ õ  Ð&­Ñ,Ô,ð 	Ø&ˆGðð ð ð ð ð ð Ðà!ˆå�F˜N¨DÑ1Ô1ð Ý�v˜z¨4Ñ0Ô0ðå�6‰{Œ{ð 	õ
 	�F˜J­{¸4ÀÑ/IÔ/IÐJÑJÔJÐJØˆr   r   c                 óN   — | rdS t          | j        || j        | j        ¬¦  «        ‚)aB  Eagerly attempt to import the dependencies in this object, and raise an exception if they
        cannot be imported.

        Args:
            feature: the name of the feature that is requiring these dependencies.

        Raises:
            MissingOptionalLibraryError: if the dependencies cannot be imported.
        N)Úlibnamer.   Úpip_installr1   )r	   r*   r+   r,   )r   r   s     r   r   z!LazyDependencyManager.require_nowÑ   s7   € ð ð 	ØˆFÝ)Ø”J W¸$¼-ÈTÌYð
ñ 
ô 
ð 	
r   c              #   óV   K  — | j         }d| _         	 dV — || _         dS # || _         w xY w)a„  
        Create a context, during which the value of the dependency manager will be ``False``.  This
        means that within the context, any calls to this object will behave as if the dependency is
        not available, including raising errors.  It is valid to call this method whether or not the
        dependency has already been evaluated.  This is most useful in tests.
        FN)r(   )r   Úpreviouss     r   Údisable_locallyz%LazyDependencyManager.disable_locallyá   sB   è è € ð ”:ˆØˆŒ
ð	"ØˆEˆEˆEà!ˆDŒJˆJˆJø˜ˆDŒJÐ!Ð!Ð!Ð!s   ’ Ÿ	()r   r    r!   r"   r#   r   ÚabcÚabstractmethodÚboolr6   r8   ÚtypingÚoverloadr   r<   rL   r   rQ   r   Ú
contextlibÚcontextmanagerr]   r$   r   r   r&   r&   ,   s�  € € € € € ð'ð 'ðR D€Ià#¨d¸DÀdð ð ð ð ð ð  	Ôð˜tð ð ð ñ Ôððð ð ð „_ØM°8ÐMÀÐMÐMÐMñ „_ØMà„_Ø^°3Ð^¸8ÀXÀJÐPXÐDXÔ;YÐ^Ð^Ð^ñ „_Ø^ð(ð (ð (ðT „_ØF°DÐF¸TÐFÐFÐFñ „_ØFà„_ØW°CÐW¸HÀdÀVÈTÀ\Ô<RÐWÐWÐWñ „_ØWð ð  ð  ðD
 3ð 
ð 
ð 
ð 
ð  Ôð"ð "ñ Ôð"ð "ð "r   r&   c                   óÊ   ‡ — e Zd ZdZdZdddddœdeeeeee         f         ee         f         de	e         de	e
egdf                  de	e         d	e	e         f
ˆ fd
„Zd„ Zˆ xZS )ÚLazyImportTesterz¦A lazy dependency tester for importable Python modules.  Any required objects will only be
    imported at the point that this object is tested for its Boolean value.)Ú_modulesNr-   Úname_map_or_modulesr.   r/   r0   r1   c                óZ  •— t          |t          ¦  «        r$d„ |                     ¦   «         D ¦   «         | _        n0t          |t          ¦  «        r
|di| _        nd„ |D ¦   «         | _        | j        st          d¦  «        ‚|�n‚t          | j        ¦  «        dk    r| j                             ¦   «         \  }nNt          | j                             ¦   «         ¦  «        }d 	                    |dd…         ¦  «        › d	|d         › �}t          ¦   «                              ||||¬
¦  «         dS )a  
        Args:
            name_map_or_modules: if a name map, then a dictionary where the keys are modules or
                packages, and the values are iterables of names to try and import from that
                module.  It should be valid to write ``from <module> import <name1>, <name2>, ...``.
                If simply a string or iterable of strings, then it should be valid to write
                ``import <module>`` for each of them.

        Raises:
            ValueError: if no modules are given.
        c                 ó4   — i | ]\  }}|t          |¦  «        “ŒS r$   )Útuple)Ú.0ÚmoduleÚnamess      r   ú
<dictcomp>z-LazyImportTester.__init__.<locals>.<dictcomp>  s$   € ÐcÐcÐc±m°f¸e˜V¥U¨5¡\¤\ÐcÐcÐcr   r$   c                 ó   — i | ]}|d “ŒS )r$   r$   )rl   rm   s     r   ro   z-LazyImportTester.__init__.<locals>.<dictcomp>  s   € ÐJÐJÐJ¨F˜V RÐJÐJÐJr   zno modules suppliedNr   z, éÿÿÿÿz and r-   )rK   ÚdictÚitemsrg   rL   Ú
ValueErrorÚlenÚkeysrk   ÚjoinÚsuperr   )r   rh   r.   r/   r0   r1   Ú	all_namesÚ	__class__s          €r   r   zLazyImportTester.__init__÷   s0  ø€ õ( Ð)­4Ñ0Ô0ð 	KØcÐcÐGZ×G`ÒG`ÑGbÔGbÐcÑcÔcˆDŒMˆMÝÐ+­SÑ1Ô1ð 	KØ0°"Ð5ˆDŒMˆMàJÐJÐ6IÐJÑJÔJˆDŒMØŒ}ð 	4ÝÐ2Ñ3Ô3Ð3ØÐØÝ�”ÑÔ 1Ò$Ð$Ø”m×(Ò(Ñ*Ô*‰GˆTˆTå˜dœm×0Ò0Ñ2Ô2Ñ3Ô3ˆIØ—i’i 	¨#¨2¨#¤Ñ/Ô/ÐEÐE°iÀ´mÐEÐEˆDÝ‰Œ×Ò˜d¨X¸wÈCÐÑPÔPÐPÐPÐPr   c           	      óÐ  — i }t          j        t          ¦  «        }| j                             ¦   «         D ]à\  }}	 t          j        |¦  «        }n„# t          $ r`}|j         	                    d¦  «        }| 	                    d¦  «        }||d t          |¦  «        …         k    rY d }~ dS |||<   Y d }~Œ€d }~wt          $ r}|||<   Y d }~Œ—d }~ww xY w|D ]>}		 t          ||	¦  «        }
Œ# t          $ r ||                              |	¦  «         Y Œ;w xY wŒá|s|rÌ| j        rd| j        › d�nd}d|› d�}|                     ¦   «         D ]#\  }}|d                     d|› d	|›�¦  «        z  }Œ$|                     ¦   «         D ]H\  }}t          |¦  «        d
k    rd|d         › d�nd|› �}|d                     d|› d|› d�¦  «        z  }ŒIt#          j        |t&          ¬¦  «         dS dS )Nú.Fú'zoptional packageszWhile trying to import z±, some components were located but raised other errors during import. You might have an incompatible version installed. Qiskit will continue as if the optional is not available.Ú z
 - module 'z' failed to import with: r   zattribute 'r   zattributes z
 - 'z' imported, but z couldn't be found)ÚcategoryT)ÚcollectionsÚdefaultdictÚlistrg   rs   Ú	importlibÚimport_moduleÚModuleNotFoundErrorr.   Úsplitru   ÚImportErrorrM   ÚAttributeErrorÚappendr*   rw   ÚwarningsÚwarnr
   )r   Úfailed_modulesÚfailed_namesrm   rn   ÚimportedÚexcÚfailed_partsÚtarget_partsr.   Ú_Úpackage_descriptionÚmessageÚ
attributess                 r   r6   zLazyImportTester._is_available  s¥  € ØˆÝ"Ô.­tÑ4Ô4ˆØ!œ]×0Ò0Ñ2Ô2ð 	6ð 	6‰MˆF�EðÝ$Ô2°6Ñ:Ô:��øÝ&ð 	ð 	ð 	Ø"œxŸ~š~¨cÑ2Ô2�Ø%Ÿ|š|¨CÑ0Ô0�Ø <Ð0Cµ#°lÑ2CÔ2CÐ0CÔ#DÒDÐDð !˜5˜5˜5˜5˜5˜5à),�˜vÑ&Ø����øøøøÝð ð ð Ø),�˜vÑ&Ø����øøøøðøøøð ð 6ð 6�ð6Ý ¨$Ñ/Ô/�A�AøÝ%ð 6ð 6ð 6Ø  Ô(×/Ò/°Ñ5Ô5Ð5Ð5Ð5ð6øøøð6ð
 ð 	˜\ð 	Ø7;´zÐ"ZÐ"3 d¤jÐ"3Ð"3Ð"3Ð"3ÐGZÐðMÐ*=ð Mð Mð Mð ð  .×3Ò3Ñ5Ô5ð ]ð ]‘�˜Ø˜2Ÿ7š7Ð#[°6Ð#[Ð#[ÐTWÐ#[Ð#[Ñ\Ô\Ñ\��Ø!-×!3Ò!3Ñ!5Ô!5ð dð d‘�˜Ý:=¸e¹*¼*Èº/¸/Ð6¨5°¬8Ð6Ð6Ð6Ð6ÐOdÐ]bÐOdÐOd�
Ø˜2Ÿ7š7Ð#b¨FÐ#bÐ#bÀJÐ#bÐ#bÐ#bÑcÔcÑc��ÝŒM˜'Õ,KÐLÑLÔLÐLØ�5Øˆts<   »AÁ
CÁA
B5Â+B5Â5CÃCÃCÃC*Ã*%DÄD)r   r    r!   r"   r#   r   rL   r   r   r   r   r`   r   r6   Ú__classcell__©rz   s   @r   rf   rf   ñ   sï   ø€ € € € € ðOð Oð €Ið #Ø59Ø!%Ø!ð#Qð #Qð #Qà" 3¨¨S°(¸3´-Ð-?Ô(@À(È3Ä-Ð#OÔPð#Qð �sŒmð	#Qð
 ˜8 T F¨D LÔ1Ô2ð#Qð ˜#”ð#Qð �cŒ]ð#Qð #Qð #Qð #Qð #Qð #QðJ'ð 'ð 'ð 'ð 'ð 'ð 'r   rf   c                   ó¬   ‡ — e Zd ZdZdZdddddœdeeee         f         dee         dee	e
gdf                  dee         d	ee         f
ˆ fd
„Zd„ Zˆ xZS )ÚLazySubprocessTesterz¢A lazy checker that a command-line tool is available.  The command will only be run once, at
    the point that this object is checked for its Boolean value.
    )Ú_commandNr-   Úcommandr.   r/   r0   r1   c                óð   •— t          |t          ¦  «        r|fnt          |¦  «        | _        | j        st	          d¦  «        ‚t          ¦   «                              |p| j        d         |||¬¦  «         dS )zß
        Args:
            command: the strings that make up the command to be run.  For example,
                ``["pdflatex", "-version"]``.

        Raises:
            ValueError: if an empty command is given.
        zno command suppliedr   r-   N)rK   rL   rk   rš   rt   rx   r   )r   r›   r.   r/   r0   r1   rz   s         €r   r   zLazySubprocessTester.__init__M  su   ø€ õ" '1°½#Ñ&>Ô&>ÐR˜˜
˜
ÅEÈ'ÁNÄNˆŒØŒ}ð 	4ÝÐ2Ñ3Ô3Ð3Ý‰Œ×Ò˜dÐ6 d¤m°AÔ&6ÀÐSZÐ`cÐÑdÔdÐdÐdÐdr   c                 ó¤   — 	 t          j        | j        dt           j        t           j        ¬¦  «         dS # t          t           j        f$ r Y dS w xY w)NT)ÚcheckÚstdoutÚstderrF)Ú
subprocessÚrunrš   ÚDEVNULLÚOSErrorÚSubprocessErrorr5   s    r   r6   z"LazySubprocessTester._is_availablec  sd   € ð	ÝŒNØ” Tµ*Ô2DÍZÔM_ðñ ô ð ð �4øõ �Ô3Ð4ð 	ð 	ð 	Ø�5�5ð	øøøs   ‚15 µAÁA)r   r    r!   r"   r#   r   rL   r   r   r   r`   r   r6   r–   r—   s   @r   r™   r™   F  sÜ   ø€ € € € € ðð ð €Ið #Ø59Ø!%Ø!ðeð eð eà�s˜H SœMÐ)Ô*ðeð �sŒmð	eð
 ˜8 T F¨D LÔ1Ô2ðeð ˜#”ðeð �cŒ]ðeð eð eð eð eð eð,ð ð ð ð ð ð r   r™   )r"   r^   r€   rc   rG   rƒ   r¡   ra   rŠ   r   r   r   r   r   r   Úqiskit.exceptionsr	   r
   Ú
classtoolsr   r   ÚABCr&   rf   r™   r$   r   r   ú<module>r©      s„  ðð *Ð )à 
€
€
€
Ø Ð Ð Ð Ø Ð Ð Ð Ø Ð Ð Ð Ø Ð Ð Ð Ø Ð Ð Ð Ø €€€Ø €€€Ø BÐ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BÐ Bà ZÐ ZÐ ZÐ ZÐ ZÐ ZÐ ZÐ ZØ #Ð #Ð #Ð #Ð #Ð #ð0ð 0ð 0ð 0ð 0ñ 0ô 0ð 0ðB"ð B"ð B"ð B"ð B"˜CœGñ B"ô B"ð B"ðJRð Rð Rð Rð RÐ,ñ Rô Rð Rðj%ð %ð %ð %ð %Ð0ñ %ô %ð %ð %ð %r   