From 83d686e93ef3e85be2629a2534d708410d7b77e9 Mon Sep 17 00:00:00 2001 From: Mark Skilbeck Date: Mon, 12 Nov 2018 15:41:50 -0800 Subject: [PATCH] Fix parsing DEFCIRCUIT without qubits Parsing a DEFCIRCUIT which has no qubit parameters outputs an extraneous spacing before the colon: ``` >>> from pyquil.parser import parse >>> from pyquil._parser.PyQuilListener import run_parser >>> from pyquil.quilbase import RawInstr >>> print(run_parser("DEFCIRCUIT TEST:\n MEASURE 0 ro\n"))' [] ``` --- pyquil/_parser/PyQuilListener.py | 5 +++-- pyquil/tests/test_parser.py | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pyquil/_parser/PyQuilListener.py b/pyquil/_parser/PyQuilListener.py index 0c07ee293..39d5b08f3 100644 --- a/pyquil/_parser/PyQuilListener.py +++ b/pyquil/_parser/PyQuilListener.py @@ -128,11 +128,12 @@ def exitDefCircuit(self, ctx: QuilParser.DefCircuitContext): circuit_name = ctx.name().getText() variables = [variable.getText() for variable in ctx.variable()] qubitVariables = [qubitVariable.getText() for qubitVariable in ctx.qubitVariable()] + space = ' ' if qubitVariables else '' if variables: - raw_defcircuit = 'DEFCIRCUIT {}({}) {}:'.format(circuit_name, ', '.join(variables), ' '.join(qubitVariables)) + raw_defcircuit = 'DEFCIRCUIT {}({}){}{}:'.format(circuit_name, ', '.join(variables), space, ' '.join(qubitVariables)) else: - raw_defcircuit = 'DEFCIRCUIT {} {}:'.format(circuit_name, ' '.join(qubitVariables)) + raw_defcircuit = 'DEFCIRCUIT {}{}{}:'.format(circuit_name, space, ' '.join(qubitVariables)) raw_defcircuit += '\n '.join([''] + [instr.out() for instr in self.result]) self.previous_result.append(RawInstr(raw_defcircuit)) diff --git a/pyquil/tests/test_parser.py b/pyquil/tests/test_parser.py index a37adaabe..7994e7844 100644 --- a/pyquil/tests/test_parser.py +++ b/pyquil/tests/test_parser.py @@ -212,8 +212,14 @@ def test_def_circuit(): DEFCIRCUIT bell a b: H a CNOT a b +""".strip() + defcircuit_no_qubits = """ +DEFCIRCUIT bell: + H 0 + CNOT 0 1 """.strip() parse_equals(defcircuit, RawInstr(defcircuit)) + parse_equals(defcircuit_no_qubits, RawInstr(defcircuit_no_qubits)) def test_parse_reset_qubit():