XanaduAI / strawberryfields

Strawberry Fields is a full-stack Python library for designing, simulating, and optimizing continuous variable (CV) quantum optical circuits.
https://strawberryfields.ai
Apache License 2.0
747 stars 186 forks source link

[unitaryhack] Add support for threshold detection of Gaussian states with displacement #572

Closed josh146 closed 2 years ago

josh146 commented 3 years ago

This issue has been tagged for contributions during unitaryHACK

When using the Gaussian backend, the MeasureThreshold measurement operation is currently not allowed if the state has displacement (that is, the Gaussian state has a non-zero mean):

https://github.com/XanaduAI/strawberryfields/blob/adb0f9919decc3a6544ddae26eb98c1afb76f7c8/strawberryfields/backends/gaussianbackend/backend.py#L262

This is because this functionality depends on the torontonian_sample_state function from The Walrus, which (at the time) did not support passing in a means vector.

However, a recent addition to The Walrus added in this functionality via the new function threshold_detection_prob. It would be great to modify the Gaussian backend to support threshold detection of Gaussian states with displacement using this new functionality.

Hannibalcarthaga commented 3 years ago

I am interested in working on this issue, but I am a total beginner, could you point me towards some resources to get started?

crazy4pi314 commented 3 years ago

Pinging @josh146 for suggestions of resources for @Hannibalcarthaga :)

josh146 commented 3 years ago

Hi @Hannibalcarthaga, fantastic!

Are you looking mainly for resources on the coding side, or more to understand the concepts behind this change? For what is required on the coding side, @nquesada might be able to provide a list of steps that need to be undertaken for the PR :)

Hannibalcarthaga commented 3 years ago

@josh146 and @nquesada I am primarily looking for resources for coding, and would like to refer to some regarding the concept as well. Thanks.

Hannibalcarthaga commented 3 years ago

I am mostly referring to the Development guide in the Strawberry Fields documentation and the tutorials.

josh146 commented 3 years ago

No worries @Hannibalcarthaga! As you note, the best place for details on SF documentation is the Strawberry Fields development guide. This guide has details on:

I hope that helps @Hannibalcarthaga! Let me know if there is anything missing that I can help you with.

Hannibalcarthaga commented 3 years ago

Hi, @josh146 , I have modified the Gaussian backend as per thewalrus._torontonian source code, but running the pytest generates an error regarding the frontend (Quanta) sarang@sarang-Inspiron-3521:~/strawberryfields$ pytest ============================= test session starts ============================== platform linux -- Python 3.8.5, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 Using --randomly-seed=3026692562 rootdir: /home/sarang/strawberryfields plugins: randomly-3.8.0, cov-2.12.0, mock-3.6.1 collected 12345 items / 1 error / 12344 selected

==================================== ERRORS ==================================== ___ ERROR collecting tests/frontend/test_sf_plot.py ____ ImportError while importing test module '/home/sarang/strawberryfields/tests/frontend/test_sf_plot.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: ../anaconda3/envs/Quanta/lib/python3.8/importlib/init.py:127: in import_module return _bootstrap._gcd_import(name[level:], package, level) tests/frontend/test_sf_plot.py:19: in import plotly.io as pio E ModuleNotFoundError: No module named 'plotly' and the test stops. Also make test generates a similar error. How do I resolve this error?

josh146 commented 3 years ago

Hi @Hannibalcarthaga, you will need to install plotly :)

pip install plotly
Hannibalcarthaga commented 3 years ago

@josh146 thanks, that issue is resolved, so I ran the tests again. During the "pytest -m gaussian" I got the following six errors FAILED tests/backend/test_threshold_measurement.py::TestRepresentationIndependent::test_two_mode_squeezed_measurements[GaussianBackend-False] FAILED tests/backend/test_threshold_measurement.py::TestRepresentationIndependent::test_vacuum_measurements[GaussianBackend-True] FAILED tests/backend/test_threshold_measurement.py::TestRepresentationIndependent::test_binary_outcome[GaussianBackend-True] FAILED tests/backend/test_threshold_measurement.py::TestRepresentationIndependent::test_two_mode_squeezed_measurements[GaussianBackend-True] FAILED tests/backend/test_threshold_measurement.py::TestRepresentationIndependent::test_vacuum_measurements[GaussianBackend-False] FAILED tests/backend/test_threshold_measurement.py::TestRepresentationIndependent::test_binary_outcome[GaussianBackend-False] Does this mean I have to modify the test_threshold_measurement.py similar to the backend.py? P.S. Apologies for spamming.

nquesada commented 3 years ago

Hi @Hannibalcarthaga --- It does seem like the test are failing after your modifications. Could you post what the first error is saying? To run the tests specific to threshold detection you could run them by typing pytest tests/backend/test_threshold_measurement.py

Hannibalcarthaga commented 3 years ago

Hi, @nquesada here is the error

=================================== FAILURES ===================================
_ TestRepresentationIndependent.test_vacuum_measurements[GaussianBackend-False] _

self = <test_threshold_measurement.TestRepresentationIndependent object at 0x7fa0bcd2da00>
setup_backend = <function setup_backend.<locals>._setup_backend at 0x7fa0bcd695e0>
pure = False

    def test_vacuum_measurements(self, setup_backend, pure):
        """Tests Threshold measurement on the vacuum state."""
        backend = setup_backend(3)

        for _ in range(NUM_REPEATS):
            backend.reset(pure=pure)

>           meas = backend.threshold_detection_prob_displacement(mu, cov, det_pattern, hbar)
E           NameError: name 'mu' is not defined
nquesada commented 3 years ago

it seems like you are calling a variable, mu, which you have not defined.

Hannibalcarthaga commented 3 years ago

Hi, @nquesada , I am getting confused in the TestRepresentationIndependent class. The name error mu is the defined parameter in the threshold_detection_prob_displacement as per theWalrus. So when I called the threshold backend in the above mentioned class the argument mu gave an error. Is there a specific way to call the backends of (test_two_mode_squeezed_measurements, vacuum_measurements and the binary_outcomes)? Could you point me towards any documentations?

josh146 commented 3 years ago

Hi @Hannibalcarthaga I recommend opening a work-in-progress pull request 🙂 Even though it's not complete and still needs more work to get the tests passing, by having a PR open, @nquesada and I (and the rest of the Strawberry Fields team) will be able to have a look at your code, and provide more specific technical help.

Hannibalcarthaga commented 3 years ago

Hi @Hannibalcarthaga I recommend opening a work-in-progress pull request
@josh146 Hi, could you guide me here? I have forked the repository but am having a little trouble making the pull request.

nquesada commented 3 years ago

Hi @Hannibalcarthaga : you can find an easy to follow guide on "Creating a pull request from a fork" in the github documentation here: https://docs.github.com/en/github/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork

Hannibalcarthaga commented 3 years ago

@josh146 Hi, I have made a PR, but I am not able to run the make docs test. With pip install -rdoc/requirements.txt command it starts installation of tensorflow. It is a very large file and my connection is not stable to install it. Any suggestions on how to resolve this?

antalszava commented 3 years ago

Hi @Hannibalcarthaga, there could be two ways to aid your problem:

  1. TensorFlow could be removed locally from the requirements.txt file, care should be taken to undo the changes without committing them. There might be, however, parts of the docs that rely on TensorFlow which might result in errors.
  2. The documentation is built for each PR, so the built docs can be checked online (by clicking on the "Details" button at the check): image