U
    9%e                     @   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   Y/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/physics/quantum/qasm.py	read_qasm   s    r   c                 C   s   t t|   S r   )r   open	readlines)filenamer   r   r   read_qasm_file   s    r   c                 C   s   ||  d S )zReorder 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| kr| S |  dd S )zRemove 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 )zGet 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labelsZ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    r.   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    r7   c                 C   s4   dg}|   } |D ]}| |d} q| dkr0dS | S )zwFix Qasm command names.

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

    >>> from sympy.physics.quantum.qasm import stripquotes
    >>> stripquotes("'S'") == 'S'
    True
    >>> stripquotes('"S"') == 'S'
    True
    >>> stripquotes('S') == 'S'
    True
    "r9   ')r=   )r1   r   r   r   stripquotesd   s    rB   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-   rG   r   r   r   __init__~   s    
zQasm.__init__c                 G   s   t |D ]}t|\}}| j|r|| j|}| |}t|dkr\| j||d  q| j||d d |d  qt| |rt	| |}||  qt
d|  qd S )Nr   r   z!Function %s not defined. Skipping)r.   r7   rC   getindicesr!   rD   appendhasattrgetattrprint)rH   r   r   commandr6   functionrL   r   r   r   rF      s    
 


zQasm.addc                 C   s   t t| jS r   )r   reversedrD   rH   r   r   r   get_circuit   s    zQasm.get_circuitc                 C   s   t t| jS r   )listrS   r$   rT   r   r   r   
get_labels   s    zQasm.get_labelsc                 C   s8   ddl m} |  |   }}||t||| jd d S )Nr   )CircuitPlot)r$   rE   )!sympy.physics.quantum.circuitplotrX   rU   rW   r!   rE   )rH   rX   rD   r$   r   r   r   plot   s    z	Qasm.plotNc                 C   s   | j | |r|| j|< d S r   )r$   rM   rE   )rH   arginitr   r   r   qubit   s     z
Qasm.qubitc                 C   s   t || jS r   )r+   r$   rH   r-   r   r   r   rL      s    zQasm.indicesc                 C   s   t || jS r   )r%   r$   rH   r[   r   r   r   r"      s    z
Qasm.indexc                 G   s   d S r   r   r^   r   r   r   nop   s    zQasm.nopc                 C   s   | j t| | d S r   )rD   rM   r   r"   r_   r   r   r   x   s    zQasm.xc                 C   s   | j t| | d S r   )rD   rM   r   r"   r_   r   r   r   z   s    zQasm.zc                 C   s   | j t| | d S r   )rD   rM   r   r"   r_   r   r   r   h   s    zQasm.hc                 C   s   | j t| | d S r   )rD   rM   r   r"   r_   r   r   r   r1      s    zQasm.sc                 C   s   | j t| | d S r   )rD   rM   r   r"   r_   r   r   r   r'      s    zQasm.tc                 C   s   | j t| | d S r   )rD   rM   r   r"   r_   r   r   r   measure   s    zQasm.measurec                 C   s   | j t| ||g  d S r   )rD   rM   r   rL   rH   a1a2r   r   r   cnot   s    z	Qasm.cnotc                 C   s   | j t| ||g  d S r   )rD   rM   r
   rL   re   r   r   r   swap   s    z	Qasm.swapc                 C   s   | j t| ||g  d S r   )rD   rM   r   rL   re   r   r   r   cphase   s    zQasm.cphasec                 C   s4   |  |||g\}}}| jt||ft| d S r   )rL   rD   rM   r	   r   )rH   rf   rg   a3i1i2Zi3r   r   r   toffoli   s    zQasm.toffolic                 C   s,   |  ||g\}}| jt|t| d S r   )rL   rD   rM   r   r   rH   rf   rg   fifjr   r   r   cx   s    zQasm.cxc                 C   s,   |  ||g\}}| jt|t| d S r   )rL   rD   rM   r   r   ro   r   r   r   cz   s    zQasm.czc                 G   s   t d| d S )Nz$defbox not supported yet. Skipping: )rP   r^   r   r   r   defbox   s    zQasm.defboxc                 C   sR   ddl m}m} t|}t|}t|}|dkr@||| j|< n||| j|< d S )Nr   )CreateOneQubitGateCreateCGate)rY   ru   rv   intr4   rB   rC   )rH   nameZ	ncontrolssymbolru   rv   rQ   r   r   r   r;      s    z	Qasm.qdef)N)__name__
__module____qualname____doc__rI   rF   rU   rW   rZ   r]   rL   r"   r`   ra   rb   rc   r1   r'   rd   rh   ri   rj   rn   rr   rs   rt   r;   r   r   r   r   r   s   s0   

N)r}   __all__mathr   Zsympy.physics.quantum.gater   r   r   r   r   r	   r
   r   r   r   rY   r   r   r   r   r    r%   r+   r.   r7   r4   rB   r   r   r   r   r   <module>   s    0