Skip to content

Commit

Permalink
Measure threshold for displaced Gaussian states (#615)
Browse files Browse the repository at this point in the history
* displaced Gaussian states work with measure threshold, (needs thewalrus >=0.16.0)

* require dev version of thewalrus

* update changelog

* update requirements

* updates thewalrus in requirements

Co-authored-by: Nicolas Quesada <zeitus@gmail.com>
  • Loading branch information
jakeffbulmer and nquesada committed Aug 19, 2021
1 parent 45309dc commit 8168612
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,13 @@
broadcasting.
[(#567)](https://github.com/XanaduAI/strawberryfields/pull/567)

* `measure_threshold` in the `gaussian` backend now supports displaced Gaussian states.
[(#615)](https://github.com/XanaduAI/strawberryfields/pull/615)

* Speed improvements to ``gaussian_unitary`` compiler
[(#603)](https://github.com/XanaduAI/strawberryfields/pull/603)


<h3>Breaking Changes</h3>

<h3>Bug fixes</h3>
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ tensorboard>=2.0
networkx>=2.0
git+https://github.com/XanaduAI/blackbird@master#egg=quantum-blackbird
python-dateutil==2.8.0
thewalrus>=0.15.0
thewalrus>=0.16.0
toml
appdirs
numba>=0.48.0
Expand Down
9 changes: 4 additions & 5 deletions strawberryfields/backends/gaussianbackend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,15 @@ def measure_threshold(self, modes, shots=1, select=None, **kwargs):

mu = self.circuit.mean
cov = self.circuit.scovmatxp()
mean = self.circuit.smeanxp()
# check we are sampling from a gaussian state with zero mean
if not allclose(mu, zeros_like(mu)):
raise NotImplementedError(
"Threshold measurement is only supported for " "Gaussian states with zero mean"
)

x_idxs = array(modes)
p_idxs = x_idxs + len(mu)
modes_idxs = concatenate([x_idxs, p_idxs])
reduced_cov = cov[ix_(modes_idxs, modes_idxs)]
samples = torontonian_sample_state(reduced_cov, shots)
reduced_mean = mean[modes_idxs]
samples = torontonian_sample_state(mu=reduced_mean, cov=reduced_cov, samples=shots)

return samples

Expand Down
7 changes: 4 additions & 3 deletions tests/backend/test_threshold_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,21 @@ def test_vacuum_measurements(self, setup_backend, pure):
meas = backend.measure_threshold([0, 1, 2])[0]
assert np.all(np.array(meas) == 0)


def test_binary_outcome(self, setup_backend, pure):
@pytest.mark.parametrize("alpha", [0, 1, 0.5+0.5j])
def test_binary_outcome(self, setup_backend, pure, alpha):
"""Test that the outcomes of a threshold measurement is zero or one."""
num_modes = 2
for _ in range(NUM_REPEATS):
backend = setup_backend(num_modes)
backend.reset(pure=pure)

r = 0.5

backend.squeeze(r, 0, 0)
backend.displacement(abs(alpha), np.angle(alpha), 0)
backend.beamsplitter(np.pi/4, np.pi, 0, 1)
meas_modes = [0, 1]
meas_results = backend.measure_threshold(meas_modes)

for i in range(num_modes):
assert meas_results[0][i] == 0 or meas_results[0][i] == 1

0 comments on commit 8168612

Please sign in to comment.