o
    §ƒðj.  ã                   @  sî   d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
mZ g d¢ZedƒZedd	d
giefd�ZerBerBejZejjZejjZG dd„ dƒZdd„ Zi fdd„Zddd„ZG dd„ de	ƒZG dd„ de	ƒZG dd„ deƒZddd„ZdS ) 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Úcircuit_plotÚlabellerÚMzÚMxÚCreateOneQubitGateÚCreateCGateÚnumpyÚ
matplotlibÚfromlistÚpyplot)Úimport_kwargsÚcatchc                   @  sÆ   e Zd ZU dZdZdZdZdZdZdZ	g Z
ded< i Zded	< d
Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+S ),r	   z$A class for managing a circuit plot.g      ð?g      4@gš™™™™™©?g333333Ã?z	list[str]Úlabelszdict[str, str]Úinitsç      à?c                 K  s`   t rtstdƒ‚|| _t| jjƒ| _|| _|  |¡ |  	¡  |  
¡  |  ¡  |  ¡  |  ¡  d S )Nz"numpy or matplotlib not available.)Únpr   ÚImportErrorÚcircuitÚlenÚargsÚngatesÚnqubitsÚupdateÚ_create_gridÚ_create_figureÚ_plot_wiresÚ_plot_gatesÚ_finish)ÚselfÚcr   Úkwargs© r)   ú£/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/physics/quantum/circuitplot.pyÚ__init__<   s   
zCircuitPlot.__init__c                 C  s   | j  |¡ dS )z'Load the kwargs into the instance dict.N)Ú__dict__r    )r&   r(   r)   r)   r*   r    I   s   zCircuitPlot.updatec                 C  sF   | j }tjd| j| |td�}tjd| j| |td�}|| _|| _dS )zCreate the grid of wires.g        )ÚdtypeN)Úscaler   Úaranger   Úfloatr   Ú
_wire_gridÚ
_gate_grid)r&   r.   Ú	wire_gridÚ	gate_gridr)   r)   r*   r!   M   s
   
zCircuitPlot._create_gridc                 C  s    t j| j| j | j| j fddd�| _| jjddddd�}| ¡  d| j }| | j	d | | j	d | ¡ | 
| jd | | jd | ¡ | d	¡ || _d
S )z"Create the main matplotlib figure.Úw)ÚfigsizeÚ	facecolorÚ	edgecoloré   T)Úframeonr   r   éÿÿÿÿÚequalN)r   Úfigurer   r.   r   Ú_figureÚadd_subplotÚset_axis_offÚset_xlimr2   Úset_ylimr1   Ú
set_aspectÚ_axes)r&   ÚaxÚoffsetr)   r)   r*   r"   U   s   ýþ
  

zCircuitPlot._create_figurec              
   C  sÒ   | j d }| j d }|| j || j f}t| jƒD ]I}| j| | j| f}t||d| jd�}| j |¡ | j	rbd}| j
 | j	| ¡rDd}| jj|d | j | |d t| j	| | j
ƒ| jdddd� q|  ¡  dS )	z&Plot the wires of the circuit diagram.r   r;   Úk©ÚcolorÚlwg      Ð?Úcenter)ÚsizerI   ÚhaÚvaN)r2   r.   Úranger   r1   ÚLine2DÚ	linewidthrD   Úadd_liner   r   ÚgetÚtextÚlabel_bufferÚrender_labelÚfontsizeÚ_plot_measured_wires)r&   ÚxstartÚxstopÚxdataÚiÚydataÚlineÚinit_label_bufferr)   r)   r*   r#   g   s,   

ýü€zCircuitPlot._plot_wiresc                 C  s  |   ¡ }| jd }d}|D ]+}| j||  || j f}| j| | | j| | f}t||d| jd�}| j |¡ qt|  	¡ ƒD ]K\}}	t
|	ttfƒrŠ|	j|	j }
|
D ]7}||v r‰| j| | j||  kr‰t|
ƒt|
ƒf}| j| | | j| | f}t||d| jd�}| j |¡ qRq?d S )Nr;   g{®Gáz¤?rG   rH   )Ú_measurementsr2   r.   r1   rP   rQ   rD   rR   Ú	enumerateÚ_gatesÚ
isinstancer   r   ÚcontrolsÚtargetsÚminÚmax)r&   Ú
ismeasuredrZ   ÚdyÚimr[   r]   r^   r\   ÚgÚwiresÚwirer)   r)   r*   rX   ~   s8   
ýý€óz CircuitPlot._plot_measured_wiresc                 C  sV   g }t | jtƒrt| jjƒD ]}t |tƒr| |¡ q|S t | jtƒr)| | j¡ |S )z/Create a list of all gates in the circuit plot.)rc   r   r   Úreversedr   r   Úappend)r&   Úgatesrk   r)   r)   r*   rb   ›   s   

€þzCircuitPlot._gatesc                 C  s&   t |  ¡ ƒD ]
\}}| | |¡ qdS )z0Iterate through the gates and plot each of them.N)ra   rb   Ú	plot_gate)r&   r\   Úgater)   r)   r*   r$   ¦   s   ÿzCircuitPlot._plot_gatesc                 C  s\   i }t |  ¡ ƒD ]#\}}t|ddƒr+|jD ]}||v r&|| |kr%|||< q|||< qq|S )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)ra   rb   Úgetattrre   )r&   rh   r\   rk   Útargetr)   r)   r*   r`   «   s   
€
€zCircuitPlot._measurementsc                 C  s   | j  ¡ D ]}| d¡ qd S )NF)r>   ÚfindobjÚset_clip_on)r&   Úor)   r)   r*   r%   º   s   ÿzCircuitPlot._finishc                 C  sB   | j | }| j| }| jj|||dddddd| jdœ| jd� dS )z#Draw a box for a single qubit gate.rG   rK   r5   T©ÚecÚfcÚfillrJ   )rI   rM   rN   ÚbboxrL   N)r2   r1   rD   rT   rQ   rW   )r&   ÚtÚgate_idxÚwire_idxÚxÚyr)   r)   r*   Úone_qubit_box¿   s   


