K i.hdZddlmZddlmZddlmZddlmZm Z m Z m Z gdZ edZ edd d gief Ze r:er8ej Zej"j$Zej&j(ZGd d ZdZifdZddZGdde ZGdde ZGddeZddZy)aMatplotlib 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 OneQubitGateCGateCGateS) CircuitPlot circuit_plotlabellerMzMxCreateOneQubitGate CreateCGatenumpy matplotlibfromlistpyplot) import_kwargscatchceZdZUdZdZdZdZdZdZdZ gZ de d<iZ de d <d Z d Zd Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZy)r z$A class for managing a circuit plot.g?g4@g?g333333?z list[str]labelszdict[str, str]inits?c Xtrts td||_t |jj |_||_|j||j|j|j|j|jy)Nz"numpy or matplotlib not available.)npr ImportErrorcircuitlenargsngatesnqubitsupdate _create_grid_create_figure _plot_wires _plot_gates_finish)selfcr"kwargss g/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/physics/quantum/circuitplot.py__init__zCircuitPlot.__init__<s|BC C $,,++,   F     c:|jj|y)z'Load the kwargs into the instance dict.N)__dict__r#)r)r+s r,r#zCircuitPlot.updateIs V$r.c|j}tjd|j|z|t}tjd|j |z|t}||_||_y)zCreate the grid of wires.g)dtypeN)scaleraranger"floatr! _wire_grid _gate_grid)r)r3 wire_grid gate_grids r,r$zCircuitPlot._create_gridMsW IIc4<<#5uEIJ IIc4;;u#4e5II ##r.ctj|j|jz|j|jzfdd|_|j j dddd}|jd|jz}|j|jd|z |jd|z|j|jd|z |jd|z|jd ||_ y ) z"Create the main matplotlib figure.w)figsize facecolor edgecolorT)frameonrrequalN)rfigurer!r3r"_figure add_subplot set_axis_offset_xlimr7set_ylimr6 set_aspect_axes)r)axoffsets r,r%zCircuitPlot._create_figureUs}}[[+T\\$**-DE%  \\ % % q!&  TZZ DOOA&/1Dv1MN DOOA&/1Dv1MN g r.c |jd}|jd}||jz ||jzf}t|jD]}|j||j|f}t ||d|j }|jj||jsbd}|jj|j|rd}|jj|d|jz |z |dt|j||j|jddd|j!y) z&Plot the wires of the circuit diagram.rrAkcolorlwg?center)sizerPhavaN)r7r3ranger"r6Line2D linewidthrJadd_linerrgettext label_buffer render_labelfontsize_plot_measured_wires)r)xstartxstopxdataiydatalineinit_label_buffers r,r&zCircuitPlot._plot_wiresgs*##$**$edjj&89t||$ 7A__Q');>D JJ   %{{$%!::>>$++a.1t3D !HT.../@@q Q ;X  7 7 !!#r.c"|j}|jd}d}|D]{}|j||||jzf}|j||z|j||zf}t ||d|j }|j j|}t|jD]\}} t| ttfs| j| jz} | D]} | |vs|j||j|| kDs+t| t!| f}|j||z |j||z f}t ||d|j }|j j|y)NrAg{Gz?rNrO) _measurementsr7r3r6rWrXrJrY enumerate_gates isinstancerr controlstargetsminmax) r) ismeasuredradyimrbrdrercgwireswires r,r_z CircuitPlot._plot_measured_wires~s'') #  &B__Z^4U4::5EFE__R(+DOOB,?,BCEu>>D JJ   % &T[[]+ 2CAa!eV_- QYY.! 2Dz)??1- 4@P0QQ #E CJ 6 $ 22 5tq7I"7L L%!5"%#~~   ++D1 2 2r.c8g}t|jtrHt|jjD]$}t|t s|j |&|St|jt r|j |j|S)z/Create a list of all gates in the circuit plot.)rkrrreversedr rappend)r)gatesrss r,rjzCircuitPlot._gatesst dllC (dll//0 $a&LLO $   d + LL & r.cjt|jD]\}}|j||y)z0Iterate through the gates and plot each of them.N)rirj plot_gate)r)rcgates r,r'zCircuitPlot._plot_gatess. / $GAt NN4 # $r.ci}t|jD]<\}}t|dds|jD]}||vr|||kDs|||<|||<>|S)zReturn 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)rirjgetattrrm)r)rprcrstargets r,rhzCircuitPlot._measurementssw T[[]+ /CAaqu-ii/F+%f-112Jv.-. 6* / /r.cd|jjD]}|jdy)NF)rDfindobj set_clip_on)r)os r,r(zCircuitPlot._finishs*%%' !A MM%  !r.c |j|}|j|}|jj|||dddddd|jd|j y)z#Draw a box for a single qubit gate.rNrRr;TecfcfillrQ)rPrTrUbboxrSN)r7r6rJr[rXr^)r)tgate_idxwire_idxxys r, one_qubit_boxzCircuitPlot.one_qubit_boxsZ OOH % OOH %  q!3DNNK  r.cXt|jt|jy)zL %$$$.2: $ !  " "  * r.r c t||fi|S)aEDraw 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,r r s q' ,V ,,r.cD|j|}|r d|d|dSd|zS)aSlightly 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$)rZ)labelrinits r,r]r].s+ 99U D CH$OO % --r.cTt|Dcgc]}d|||z dz fzc}Scc}w)aAutogenerate 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_%dr?)rV)nsymbolrcs r,r r <s-"/4Ah 7Gvac!en $ 77 7s%ceZdZdZdZdZdZy)r zMock-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. TM_zNrrrrr~ gate_namegate_name_latexrr.r,r r O KIOr.r ceZdZdZdZdZdZy)rzMock-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. TM_xNrrr.r,rrYrr.rceZdZddZy)rNc:|s|}t|dztf||dS)Nr)rr)typer)mclname latexnames r,__new__zCreateOneQubitGate.__new__ds)ID6ML?9 =? ?r.N)rrrrrr.r,rrcs?r.rNc2|s|}t||fd}|S)z5Use a lexical closure to make a controlled gate. c:tt||Sr)rtuple)ctrlsr onequbitgates r,ControlledGatez#CreateCGate..ControlledGatepsU5\,v"677r.)r)rrrrs @r,rrjs$  %dI6L8 r.)qr)r __future__rsympy.core.mulrsympy.externalrsympy.physics.quantum.gaterrrr __all__r RuntimeErrorrrlinesrWpatchesrr r r]r r rrrrrr.r,rs #(HH 7 hZ 8 / *   F    $ $F    & &F o o b -! .8&??r.