From 8ae2d7d58ffa73a03387ab8e183355b1649c3253 Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Fri, 10 Jan 2025 14:45:34 +0100 Subject: [PATCH 1/6] Add EXTERN PORT Identifier statement to grammar to match spec. --- source/grammar/openpulseParser.g4 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/grammar/openpulseParser.g4 b/source/grammar/openpulseParser.g4 index 16c0fcd..6876381 100644 --- a/source/grammar/openpulseParser.g4 +++ b/source/grammar/openpulseParser.g4 @@ -30,6 +30,7 @@ openpulseStatement: | endStatement | expressionStatement | externStatement + | externPortStatement | forStatement | gateCallStatement | ifStatement @@ -59,3 +60,6 @@ scalarType: | PORT | FRAME ; + + +externPortStatement: EXTERN PORT Identifier SEMICOLON; From bcfdbaec00bab32323b8ca8fdc68aa07a23af0dc Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Fri, 10 Jan 2025 14:45:55 +0100 Subject: [PATCH 2/6] Add simple test for extern port statement. --- source/openpulse/tests/test_openpulse_parser.py | 1 + 1 file changed, 1 insertion(+) diff --git a/source/openpulse/tests/test_openpulse_parser.py b/source/openpulse/tests/test_openpulse_parser.py index b59836a..5dcb9f7 100644 --- a/source/openpulse/tests/test_openpulse_parser.py +++ b/source/openpulse/tests/test_openpulse_parser.py @@ -399,6 +399,7 @@ def test_permissive_parsing(capsys): port xy_port; port tx_port; port rx_port; + extern port cx_port; frame xy_frame = newframe(xy_port, 3714500000.0, 0); frame tx_frame = newframe(tx_port, 7883050000.0, 0); frame rx_frame = newframe(rx_port, 7883050000.0, 0); From 559439513a49a282d3cc7ba279243a4ab690c322 Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Fri, 10 Jan 2025 14:46:32 +0100 Subject: [PATCH 3/6] Add visitor for ExternPortStatement to parser. --- source/openpulse/openpulse/ast.py | 9 +++++++++ source/openpulse/openpulse/parser.py | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/source/openpulse/openpulse/ast.py b/source/openpulse/openpulse/ast.py index eca5cb3..6cdcedb 100644 --- a/source/openpulse/openpulse/ast.py +++ b/source/openpulse/openpulse/ast.py @@ -131,6 +131,15 @@ class CalibrationBlock(QASMNode): body: List[Statement] +@dataclass +class ExternPortStatement(QASMNode): + """ + Node representing an extern port statement. + """ + + name: Identifier + + # Override the class from openqasm3 @dataclass class CalibrationStatement(Statement): diff --git a/source/openpulse/openpulse/parser.py b/source/openpulse/openpulse/parser.py index 5f9c6d2..8dd6b7b 100644 --- a/source/openpulse/openpulse/parser.py +++ b/source/openpulse/openpulse/parser.py @@ -43,6 +43,7 @@ span, QASMNodeVisitor, _raise_from_context, + _visit_identifier, parse as parse_qasm3, ) from openqasm3.visitor import QASMVisitor @@ -194,6 +195,9 @@ def visitCalibrationBlock(self, ctx: openpulseParser.CalibrationBlockContext): body=[self.visit(statement) for statement in ctx.openpulseStatement()] ) + def visitExternPortStatement(self, ctx: openpulseParser.OpenpulseStatementContext): + return openpulse_ast.ExternPortStatement(name=_visit_identifier(ctx.Identifier())) + def visitScalarType(self, ctx: openpulseParser.ScalarTypeContext): if ctx.WAVEFORM() or ctx.PORT() or ctx.FRAME(): return self._visitPulseType(ctx) From c7ca174267786899628b3f086dc0ddc4db1b0c71 Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Fri, 10 Jan 2025 15:26:47 +0100 Subject: [PATCH 4/6] Add EXTERN FRAME Identifier statement to grammar to match spec. --- source/grammar/openpulseParser.g4 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/grammar/openpulseParser.g4 b/source/grammar/openpulseParser.g4 index 6876381..2ba4f33 100644 --- a/source/grammar/openpulseParser.g4 +++ b/source/grammar/openpulseParser.g4 @@ -30,6 +30,7 @@ openpulseStatement: | endStatement | expressionStatement | externStatement + | externFrameStatement | externPortStatement | forStatement | gateCallStatement @@ -62,4 +63,5 @@ scalarType: ; +externFrameStatement: EXTERN FRAME Identifier SEMICOLON; externPortStatement: EXTERN PORT Identifier SEMICOLON; From b7247bdcedb04a647aee31b8b1d9de4b65d9c770 Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Fri, 10 Jan 2025 15:27:10 +0100 Subject: [PATCH 5/6] Add visitor for ExternFrameStatement to parser. --- source/openpulse/openpulse/ast.py | 9 +++++++++ source/openpulse/openpulse/parser.py | 3 +++ 2 files changed, 12 insertions(+) diff --git a/source/openpulse/openpulse/ast.py b/source/openpulse/openpulse/ast.py index 6cdcedb..6801ed2 100644 --- a/source/openpulse/openpulse/ast.py +++ b/source/openpulse/openpulse/ast.py @@ -131,6 +131,15 @@ class CalibrationBlock(QASMNode): body: List[Statement] +@dataclass +class ExternFrameStatement(QASMNode): + """ + Node representing an extern frame statement. + """ + + name: Identifier + + @dataclass class ExternPortStatement(QASMNode): """ diff --git a/source/openpulse/openpulse/parser.py b/source/openpulse/openpulse/parser.py index 8dd6b7b..82e702f 100644 --- a/source/openpulse/openpulse/parser.py +++ b/source/openpulse/openpulse/parser.py @@ -195,6 +195,9 @@ def visitCalibrationBlock(self, ctx: openpulseParser.CalibrationBlockContext): body=[self.visit(statement) for statement in ctx.openpulseStatement()] ) + def visitExternFrameStatement(self, ctx: openpulseParser.OpenpulseStatementContext): + return openpulse_ast.ExternFrameStatement(name=_visit_identifier(ctx.Identifier())) + def visitExternPortStatement(self, ctx: openpulseParser.OpenpulseStatementContext): return openpulse_ast.ExternPortStatement(name=_visit_identifier(ctx.Identifier())) From e079cff21dd2de5967ac4e88b2c3ebaba598e54d Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Fri, 10 Jan 2025 15:27:42 +0100 Subject: [PATCH 6/6] Add simple test for extern frame statement. --- source/openpulse/tests/test_openpulse_parser.py | 1 + 1 file changed, 1 insertion(+) diff --git a/source/openpulse/tests/test_openpulse_parser.py b/source/openpulse/tests/test_openpulse_parser.py index 5dcb9f7..475f9e0 100644 --- a/source/openpulse/tests/test_openpulse_parser.py +++ b/source/openpulse/tests/test_openpulse_parser.py @@ -403,6 +403,7 @@ def test_permissive_parsing(capsys): frame xy_frame = newframe(xy_port, 3714500000.0, 0); frame tx_frame = newframe(tx_port, 7883050000.0, 0); frame rx_frame = newframe(rx_port, 7883050000.0, 0); + extern frame cx_frame; waveform rabi_pulse_wf = gaussian(1e-07, 2.5e-08, 1.0, 0.0); waveform readout_waveform_wf = constant(5e-06, 0.03); waveform readout_kernel_wf = constant(5e-06, 1.0);