ó
¡¼™\c           @   sâ   d  Z  d g 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 „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d e f d „  ƒ  YZ d S(   sê  

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

Examples taken from Chuang's page: http://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)
t   Qasmiÿÿÿÿ(
   t   Ht   CNOTt   Xt   Zt   CGatet   CGateSt   SWAPt   St   Tt   CPHASE(   t   Mzc         C   s   t  |  j ƒ  Œ  S(   N(   R    t
   splitlines(   t   lines(    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt	   read_qasm   s    c         C   s   t  t |  ƒ j ƒ  Œ  S(   N(   R    t   opent	   readlines(   t   filename(    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   read_qasm_file   s    c         C   s%   d } x |  D] } | | 9} q W| S(   Ni   (    (   t   ct   pt   ci(    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   prod    s    c         C   s   | |  d S(   s­   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
    i   (    (   t   it   n(    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt
   flip_index&   s    	c         C   s!   d |  k r |  S|  j  d ƒ d S(   sì   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 '
    t   #i    (   t   split(   t   line(    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   trim1   s    	c         C   s"   t  | ƒ } t | j |  ƒ | ƒ S(   sÔ   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
    (   t   lenR   t   index(   t   targett   labelst   nq(    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt	   get_index>   s    	c         C   s    g  |  D] } t  | | ƒ ^ q S(   N(   R#   (   t   targetsR!   t   t(    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   get_indicesJ   s    c         c   s8   x1 |  D]) } t  | ƒ } | j ƒ  r+ q n  | Vq Wd  S(   N(   R   t   isspace(   t   argsR   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   nonblankM   s    	c         C   sU   |  j  ƒ  } d j | d ƒ } t | d ƒ g  | j  d ƒ D] } | j ƒ  ^ q< f S(   Nt    i   i    t   ,(   R   t   joint
   fixcommandt   strip(   R   t   wordst   restt   s(    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt	   fullsplitU   s    c         C   sL   d g } |  j  ƒ  }  x  | D] } |  j | d ƒ }  q W|  d k rH d S|  S(   sw   Fix Qasm command names.

    Remove all of forbidden characters from command c, and
    replace 'def' with 'qdef'.
    t   -t    t   deft   qdef(   t   lowert   replace(   R   t   forbidden_characterst   char(    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyR-   Z   s    	c         C   s(   |  j  d d ƒ }  |  j  d d ƒ }  |  S(   sä   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
    t   "R4   t   '(   R8   (   R1   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   stripquotesh   s    c           B   sà   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sV  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(   N(   t   defst   circuitR!   t   initst   addt   kwargs(   t   selfR(   RB   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   __init__‚   s    				c         G   sç   xà t  | ƒ D]Ò } t | ƒ \ } } |  j j | ƒ r« |  j j | ƒ } |  j | ƒ } t | ƒ d k r‡ |  j j | | d ƒ ƒ qß |  j j | | d  | d ƒ ƒ q t |  | ƒ rÖ t	 |  | ƒ } | | Œ  q d | GHq Wd  S(   Ni   i    iÿÿÿÿs!   Function %s not defined. Skipping(
   R)   R2   R>   t   gett   indicesR   R?   t   appendt   hasattrt   getattr(   RC   R   R   t   commandR0   t   functionRF   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyRA   Š   s    $c         C   s   t  t |  j ƒ ƒ S(   N(   R   t   reversedR?   (   RC   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   get_circuitš   s    c         C   s   t  t |  j ƒ ƒ S(   N(   t   listRL   R!   (   RC   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt
   get_labels   s    c         C   sO   d d l  m } |  j ƒ  |  j ƒ  } } | | t | ƒ d | d |  j ƒd  S(   Niÿÿÿÿ(   t   CircuitPlotR!   R@   (   t!   sympy.physics.quantum.circuitplotRP   RM   RO   R   R@   (   RC   RP   R?   R!   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   plot    s    c         C   s*   |  j  j | ƒ | r& | |  j | <n  d  S(   N(   R!   RG   R@   (   RC   t   argt   init(    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   qubit¥   s     c         C   s   t  | |  j ƒ S(   N(   R&   R!   (   RC   R(   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyRF   ©   s    c         C   s   t  | |  j ƒ S(   N(   R#   R!   (   RC   RS   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyR   ¬   s    c         G   s   d  S(   N(    (   RC   R(   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   nop¯   s    c         C   s#   |  j  j t |  j | ƒ ƒ ƒ d  S(   N(   R?   RG   R   R   (   RC   RS   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   x²   s    c         C   s#   |  j  j t |  j | ƒ ƒ ƒ d  S(   N(   R?   RG   R   R   (   RC   RS   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   zµ   s    c         C   s#   |  j  j t |  j | ƒ ƒ ƒ d  S(   N(   R?   RG   R   R   (   RC   RS   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   h¸   s    c         C   s#   |  j  j t |  j | ƒ ƒ ƒ d  S(   N(   R?   RG   R   R   (   RC   RS   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyR1   »   s    c         C   s#   |  j  j t |  j | ƒ ƒ ƒ d  S(   N(   R?   RG   R	   R   (   RC   RS   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyR%   ¾   s    c         C   s#   |  j  j t |  j | ƒ ƒ ƒ d  S(   N(   R?   RG   R   R   (   RC   RS   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   measureÁ   s    c         C   s)   |  j  j t |  j | | g ƒ Œ  ƒ d  S(   N(   R?   RG   R   RF   (   RC   t   a1t   a2(    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   cnotÄ   s    c         C   s)   |  j  j t |  j | | g ƒ Œ  ƒ d  S(   N(   R?   RG   R   RF   (   RC   R[   R\   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   swapÇ   s    c         C   s)   |  j  j t |  j | | g ƒ Œ  ƒ d  S(   N(   R?   RG   R
   RF   (   RC   R[   R\   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   cphaseÊ   s    c         C   sJ   |  j  | | | g ƒ \ } } } |  j j t | | f t | ƒ ƒ ƒ d  S(   N(   RF   R?   RG   R   R   (   RC   R[   R\   t   a3t   i1t   i2t   i3(    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   toffoliÍ   s    !c         C   s>   |  j  | | g ƒ \ } } |  j j t | t | ƒ ƒ ƒ d  S(   N(   RF   R?   RG   R   R   (   RC   R[   R\   t   fit   fj(    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   cxÑ   s    c         C   s>   |  j  | | g ƒ \ } } |  j j t | t | ƒ ƒ ƒ d  S(   N(   RF   R?   RG   R   R   (   RC   R[   R\   Re   Rf   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   czÕ   s    c         G   s   d | f GHd  S(   Ns$   defbox not supported yet. Skipping: (    (   RC   R(   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   defboxÙ   s    c         C   ss   d d l  m } m } t | ƒ } t | ƒ } t | ƒ } | d k r\ | | ƒ |  j | <n | | ƒ |  j | <d  S(   Niÿÿÿÿ(   t   CreateOneQubitGatet   CreateCGatei    (   RQ   Rj   Rk   t   intR-   R=   R>   (   RC   t   namet	   ncontrolst   symbolRj   Rk   RJ   (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyR6   Ü   s    N(   t   __name__t
   __module__t   __doc__RD   RA   RM   RO   RR   t   NoneRU   RF   R   RV   RW   RX   RY   R1   R%   RZ   R]   R^   R_   Rd   Rg   Rh   Ri   R6   (    (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyR    w   s0   
																					N(   Rr   t   __all__t   sympy.physics.quantum.gateR   R   R   R   R   R   R   R   R	   R
   RQ   R   R   R   R   R   R   R#   R&   R)   R2   R-   R=   t   objectR    (    (    (    s9   lib/python2.7/site-packages/sympy/physics/quantum/qasm.pyt   <module>   s   	F											