rigetti / pyquil

A Python library for quantum programming using Quil.
http://docs.rigetti.com
Apache License 2.0
1.4k stars 341 forks source link

Running a parameterized program with value-symbols errors out #190

Closed jotterbach closed 6 years ago

jotterbach commented 6 years ago

I'm trying to parse a plain Quil program using pyquil.parser:

PRAGMA EXPECTED_REWIRING "#(0 1 2 3 4 5 6 7)"
RZ(-pi/2) 2
RX(-pi/2) 2
CZ 3 2
RZ(-pi/2) 3
RX(-pi/2) 3
RZ(-2.968811991889793) 3
RX(pi/2) 3
CZ 3 2
RZ(-pi/2) 4
RX(-pi/2) 4
CZ 5 4
RZ(-pi/2) 5
RX(-pi/2) 5
RZ(-3.055202322739793) 5
RX(pi/2) 5
CZ 5 4
RZ(-pi/2) 6
RX(-pi/2) 6
CZ 7 6
RZ(-pi/2) 7
RX(-pi/2) 7
RZ(-3.055202322739793) 7
RX(pi/2) 7
CZ 7 6
RZ(pi/2) 0
RX(pi/2) 0
CZ 1 0
RZ(-pi/2) 1
RX(-pi/2) 1
RZ(-2.968811991889793) 1
RX(pi/2) 1
CZ 1 0
RX(-pi/2) 1
RX(pi/2) 2
CZ 2 1
RX(-pi/2) 2
RZ(0.17278066170000006) 2
RX(pi/2) 2
CZ 2 1
RX(-pi/2) 3
RX(pi/2) 4
CZ 4 3
RX(-pi/2) 4
RZ(0.17278066170000006) 4
RX(pi/2) 4
CZ 4 3
RX(-pi/2) 5
RX(pi/2) 6
CZ 6 5
RX(-pi/2) 6
RZ(0.17278066170000006) 6
RX(pi/2) 6
CZ 6 5
CZ 7 0
RX(-pi/2) 7
RZ(0.2591709925500001) 7
RX(pi/2) 7
CZ 7 0
RX(pi/2) 0
RZ(0.2649) 0
RX(-pi/2) 0
RZ(-pi/2) 0
RX(-pi/2) 1
RZ(-2.876692653589793) 1
RX(-pi/2) 1
RZ(-pi/2) 1
RZ(-0.2648999999999999) 2
RX(-pi/2) 2
RZ(pi/2) 2
RX(-pi/2) 3
RZ(-2.876692653589793) 3
RX(-pi/2) 3
RZ(-pi/2) 3
RZ(-0.2648999999999999) 4
RX(-pi/2) 4
RZ(pi/2) 4
RX(-pi/2) 5
RZ(-2.876692653589793) 5
RX(-pi/2) 5
RZ(-pi/2) 5
RZ(-0.2648999999999999) 6
RX(-pi/2) 6
RZ(pi/2) 6
RZ(-0.2648999999999999) 7
RX(-pi/2) 7
RZ(pi/2) 7
PRAGMA CURRENT_REWIRING "#(0 1 2 3 4 5 6 7)"

however I get an error:


~/code/pyquil/pyquil/parser.py in parse_program(quil)
     29     :return: PyQuil Program object
     30     """
---> 31     return Program(parse(quil))
     32 
     33 

~/code/pyquil/pyquil/parser.py in parse(quil)
     39     :return: list of instructions
     40     """
---> 41     return run_parser(quil.strip())

~/code/pyquil/pyquil/_parser/PyQuilListener.py in run_parser(quil)
     44     parser.removeErrorListeners()
     45     parser.addErrorListener(CustomErrorListener())
---> 46     tree = parser.quil()
     47 
     48     # Step 3: Run the Listener

~/code/pyquil/pyquil/_parser/gen3/QuilParser.py in quil(self)
    388 
    389                 self.state = 92
--> 390                 self.allInstr()
    391                 self.state = 97
    392                 self._errHandler.sync(self)

