Skip to content

Commit

Permalink
shots=None will not execute any measurement (#682)
Browse files Browse the repository at this point in the history
* impl+test [sf] - `shots=None`

* changelog entry

* fix typo

Co-authored-by: Theodor <theodor@xanadu.ai>

* test [tdmprogram] - test_shots_passed and test_shots_run_options

Co-authored-by: Theodor <theodor@xanadu.ai>
  • Loading branch information
sduquemesa and thisac committed Feb 16, 2022
1 parent 77475b0 commit 9e6eff1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
4 changes: 3 additions & 1 deletion .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* A `Device.certificate` method is added which returns the hardware device certificate.
[(#679)](https://github.com/XanaduAI/strawberryfields/pull/679)

* Setting `shots=None` in the engine or program run options will not execute any measurements applied on the circuit. [(#682)](https://github.com/XanaduAI/strawberryfields/pull/682)

<h3>Breaking Changes</h3>

* `DeviceSpec` is renamed to `Device`.
Expand All @@ -33,7 +35,7 @@

This release contains contributions from (in alphabetical order):

Theodor Isacsson, Jon Schlipf, Hossein Seifoory
Sebastian Duque, Theodor Isacsson, Jon Schlipf, Hossein Seifoory

# Release 0.21.0 (current release)

Expand Down
8 changes: 5 additions & 3 deletions strawberryfields/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,14 @@ def run(self, program, *, args=None, compile_options=None, **kwargs):
# if a tdm program is input in a rolled state, then unroll it
if not program.is_unrolled:
received_rolled_circuit = True
program.unroll(shots=shots)
# if shots = None, then set shots=1 here only to unroll the program
program.unroll(shots=shots if shots is not None else 1)

# Shots >1 for a TDM program simply corresponds to creating
# multiple copies of the program, and appending them to run sequentially.
# As a result, we set the backend shots to 1 for the Gaussian backend.
kwargs["shots"] = 1
# As a result, we set the backend shots to 1 for the Gaussian backend
# unless shots was set to `None`.
kwargs["shots"] = 1 if shots else None

args = args or {}
compile_options = compile_options or {}
Expand Down
6 changes: 6 additions & 0 deletions strawberryfields/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ def apply(self, reg, backend, **kwargs):
shots (int): Number of independent evaluations to perform.
Only applies to Measurements.
"""
# Do not apply any measurement operation in the circuit
# if `shots` is set to None
_shots = kwargs.get("shots", 1)
if _shots is None:
return None

values = super().apply(reg, backend, **kwargs)

# store the results in the register reference objects
Expand Down
15 changes: 8 additions & 7 deletions tests/frontend/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ def test_shots_default(self):
results = eng.run(prog)
assert len(results.samples) == 1

def test_shots_run_options(self):
@pytest.mark.parametrize("shots,len_samples", [(None, 0), (5, 5)])
def test_shots_run_options(self, shots, len_samples):
"""Test that run_options takes precedence over default"""
prog = sf.Program(1)
eng = sf.Engine("gaussian")
Expand All @@ -93,23 +94,23 @@ def test_shots_run_options(self):
ops.Sgate(0.5) | q[0]
ops.MeasureFock() | q

prog.run_options = {"shots": 5}
prog.run_options = {"shots": shots}
results = eng.run(prog)
assert len(results.samples) == 5
assert len(results.samples) == len_samples

def test_shots_passed(self):
@pytest.mark.parametrize("shots,len_samples", [(None, 0), (2, 2)])
def test_shots_passed(self, shots, len_samples):
"""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 len(results.samples) == 2
results = eng.run(prog, shots=shots)
assert len(results.samples) == len_samples
assert prog.run_options["shots"] == 5

def test_history(self, eng, prog):
Expand Down
14 changes: 8 additions & 6 deletions tests/frontend/test_tdmprogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,8 @@ def test_shots_default(self):
results = eng.run(prog)
assert len(results.samples) == 1

def test_shots_run_options(self):
@pytest.mark.parametrize("shots,len_samples", [(None, 0), (5, 5)])
def test_shots_run_options(self, shots, len_samples):
"""Test that run_options takes precedence over default"""
prog = sf.TDMProgram(2)
eng = sf.Engine("gaussian")
Expand All @@ -711,11 +712,12 @@ def test_shots_run_options(self):
ops.Sgate(p[0]) | q[0]
ops.MeasureHomodyne(p[1]) | q[0]

prog.run_options = {"shots": 5}
prog.run_options = {"shots": shots}
results = eng.run(prog)
assert len(results.samples) == 5
assert len(results.samples) == len_samples

def test_shots_passed(self):
@pytest.mark.parametrize("shots,len_samples", [(None, 0), (2, 2)])
def test_shots_passed(self, shots, len_samples):
"""Test that shots supplied via eng.run takes precedence over
run_options and that run_options isn't changed"""
prog = sf.TDMProgram(2)
Expand All @@ -726,8 +728,8 @@ def test_shots_passed(self):
ops.MeasureHomodyne(p[1]) | q[0]

prog.run_options = {"shots": 5}
results = eng.run(prog, shots=2)
assert len(results.samples) == 2
results = eng.run(prog, shots=shots)
assert len(results.samples) == len_samples
assert prog.run_options["shots"] == 5

def test_shots_with_timebins_non_multiple_of_concurrent_modes(self):
Expand Down

0 comments on commit 9e6eff1

Please sign in to comment.