úzCircuitPlot.one_qubit_boxc                 C  s   t | jƒ t | jƒ dS )z<Draw a box for a two qubit gate. Does not work yet.
        N)Úprintr2   r1   )r&   r~   r   r€   r)   r)   r*   Útwo_qubit_boxÌ   s   
zCircuitPlot.two_qubit_boxc                 C  sJ   | j | | j | f}| j| | j| f}t||d| jd�}| j |¡ dS )zDraw a vertical control line.rG   rH   N)r2   r1   rP   rQ   rD   rR   )r&   r   Úmin_wireÚmax_wirer[   r]   r^   r)   r)   r*   Úcontrol_lineÝ   s   ýzCircuitPlot.control_linec                 C  sJ   | j | }| j| }| j}t||f|| j ddd| jd�}| j |¡ dS )zDraw a control point.rG   Try   N)r2   r1   Úcontrol_radiusÚCircler.   rQ   rD   Ú	add_patch)r&   r   r€   r�   r‚   Úradiusr'   r)   r)   r*   Úcontrol_pointè   s   

úzCircuitPlot.control_pointc                 C  sr   | j | }| j| }| j}t||f|ddd| jd�}| j |¡ t||f|| || fd| jd�}| j |¡ dS )z7Draw a NOT gates as the circle with plus in the middle.rG   r5   Fry   rH   N)	r2   r1   Ú
not_radiusrŠ   rQ   rD   r‹   rP   rR   )r&   r   r€   r�   r‚   rŒ   r'   Úlr)   r)   r*   Ú	not_point÷   s$   

úýzCircuitPlot.not_pointc                 C  sŠ   | j | }| j| }| j}t|| || f|| || fd| jd�}t|| || f|| || fd| jd�}| j |¡ | j |¡ dS )zDraw a swap point as a cross.rG   rH   N)r2   r1   Ú
swap_deltarP   rQ   rD   rR   )r&   r   r€   r�   r‚   ÚdÚl1Úl2r)   r)   r*   Ú
swap_point  s"   

üüzCircuitPlot.swap_pointN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r.   rW   rQ   r‰   rŽ   r‘   r   Ú__annotations__r   rU   r+   r    r!   r"   r#   rX   rb   r$   r`   r%   rƒ   r…   rˆ   r�   r�   r•   r)   r)   r)   r*   r	   /   s6   
 r	   c                 K  s   t | |fi |¤ŽS )aE  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(   r)   r)   r*   r
      s   r
   c                 C  s"   |  | ¡}|rd| |f S d|  S )a  Slightly 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|%s\right\rangle=\left|%s\right\rangle$z$\left|%s\right\rangle$)rS   )Úlabelr   Úinitr)   r)   r*   rV   .  s   
	rV   Úqc                   s   ‡ ‡fdd„t ˆ ƒD ƒS )aˆ  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']
    c                   s    g | ]}d ˆˆ | d f ‘qS )z%s_%dr9   r)   )Ú.0r\   ©ÚnÚsymbolr)   r*   Ú
<listcomp>M  s     zlabeller.<locals>.<listcomp>)rO   rŸ   r)   rŸ   r*   r   <  s   r   c                   @  ó   e Zd ZdZdZd ZdZdS )r   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_zN©r–   r—   r˜   r™   rs   Ú	gate_nameÚgate_name_latexr)   r)   r)   r*   r   O  ó
    r   c                   @  r£   )r   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_xNr¥   r)   r)   r)   r*   r   Y  r¨   r   c                   @  s   e Zd Zddd„ZdS )r   Nc                 C  s    |s|}t |d tf||dœƒS )Nr   )r¦   r§   )Útyper   )ÚmclÚnameÚ	latexnamer)   r)   r*   Ú__new__d  s
   ÿzCreateOneQubitGate.__new__©N)r–   r—   r˜   r®   r)   r)   r)   r*   r   c  s    r   Nc                   s"   |s| }t | |ƒ‰ ‡ fdd„}|S )z5Use a lexical closure to make a controlled gate.
    c                   s   t t| ƒˆ |ƒƒS r¯   )r   Útuple)Úctrlsru   ©Úonequbitgater)   r*   ÚControlledGatep  s   z#CreateCGate.<locals>.ControlledGate)r   )r¬   r­   r´   r)   r²   r*   r   j  s
   
r   )r�   r¯   )r™   Ú
__future__r   Úsympy.core.mulr   Úsympy.externalr   Úsympy.physics.quantum.gater   r   r   r   Ú__all__r   ÚRuntimeErrorr   r   ÚlinesrP   ÚpatchesrŠ   r	   r
   rV   r   r   r   rª   r   r   r)   r)   r)   r*   Ú<module>   s0    

þ r