~/code/pyquil/pyquil/_parser/gen3/QuilParser.py in allInstr(self)
    456                 self.enterOuterAlt(localctx, 3)
    457                 self.state = 102
--> 458                 self.instr()
    459                 pass
    460             else:

~/code/pyquil/pyquil/_parser/gen3/QuilParser.py in instr(self)
    556                 self.enterOuterAlt(localctx, 1)
    557                 self.state = 105
--> 558                 self.gate()
    559                 pass
    560             elif token in [QuilParser.MEASURE]:

~/code/pyquil/pyquil/_parser/gen3/QuilParser.py in gate(self)
    700                 self.match(QuilParser.LPAREN)
    701                 self.state = 123
--> 702                 self.param()
    703                 self.state = 128
    704                 self._errHandler.sync(self)

~/code/pyquil/pyquil/_parser/gen3/QuilParser.py in param(self)
    859                 self.enterOuterAlt(localctx, 2)
    860                 self.state = 145
--> 861                 self.expression(0)
    862                 pass
    863             else:

~/code/pyquil/pyquil/_parser/gen3/QuilParser.py in expression(self, _p)
   2728                 _prevctx = localctx
   2729                 self.state = 335
-> 2730                 self.number()
   2731                 pass
   2732             elif token in [QuilParser.PERCENTAGE]:

~/code/pyquil/pyquil/_parser/gen3/QuilParser.py in number(self)
   2940         except RecognitionException as re:
   2941             localctx.exception = re
-> 2942             self._errHandler.reportError(self, re)
   2943             self._errHandler.recover(self, re)
   2944         finally:

~/.virtualenvs/analysis-py36/lib/python3.6/site-packages/antlr4/error/ErrorStrategy.py in reportError(self, recognizer, e)
    122         self.beginErrorCondition(recognizer)
    123         if isinstance( e, NoViableAltException ):
--> 124             self.reportNoViableAlternative(recognizer, e)
    125         elif isinstance( e, InputMismatchException ):
    126             self.reportInputMismatch(recognizer, e)

~/.virtualenvs/analysis-py36/lib/python3.6/site-packages/antlr4/error/ErrorStrategy.py in reportNoViableAlternative(self, recognizer, e)
    248             input = "<unknown input>"
    249         msg = "no viable alternative at input " + self.escapeWSAndQuote(input)
--> 250         recognizer.notifyErrorListeners(msg, e.offendingToken, e)
    251 
    252     #

~/.virtualenvs/analysis-py36/lib/python3.6/site-packages/antlr4/Parser.py in notifyErrorListeners(self, msg, offendingToken, e)
    304         column = offendingToken.column
    305         listener = self.getErrorListenerDispatch()
--> 306         listener.syntaxError(self, offendingToken, line, column, msg, e)
    307 
    308     #

~/.virtualenvs/analysis-py36/lib/python3.6/site-packages/antlr4/error/ErrorListener.py in syntaxError(self, recognizer, offendingSymbol, line, column, msg, e)
     58     def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
     59         for delegate in self.delegates:
---> 60             delegate.syntaxError(recognizer, offendingSymbol, line, column, msg, e)
     61 
     62     def reportAmbiguity(self, recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs):

~/code/pyquil/pyquil/_parser/PyQuilListener.py in syntaxError(self, recognizer, offendingSymbol, line, column, msg, e)
     61         raise RuntimeError(
     62             "Error encountered while parsing the quil program at line {} and column {}\n".format(line, column + 1) +
---> 63             "Received an '{}' but was expecting one of [ {} ]".format(offendingSymbol.text, ', '.join(expected_tokens))
     64         )
     65 

RuntimeError: Error encountered while parsing the quil program at line 2 and column 5
Received an 'pi' but was expecting one of [ 'pi', 'i', '+', '-', UNSIGNED_INT, UNSIGNED_FLOAT ]```
stevenheidel commented 6 years ago

-pi/2 is the problem here, pi/2 works though