o
    §ƒðj�  ã                   @   s´   d Z dgZddlmZ ddlmZmZmZmZm	Z	m
Z
mZmZmZmZ ddlm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G dd„ dƒZdS )a  

qasm.py - Functions to parse a set of qasm commands into a SymPy Circuit.

Examples taken from Chuang's page: https://web.archive.org/web/20220120121541/https://www.media.mit.edu/quanta/qasm2circ/

The code returns a circuit and an associated list of labels.

>>> from sympy.physics.quantum.qasm import Qasm
>>> q = Qasm('qubit q0', 'qubit q1', 'h q0', 'cnot q0,q1')
>>> q.get_circuit()
CNOT(1,0)*H(1)

>>> q = Qasm('qubit q0', 'qubit q1', 'cnot q0,q1', 'cnot q1,q0', 'cnot q0,q1')
>>> q.get_circuit()
CNOT(1,0)*CNOT(0,1)*CNOT(1,0)
ÚQasmé    )Úprod)
ÚHÚCNOTÚXÚZÚCGateÚCGateSÚSWAPÚSÚTÚCPHASE)ÚMzc                 C   s   t |  ¡ Ž S ©N)r   Ú
splitlines)Úlines© r   úœ/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/physics/quantum/qasm.pyÚ	read_qasm   ó   r   c                 C   s   t t| ƒ ¡ Ž S r   )r   ÚopenÚ	readlines)Úfilenamer   r   r   Úread_qasm_file   s   r   c                 C   s   ||  d S )z­Reorder qubit indices from largest to smallest.

    >>> from sympy.physics.quantum.qasm import flip_index
    >>> flip_index(0, 2)
    1
    >>> flip_index(1, 2)
    0
    é   r   )ÚiÚnr   r   r   Ú
flip_index"   s   	r   c                 C   s   d| vr| S |   d¡d S )zìRemove everything following comment # characters in line.

    >>> from sympy.physics.quantum.qasm import trim
    >>> trim('nothing happens here')
    'nothing happens here'
    >>> trim('something #happens here')
    'something '
    ú#r   )Úsplit)Úliner   r   r   Útrim-   s   	r!   c                 C   s   t |ƒ}t| | ¡|ƒS )zÔGet qubit labels from the rest of the line,and return indices

    >>> from sympy.physics.quantum.qasm import get_index
    >>> get_index('q0', ['q0', 'q1'])
    1
    >>> get_index('q1', ['q0', 'q1'])
    0
    )Úlenr   Úindex)ÚtargetÚlabelsÚnqr   r   r   Ú	get_index:   s   	r'   c                    s   ‡ fdd„| D ƒS )Nc                    s   g | ]}t |ˆ ƒ‘qS r   )r'   )Ú.0Út©r%   r   r   Ú
<listcomp>G   s    zget_indices.<locals>.<listcomp>r   )Útargetsr%   r   r*   r   Úget_indicesF   s   r-   c                 c   s(   � | D ]}t |ƒ}| ¡ rq|V  qd S r   )r!   Úisspace)Úargsr    r   r   r   ÚnonblankI   s   €r0   c                 C   s:   |   ¡ }d |dd … ¡}t|d ƒdd„ |  d¡D ƒfS )Nú r   r   c                 S   s   g | ]}|  ¡ ‘qS r   )Ústrip)r(   Úsr   r   r   r+   T   s    zfullsplit.<locals>.<listcomp>ú,)r   ÚjoinÚ
fixcommand)r    ÚwordsÚrestr   r   r   Ú	fullsplitQ   s    r9   c                 C   s4   dg}|   ¡ } |D ]}|  |d¡} q	| dkrdS | S )zwFix Qasm command names.

    Remove all of forbidden characters from command c, and
    replace 'def' with 'qdef'.
    ú-Ú ÚdefÚqdef)ÚlowerÚreplace)ÚcÚforbidden_charactersÚcharr   r   r   r6   V   s   r6   c                 C   s   |   dd¡} |   dd¡} | S )zäReplace explicit quotes in a string.

    >>> from sympy.physics.quantum.qasm import stripquotes
    >>> stripquotes("'S'") == 'S'
    True
    >>> stripquotes('"S"') == 'S'
    True
    >>> stripquotes('S') == 'S'
    True
    ú"r;   ú')r?   )r3   r   r   r   Ústripquotesd   s   rE   c                   @   sÊ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zd1d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/d0„ ZdS )2r   aV  Class to form objects from Qasm lines

    >>> from sympy.physics.quantum.qasm import Qasm
    >>> q = Qasm('qubit q0', 'qubit q1', 'h q0', 'cnot q0,q1')
    >>> q.get_circuit()
    CNOT(1,0)*H(1)
    >>> q = Qasm('qubit q0', 'qubit q1', 'cnot q0,q1', 'cnot q1,q0', 'cnot q0,q1')
    >>> q.get_circuit()
    CNOT(1,0)*CNOT(0,1)*CNOT(1,0)
    c                 O   s,   i | _ g | _g | _i | _| j|Ž  || _d S r   )ÚdefsÚcircuitr%   ÚinitsÚaddÚkwargs)Úselfr/   rJ   r   r   r   Ú__init__~   s   

