qasm parser

49 views
Skip to first unread message

Rick Muller

unread,
Jul 18, 2013, 12:20:06 PM7/18/13
to sy...@googlegroups.com
I created a new branch in my fork that contains the start of a qasm parser. If you're interested in this, it's available at:


Right now it doesn't do all that much. I plan to add a few more gates (X_pi/2, Z_pi/2, S, T), at which point the functionality we have should be basically useful.

However, the big thing missing is the ability to define a gate in qasm using the 'def' command. Example 4 from Chuang's page is:

	def	c-S,1,'S'
	def	c-T,1,'T'

	qubit	j0
	qubit	j1
	qubit	j2

	h	j0	
	c-S	j1,j0
	c-T	j2,j0
	nop	j1
	h	j1
	c-S	j2,j1
	h	j2
	swap	j0,j2

We can make gates on the fly by defining a new class, e.g.

 
 
class VGate(OneQubitGate):
    gate_name = 'V'
    gate_name_latex = u'V'
    
class SqrtX(OneQubitGate):
    gate_name = 'sqrt-X'
    gate_name_latex = u'\sqrt{X}'

But I don't know how do define a function that does this, without defining arguments or modifying the object after creation, both of which I understand are forbidden in sympy operators.

Any thoughts?

Rick Muller

unread,
Jul 18, 2013, 12:22:31 PM7/18/13
to sy...@googlegroups.com
We could do some metaprogramming like they do to define namedtuple, but I don't know whether hacks like that are looked down upon in sympy.

Aaron Meurer

unread,
Jul 18, 2013, 12:27:04 PM7/18/13
to sy...@googlegroups.com
Take a look at how undefined functions (like Function('f')) work in
sympy/core/function.py (look at UndefinedFunction and all its super
classes and metaclasses). They are basically dynamically created
classes.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Rick Muller

unread,
Jul 18, 2013, 12:42:24 PM7/18/13
to sy...@googlegroups.com
Sweet. I'll check it out.

Brian Granger

unread,
Jul 18, 2013, 2:46:32 PM7/18/13
to sympy
Fantastic!
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Brian E. Granger
Cal Poly State University, San Luis Obispo
bgra...@calpoly.edu and elli...@gmail.com

Rick Muller

unread,
Jul 24, 2013, 10:14:11 AM7/24/13
to sy...@googlegroups.com
Aaron,

I'm being dense. Can you point me to some tips on how to use this? Say I want to define something like

class VGate(OneQubitGate):
    gate_name = 'V'
    gate_name_latex = u'V'

on the fly (e.g. so that the 'V' is defined) how do I do this with UndefinedFunction or BasicMeta or some other metaclass in function.py?

(Not asking you to do it, just point me to some examples or something). 

On Thursday, July 18, 2013 10:27:04 AM UTC-6, Aaron Meurer wrote:

Aaron Meurer

unread,
Jul 24, 2013, 10:37:06 AM7/24/13
to sy...@googlegroups.com
No problem. Metaclasses are confusing, at best.

It's basically an exact copy of UndefinedFunction in
sympy/core/function.py. Assuming that OneQuibitGate is an instance of
Basic (so that its metaclass is ManagedProperties), just have

from sympy.core.core import BasicMeta
from sympy.core.assumptions import ManagedProperties

class CreateOneQuibitGate(ManagedProperties):
def __new__(mcl, name):
return BasicMeta.__new__(mcl, name + "Gate", (OneQubitGate,),
{'gate_name': name, 'gate_name_latex': name})

Don't worry about the __module__ = None thing from UndefinedFunction;
that's there to make it picklable in certain situations, but it
doesn't really even work. You could probably get away with using
BasicMeta instead of ManagedProperties, but it's probably a bad idea
to use a different metaclass than the rest of the core.

You can of course get more clever with the gate_name and
gate_name_latex. I just used the same name for all, but you might want
to have two or three arguments in the constructor.

It works like this:

In [20]: a = CreateOneQuibitGate('V')

In [21]: a.gate_name
Out[21]: 'V'

In [22]: a()
Out[22]:
V
O

Aaron Meurer
Reply all
Reply to author
Forward
0 new messages