
    z	i:4                     ,   S r SSKrSSKrSSKJr  SSKJrJrJr  SSK	J
r
  SSKJrJrJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJrJr  1 Skr\R@                  RB                  \RD                  RB                     SS j5       5       r#g)z8
Visualization function for DAG circuit representation.
    N)graphviz_draw)	DAGOpNode	DAGInNode
DAGOutNode)
DAGCircuit)QubitClbitClassicalRegister)expr)dagdependency_to_circuit)	optionals)InvalidFileError   )VisualizationError)
load_style)DAGStyleDictDAGDefaultStyle>$   gdmppsdiadotfiggd2gifjpejpgmifpclpdfpicpngps2svgvmlvtxcmaphpglimapjpegsvgzvmlzvrmlwbmpxdorxlibcanoncmapxismapplainimap_npcmapx_np	plain-extc                   ^ ^^^^^^^ SSK Jn  [        [        T 5      5      m[	        T R
                  R                  5       5      [	        T R                  R                  5       5      -    VVVs0 s H*  n[        U5        H  u  pgXuR                   SU S3_M     M,     snnnm0 n[        T[        5      (       a!  S H  n	U	T;   d  M  [        TU	   5      X'   M     [        T[        [        5       SSSS	9u  mn
S
T;   a  SSKJm  [        T R"                  5       VVs0 s H  u  pX{_M	     snnm[        T R$                  5       VVs0 s H  u  pX{_M	     snnmUR'                  S[        SU-  5      05        [)        T 5      mUUUUUUU4S jnSnO/UR'                  S[        SU-  5      05        U UU4S jnU UU4S jnSnU(       aD  SU;  a  [+        S5      eUR-                  S5      S   nU[.        ;  a  [1        SU S[.         35      e[        T [2        5      (       a  T R5                  UUU5      nSnU(       d^  [6        R8                  " USU/UR;                  S5      SSSSS9n[<        R>                  " UR@                  5      nURC                  U5      nU$ [6        R8                  " USUSU/USSSS9  g[E        T RF                  UUUUU5      $ s  snnnf s  snnf s  snnf ) a	  Plot the directed acyclic graph (dag) to represent operation dependencies
in a quantum circuit.

This function calls the :func:`~rustworkx.visualization.graphviz_draw` function from the
``rustworkx`` package to draw the DAG.

Args:
    dag (DAGCircuit or DAGDependency): The dag to draw.
    scale (float): scaling factor
    filename (str): file path to save image to (format inferred from name)
    style (dict or str): Style name, file name of style JSON file, or a
        dictionary specifying the style.

        * The supported style names are 'plain': B&W graph, 'color' (default):
            (color input/output/op nodes)
        * If given a JSON file, e.g. ``my_style.json`` or ``my_style`` (the ``.json``
            extension may be omitted), this function attempts to load the style dictionary
            from that location. Note, that the JSON file must completely specify the
            visualization specifications. The file is searched for in
            ``qiskit/visualization/circuit/styles``, the current working directory, and
            the location specified in ``~/.qiskit/settings.conf``.
        * If ``None`` the default style ``"color"`` is used or, if given, the default style
            specified in ``~/.qiskit/settings.conf``.

Returns:
    PIL.Image: if in Jupyter notebook and not saving to file,
        otherwise None.

Raises:
    VisualizationError: when style is not recognized.
    InvalidFileError: when filename provided is not valid
    ValueError: If the file extension for ``filename`` is not an image
        type supported by Graphviz.

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

        from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
        from qiskit.converters import circuit_to_dag
        from qiskit.visualization import dag_drawer

        q = QuantumRegister(3, 'q')
        c = ClassicalRegister(3, 'c')
        circ = QuantumCircuit(q, c)
        circ.h(q[0])
        circ.cx(q[0], q[1])
        circ.measure(q[0], c[0])
        with circ.if_test((c, 2)):
            circ.rz(0.5, q[1])

        dag = circuit_to_dag(circ)

        style = {
            "inputnodecolor": "pink",
            "outputnodecolor": "lightblue",
            "opnodecolor": "red",
        }

        dag_drawer(dag, style=style)
r   )Image[])fontsizebgcolordpipadcircuit_graphviz_stylecircuit_graphviz_style_pathT)
style_dictdefault_styleuser_config_optuser_config_path_optraise_error_if_not_foundDAGDependency)get_bit_reg_indexr>   d   c           	      (  > ST;   a  [        U R                  5      nO[        U R                  5      n0 n/ n[        U R                  U R
                  -   5       H  u  pEUS:  a  UR                  S5          Oo[        U[        5      (       a	  STU    3nO+[        U[        5      (       a	  STU    3nOUR                   nTR                  XV5      nUR                  U5        M     US-   [        U R                  5      -   S-   [        U5      SS	 R                  S
S5      -   S-   US'   [        U R                  SS 5      (       Ga   U R                  R                   n[        U["        R$                  5      (       a  Sn	O[        US   [&        5      (       a#  SUS   R                   S[)        US   5       S3n	OJT" TUS   5      u  pnU
b#  SU
R                   SU S[)        US   5       S3n	OSU S[)        US   5       S3n	US-   [        U R                  5      -   U	-   [        U5      SS	 R                  S
S5      -   S-   US'   [        T[*        5      (       a  SUS'   ST;   a  TS   US'   ST;   a  [        TS   5      US'   U R                  S:X  a
  TS   US'   U$ [        U R                  SS5      (       a
  TS   US'   U$ [        U R                  SS 5      (       a
  TS    US'   U$ U R                  S!:X  a  TS"   US'   U$ [-        S#T S$35      e)%NDAGDependencyV2   z...q_c_z: z (r   ' )label	conditionz (cond: [Expr]) (r   z (cond: z, z) (r:   z], filledstyle	nodecolor	fillcolorr<   barrierbarriercolor
_directiveFdirectivecolorconditioncolormeasuremeasurecolorzUnrecognized style z for the dag_drawer.)str_node_idnode_id	enumerateqargscargsappend
isinstancer   r	   indexgetnamereplacegetattroprT   r   Exprr
   intdictr   )nodenid_strnargscountargf_strarg_strrT   cond_txtregister	bit_index	reg_indexclbit_indicesdag_dep_circrH   qubit_indicesregister_bit_labelsrV   type_strs                `/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/visualization/dag_visualization.pynode_attr_func"dag_drawer.<locals>.node_attr_func   sA    H,dmm,dll+AD'

TZZ(?@
19KK&c5)) s!3 45EU++ s!3 45E"yykE-11#=G$ A $TYY/$6T1R9P9PQTVX9YY\__ gJ twwT22 GG--	i332H	!.?@@!))A,*;*;)<Bs9Q<?P>QQTUH5F|U^_`Ua5b2H+%-hmm_Ai[CPYZ[P\L]K^^a#b%-i[3y|;L:MS#Q$))n%  $i"o--c26	7
  '
 %&&%'
%'%*;%7AkN&$'j(9$:AjM99	)%*>%:AkN  TWWlE::%*+;%<AkN  TWWk488%*+;%<AkN  YY)+%*>%:AkN(+>ugEY)Z[[    Nc                   > 0 n[        U [        5      (       a  U R                  US'   [        U [        5      (       a  [        U R                  [
        5      (       aC  TR                  U R                  STR                  U R                  5      R                   35      nO[        U R                  [        5      (       aC  TR                  U R                  STR                  U R                  5      R                   35      nO[        U R                  R                  5      nX!S'   [        U [        5      (       a  [        U R                  [
        5      (       aD  TR                  U R                  STR                  U R                  5      R                   S35      nO[        U R                  [        5      (       aD  TR                  U R                  STR                  U R                  5      R                   S35      nO[        U R                  R                  5      nX!S'   [        T[        5      (       a  SUS'   S	T;   a  TS	   US
'   ST;   a  [        TS   5      US'   [        U [        5      (       a  ST;   a  TS   US
'   ST;   a  TS   US'   [        U [        5      (       a  ST;   a  TS   US
'   ST;   a  TS   US'   [        U [        5      (       a  ST;   a  TS   US
'   ST;   a  TS   US'   U$ [        ST 35      e)NrS   rM   rN   zq[r;   zc[rU   rV   rW   rX   r<   inputnodecolorinputnodefontcolor	fontcoloroutputnodecoloroutputnodefontcoloropnodecoloropnodefontcolorzInvalid style )rg   r   rj   r   wirer   ri   find_bitrh   r	   r`   r   rp   r   )rq   rs   rS   dagr   rV   s      r   r   r     s   A$	**!YY'
$	**dii///33DIICLLQUQZQZD[DaDaCb?cdE		511/33DIICLLQUQZQZD[DaDaCb?cdE		/E"'
$
++dii///33		RTYY(?(E(E'Fa#HE  		511/33		RTYY(?(E(E'Fa#HE  		/E"'
%&&%'
%'%*;%7AkN&$'j(9$:AjMdI..'50)./?)@++u4)./C)D+dJ//(E1)./@)A+,5)./D)E+dI..$-).})=+(E1)./@)A+(>%)ABBr   c                 R  > 0 n[        U [        5      (       a/  TR                  U STR                  U 5      R                   35      nOY[        U [
        5      (       a/  TR                  U STR                  U 5      R                   35      nO[        U R                  5      nX!S'   [        T[        5      (       am  ST;   a  TS   US'   ST;   a  [        TS   5      US'   [        U [        5      (       a  ST;   a  TS   US'   [        U [
        5      (       a  ST;   a  TS   US'   U$ U$ )	NrM   rN   rS   	edgecolorcolorr<   qubitedgecolorclbitedgecolor)	rg   r   ri   r   rh   r	   r`   rj   rp   )edgeerS   r   r   rV   s      r   edge_attr_func"dag_drawer.<locals>.edge_attr_func8  s   A$&&+//3<<;M;S;S:T6UVD%((+//3<<;M;S;S:T6UVDIIgJ%&&%'!&{!3AgJ&$'j(9$:AjMdE**'50%*+;%<'
dE**'50%*+;%<'
Hr   r"   .z7Parameter 'filename' must be in format 'name.extension'rO   z2The specified value for the image_type argument, 'z,' is not a valid choice. It must be one of: r   z-Tzutf-8F)inputcapture_outputencodingchecktextz-outf8)r   r   r   r   )$PILr9   r`   typelistqregsvaluescregsrc   rj   rg   rp   r   r   r   #qiskit.visualization.circuit._utilsrH   qubitsclbitsupdater   r   splitIMAGE_TYPES
ValueErrorr   _to_dot
subprocessrunencodeioBytesIOstdoutopenr   _multi_graph)r   scalefilenamerV   r9   regidxbitgraph_attrsattr_rh   r   r   
image_typedot_strprog
dot_resultdot_bytes_imageimager}   r~   rH   r   r   r   s   `  `                @@@@@@r   
dag_drawerr   M   s   N 
 49~H 		((*+d3993C3C3E.FFFC#C.JS 	z3%q!!( 	F K%9Du}$'d$4! : %'0:!%HE1 ("I6?

6KL6K
6KL6?

6KL6K
6KLE3sU{#345/4A	\ A	\F  	E3sU{#3455	Cn	6 Jh"#\]]^^C(,
[(<K-!  #z""++
 #tZ(nnW-#J !jj):):;OJJ/ELNNtZx8  
 	
I2 MLs   .1KKK")gffffff?Nr   )$__doc__r   r   rustworkx.visualizationr   qiskit.dagcircuit.dagnoder   r   r   qiskit.dagcircuit.dagcircuitr   qiskit.circuitr   r	   r
   qiskit.circuit.classicalr   qiskit.convertersr   qiskit.utilsr   
_optionalsqiskit.exceptionsr   
exceptionsr   rV   r   dag.dagstyler   r   r   HAS_GRAPHVIZrequire_in_callHAS_PILr    r   r   <module>r      s    
  1 F F 3 : : ) 6 0 . *  7%P ((## 
	v
 $ )v
r   