zQasm.__init__c                 G   s¬   t |ƒD ]O}t|ƒ\}}| j |¡r>| j |¡}|  |¡}t|ƒdkr.| j ||d ƒ¡ q| j ||d d… |d ƒ¡ qt| |ƒrMt	| |ƒ}||Ž  qt
d| ƒ qd S )Nr   r   éÿÿÿÿz!Function %s not defined. Skipping)r0   r9   rF   ÚgetÚindicesr"   rG   ÚappendÚhasattrÚgetattrÚprint)rK   r   r    Úcommandr8   ÚfunctionrO   r   r   r   rI   †   s   
 


ózQasm.addc                 C   ó   t t| jƒƒS r   )r   ÚreversedrG   ©rK   r   r   r   Úget_circuit–   ó   zQasm.get_circuitc                 C   rV   r   )ÚlistrW   r%   rX   r   r   r   Ú
get_labels™   rZ   zQasm.get_labelsc                 C   s8   ddl m} |  ¡ |  ¡ }}||t|ƒ|| jd� d S )Nr   )ÚCircuitPlot)r%   rH   )Ú!sympy.physics.quantum.circuitplotr]   rY   r\   r"   rH   )rK   r]   rG   r%   r   r   r   Úplotœ   s   z	Qasm.plotNc                 C   s"   | j  |¡ |r|| j|< d S d S r   )r%   rP   rH   )rK   ÚargÚinitr   r   r   Úqubit¡   s   z
Qasm.qubitc                 C   ó   t || jƒS r   )r-   r%   ©rK   r/   r   r   r   rO   ¥   r   zQasm.indicesc                 C   rc   r   )r'   r%   ©rK   r`   r   r   r   r#   ¨   r   z
Qasm.indexc                 G   s   d S r   r   rd   r   r   r   Únop«   s   zQasm.nopc                 C   ó   | j  t|  |¡ƒ¡ d S r   )rG   rP   r   r#   re   r   r   r   Úx®   ó   zQasm.xc                 C   rg   r   )rG   rP   r   r#   re   r   r   r   Úz±   ri   zQasm.zc                 C   rg   r   )rG   rP   r   r#   re   r   r   r   Úh´   ri   zQasm.hc                 C   rg   r   )rG   rP   r   r#   re   r   r   r   r3   ·   ri   zQasm.sc                 C   rg   r   )rG   rP   r   r#   re   r   r   r   r)   º   ri   zQasm.tc                 C   rg   r   )rG   rP   r   r#   re   r   r   r   Úmeasure½   ri   zQasm.measurec                 C   ó   | j  t|  ||g¡Ž ¡ d S r   )rG   rP   r   rO   ©rK   Úa1Úa2r   r   r   ÚcnotÀ   ó   z	Qasm.cnotc                 C   rm   r   )rG   rP   r
   rO   rn   r   r   r   ÚswapÃ   rr   z	Qasm.swapc                 C   rm   r   )rG   rP   r   rO   rn   r   r   r   ÚcphaseÆ   rr   zQasm.cphasec                 C   s4   |   |||g¡\}}}| j t||ft|ƒƒ¡ d S r   )rO   rG   rP   r	   r   )rK   ro   rp   Úa3Úi1Úi2Úi3r   r   r   ÚtoffoliÉ   s   zQasm.toffolic                 C   ó,   |   ||g¡\}}| j t|t|ƒƒ¡ d S r   )rO   rG   rP   r   r   ©rK   ro   rp   ÚfiÚfjr   r   r   ÚcxÍ   ó   zQasm.cxc                 C   rz   r   )rO   rG   rP   r   r   r{   r   r   r   ÚczÑ   r   zQasm.czc                 G   s   t d|ƒ d S )Nz$defbox not supported yet. Skipping: )rS   rd   r   r   r   ÚdefboxÕ   rZ   zQasm.defboxc                 C   sT   ddl m}m} t|ƒ}t|ƒ}t|ƒ}|dkr!||ƒ| j|< d S ||ƒ| j|< d S )Nr   )ÚCreateOneQubitGateÚCreateCGate)r^   r‚   rƒ   Úintr6   rE   rF   )rK   ÚnameÚ	ncontrolsÚsymbolr‚   rƒ   rT   r   r   r   r=   Ø   s   z	Qasm.qdefr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rL   rI   rY   r\   r_   rb   rO   r#   rf   rh   rj   rk   r3   r)   rl   rq   rs   rt   ry   r~   r€   r�   r=   r   r   r   r   r   s   s2    

N)r‹   Ú__all__Úmathr   Úsympy.physics.quantum.gater   r   r   r   r   r	   r
   r   r   r   r^   r   r   r   r   r!   r'   r-   r0   r9   r6   rE   r   r   r   r   r   Ú<module>   s"    ÿ0