项目作者: libtangle

项目描述 :
An Antlr4 Grammar for OPENQASM 2.0
高级语言: ANTLR
项目地址: git://github.com/libtangle/QASM-Grammar.git
创建时间: 2018-02-21T17:24:26Z
项目社区:https://github.com/libtangle/QASM-Grammar

开源协议:

下载


OPENQASM 2.0 Grammar

This is the grammar for OPENQASM 2.0, written for Antlr4.
NOTE: This grammar doesn’t have ‘include’ statements, as that should be done in preprocessing

Usage

You should have antlr4 installed: here.

You can test the parser and see an AST like so:

  1. $ antlr4 ./QASM.g4
  2. $ javac QASM*.java
  1. $ grun QASM mainprog -gui

Examples

The following parse tree is from the QASM code below:

Parse Tree

  1. // Repetition code syndrome measurement
  2. OPENQASM 2.0;
  3. qreg q[3];
  4. qreg a[2];
  5. creg c[3];
  6. creg syn[2];
  7. gate syndrome d1,d2,d3,a1,a2
  8. {
  9. cx d1,a1; cx d2,a1;
  10. cx d2,a2; cx d3,a2;
  11. }
  12. x q[0]; // error
  13. barrier q;
  14. syndrome q[0],q[1],q[2],a[0],a[1];
  15. measure a -> syn;
  16. if(syn==1) x q[0];
  17. if(syn==2) x q[2];
  18. if(syn==3) x q[1];
  19. measure q -> c;

BNF Grammar

This grammar is based off the BNF grammar, detailed in this paper:

  1. mainprogram: "OPENQASM" real ";" program
  2. program: statement | program statement
  3. statement: decl
  4. :| gatedecl goplist }
  5. :| gatedecl }
  6. :| "opaque" id idlist ";"
  7. :| "opaque" id "( )" idlist ";"
  8. :| "opaque" id "(" idlist ")" idlist ";"
  9. :| qop
  10. :| "if (" id "==" nninteger ")" qop
  11. :| "barrier" anylist ";"
  12. decl: "qreg" id [ nninteger ] ";" | "creg" id [ nninteger ] ";"
  13. gatedecl: "gate" id idlist {
  14. :| "gate" id "( )" idlist {
  15. :| "gate" id "(" idlist ")" idlist {
  16. goplist: uop
  17. :| "barrier" idlist ";"
  18. :| goplist uop
  19. :| goplist "barrier" idlist ";"
  20. qop: uop
  21. :| "measure" argument "->" argument ";"
  22. :| "reset" argument ";"
  23. uop: "U (" explist ")" argument ";"
  24. :| "CX" argument "," argument ";"
  25. :| id anylist ";" | id "( )" anylist ";"
  26. :| id "(" explist ")" anylist ";"
  27. anylist: idlist | mixedlist
  28. idlist: id | idlist "," id
  29. mixedlist: id [ nninteger ] | miedlist "," id
  30. :| mixedlist "," id [ nninteger ]
  31. :| idlist "," id [ nninteger ]
  32. argument: id | id [ nninteger ]
  33. explist: exp | explist "," exp
  34. exp: real | nninteger | "pi" | id
  35. :| exp + exp | exp - exp | exp * exp
  36. :| exp / exp | -exp | exp ^ exp
  37. :| "(" exp ")" | unaryop "(" exp ")"
  38. unaryop: "sin" | "cos" | "tan" | "exp" | "ln" | "sqrt"
  39. id := [a-z][A-Za-z0-9_]*
  40. real := ([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?
  41. nninteger := [1-9]+[0-9]*|0

Licence

Copyright Adam Kelly, MIT License