o
    8Va.                     @   s
  d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZmZ ddlmZ ddlmZ g dZed	Zed
ddgiefdZerPerPe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!
    )ListDict)Mul)import_module)GateOneQubitGateCGateCGateS)	BasicMeta)ManagedProperties)CircuitPlotcircuit_plotlabellerMzMxCreateOneQubitGateCreateCGateZnumpy
matplotlibfromlistpyplot)Zimport_kwargsZcatchc                   @   s   e Zd ZdZdZdZdZdZdZdZ	g Z
i Z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?      ?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'   C/usr/lib/python3/dist-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   K   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        )ZdtypeN)scaler   Zaranger   floatr   
_wire_grid
_gate_grid)r$   r+   Z	wire_gridZ	gate_gridr'   r'   r(   r   O   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)ZfigsizeZ	facecolorZ	edgecolor   T)Zframeonr   r   ZequalN)r   Zfigurer   r+   r   _figureZadd_subplotZset_axis_offZset_xlimr.   Zset_ylimr-   Z
set_aspect_axes)r$   Zaxoffsetr'   r'   r(   r    W   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   r1   kcolorlwg      ?center)sizer7   havaN)r.   r+   ranger   r-   Line2D	linewidthr3   add_linelabelsinitsgettextlabel_bufferrender_labelfontsize_plot_measured_wires)r$   ZxstartxstopxdataiydatalineZinit_label_bufferr'   r'   r(   r!   i   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 ]N\}}	t
|	tsMt
|	tr|	j|	j }
|
D ]7}||v r| j| | j||  krt|
t|
f}| j| | | j| | f}t||d| jd}| j| qUq?d S )Nr1   g{Gz?r5   r6   )_measurementsr.   r+   r-   r>   r?   r3   r@   	enumerate_gates
isinstancer   r	   Zcontrolstargetsminmax)r$   
ismeasuredrI   ZdyZimrJ   rL   rM   rK   gZwiresZwirer'   r'   r(   rH      s:   
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.)rQ   r   r   reversedr   r   append)r$   ZgatesrV   r'   r'   r(   rP      s   

zCircuitPlot._gatesc                 C   s&   t |  D ]
\}}|| | qdS )z0Iterate through the gates and plot each of them.N)rO   rP   Z	plot_gate)r$   rK   Z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 )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)rO   rP   getattrrR   )r$   rU   rK   rV   targetr'   r'   r(   rN      s   

zCircuitPlot._measurementsc                 C   s   | j  D ]}|d qd S )NF)r2   ZfindobjZset_clip_on)r$   or'   r'   r(   r#      s   zCircuitPlot._finishc                 C   sD   | j | }| j| }| jj|||dddtddd| jd| jd dS )z#Draw a box for a single qubit gate.r5   r9   r/   TZecZfcfillr8   )r7   r;   r<   Zbboxr:   N)r.   r-   r3   rD   dictr?   rG   )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. Doesn't work yet.
        N)printr.   r-   )r$   r`   ra   rb   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.r5   r6   N)r.   r-   r>   r?   r3   r@   )r$   ra   Zmin_wireZmax_wirerJ   rL   rM   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.r5   Tr]   N)r.   r-   control_radiusCircler+   r?   r3   	add_patch)r$   ra   rb   rc   rd   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.r5   r/   Fr]   r6   N)	r.   r-   
not_radiusrj   r?   r3   rk   r>   r@   )r$   ra   rb   rc   rd   rl   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.r5   r6   N)r.   r-   
swap_deltar>   r?   r3   r@   )r$   ra   rb   rc   rd   dl1l2r'   r'   r(   
swap_point  s"   

zCircuitPlot.swap_pointN)__name__
__module____qualname____doc__r+   rG   r?   ri   rn   rq   rA   rB   rE   r)   r   r   r    r!   rH   rP   r"   rN   r#   re   rg   rh   rm   rp   ru   r'   r'   r'   r(   r   1   s6    r   c                 K   s   t | |fi |S )aD  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$)rC   )ZlabelrB   Zinitr'   r'   r(   rF   0  s   
	rF   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_%dr0   r'   ).0rK   nsymbolr'   r(   
<listcomp>O  s     zlabeller.<locals>.<listcomp>)r=   r|   r'   r|   r(   r   >  s   r   c                   @      e Zd ZdZdZd ZdZdS )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.
    TZM_zNrv   rw   rx   ry   rY   	gate_namegate_name_latexr'   r'   r'   r(   r   Q  
    r   c                   @   r   )r   zMock-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.
    TZM_xNr   r'   r'   r'   r(   r   [  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   )r
   __new__r   )Zmclname	latexnamer'   r'   r(   r   f  s
   zCreateOneQubitGate.__new__N)rv   rw   rx   r   r'   r'   r'   r(   r   e  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)Zctrlsr[   Zonequbitgater'   r(   ControlledGater  s   z#CreateCGate.<locals>.ControlledGate)r   )r   r   r   r'   r   r(   r   l  s
   
r   )rz   r   )"ry   typingr   r   Zsympyr   Zsympy.externalr   Zsympy.physics.quantum.gater   r   r   r	   Zsympy.core.corer
   Zsympy.core.assumptionsr   __all__r   RuntimeErrorr   r   linesr>   Zpatchesrj   r   r   rF   r   r   r   r   r   r'   r'   r'   r(   <module>   s4    

 r


