Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QasmParser throws Syntax-Error #4487

Closed
lasys opened this issue Sep 6, 2021 · 2 comments
Closed

QasmParser throws Syntax-Error #4487

lasys opened this issue Sep 6, 2021 · 2 comments
Labels
kind/bug-report Something doesn't seem to work. triage/duplicate This issue or pull request is addressed by another issue or pull request

Comments

@lasys
Copy link

lasys commented Sep 6, 2021

Description of the issue

QasmParser cannot parse Qasm-code.
Is there an error in my Qasm-code or a bug in the QasmParser?
Thanks for your help!

How to reproduce the issue

I convert my circuit from Qiskit to Qasm, which gives the following results:

OPENQASM 2.0;

include "qelib1.inc";

gate QAOA(param0,param1) q0,q1,q2,q3,q4 { h q0; h q1; h q2; h q3; h q4; cx q3,q0; rz(0.4) q0; cx q3,q0; cx q4,q0; rz(0.4) q0; cx q4,q0; cx q2,q1; rz(0.4) q1; cx q2,q1; cx q4,q1; rz(0.4) q1; cx q4,q1; cx q4,q2; rz(0.4) q2; cx q4,q2; cx q4,q3; rz(0.4) q3; cx q4,q3; rx(0.2) q0; rx(0.2) q1; rx(0.2) q2; rx(0.2) q3; rx(0.2) q4; }

qreg q[5];

QAOA(0.4,0.1) q[0],q[1],q[2],q[3],q[4];

Now I would like to create a Cirq circuit using the QasmParser from Cirq (https://github.com/quantumlib/Cirq/blob/master/cirq-core/cirq/contrib/qasm_import/qasm.py), but i get this Syntax Error Message:

~/.local/lib/python3.8/site-packages/cirq/contrib/qasm_import/qasm.py in circuit_from_qasm(qasm)
     28 
---> 29     return QasmParser().parse(qasm).circuit

~/.local/lib/python3.8/site-packages/cirq/contrib/qasm_import/_parser.py in parse(self, qasm)
    518             self.lexer.input(self.qasm)
--> 519             self.parsedQasm = self.parser.parse(lexer=self.lexer)
    520         return self.parsedQasm

~/.local/lib/python3.8/site-packages/ply/yacc.py in parse(self, input, lexer, debug, tracking, tokenfunc)
    332         else:
--> 333             return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
    334 

~/.local/lib/python3.8/site-packages/ply/yacc.py in parseopt_notrack(self, input, lexer, debug, tracking, tokenfunc)
   1200                         self.state = state
-> 1201                         tok = call_errorfunc(self.errorfunc, errtoken, self)
   1202                         if self.errorok:

~/.local/lib/python3.8/site-packages/ply/yacc.py in call_errorfunc(errorfunc, token, parser)
    191     _restart = parser.restart
--> 192     r = errorfunc(token)
    193     try:

~/.local/lib/python3.8/site-packages/cirq/contrib/qasm_import/_parser.py in p_error(self, p)
    499 
--> 500         raise QasmException(
    501             """Syntax error: '{}'

QasmException: Syntax error: '('
... QAOA(param0,param1) q0,q1,q2,q3,q4 { h q0; h q1; h q2; h q3; h q4; cx q3,q0; rz(0.4) q0; cx q3,q0; cx q4,q0; rz(0.4) q0; cx q4,q0; cx q2,q1; rz(0.4) q1; cx q2,q1; cx q4,q1; rz(0.4) q1; cx q4,q1; cx q4,q2; rz(0.4) q2; cx q4,q2; cx q4,q3; rz(0.4) q3; cx q4,q3; rx(0.2) q0; rx(0.2) q1; rx(0.2) q2; rx(0.2) q3; rx(0.2) q4; }
qreg q[5];
QAOA(0.4,0.1) q[0],q[1],q[2],q[3],q[4]
        ^
at line 3, column 10

Removing the parameters of QAOA-Gate ends in a Syntax-Error, too.
When i remove the custom gate and insert this into the parser everything works fine.

OPENQASM 2.0;

include "qelib1.inc";

qreg q[5];

h q[0]; h q[1]; h q[2]; h q[3]; h q[4]; cx q[3],q[0]; rz(0.4) q[0]; cx q[3],q[0]; cx q[4],q[0]; rz(0.4) q[0]; cx q[4],q[0]; cx q[2],q[1]; rz(0.4) q[1]; cx q[2],q[1]; cx q[4],q[1]; rz(0.4) q[1]; cx q[4],q[1]; cx q[4],q[2]; rz(0.4) q[2]; cx q[4],q[2]; cx q[4],q[3]; rz(0.4) q[3]; cx q[4],q[3]; rx(0.2) q[0]; rx(0.2) q[1]; rx(0.2) q[2]; rx(0.2) q[3]; rx(0.2) q[4];

Cirq version
You can get the cirq version by printing cirq.__version__. From the command line:

0.10.0
@lasys lasys added the kind/bug-report Something doesn't seem to work. label Sep 6, 2021
@vtomole
Copy link
Collaborator

vtomole commented Sep 6, 2021

QasmParser doesn't support the ability to define custom gates yet. See #3560. Please feel free to look into adding this if you'd like.

@vtomole vtomole added the triage/duplicate This issue or pull request is addressed by another issue or pull request label Sep 6, 2021
@lasys
Copy link
Author

lasys commented Sep 9, 2021

Alright.
I found a workaround that rewrites the Qasm-String without Custom Gates and finally can be parsed.
Thanks!

@lasys lasys closed this as completed Sep 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug-report Something doesn't seem to work. triage/duplicate This issue or pull request is addressed by another issue or pull request
Projects
None yet
Development

No branches or pull requests

2 participants