Coverage for /home/runner/work/tket/tket/pytket/pytket/qasm/grammar.py: 100%
1 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-14 11:30 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-14 11:30 +0000
1# Copyright Quantinuum
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
16grammar = r"""
17prog: oqasm? (incl|creg|qreg|gdef|opaq|reset|meas|barr|extern|ifc|cop|mixedcall|ccall)*
19oqasm: "OPENQASM" version ";"
20?version: DECIMAL
22incl: "include" "\"" INAM "\"" ";"
23INAM: CNAME ("." CNAME)*
25creg: "creg" _iarg ";"
26qreg: "qreg" _iarg ";"
27mixedcall: _arg pars? margs ";"
28ccall: _arg pars ";"
29gatecall: id pars? args? ";"
30gdef: "gate" id pars? args "{" gatecall* "}"
31opaq: "opaque" id pars? args ";"
32reset: "reset" _marg ";"
33meas: "measure" _marg "->" _marg ";"
34barr: "barrier" margs ";"
35extern: "extern" bwargs "=" id "(" bwargs ")" ";"
36ifc: "if" (cond) (mixedcall|ccall|meas|reset|cop|barr)
37cop: assign
38assign: margs "=" _exp ";"
40!cond: "(" (_arg|_iarg) ("<"|">"|"<="|">="|"!="|"==") pint ")"
42_exp: b_or | _xor_exp
43b_or: _exp "|" _xor_exp
45_xor_exp: xor | _b_and_exp
46xor: _xor_exp "^" _b_and_exp
48_b_and_exp: b_and | _shift_exp
49b_and: _b_and_exp "&" _shift_exp
51_shift_exp: lshift | rshift | _add_exp
52lshift: _shift_exp "<<" _add_exp
53rshift: _shift_exp ">>" _add_exp
55_add_exp: add | sub | _prod_exp
56add: _add_exp "+" _prod_exp
57sub: _add_exp "-" _prod_exp
59_prod_exp: mul | div | _neg_exp
60mul: _prod_exp "*" _neg_exp
61div: _prod_exp "/" _neg_exp
63_neg_exp: neg | _b_not_exp
64neg: "-" _neg_exp
66_b_not_exp: b_not | _pow_exp
67b_not: "~" _b_not_exp
69_pow_exp: ipow | _atom_exp
70ipow: _pow_exp "**" _atom_exp
72_atom_exp: "(" _exp ")"
73 | cce_call
74 | _atom
76cce_call: ARG exp_args
77_atom: _arg | _iarg | INT
79_par_exp: par_pow | _par_add_exp
81par_pow: _par_exp "**" _par_add_exp
83_par_add_exp: par_add | par_sub | _par_prod_exp
84par_add: _par_add_exp "+" _par_prod_exp
85par_sub: _par_add_exp "-" _par_prod_exp
87_par_prod_exp: par_mul | par_div | _par_neg_exp
88par_mul: _par_prod_exp "*" _par_neg_exp
89par_div: _par_prod_exp "/" _par_neg_exp
91_par_neg_exp: par_neg | _par_atom_exp
92par_neg: "-" _par_neg_exp
94_par_atom_exp: "(" _par_exp ")"
95 | sin | cos | tan | ln | sqrt
96 | _fatom
97sin: "sin" "(" _par_exp ")"
98cos: "cos" "(" _par_exp ")"
99tan: "tan" "(" _par_exp ")"
100ln: "ln" "(" _par_exp ")"
101sqrt: "sqrt" "(" _par_exp ")"
103_fatom: snum | ARG
105pars: "(" _par_exp? ("," _par_exp)* ")"
106exp_args: "(" _exp? ("," _exp)* ")"
107args: _arg ("," _arg)*
108iargs: _iarg ("," _iarg)*
109margs: _marg ("," _marg)*
110bwargs: _bwarg ("," _bwarg)*
112?id: CNAME
113_arg: ARG
114_iarg: IARG
115_marg: (ARG|IARG)
116_bwarg: "[" INT "]"
117?snum: SIGNED_NUMBER
118?pint: INT
119ARG: CNAME
120IARG: ARG ("[" INT "]")
122COMMENT: "//" /[^\n]/*
123%import common (WS, DECIMAL, INT, SIGNED_NUMBER, CNAME)
124%ignore WS
125%ignore COMMENT
126"""