+
    i.                       R t ^ RIHt ^ RIHt ^ RIHt ^ RIHtH	t	H
t
Ht . ROt]! R	4      t]! R
RR./]3R7      t]'       dA   ]'       d9   ]P                   t]P"                  P$                  t]P&                  P(                  t ! R R4      tR t/ 3R ltRR lt ! R R]	4      t ! R R]	4      t ! R R]4      tRR ltR# )a  Matplotlib based plotting of quantum circuits.

Todo:

* Optimize printing of large circuits.
* Get this to work with single gates.
* Do a better job checking the form of circuits to make sure it is a Mul of
  Gates.
* Get multi-target gates plotting.
* Get initial and final states to plot.
* Get measurements to plot. Might need to rethink measurement as a gate
  issue.
* Get scale and figsize to be handled in a better way.
* Write some tests/examples!
)annotations)Mul)import_module)GateOneQubitGateCGateCGateSCircuitPlotMzMxCreateOneQubitGatenumpy
matplotlibfromlistpyplot)import_kwargscatchc                      ] tR t^/t$ RtRtRtRtRtRt	Rt
. tR]R&   / tR]R	&   R
tR tR tR tR tR tR tR tR tR tR tR tR tR tR tR tR tRtR# )r	   z$A class for managing a circuit plot.g      ?g      4@g?g333333?z	list[str]labelszdict[str, str]inits      ?c                	n   \         '       d   \        '       g   \        R 4      hWn        \	        V P                  P
                  4      V n        W n        V P                  V4       V P                  4        V P                  4        V P                  4        V P                  4        V P                  4        R# )z"numpy or matplotlib not available.N)npr   ImportErrorcircuitlenargsngatesnqubitsupdate_create_grid_create_figure_plot_wires_plot_gates_finish)selfcr   kwargss   &&&,ځ/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/site-packages/sympy/physics/quantum/circuitplot.py__init__CircuitPlot.__init__<   s|    rBCC$,,++,F    c                <    V P                   P                  V4       R# )z'Load the kwargs into the instance dict.N)__dict__r   )r%   r'   s   &&r(   r   CircuitPlot.updateI   s    V$r+   c                    V P                   p\        P                  RV P                  V,          V\        R7      p\        P                  RV P
                  V,          V\        R7      pW n        W0n        R# )zCreate the grid of wires.g        )dtypeN)scaler   aranger   floatr   
