From e95815effd78f5f77c684d500c9223e696812c24 Mon Sep 17 00:00:00 2001 From: Theodor Isacsson Date: Mon, 29 Mar 2021 12:46:34 -0400 Subject: [PATCH 1/3] fix nonumeric in for --- blackbird_python/blackbird/listener.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/blackbird_python/blackbird/listener.py b/blackbird_python/blackbird/listener.py index b14f141..3483715 100644 --- a/blackbird_python/blackbird/listener.py +++ b/blackbird_python/blackbird/listener.py @@ -521,10 +521,13 @@ def exitForloop(self, ctx: blackbirdParser.ForloopContext): if c.getText() != ":" ]) elif ctx.vallist(): - for_var = [ - _expression(c.expression()) for c in ctx.vallist().getChildren() - if isinstance(c, blackbirdParser.ValContext) - ] + for_var = [] + for c in ctx.vallist().getChildren(): + if isinstance(c, blackbirdParser.ValContext): + if c.expression() is not None: + for_var.append(_expression(c.expression())) + elif c.nonnumeric() is not None: + for_var.append(_literal(c.nonnumeric())) for var in for_var: if ctx.NAME(): From cfe36f1a72084dcb043998987a27f7ed003bdea1 Mon Sep 17 00:00:00 2001 From: Theodor Isacsson Date: Mon, 29 Mar 2021 13:07:46 -0400 Subject: [PATCH 2/3] add test --- .../blackbird/tests/test_listener.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/blackbird_python/blackbird/tests/test_listener.py b/blackbird_python/blackbird/tests/test_listener.py index b63a00c..90d9c66 100644 --- a/blackbird_python/blackbird/tests/test_listener.py +++ b/blackbird_python/blackbird/tests/test_listener.py @@ -832,6 +832,24 @@ def test_for_loop(self, modes, parse_input_mocked_metadata): {'op': 'MeasureFock', 'args': [], 'kwargs': {}, 'modes': [modes[1]]}, {'op': 'MeasureFock', 'args': [], 'kwargs': {}, 'modes': [modes[2]]} ] + + def test_for_bool(self, parse_input_mocked_metadata): + """Test that a for-loop over a list containing bools is parsed correctly""" + bb = parse_input_mocked_metadata( + "for bool b in [True, False]\n\tMeasureFock() | 0" + ) + assert np.all( + bb._forvar["b"] == np.array([True, False]) + ) + + def test_for_str(self, parse_input_mocked_metadata): + """Test that a for-loop over a list containing strings is parsed correctly""" + bb = parse_input_mocked_metadata( + 'for str s in ["one", "two"]\n\tMeasureFock() | 0' + ) + assert np.all( + bb._forvar["s"] == np.array(["one", "two"]) + ) @pytest.mark.parametrize("rnge", ["2:10:3", "3:6"]) def test_for_range(self, rnge, parse_input_mocked_metadata): From be4f1d32957bd672a8b1835d70529016f228b50a Mon Sep 17 00:00:00 2001 From: Theodor Isacsson Date: Tue, 6 Apr 2021 16:45:40 -0400 Subject: [PATCH 3/3] update test --- blackbird_python/blackbird/tests/test_listener.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/blackbird_python/blackbird/tests/test_listener.py b/blackbird_python/blackbird/tests/test_listener.py index 90d9c66..0705cf9 100644 --- a/blackbird_python/blackbird/tests/test_listener.py +++ b/blackbird_python/blackbird/tests/test_listener.py @@ -832,16 +832,16 @@ def test_for_loop(self, modes, parse_input_mocked_metadata): {'op': 'MeasureFock', 'args': [], 'kwargs': {}, 'modes': [modes[1]]}, {'op': 'MeasureFock', 'args': [], 'kwargs': {}, 'modes': [modes[2]]} ] - + def test_for_bool(self, parse_input_mocked_metadata): """Test that a for-loop over a list containing bools is parsed correctly""" bb = parse_input_mocked_metadata( - "for bool b in [True, False]\n\tMeasureFock() | 0" + "for bool b in [True, False]\n\tUnaryGate(b, 0) | 0" ) assert np.all( bb._forvar["b"] == np.array([True, False]) ) - + def test_for_str(self, parse_input_mocked_metadata): """Test that a for-loop over a list containing strings is parsed correctly""" bb = parse_input_mocked_metadata(