a
    kh                     @   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   H/var/www/auris/lib/python3.9/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| v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       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   r*   zfullsplit.<locals>.<listcomp>,)r   join
fixcommand)r   wordsrestr   r   r   	fullsplitQ   s    r8   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   r5   V   s    r5   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
    "r:   ')r>   )r2   r   r   r   stripquotesd   s    rC   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.   rH   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/   r8   rD   getindicesr!   rE   appendhasattrgetattrprint)rI   r   r   commandr7   functionrM   r   r   r   rG      s    
 


zQasm.addc                 C   s   t t| jS r   )r   reversedrE   rI   r   r   r   get_circuit   s    zQasm.get_circuitc                 C   s   t t| jS r   )listrT   r$   rU   r   r   r   
get_labels   s    zQasm.get_labelsc                 C   s8   ddl m} |  |   }}||t||| jd d S )Nr   )CircuitPlot)r$   rF   )!sympy.physics.quantum.circuitplotrY   rV   rX   r!   rF   )rI   rY   rE   r$   r   r   r   plot   s    z	Qasm.plotNc                 C   s   | j | |r|| j|< d S r   )r$   rN   rF   )rI   arginitr   r   r   qubit   s    z
Qasm.qubitc                 C   s   t || jS r   )r,   r$   rI   r.   r   r   r   rM      s    zQasm.indicesc                 C   s   t || jS r   )r%   r$   rI   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   )rE   rN   r   r"   r`   r   r   r   x   s    zQasm.xc                 C   s   | j t| | d S r   )rE   rN   r   r"   r`   r   r   r   z   s    zQasm.zc                 C   s   | j t| | d S r   )rE   rN   r   r"   r`   r   r   r   h   s    zQasm.hc                 C   s   | j t| | d S r   )rE   rN   r   r"   r`   r   r   r   r2      s    zQasm.sc                 C   s   | j t| | d S r   )rE   rN   r   r"   r`   r   r   r   r'      s    zQasm.tc                 C   s   | j t| | d S r   )rE   rN   r   r"   r`   r   r   r   measure   s    zQasm.measurec                 C   s   | j t| ||g  d S r   )rE   rN   r   rM   rI   a1a2r   r   r   cnot   s    z	Qasm.cnotc                 C   s   | j t| ||g  d S r   )rE   rN   r
   rM   rf   r   r   r   swap   s    z	Qasm.swapc                 C   s   | j t| ||g  d S r   )rE   rN   r   rM   rf   r   r   r   cphase   s    zQasm.cphasec                 C   s4   |  |||g\}}}| jt||ft| d S r   )rM   rE   rN   r	   r   )rI   rg   rh   a3i1i2Zi3r   r   r   toffoli   s    zQasm.toffolic                 C   s,   |  ||g\}}| jt|t| d S r   )rM   rE   rN   r   r   rI   rg   rh   fifjr   r   r   cx   s    zQasm.cxc                 C   s,   |  ||g\}}| jt|t| d S r   )rM   rE   rN   r   r   rp   r   r   r   cz   s    zQasm.czc                 G   s   t d| d S )Nz$defbox not supported yet. Skipping: )rQ   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)rZ   rv   rw   intr5   rC   rD   )rI   nameZ	ncontrolssymbolrv   rw   rR   r   r   r   r<      s    z	Qasm.qdef)N)__name__
__module____qualname____doc__rJ   rG   rV   rX   r[   r^   rM   r"   ra   rb   rc   rd   r2   r'   re   ri   rj   rk   ro   rs   rt   ru   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   rZ   r   r   r   r   r    r%   r,   r/   r8   r5   rC   r   r   r   r   r   <module>   s    0