
    z	i*                    j   S r SSKJr  SSKrSSKrSSKrSSKJr  SSK	J
r
Jr  SSKJr  SSKJrJr  SS	KJr  \S
\S0r\R(                  R*                  \R,                  R*                  SS j5       5       rS r\R(                  R*                  \R,                  R*                  SS j5       5       rS rS rg)z
Visualization function for a pass manager. Passes are grouped based on their
flow controller, and coloured based on the type of pass.
    )annotationsN)	optionals)BaseControllerGenericPass)FlowControllerLinear)AnalysisPassTransformationPass   )VisualizationErrorredbluec                   SSK nU(       d  [        nUR                  5       nSnSn[        U R	                  5       R
                  5       H&  u  p[        XX'U5      u  pnUR                  U
5        M(     [        XSU5      nU$ )aQ  
Draws the pass manager.

This function needs `pydot <https://github.com/pydot/pydot>`__, which in turn needs
`Graphviz <https://www.graphviz.org/>`__ to be installed.

Args:
    pass_manager (PassManager): the pass manager to be drawn
    filename (str): file path to save image to
    style (dict or OrderedDict): keys are the pass classes and the values are
        the colors to make them. An example can be seen in the DEFAULT_STYLE. An ordered
        dict can be used to ensure a priority coloring when pass falls into multiple
        categories. Any values not included in the provided dict will be filled in from
        the default dict
    raw (Bool) : True if you want to save the raw Dot output not an image. The
        default is False.
Returns:
    PIL.Image or None: an in-memory representation of the pass manager. Or None if
    no image was generated or PIL is not installed.
Raises:
    MissingOptionalLibraryError: when nxpd or pydot not installed.
    VisualizationError: If raw=True and filename=None.

Example:
    .. plot::
        :include-source:
        :nofigs:

        from qiskit import QuantumCircuit
        from qiskit.transpiler import generate_preset_pass_manager
        from qiskit.visualization import pass_manager_drawer

        pm = generate_preset_pass_manager(optimization_level=0)
        pass_manager_drawer(pm)
r   N)	pydotDEFAULT_STYLEDot	enumerateto_flow_controllertasksdraw_subgraphadd_subgraphmake_output)pass_managerfilenamestylerawr   graphcomponent_id	prev_nodeindexcontroller_groupsubgraphoutputs               i/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/visualization/pass_manager_visualization.pypass_manager_drawerr$       s    L  IIKE
 LI#,\-L-L-N-T-T#U,9Ee-
)	 	8$	 $V X.FM    c                    UR                  5        H  u  p#[        X5      (       d  M  Us  $    [        R                  5        H  u  p#[        X5      (       d  M  Us  $    g)Nblack)items
isinstancer   )pssr   typcolors       r#   _get_node_colorr-   _   sP    kkm
cL $
 $))+
