Open therooler opened 3 years ago
Thanks @therooler, very helpful feedback!
Flagging @josh146 so he sees as well
Thanks @therooler! This is really good to know.
I think this requires fixes in two places:
Updates to the grammar, so that when lexing, there is a better distinction made between Operators, Terms, Gates, Tensor products.
Updates to the parser to correctly create the correct IR classes.
This feels like something that should be modified more at the lexing level; the parser should simply be using the generated tokens to map directly from the AST to the IR.
I tried to write a parser that takes the QasmProgram output, grabs the operators defined in the
tfi_chain.qasm
andxxz_chain.qasm
file and then translates these to cirq.Paulisum objects.It took me while to figure out a way to do this, but the following code does the job:
The things I struggled with were the differences between Terms, Gates, Ops and TensorOps objects. For instance, this is a
TensorOp
:, which is fine. But this is a
Term
, even though it is not a linear combinations of terms. This is a 'Gate':
, but it should be an
Op
right?In the next example, the first line is again a
Gate
, not aTerm
, even though the second line defines aTerm
.Same here:
, neither are
Term
s, butGate
s instead.All of this can make it confusing how to add things, because
Term
s have acoeff
attribute, andGate
s orOp
s do not. My suggestion is that if we have multiple lines in the operator definition, then each line is aTerm
so that it has acoeff
attribute. Or you could always define operators asTerms
, even if they contain a single operator definition. I hope this makes sense.