Skip to content

Commit

Permalink
Bosonic backend - backend.py and states.py (#539)
Browse files Browse the repository at this point in the history
* Adds __init__ and backend.py for bosonic backend

- Contains all the gaussian methods for the bosonic backend

Co-Authored-By: ilan-tz <57886357+ilan-tz@users.noreply.github.com>
Co-Authored-By: Nicolas Quesada <991946+nquesada@users.noreply.github.com>
Co-Authored-By: GDauphinais <48553380+GDauphinais@users.noreply.github.com>

* Modifies the backend __init__ and base.py files

- Includes BaseBosonic object
- Includes references to bosonic backend in init file

Co-Authored-By: ilan-tz <57886357+ilan-tz@users.noreply.github.com>
Co-Authored-By: Nicolas Quesada <991946+nquesada@users.noreply.github.com>

* Update states.py file

Adds BaseBosonicState and its tailored methods

Co-Authored-By: ilan-tz <57886357+ilan-tz@users.noreply.github.com>
Co-Authored-By: Nicolas Quesada <991946+nquesada@users.noreply.github.com>

* Updates conftest.py

Adds bosonic backend to backends for testing

Co-Authored-By: ilan-tz <57886357+ilan-tz@users.noreply.github.com>

* Tests for bosonic backend

- Adds/removes bosonic marking to/from some existing tests
- New tests for gaussian_cptp, mbsqueezing, and BaseBosonicState methods

* Tidying code based on codefactor

* More codefactor tidying

* More codefactor

* ran black

* Mark integration tests

so that they don't apply to the bosonic backend yet.
We will mark them for the bosonic backend once the PR with the bosonic engine is added.

* Ensure peaks in add_mode are integers

* Apply suggestions from code review

Mostly minor docstring/formatting edits

Co-authored-by: Josh Izaac <josh146@gmail.com>
Co-authored-by: Nicolas Quesada <zeitus@gmail.com>

* Update BaseBosonic inheritance

Now inherits from BaseGaussian

Co-Authored-By: Josh Izaac <josh@iza.ac>

* Update strawberryfields/backends/base.py

Co-authored-by: antalszava <antalszava@gmail.com>

* Adding reference to bosonic in docstrings

* Applies Antal and Theo's suggestions

- Tidies or simply deletes inheritable docstrings
- Changes is_vacuum method

* Ran black

* Minor changes

- Add back in state method for BaseBosonic since the output is different than BaseGaussian
- Add shortcut to BosonicModes for fidelity_coherent if all modes have been deleted
- Tidy docstring of state purity function

* Ran black

* Tidying code + test

- wrote new test that checks fidelity_vacuum in states.py, since the is_vacuum method of the backend changed, and consequently this was no longer being tested

* Apply suggestions from code review

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

* ran black

* Apply suggestions from code review

* Fix photon number variance for non-gaussian states

* Deletes TODO note

* Apply suggestions from code review

Co-authored-by: antalszava <antalszava@gmail.com>

* Ran black

* Tidying docstrings

* Breaks the test test_multimode_gaussian_random_state

Co-authored-by: ilan-tz <57886357+ilan-tz@users.noreply.github.com>
Co-authored-by: Nicolas Quesada <991946+nquesada@users.noreply.github.com>
Co-authored-by: GDauphinais <48553380+GDauphinais@users.noreply.github.com>
Co-authored-by: Josh Izaac <josh146@gmail.com>
Co-authored-by: Nicolas Quesada <zeitus@gmail.com>
Co-authored-by: Josh Izaac <josh@iza.ac>
Co-authored-by: antalszava <antalszava@gmail.com>
Co-authored-by: Theodor <theodor@xanadu.ai>
  • Loading branch information
9 people committed Feb 16, 2021
1 parent 31cc0d5 commit 3bd5318
Show file tree
Hide file tree
Showing 23 changed files with 1,240 additions and 29 deletions.
15 changes: 12 additions & 3 deletions strawberryfields/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
FockBackend
GaussianBackend
~tfbackend.TFBackend
BosonicBackend
.. raw:: html
Expand All @@ -43,6 +44,7 @@
BaseFockState
BaseGaussianState
~tfbackend.states.FockStateTF
BaseBosonicState
.. raw:: html
Expand All @@ -62,6 +64,7 @@
BaseBackend
BaseFock
BaseGaussian
BaseBosonic
Utility modules
---------------
Expand All @@ -76,10 +79,11 @@
shared_ops
"""

from .base import BaseBackend, BaseFock, BaseGaussian, ModeMap
from .base import BaseBackend, BaseFock, BaseGaussian, BaseBosonic, ModeMap
from .gaussianbackend import GaussianBackend
from .fockbackend import FockBackend
from .states import BaseState, BaseGaussianState, BaseFockState
from .bosonicbackend import BosonicBackend
from .states import BaseState, BaseGaussianState, BaseFockState, BaseBosonicState

# There is no import for the TFBackend to avoid TensorFlow being a direct
# requirement of SF through a chain of imports
Expand All @@ -88,18 +92,23 @@
"BaseBackend",
"BaseFock",
"BaseGaussian",
"BaseBosonic",
"FockBackend",
"GaussianBackend",
"BosonicBackend",
"TFBackend",
"BaseState",
"BaseFockState",
"BaseGaussianState",
"BaseBosonicState",
]


virtual_backends = ["X8_01"]

local_backends = {b.short_name: b for b in (BaseBackend, GaussianBackend, FockBackend)}
local_backends = {
b.short_name: b for b in (BaseBackend, GaussianBackend, FockBackend, BosonicBackend)
}


def load_backend(name):
Expand Down
54 changes: 53 additions & 1 deletion strawberryfields/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ def supports(self, name):
* "gaussian": for manipulations in the Gaussian representation using the
displacements and covariance matrices
* "fock_basis": for manipulations in the Fock representation
* "bosonic": for manipulations of states represented as linear combinations
of Gaussian functions in phase space
* "mixed_states": for representations where the quantum state is mixed
* "batched": allows for a multiple circuits to be simulated in parallel
Expand Down Expand Up @@ -170,7 +172,7 @@ def begin_circuit(self, num_subsystems, **kwargs):
"""
# BaseBackend can be instantiated for testing purposes, even though it does not do anything.

def add_mode(self, n=1):
def add_mode(self, n=1, **kwargs):
"""Add modes to the circuit.
The new modes are initialized to the vacuum state.
Expand All @@ -181,6 +183,9 @@ def add_mode(self, n=1):
Returns:
list[int]: indices of the newly added modes
Keyword Args:
peaks (list): number of Gaussian peaks for each new mode (for bosonic backend only)
"""
raise NotImplementedError

Expand Down Expand Up @@ -677,3 +682,50 @@ def state(self, modes=None, **kwargs):
BaseGaussianState: state description
"""
raise NotImplementedError


# ==============================
# Base bosonic backend
# ==============================


class BaseBosonic(BaseGaussian):
"""Abstract base class for backends that are only capable of manipulating states
represented as linear combinations of Gaussian functions in phase space."""

compiler = None

def __init__(self):
super().__init__()
self._supported["gaussian_linear_combo"] = True

def prepare_gaussian_state(self, r, V, modes):
r"""Prepare a Gaussian state.
Note the different basis-ordering from the :class:`~.GaussianBackend`.
The specified modes are traced out and replaced with a Gaussian state
provided via a vector of means and a covariance matrix.
Args:
r (array): vector of means in :math:`(x_1,p_1,x_2,p_2,\dots)` ordering
V (array): covariance matrix in :math:`(x_1,p_1,x_2,p_2,\dots)` ordering
modes (int or Sequence[int]): Which modes to prepare the state in.
If the modes are not sorted, this is taken into account when preparing the state.
I.e., when a two mode state is prepared with ``modes=[3,1]``, the first
mode of the given state goes into mode 3 and the second mode goes into mode 1.
Raises:
ValueError: if the shapes of r or V do not match the number of modes.
"""
raise NotImplementedError

def state(self, modes=None, **kwargs):
"""Returns the state of the quantum simulation.
See :meth:`.BaseBackend.state`.
Returns:
BaseBosonicState: state description
"""
raise NotImplementedError
16 changes: 16 additions & 0 deletions strawberryfields/backends/bosonicbackend/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2021 Xanadu Quantum Technologies Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
r"""Bosonic qubit simulator backend"""

from .backend import BosonicBackend

0 comments on commit 3bd5318

Please sign in to comment.