_wire_grid
_gate_grid)r%   r1   	wire_grid	gate_grids   &   r(   r    CircuitPlot._create_gridM   sS    

IIc4<<#5uEIJ	IIc4;;u#4e5II	##r+   c                j   \         P                  V P                  V P                  ,          V P                  V P                  ,          3RRR7      V n        V P
                  P                  ^^^RR7      pVP                  4        RV P                  ,          pVP                  V P                  ^ ,          V,
          V P                  R,          V,           4       VP                  V P                  ^ ,          V,
          V P                  R,          V,           4       VP                  R4       Wn        R# )	z"Create the main matplotlib figure.w)figsize	facecolor	edgecolorT)frameonr   equalN)r   figurer   r1   r   _figureadd_subplotset_axis_offset_xlimr5   set_ylimr4   
set_aspect_axes)r%   axoffsets   &  r(   r!   CircuitPlot._create_figureU   s    }}[[+T\\$**-DE % 

 \\%%q! & 
 	TZZ
DOOA&/1Dv1MN
DOOA&/1Dv1MN
g
r+   c                *   V P                   ^ ,          pV P                   R,          pWP                  ,
          W P                  ,           3p\        V P                  4       EF  pV P                  V,          V P                  V,          3p\        W5RV P                  R7      pV P                  P                  V4       V P                  '       g   Kq  ^ pV P                  P                  V P                  V,          4      '       d   RpV P                  P                  V^ ,          V P                  ,
          V,
          V^ ,          \        V P                  V,          V P                  4      V P                  RRRR7       EK!  	  V P!                  4        R# )z&Plot the wires of the circuit diagram.kcolorlwg      ?center)sizerO   havaNr@   )r5   r1   ranger   r4   Line2D	linewidthrH   add_liner   r   gettextlabel_bufferrender_labelfontsize_plot_measured_wires)r%   xstartxstopxdataiydatalineinit_label_buffers   &       r(   r"   CircuitPlot._plot_wiresg   s$   ##**$ejj&89t||$A__Q');<E>>D
 JJ%{{{$%!::>>$++a.11t3D

!HT.../@@q Q

;X	   7 %  	!!#r+   c           	     	   V P                  4       pV P                  R,          pRpV F  pV P                  W,          ,          W P                  ,           3pV P                  V,          V,           V P                  V,          V,           3p\	        WVRV P
                  R7      pV P                  P                  V4       K  	  \        V P                  4       4       EF   w  r\        V	\        \        34      '       g   K$  V	P                  V	P                  ,           p
V
 F  pW9   g   K  V P                  V,          V P                  W,          ,          8  g   K<  \        V
4      \!        V
4      3pV P                  V,          V,
          V P                  V,          V,
          3p\	        WVRV P
                  R7      pV P                  P                  V4       K  	  EK  	  R# )   g{Gz?rM   rN   Nr@   )_measurementsr5   r1   r4   rV   rW   rH   rX   	enumerate_gates
isinstancer   r   controlstargetsminmax)r%   
ismeasuredr`   dyimra   rc   rd   rb   gwireswires   &           r(   r^    CircuitPlot._plot_measured_wires~   sf   '')
#B__Z^4U::5EFE__R(+DOOB,?,BCE>>D
 JJ%  T[[]+CA!eV_--

QYY.!D)??1-
@P0QQ #E
CJ 6 $ 22 5tq7I"7L L%!"%#~~ 
 

++D1 " ,r+   c                b   . p\        V P                  \        4      '       dR   \        V P                  P                  4       F,  p\        V\
        4      '       g   K  VP                  V4       K.  	  V# \        V P                  \
        4      '       d   VP                  V P                  4       V# )z/Create a list of all gates in the circuit plot.)rl   r   r   reversedr   r   append)r%   gatesrt   s   &  r(   rk   CircuitPlot._gates   sx    dllC((dll//0a&&LLO 1
  d++LL&r+   c                l    \        V P                  4       4       F  w  rVP                  W4       K  	  R# )z0Iterate through the gates and plot each of them.N)rj   rk   	plot_gate)r%   rb   gates   &  r(   r#   CircuitPlot._plot_gates   s%     /GANN4# 0r+   c                    / p\        V P                  4       4       FL  w  r#\        VRR4      '       g   K  VP                   F"  pWA9   d   W,          V8  d   W!V&   K  K  W!V&   K$  	  KN  	  V# )zReturn a dict ``{i:j}`` where i is the index of the wire that has
been measured, and j is the gate where the wire is measured.
measurementF)rj   rk   getattrrn   )r%   rq   rb   rt   targets   &    r(   ri   CircuitPlot._measurements   sm     
T[[]+CAqu--iiF+%-112v. 2 ./6* ( , r+   c                	j    V P                   P                  4        F  pVP                  R 4       K  	  R# )FN)rB   findobjset_clip_on)r%   os   & r(   r$   CircuitPlot._finish   s%    %%'AMM%  (r+   c                    V P                   V,          pV P                  V,          pV P                  P                  WEVRRRRRRRRRRV P                  /V P
                  R	7       R
# )z#Draw a box for a single qubit gate.rM   rQ   ecfcr:   fillTrP   )rO   rS   rT   bboxrR   N)r5   r4   rH   rZ   rW   r]   )r%   tgate_idxwire_idxxys   &&&&  r(   one_qubit_boxCircuitPlot.one_qubit_box   s`    OOH%OOH%

!T3dDNNK 	 	
r+   c                Z    \        V P                  4       \        V P                  4       R# )z<Draw a box for a two qubit gate. Does not work yet.
        N)printr5   r4   )r%   r   r   r   s   &&&&r(   two_qubit_boxCircuitPlot.two_qubit_box   s    
 	doodoor+   c                   V P                   V,          V P                   V,          3pV P                  V,          V P                  V,          3p\        WERV P                  R7      pV P                  P                  V4       R# )zDraw a vertical control line.rM   rN   N)r5   r4   rV   rW   rH   rX   )r%   r   min_wiremax_wirera   rc   rd   s   &&&&   r(   control_lineCircuitPlot.control_line   se    *DOOH,EF*DOOH,EF~~

 	

D!r+   c           	         V P                   V,          pV P                  V,          pV P                  p\        W43WPP                  ,          RRRV P
                  R7      pV P                  P                  V4       R# )zDraw a control point.rM   Tr   r   r   rP   N)r5   r4   control_radiusCircler1   rW   rH   	add_patch)r%   r   r   r   r   radiusr&   s   &&&    r(   control_pointCircuitPlot.control_point   sf    OOH%OOH%$$F::~~
 	

Qr+   c           	     ^   V P                   V,          pV P                  V,          pV P                  p\        W43VRRRV P                  R7      pV P
                  P                  V4       \        W33WE,
          WE,           3RV P                  R7      pV P
                  P                  V4       R# )z7Draw a NOT gates as the circle with plus in the middle.rM   r:   Fr   rN   N)	r5   r4   
not_radiusr   rW   rH   r   rV   rX   )r%   r   r   r   r   r   r&   ls   &&&     r(   	not_pointCircuitPlot.not_point   s    OOH%OOH%F~~
 	

QFQZ,~~

 	

Ar+   c                   V P                   V,          pV P                  V,          pV P                  p\        W5,
          W5,           3WE,
          WE,           3RV P                  R7      p\        W5,
          W5,           3WE,           WE,
          3RV P                  R7      pV P
                  P                  V4       V P
                  P                  V4       R# )zDraw a swap point as a cross.rM   rN   N)r5   r4   
swap_deltarV   rW   rH   rX   )r%   r   r   r   r   dl1l2s   &&&     r(   
swap_pointCircuitPlot.swap_point  s    OOH%OOH%OOUAENUAEN~~	
 UAENUAEN~~	
 	

B

Br+   )rH   rB   r5   r4   r   r   r   N) __name__
__module____qualname____firstlineno____doc__r1   r]   rW   r   r   r   r   __annotations__r   r[   r)   r   r    r!   r"   r^   rk   r#   ri   r$   r   r   r   r   r   r   __static_attributes__ r+   r(   r	   r	   /   s    .EHINJJFIE>L%$$$.2:	$
!

