Skip to content

Commit

Permalink
Removing shared ops (#573)
Browse files Browse the repository at this point in the history
* removes unused variables in test_tdmprogram

* updates test_decompositions_integration

* updates ops.py

* updates decompositions.py

* updates backends/__init__py

* updates states.py

* updates strawberryfields/backends/gaussianbackend/gaussiancircuit.py

* updates  strawberryfields/backends/bosonicbackend/backend.py

* updates strawberryfields/backends/gaussianbackend/backend.py

* deletes shared_ops and its tests

* Fixes williamson

* unused import removed

* unused import removed

* updates backends/__init__.py

* updates requirement.txt setup.py and CHANGELOG

* Update strawberryfields/backends/__init__.py

* Update .github/CHANGELOG.md

* black

* update test suite

* update changelog

* fix

* fix

* fix

* fix

* fix

Co-authored-by: Josh Izaac <[email protected]>
  • Loading branch information
nquesada and josh146 authored Apr 23, 2021
1 parent b2d2f33 commit 896557c
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 175 deletions.
9 changes: 9 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@
method.
[(#564)](https://github.com/XanaduAI/strawberryfields/pull/564)

* The backend utility module `shared_ops.py` has been removed, with all of its
functionality now provided by The Walrus.
[(#573)](https://github.com/XanaduAI/strawberryfields/pull/573)

<h3>Breaking changes</h3>

* Removes support for Python 3.6.
[(#573)](https://github.com/XanaduAI/strawberryfields/pull/573)

<h3>Bug fixes</h3>

* `Connection` objects now send requests to the platform API at version `0.2.0`
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.6
python-version: 3.8

- name: Install dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sphinx:
configuration: doc/conf.py

python:
version: 3.6
version: 3.8
install:
- requirements: doc/requirements.txt
- method: pip
Expand Down
2 changes: 1 addition & 1 deletion doc/code/sf_decompositions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ sf.decompositions
.. automodapi:: strawberryfields.decompositions
:no-heading:
:include-all-objects:
:skip: block_diag, sqrtm, polar, schur, sympmat, changebasis, adj_scaling
:skip: block_diag, sqrtm, polar, schur, sympmat, xpxp_to_xxpp, adj_scaling
5 changes: 2 additions & 3 deletions doc/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ipykernel
sphinx==2.2.2
m2r
networkx>=2.0
numpy>=1.16.3
numpy>=1.20
plotly
quantum-blackbird
scipy>=1.0.0
Expand All @@ -13,6 +13,5 @@ sphinx-autodoc-typehints==1.10.3
sphinx-copybutton
sphinx-automodapi
sphinxcontrib-bibtex==0.4.2
tensorflow-tensorboard==0.1.8
tensorflow==2.0.3
tensorflow==2.2.0
toml
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
numpy>=1.17.4
scipy==1.4.1
numpy>=1.20
scipy
sympy>=1.5
tensorflow>=2.0
tensorboard>=2.0
networkx>=2.0
quantum-blackbird>=0.3.0
python-dateutil==2.8.0
thewalrus>=0.14.0
thewalrus>=0.15.0
toml
appdirs
numba>=0.48.0
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"networkx>=2.0",
"quantum-blackbird>=0.3.0",
"python-dateutil>=2.8.0",
"thewalrus>=0.14.0",
"thewalrus>=0.15.0",
"numba",
"toml",
"appdirs",
Expand Down
11 changes: 0 additions & 11 deletions strawberryfields/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,6 @@
BaseGaussian
BaseBosonic
Utility modules
---------------
The following utility modules are provided for
backend development.
.. currentmodule:: strawberryfields.backends
.. autosummary::
:toctree: api
shared_ops
"""

from .base import BaseBackend, BaseFock, BaseGaussian, BaseBosonic, ModeMap
Expand Down
6 changes: 3 additions & 3 deletions strawberryfields/backends/bosonicbackend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
from scipy.special import comb
from scipy.linalg import block_diag

from thewalrus.symplectic import xxpp_to_xpxp

from strawberryfields.backends import BaseBosonic
from strawberryfields.backends.shared_ops import changebasis
from strawberryfields.backends.states import BaseBosonicState

from strawberryfields.backends.bosonicbackend.bosoniccircuit import BosonicModes
Expand Down Expand Up @@ -377,8 +378,7 @@ def prepare_gaussian_state(self, r, V, modes):

# convert xp-ordering to symmetric ordering
means = np.vstack([r[:N], r[N:]]).reshape(-1, order="F")
C = changebasis(N)
cov = C @ V @ C.T
cov = xxpp_to_xpxp(V)

self.circuit.from_covmat(cov, modes)
self.circuit.from_mean(means, modes)
Expand Down
7 changes: 2 additions & 5 deletions strawberryfields/backends/gaussianbackend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@
concatenate,
array,
identity,
arctan2,
angle,
sqrt,
vstack,
zeros_like,
allclose,
ix_,
)
from thewalrus.samples import hafnian_sample_state, torontonian_sample_state
from thewalrus.symplectic import xxpp_to_xpxp

from strawberryfields.backends import BaseGaussian
from strawberryfields.backends.shared_ops import changebasis
from strawberryfields.backends.states import BaseGaussianState

from .gaussiancircuit import GaussianModes
Expand Down Expand Up @@ -199,8 +197,7 @@ def prepare_gaussian_state(self, r, V, modes):

# convert xp-ordering to symmetric ordering
means = vstack([r[:N], r[N:]]).reshape(-1, order="F")
C = changebasis(N)
cov = C @ V @ C.T
cov = xxpp_to_xpxp(V)

self.circuit.fromscovmat(cov, modes)
self.circuit.fromsmean(means, modes)
Expand Down
14 changes: 3 additions & 11 deletions strawberryfields/backends/gaussianbackend/gaussiancircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
# pylint: disable=duplicate-code,attribute-defined-outside-init
import numpy as np
from thewalrus.quantum import Xmat
from thewalrus.symplectic import xxpp_to_xpxp, xpxp_to_xxpp

from . import ops
from ..shared_ops import changebasis


class GaussianModes:
Expand Down Expand Up @@ -250,9 +250,6 @@ def scovmatxp(self):
The order for the canonical operators is :math:`q_1,..,q_n, p_1,...,p_n`.
This differs from the ordering used in [1] which is :math:`q_1,p_1,q_2,p_2,...,q_n,p_n`
Note that one ordering can be obtained from the other by using a permutation matrix.
Said permutation matrix is implemented in the function changebasis(n) where n is
the number of modes.
"""
mm11 = (
self.nmat
Expand Down Expand Up @@ -288,9 +285,6 @@ def smeanxp(self):
The order for the canonical operators is :math:`q_1, \ldots, q_n, p_1, \ldots, p_n`.
This differs from the ordering used in [1] which is :math:`q_1, p_1, q_2, p_2, \ldots, q_n, p_n`.
Note that one ordering can be obtained from the other by using a permutation matrix.
Said permutation matrix is implemented in the function changebasis(n) where n is
the number of modes.
"""
nmodes = self.nlen
r = np.empty(2 * nmodes)
Expand All @@ -300,8 +294,7 @@ def smeanxp(self):

def scovmat(self):
"""Constructs and returns the symmetric ordered covariance matrix as defined in [1]"""
rotmat = changebasis(self.nlen)
return np.dot(np.dot(rotmat, self.scovmatxp()), np.transpose(rotmat))
return xxpp_to_xpxp(self.scovmatxp())

def smean(self):
r"""the symmetric mean $[q_1,p_1,q_2,p_2,...,q_n,p_n]$"""
Expand Down Expand Up @@ -347,8 +340,7 @@ def fromscovmat(self, V, modes=None):
raise ValueError("Covariance matrix is larger than the number of subsystems.")

# convert to xp ordering
rotmat = changebasis(n)
VV = np.dot(np.dot(np.transpose(rotmat), V), rotmat)
VV = xpxp_to_xxpp(V)

A = VV[0:n, 0:n]
B = VV[0:n, n : 2 * n]
Expand Down
41 changes: 0 additions & 41 deletions strawberryfields/backends/shared_ops.py

This file was deleted.

6 changes: 2 additions & 4 deletions strawberryfields/backends/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@
from scipy.integrate import simps

from thewalrus.symplectic import rotation as _R
from thewalrus.symplectic import xpxp_to_xxpp

import thewalrus.quantum as twq

import strawberryfields as sf

from .shared_ops import changebasis

indices = string.ascii_lowercase


Expand Down Expand Up @@ -1257,8 +1256,7 @@ def poly_quad_expectation(self, A, d=None, k=0, phi=0, **kwargs):
if phi != 0:
# rotate all modes of the covariance matrix and vector of means
R = _R(phi)
C = changebasis(self._modes)
rot = C.T @ block_diag(*([R] * self._modes)) @ C
rot = xpxp_to_xxpp(block_diag(*([R] * self._modes)))

mu = rot.T @ mu
cov = rot.T @ cov @ rot
Expand Down
10 changes: 4 additions & 6 deletions strawberryfields/decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
import numpy as np
from scipy.linalg import block_diag, sqrtm, polar, schur
from thewalrus.quantum import adj_scaling
from thewalrus.symplectic import sympmat

from .backends.shared_ops import changebasis
from thewalrus.symplectic import sympmat, xpxp_to_xxpp


def takagi(N, tol=1e-13, rounding=13):
Expand Down Expand Up @@ -670,7 +668,6 @@ def williamson(V, tol=1e-11):

n = n // 2
omega = sympmat(n)
rotmat = changebasis(n)
vals = np.linalg.eigvalsh(V)

for val in vals:
Expand Down Expand Up @@ -698,8 +695,9 @@ def williamson(V, tol=1e-11):
p = block_diag(*seq)
Kt = K @ p
s1t = p @ s1 @ p
dd = np.transpose(rotmat) @ s1t @ rotmat
Ktt = Kt @ rotmat
dd = xpxp_to_xxpp(s1t)
perm_indices = xpxp_to_xxpp(np.arange(2 * n))
Ktt = Kt[:, perm_indices]
Db = np.diag([1 / dd[i, i + n] for i in range(n)] + [1 / dd[i, i + n] for i in range(n)])
S = Mm12 @ Ktt @ sqrtm(Db)
return Db, np.linalg.inv(S).T
Expand Down
5 changes: 3 additions & 2 deletions strawberryfields/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
from scipy.linalg import block_diag
import scipy.special as ssp

from thewalrus.symplectic import xxpp_to_xpxp

import strawberryfields as sf
import strawberryfields.program_utils as pu
import strawberryfields.decompositions as dec
from .backends.states import BaseFockState, BaseGaussianState, BaseBosonicState
from .backends.shared_ops import changebasis
from .program_utils import Command, RegRef, MergeFailure
from .parameters import (
par_regref_deps,
Expand Down Expand Up @@ -2842,7 +2843,7 @@ def _decompose(self, reg, **kwargs):
D = np.diag(V)
is_diag = np.all(V == np.diag(D))

BD = changebasis(self.ns) @ V @ changebasis(self.ns).T
BD = xxpp_to_xpxp(V)
BD_modes = [BD[i * 2 : (i + 1) * 2, i * 2 : (i + 1) * 2] for i in range(BD.shape[0] // 2)]
is_block_diag = (not is_diag) and np.all(BD == block_diag(*BD_modes))

Expand Down
7 changes: 3 additions & 4 deletions tests/backend/test_states_polyquad.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
from scipy.linalg import block_diag

from thewalrus.symplectic import rotation as R
from thewalrus.symplectic import xpxp_to_xxpp

from strawberryfields import backends
from strawberryfields import utils
from strawberryfields.backends.shared_ops import changebasis

# some tests require a higher cutoff for accuracy
CUTOFF = 12
Expand Down Expand Up @@ -453,9 +453,8 @@ def test_three_mode_arbitrary(self, setup_backend, pure, hbar, tol):
S1 = block_diag(BS, np.identity(2))
S2 = block_diag(np.identity(2), BS)

C = changebasis(3)
mu = C.T @ S2 @ S1 @ mu
cov = C.T @ S2 @ S1 @ cov @ S1.T @ S2.T @ C
mu = xpxp_to_xxpp(S2 @ S1 @ mu)
cov = xpxp_to_xxpp(S2 @ S1 @ cov @ S1.T @ S2.T)

modes = list(np.arange(6).reshape(2, -1).T)

Expand Down
49 changes: 0 additions & 49 deletions tests/frontend/test_shared_ops.py

This file was deleted.

Loading

0 comments on commit 896557c

Please sign in to comment.