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

Add support for DEFGATE ... AS PAULI-SUM #1125

Merged
merged 14 commits into from
Dec 18, 2019
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Changelog
instead the (equivalent) program with `RESET` omitted (@kilimanjaro, gh-1118).
- Broadened the scope of `flake8` compliance to the include the `examples` and
`docs` directories, and thus the whole repository (@tommy-moffat, gh-1113).
- `DEFGATE ... AS PAULI-SUM` is now supported (@ecpeterson, gh-1125).

### Bugfixes

Expand Down
26 changes: 24 additions & 2 deletions pyquil/_parser/PyQuilListener.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@

from pyquil.gates import QUANTUM_GATES
from pyquil.quilatom import (Addr, MemoryReference, Parameter, quil_cos, quil_cis, quil_exp,
quil_sin, quil_sqrt)
from pyquil.quilbase import (Gate, DefGate, DefPermutationGate, Measurement, JumpTarget, Label, Expression,
quil_sin, quil_sqrt, FormalArgument)
from pyquil.quilbase import (Gate, DefGate, DefPermutationGate, DefGateByPaulis,
Measurement, JumpTarget, Label, Expression,
Nop, Halt, Jump, JumpWhen, JumpUnless, Reset, Wait,
ClassicalNot, ClassicalNeg, ClassicalAnd, ClassicalInclusiveOr,
ClassicalExclusiveOr,
Expand Down Expand Up @@ -118,6 +119,14 @@ def exitDefGate(self, ctx: QuilParser.DefGateContext):
parameters = [_variable(v) for v in ctx.variable()]
self.result.append(DefGate(gate_name, matrix, parameters))

def exitDefGateAsPauli(self, ctx: QuilParser.DefGateAsPauliContext):
from pyquil.paulis import PauliSum
gate_name = ctx.name().getText()
parameters = [_variable(c) for c in ctx.variable()]
arguments = [_formalQubit(q) for q in ctx.qubitVariable()]
body = PauliSum([_pauliTerm(t) for t in ctx.pauliTerms().pauliTerm()])
self.result.append(DefGateByPaulis(gate_name, parameters, arguments, body))

# DEFCIRCUIT parsing:
# When we enter a circuit definition we create a backup of the instructions seen up to that point. Then, when the
# listener continues walking through the circuit instructions it will add to an empty list. Once we leave the
Expand Down Expand Up @@ -397,6 +406,19 @@ def exitMemoryDescriptor(self, ctx):
"""


def _formalQubit(fq):
return FormalArgument(fq.getText())


def _pauliTerm(term):
# type: (QuilParser.PauliTermContext) -> PauliTerm
from pyquil.paulis import PauliTerm
letters = term.IDENTIFIER().getText()
args = [_formalQubit(q) for q in term.qubitVariable()]
coeff = _expression(term.expression())
return PauliTerm.from_list(list(zip(letters, args)), coeff)


def _qubit(qubit):
# type: (QuilParser.QubitContext) -> Qubit
return Qubit(int(qubit.getText()))
Expand Down
6 changes: 6 additions & 0 deletions pyquil/_parser/Quil.g4
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ grammar Quil;
quil : allInstr? ( NEWLINE+ allInstr )* NEWLINE* EOF ;

allInstr : defGate
| defGateAsPauli
| defCircuit
| instr
;
Expand Down Expand Up @@ -47,6 +48,7 @@ modifier : CONTROLLED
// D. Gate Definitions

defGate : DEFGATE name (( LPAREN variable ( COMMA variable )* RPAREN ) | ( AS gatetype ))? COLON NEWLINE matrix ;
defGateAsPauli : DEFGATE name ( LPAREN variable ( COMMA variable )* RPAREN )? qubitVariable+ AS PAULISUM COLON NEWLINE pauliTerms ;

variable : PERCENTAGE IDENTIFIER ;
gatetype : MATRIX
Expand All @@ -55,6 +57,9 @@ gatetype : MATRIX
matrix : ( matrixRow NEWLINE )* matrixRow ;
matrixRow : TAB expression ( COMMA expression )* ;

pauliTerms : ( pauliTerm NEWLINE )* pauliTerm;
pauliTerm : TAB IDENTIFIER LPAREN expression RPAREN qubitVariable+ ;

// E. Circuits

defCircuit : DEFCIRCUIT name ( LPAREN variable ( COMMA variable )* RPAREN )? qubitVariable* COLON NEWLINE circuit ;
Expand Down Expand Up @@ -171,6 +176,7 @@ OFFSET : 'OFFSET' ;
AS : 'AS' ;
MATRIX : 'MATRIX' ;
PERMUTATION : 'PERMUTATION' ;
PAULISUM : 'PAULI-SUM';

NEG : 'NEG' ;
NOT : 'NOT' ;
Expand Down
216 changes: 109 additions & 107 deletions pyquil/_parser/gen3/Quil.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,64 @@ OFFSET=16
AS=17
MATRIX=18
PERMUTATION=19
NEG=20
NOT=21
TRUE=22
FALSE=23
AND=24
IOR=25
XOR=26
OR=27
ADD=28
SUB=29
MUL=30
DIV=31
MOVE=32
EXCHANGE=33
CONVERT=34
EQ=35
GT=36
GE=37
LT=38
LE=39
LOAD=40
STORE=41
PI=42
I=43
SIN=44
COS=45
SQRT=46
EXP=47
CIS=48
PLUS=49
MINUS=50
TIMES=51
DIVIDE=52
POWER=53
CONTROLLED=54
DAGGER=55
FORKED=56
IDENTIFIER=57
INT=58
FLOAT=59
STRING=60
PERIOD=61
COMMA=62
LPAREN=63
RPAREN=64
LBRACKET=65
RBRACKET=66
COLON=67
PERCENTAGE=68
AT=69
QUOTE=70
UNDERSCORE=71
TAB=72
NEWLINE=73
COMMENT=74
SPACE=75
INVALID=76
PAULISUM=20
NEG=21
NOT=22
TRUE=23
FALSE=24
AND=25
IOR=26
XOR=27
OR=28
ADD=29
SUB=30
MUL=31
DIV=32
MOVE=33
EXCHANGE=34
CONVERT=35
EQ=36
GT=37
GE=38
LT=39
LE=40
LOAD=41
STORE=42
PI=43
I=44
SIN=45
COS=46
SQRT=47
EXP=48
CIS=49
PLUS=50
MINUS=51
TIMES=52
DIVIDE=53
POWER=54
CONTROLLED=55
DAGGER=56
FORKED=57
IDENTIFIER=58
INT=59
FLOAT=60
STRING=61
PERIOD=62
COMMA=63
LPAREN=64
RPAREN=65
LBRACKET=66
RBRACKET=67
COLON=68
PERCENTAGE=69
AT=70
QUOTE=71
UNDERSCORE=72
TAB=73
NEWLINE=74
COMMENT=75
SPACE=76
INVALID=77
'DEFGATE'=1
'DEFCIRCUIT'=2
'MEASURE'=3
Expand All @@ -93,53 +94,54 @@ INVALID=76
'AS'=17
'MATRIX'=18
'PERMUTATION'=19
'NEG'=20
'NOT'=21
'TRUE'=22
'FALSE'=23
'AND'=24
'IOR'=25
'XOR'=26
'OR'=27
'ADD'=28
'SUB'=29
'MUL'=30
'DIV'=31
'MOVE'=32
'EXCHANGE'=33
'CONVERT'=34
'EQ'=35
'GT'=36
'GE'=37
'LT'=38
'LE'=39
'LOAD'=40
'STORE'=41
'pi'=42
'i'=43
'SIN'=44
'COS'=45
'SQRT'=46
'EXP'=47
'CIS'=48
'+'=49
'-'=50
'*'=51
'/'=52
'^'=53
'CONTROLLED'=54
'DAGGER'=55
'FORKED'=56
'.'=61
','=62
'('=63
')'=64
'['=65
']'=66
':'=67
'%'=68
'@'=69
'"'=70
'_'=71
' '=72
' '=75
'PAULI-SUM'=20
'NEG'=21
'NOT'=22
'TRUE'=23
'FALSE'=24
'AND'=25
'IOR'=26
'XOR'=27
'OR'=28
'ADD'=29
'SUB'=30
'MUL'=31
'DIV'=32
'MOVE'=33
'EXCHANGE'=34
'CONVERT'=35
'EQ'=36
'GT'=37
'GE'=38
'LT'=39
'LE'=40
'LOAD'=41
'STORE'=42
'pi'=43
'i'=44
'SIN'=45
'COS'=46
'SQRT'=47
'EXP'=48
'CIS'=49
'+'=50
'-'=51
'*'=52
'/'=53
'^'=54
'CONTROLLED'=55
'DAGGER'=56
'FORKED'=57
'.'=62
','=63
'('=64
')'=65
'['=66
']'=67
':'=68
'%'=69
'@'=70
'"'=71
'_'=72
' '=73
' '=76
Loading