From bb4ab9f3eb794a2f28b71d8aef42aafdd85b02e8 Mon Sep 17 00:00:00 2001 From: Theodor Isacsson Date: Fri, 4 Dec 2020 15:36:16 -0500 Subject: [PATCH 1/3] Fix run_options not being used --- strawberryfields/engine.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/strawberryfields/engine.py b/strawberryfields/engine.py index 82a7343ea..eb349fd4e 100644 --- a/strawberryfields/engine.py +++ b/strawberryfields/engine.py @@ -424,8 +424,9 @@ def run(self, program, *, args=None, compile_options=None, **kwargs): not isinstance(program, collections.abc.Sequence) and program.type == "tdm" ) if valid_tdm_program: + # priority order for the shots value should be kwargs > run_options > 1 + shots = kwargs.get("shots", program.run_options.get("shots", 1)) - shots = kwargs.get("shots", 1) program.unroll(shots=shots) # Shots >1 for a TDM program simply corresponds to creating # multiple copies of the program, and appending them to run sequentially. From d215d2ef1ba251dcc71cc776e8e6260597bdd928 Mon Sep 17 00:00:00 2001 From: Theodor Isacsson Date: Fri, 4 Dec 2020 18:42:41 -0500 Subject: [PATCH 2/3] Update changelog --- .github/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index a1b5b1109..6cbd07d00 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -55,6 +55,9 @@ information about the results. [(#493)](https://github.com/XanaduAI/strawberryfields/pull/493) +* `TDMProgram.run_options` is now correctly used when running a TDM program. + [(#500)](https://github.com/XanaduAI/strawberryfields/pull/500) +

Documentation

Contributors

From 09d2b37e0b683252d32a22d8c7cacab595796518 Mon Sep 17 00:00:00 2001 From: Theodor Isacsson Date: Mon, 7 Dec 2020 17:29:44 -0500 Subject: [PATCH 3/3] Add tests --- tests/frontend/test_engine.py | 40 ++++++++++++++++++++++++++++ tests/frontend/test_tdmprogram.py | 43 +++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/tests/frontend/test_engine.py b/tests/frontend/test_engine.py index ae3e6792c..c80a6fc9a 100644 --- a/tests/frontend/test_engine.py +++ b/tests/frontend/test_engine.py @@ -69,6 +69,46 @@ def test_bad_backend(self): class TestEngineProgramInteraction: """Test the Engine class and its interaction with Program instances.""" + def test_shots_default(self): + """Test that default shots (1) is used""" + prog = sf.Program(1) + eng = sf.Engine("gaussian") + + with prog.context as q: + ops.Sgate(0.5) | q[0] + ops.MeasureFock() | q + + results = eng.run(prog) + assert results.samples.shape[0] == 1 + + def test_shots_run_options(self): + """Test that run_options takes precedence over default""" + prog = sf.Program(1) + eng = sf.Engine("gaussian") + + with prog.context as q: + ops.Sgate(0.5) | q[0] + ops.MeasureFock() | q + + prog.run_options = {"shots": 5} + results = eng.run(prog) + assert results.samples.shape[0] == 5 + + def test_shots_passed(self): + """Test that shots supplied via eng.run takes precedence over + run_options and that run_options isn't changed""" + prog = sf.Program(1) + eng = sf.Engine("gaussian") + + with prog.context as q: + ops.Sgate(0.5) | q[0] + ops.MeasureFock() | q + + prog.run_options = {"shots": 5} + results = eng.run(prog, shots=2) + assert results.samples.shape[0] == 2 + assert prog.run_options["shots"] == 5 + def test_history(self, eng, prog): """Engine history.""" # no programs have been run diff --git a/tests/frontend/test_tdmprogram.py b/tests/frontend/test_tdmprogram.py index 018a622fe..58d5c23e7 100644 --- a/tests/frontend/test_tdmprogram.py +++ b/tests/frontend/test_tdmprogram.py @@ -706,3 +706,46 @@ def test_move_vac_modes(self, N, crop, expected): res = move_vac_modes(samples, N, crop=crop) assert np.all(res == expected) + +class TestEngineTDMProgramInteraction: + """Test the Engine class and its interaction with TDMProgram instances.""" + + def test_shots_default(self): + """Test that default shots (1) is used""" + prog = sf.TDMProgram(2) + eng = sf.Engine("gaussian") + + with prog.context([1,2], [3,4]) as (p, q): + ops.Sgate(p[0]) | q[0] + ops.MeasureHomodyne(p[1]) | q[0] + + results = eng.run(prog) + assert results.samples.shape[0] == 1 + + def test_shots_run_options(self): + """Test that run_options takes precedence over default""" + prog = sf.TDMProgram(2) + eng = sf.Engine("gaussian") + + with prog.context([1,2], [3,4]) as (p, q): + ops.Sgate(p[0]) | q[0] + ops.MeasureHomodyne(p[1]) | q[0] + + prog.run_options = {"shots": 5} + results = eng.run(prog) + assert results.samples.shape[0] == 5 + + def test_shots_passed(self): + """Test that shots supplied via eng.run takes precedence over + run_options and that run_options isn't changed""" + prog = sf.TDMProgram(2) + eng = sf.Engine("gaussian") + + with prog.context([1,2], [3,4]) as (p, q): + ops.Sgate(p[0]) | q[0] + ops.MeasureHomodyne(p[1]) | q[0] + + prog.run_options = {"shots": 5} + results = eng.run(prog, shots=2) + assert results.samples.shape[0] == 2 + assert prog.run_options["shots"] == 5