cL , r%   c           	        SSK n[        [        S U R                  5      5      nU(       d  [        nUR                  5       nSnSnSn	U H  n
[        X
5      nUc  M  UR                  [        U
5      S[        U
5      SS9nUR                  5       R                   H)  n[        XX)U5      u  pn	UR                  U5        US-  nM+     UR                  U5        M     [        XcU5      nU$ )a  
Draws the staged pass manager.

    This function needs `pydot <https://github.com/erocarrera/pydot>`__, which in turn needs
`Graphviz <https://www.graphviz.org/>`__ to be installed.

Args:
    pass_manager (StagedPassManager): the staged pass manager to be drawn
    filename (str): file path to save image to
    style (dict or OrderedDict): keys are the pass classes and the values are
        the colors to make them. An example can be seen in the DEFAULT_STYLE. An ordered
        dict can be used to ensure a priority coloring when pass falls into multiple
        categories. Any values not included in the provided dict will be filled in from
        the default dict
    raw (Bool) : True if you want to save the raw Dot output not an image. The
        default is False.
Returns:
    PIL.Image or None: an in-memory representation of the pass manager. Or None if
    no image was generated or PIL is not installed.
Raises:
    MissingOptionalLibraryError: when nxpd or pydot not installed.
    VisualizationError: If raw=True and filename=None.

Example:
    .. plot::
       :include-source:
       :nofigs:

        %matplotlib inline
        from qiskit.providers.fake_provider import GenericBackendV2
        from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager

        pass_manager = generate_preset_pass_manager(3, GenericBackendV2(num_qubits=5))
        pass_manager.draw()
r   Nc                
    U S L$ )N )ss    r#   <lambda>,staged_pass_manager_drawer.<locals>.<lambda>   s    1D=r%   	helveticalfontnamelabel	labeljustr
   )r   listfilterexpanded_stagesr   r   getattrClusterstrr   r   r   r   r   )r   r   r   r   r   stagesr   r   idxr   ststage
stagegraphr    r!   r"   s                   r#   staged_pass_manager_drawerrE   m   s    L  &0,2N2NOPF IIKE
 L CI)s2wCPRG_bcJ$)$<$<$>$D$D 4A$Ec51	 ''1q %E z*  X.FMr%   c                (   SSK nSU S3n[        U [        5      (       a.  [        U [        5      (       d  X`R                  R
                   -  nUR                  [        U5      SUSS9nUS-  n[        U [        5      (       a  [        U S	/ 5      nO;[        U [        5      (       a  U /nO"[        U [        [        45      (       a  U nOXqU4$ / n	U HF  n
[        U
[        5      (       a  U	R                  U
R                  5        M5  U	R                  U
5        MH     U	 GHo  n
[        U
[        5      (       a  UR                  [        U5      S
SSSS9nOAUR                  [        U5      [!        X5      S[        [#        U
5      R
                  5      SS9nUR%                  U5        US-  n[&        R(                  " U
R*                  5      nUS   SS nUS   (       a  [-        US   5      OSn[/        U5       Hh  u  nnSnU[-        U5      U-
  :  a  SnUR                  USSSUSUS9nUR%                  U5        US-  nUR1                  UR3                  UU5      5        Mj     U(       a   UR1                  UR3                  X;5      5        UnGMr     XqU4$ )zDraw subgraph.r   N[z] r4   r5   r6   r
   r   kzNested flow controller	rectangle)r,   r7   r8   shape   soliddashedr'   
   ellipse)r,   r7   fontsizer8   rJ   r   )r   r)   r   r   	__class____name__r>   r?   r=   r   r:   tupleextendr   appendNoder-   typeadd_nodeinspectgetfullargspec__init__lenr   add_edgeEdge)r    r   r   r   rA   r   r8   r!   r   flatten_taskstasknodearg_specargsnum_optional	arg_indexargnd_style
input_nodes                      r#   r   r      s    uBKE"N33J.= = 	..7789 }}S.E]`}aHAL"N33 ('26	$k	2	2!"	$tUm	4	4  y00M
 d011  ,  &  dN++ ::L!$.!  D ::L!%d2$$t*--.!  D 	$ ))$--8{12+3A;s8A;'A (oNIsH SY56#$ $ J j)ALejjT:;% .* ejj9:	q t 9,,r%   c                B   U(       a#  U(       a  U R                  USS9  g[        S5      e[        R                  (       d  U(       a  U R	                  U5        g[        R                  R                  S5        [        R                  " 5        nSSKJ	n  [        R                  R                  US5      nU R	                  U5        UR                  U5      n[        R                  " U5        U(       a  UR                  US	5        UsSSS5        $ ! , (       d  f       g= f)
z Produce output for pass_manager.r   )formatNz+if format=raw, then a filename is required.zpass manager drawerr   )Imagezpass_manager.pngPNG)writer   
_optionalsHAS_PIL	write_pngrequire_nowtempfileTemporaryDirectoryPILrk   ospathjoinopenremovesave)r   r   r   
tmpdirnamerk   tmppathimages          r#   r   r     s    
KKK/$%RSS(!""#89		$	$	&*'',,z+=> 	 

7#
		'JJx' 
'	&	&s   A9D
D)NNF)__doc__
__future__r   ru   rY   rr   qiskit.utilsr   rn   qiskit.passmanager.base_tasksr   r   #qiskit.passmanager.flow_controllersr   qiskit.transpiler.basepassesr   r	   
exceptionsr   r   HAS_GRAPHVIZrequire_in_call	HAS_PYDOTr$   r-   rE   r   r   r0   r%   r#   <module>r      s    # 	   0 E D I *u&8&A ((%%: & ):z ((%%G & )GT`-Fr%   