"	" * r+   c                    \        W3/ VB # )a%  Draw the circuit diagram for the circuit with nqubits.

Parameters
==========

c : circuit
    The circuit to plot. Should be a product of Gate instances.
nqubits : int
    The number of qubits to include in the circuit. Must be at least
    as big as the largest ``min_qubits`` of the gates.
)r	   )r&   r   r'   s   &&,r(   circuit_plotr      s     q,V,,r+   c                \    VP                  V 4      pV'       d   RV : RV: R2# RV ,          # )zSlightly more flexible way to render labels.

>>> from sympy.physics.quantum.circuitplot import render_label
>>> render_label('q0')
'$\\left|q0\\right\\rangle$'
>>> render_label('q0', {'q0':'0'})
'$\\left|q0\\right\\rangle=\\left|0\\right\\rangle$'
z$\left|z\right\rangle=\left|z\right\rangle$z$\left|%s\right\rangle$)rY   )labelr   inits   && r(   r\   r\   .  s-     99UDtCH$OO%--r+   c                n    \        V 4       Uu. uF  pRWV,
          ^,
          3,          NK  	  up# u upi )aX  Autogenerate labels for wires of quantum circuits.

Parameters
==========

n : int
    number of qubits in the circuit.
symbol : string
    A character string to precede all gate labels. E.g. 'q_0', 'q_1', etc.

>>> from sympy.physics.quantum.circuitplot import labeller
>>> labeller(2)
['q_1', 'q_0']
>>> labeller(3,'j')
['j_2', 'j_1', 'j_0']
z%s_%d)rU   )nsymbolrb   s   && r(   labellerr   <  s2    " /4Ah7hGvc!en$$h777s   !2c                  &    ] tR tRtRtRtR tRtRtR# )r
   iO  z~Mock-up of a z measurement gate.

This is in circuitplot rather than gate.py because it's not a real
gate, it just draws one.
TM_zr   N	r   r   r   r   r   r   	gate_namegate_name_latexr   r   r+   r(   r
   r
   O      
 KIOr+   c                  &    ] tR tRtRtRtR tRtRtR# )r   iY  zMock-up of an x measurement gate.

This is in circuitplot rather than gate.py because it's not a real
gate, it just draws one.
TM_xr   Nr   r   r+   r(   r   r   Y  r   r+   c                       ] tR tRtRR ltRtR# )r   ic  Nc                	P    V'       g   Tp\        VR ,           \        3RVRV/4      # )r   r   r   )typer   )mclname	latexnames   &&&r(   __new__CreateOneQubitGate.__new__d  s.    ID6ML?$ 19=? 	?r+   r   N)r   r   r   r   r   r   r   r+   r(   r   r   c  s    ? ?r+   Nc                >   a V'       g   T p\        W4      oV3R lpV# )z5Use a lexical closure to make a controlled gate.
    c                :   < \        \        V 4      S! V4      4      # r   )r   tuple)ctrlsr   onequbitgates   &&r(   ControlledGate#CreateCGate.<locals>.ControlledGatep  s    U5\,v"677r+   )r   )r   r   r   r   s   && @r(   CreateCGater   j  s"     	%d6L8r+   )r	   r   r   r
   r   r   r   )qr   )r   
__future__r   sympy.core.mulr   sympy.externalr   sympy.physics.quantum.gater   r   r   r   __all__r   RuntimeErrorr   r   linesrV   patchesr   r	   r   r\   r   r
   r   r   r   r   r   r+   r(   <module>r      s     #  ( H H 7hZ 8/
 *F$$F&&F
o  o b- ! .8&  ? ?r+   