Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
co9olguy committed Mar 30, 2019
1 parent 847b0e6 commit bee96b7
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ ignored-classes=numpy,tensorflow,scipy,networkx,strawberryfields
# can either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where
# it should appear only once).
disable=line-too-long,invalid-name,too-many-lines,redefined-builtin,too-many-locals,duplicate-code
disable=line-too-long,invalid-name,too-many-lines,redefined-builtin,too-many-locals,duplicate-code,too-many-arguments,too-few-public-methods
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_coherent_vacuum_interfered_fock_elements(self, setup_backend, mag_alpha
alpha_outB = r * alpha
ref_stateA = np.array([np.exp(-0.5 * np.abs(alpha_outA) ** 2) * alpha_outA ** n / np.sqrt(factorial(n)) for n in range(cutoff)])
ref_stateB = np.array([np.exp(-0.5 * np.abs(alpha_outB) ** 2) * alpha_outB ** n / np.sqrt(factorial(n)) for n in range(cutoff)])
ref_state = np.einsum('i,j->ij',ref_stateA, ref_stateB)
ref_state = np.einsum('i,j->ij', ref_stateA, ref_stateB)
if not pure:
ref_state = np.einsum('i,j,k,l->ijkl', ref_stateA, np.conj(ref_stateA), ref_stateB, np.conj(ref_stateB))
assert np.allclose(numer_state, ref_state, atol=tol, rtol=0)
assert np.allclose(numer_state, ref_state, atol=tol, rtol=0)
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@


def sech(x):
"""Hyperbolic secant"""
return 1 / np.cosh(x)


Expand Down Expand Up @@ -88,7 +89,7 @@ def test_displaced_squeezed_with_no_displacement(self, setup_backend, r, phi, cu
else:
num_state = state.dm()
even_refs = np.array([np.sqrt(sech(r)) * np.sqrt(factorial(k)) / factorial(k / 2) *
(-0.5 * np.exp(1j * phi) * np.tanh(r)) ** (k / 2) for k in range(0, cutoff, 2)])
(-0.5 * np.exp(1j * phi) * np.tanh(r)) ** (k / 2) for k in range(0, cutoff, 2)])
if batched:
if pure:
even_entries = num_state[:, ::2]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_all_fock_state_probs_of_product_coherent_state(self, setup_backend, mag
"""Tests that the numeric probabilities in the full Fock basis are correct for a two-mode product coherent state."""

if mag_alpha == 0.:
pass
pass
else:
backend = setup_backend(2)

Expand Down
33 changes: 19 additions & 14 deletions strawberryfields/backends/fockbackend/tests/test_homodyne.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@

r"""Unit tests for homodyne measurements."""

import pytest

import numpy as np
from scipy.special import factorial


MAG_ALPHAS = np.linspace(0, .8, 4)
Expand All @@ -33,30 +30,38 @@ class TestRepresentationIndependent:
"""Basic implementation-independent tests."""

def test_mode_reset_vacuum(self, setup_backend, tol):
"""Tests that modes get reset to the vacuum after measurement."""

backend = setup_backend(1)

backend.squeeze(SQUEEZE_VAL,0)
backend.displacement(MAG_ALPHAS[-1],0)
backend.measure_homodyne(0,0)
backend.squeeze(SQUEEZE_VAL, 0)
backend.displacement(MAG_ALPHAS[-1], 0)
backend.measure_homodyne(0, 0)
assert np.all(backend.is_vacuum(tol))

def test_mean_and_std_vacuum(self, setup_backend, tol):
"""Tests that the mean and standard deviation estimates of many homodyne
measurements are in agreement with the expected values for the
vacuum state"""

x = np.empty(0)
for i in range(N_MEAS):
for _ in range(N_MEAS):
backend = setup_backend(1)
meas_result = backend.measure_homodyne(0,0)
meas_result = backend.measure_homodyne(0, 0)
x = np.append(x, meas_result)
assert np.allclose(x.mean(), 0., atol = std_10 + tol, rtol=0)
assert np.allclose(x.std(), 1., atol = std_10 + tol, rtol=0)
assert np.allclose(x.mean(), 0., atol=std_10 + tol, rtol=0)
assert np.allclose(x.std(), 1., atol=std_10 + tol, rtol=0)


def test_mean_coherent(self, setup_backend, tol):
"""Tests that the mean and standard deviation estimates of many homodyne
measurements are in agreement with the expected values for a
coherent state"""

x = np.empty(0)
for i in range(N_MEAS):
for _ in range(N_MEAS):
backend = setup_backend(1)
backend.prepare_coherent_state(DISP_VAL,0)
meas_result = backend.measure_homodyne(0,0)
backend.prepare_coherent_state(DISP_VAL, 0)
meas_result = backend.measure_homodyne(0, 0)
x = np.append(x, meas_result)
assert np.allclose(x.mean(), 2 * DISP_VAL.real, atol = std_10 + tol)
assert np.allclose(x.mean(), 2 * DISP_VAL.real, atol=std_10 + tol)
13 changes: 5 additions & 8 deletions strawberryfields/backends/fockbackend/tests/test_modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@

r"""Unit tests for add_mode and del_mode functions"""

import pytest
from itertools import combinations

import numpy as np

NUM_REPEATS = 10
Expand All @@ -29,7 +26,7 @@ def test_add_mode_vacuum(self, setup_backend, tol):

backend = setup_backend(1)

for n in range(4):
for _n in range(4):
backend.add_mode(1)
assert np.all(backend.is_vacuum(tol))

Expand All @@ -42,15 +39,15 @@ def test_del_mode_vacuum(self, setup_backend, tol):
backend.del_mode([n])
assert np.all(backend.is_vacuum(tol))

def test_get_modes(self, setup_backend, tol):
def test_get_modes(self, setup_backend):
"""Tests that get modes returns the correct result after deleting modes from the circuit"""

backend = setup_backend(4)

backend.squeeze(0.1, 0)
backend.del_mode([0,2])
backend.del_mode([0, 2])
res = backend.get_modes()
assert np.all(res==[1,3])
assert np.all(res == [1, 3])


class TestFockRepresentation:
Expand Down Expand Up @@ -92,7 +89,7 @@ def test_fock_measurements_after_add_mode(self, setup_backend, cutoff):
def test_fock_measurements_after_del_mode(self, setup_backend, cutoff):
"""Tests Fock measurements on a system after tracing out an unentagled mode."""

for m in range(1,4):
for m in range(1, 4):
meas_results = []
for _ in range(NUM_REPEATS):
backend = setup_backend(4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,37 @@ class TestFockRepresentation:
def test_kerr_interaction(self, setup_backend, kappa, cutoff, tol):
"""Tests if the Kerr interaction has the right effect on states in the Fock basis"""

for kappa in KAPPAS:
backend = setup_backend(1)

backend.prepare_ket_state(np.array([1.0 for n in range(cutoff)]) / cutoff, 0)
backend.kerr_interaction(kappa, 0)
s = backend.state()
if s.is_pure:
numer_state = s.ket()
else:
numer_state = s.dm()
ref_state = np.array([np.exp(1j * kappa * n ** 2) for n in range(cutoff)]) / cutoff
assert np.allclose(numer_state, ref_state, atol=tol, rtol=0.)
backend = setup_backend(1)

backend.prepare_ket_state(np.array([1.0 for n in range(cutoff)]) / cutoff, 0)
backend.kerr_interaction(kappa, 0)
s = backend.state()
if s.is_pure:
numer_state = s.ket()
else:
numer_state = s.dm()
ref_state = np.array([np.exp(1j * kappa * n ** 2) for n in range(cutoff)]) / cutoff
assert np.allclose(numer_state, ref_state, atol=tol, rtol=0.)

@pytest.mark.parametrize("kappa", KAPPAS)
def test_cross_kerr_interaction(self, setup_backend, kappa, cutoff, tol):
"""Tests if the cross-Kerr interaction has the right effect on states in the Fock basis"""

for kappa in KAPPAS:
backend = setup_backend(2)
backend = setup_backend(2)

ket1 = np.array([1.0 for n in range(cutoff)])
ket1 /= np.linalg.norm(ket1)
ket = np.outer(ket1, ket1)
ket1 = np.array([1.0 for n in range(cutoff)])
ket1 /= np.linalg.norm(ket1)
ket = np.outer(ket1, ket1)

backend.prepare_ket_state(ket, modes=[0, 1])
backend.cross_kerr_interaction(kappa, 0, 1)
s = backend.state()
backend.prepare_ket_state(ket, modes=[0, 1])
backend.cross_kerr_interaction(kappa, 0, 1)
s = backend.state()

if s.is_pure:
numer_state = s.ket()
else:
numer_state = s.dm()
if s.is_pure:
numer_state = s.ket()
else:
numer_state = s.dm()

ref_state = np.array([np.exp(1j*kappa*n1*n2) for n1 in range(cutoff) for n2 in range(cutoff)])/cutoff
ref_state = np.reshape(ref_state, [cutoff]*2)
assert np.allclose(numer_state, ref_state, atol=tol, rtol=0.)
ref_state = np.array([np.exp(1j*kappa*n1*n2) for n1 in range(cutoff) for n2 in range(cutoff)])/cutoff
ref_state = np.reshape(ref_state, [cutoff]*2)
assert np.allclose(numer_state, ref_state, atol=tol, rtol=0.)
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_rotated_superposition_states(self, setup_backend, theta, pure, cutoff,
r"""Tests if a range of phase-shifted superposition states are equal to the form of
\sum_n exp(i * theta * n)|n>"""

for n in range(cutoff):
for _n in range(cutoff):
backend = setup_backend(1)

ref_state = np.array([np.exp(1j * theta * k) for k in range(cutoff)]) / np.sqrt(cutoff)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
from scipy.special import factorial

SQZ_R = np.linspace(0.0, 0.1, 5)
SQZ_THETA = np.linspace(0,2 * np.pi, 3, endpoint=False)
SQZ_THETA = np.linspace(0, 2 * np.pi, 3, endpoint=False)

def sech(x):
"""Hyberbolic secant"""
return 1 / np.cosh(x)


Expand Down Expand Up @@ -58,7 +59,7 @@ def test_normalized_squeezed_state(self, setup_backend, r, theta, tol):

@pytest.mark.parametrize("r", SQZ_R)
@pytest.mark.parametrize("theta", SQZ_THETA)
def test_no_odd_fock(self, setup_backend, r, theta, batched, tol):
def test_no_odd_fock(self, setup_backend, r, theta, batched):
"""Tests if a range of squeezed vacuum states have
only nonzero entries for even Fock states."""

Expand All @@ -71,7 +72,7 @@ def test_no_odd_fock(self, setup_backend, r, theta, batched, tol):
else:
num_state = s.dm()
if batched:
odd_entries = num_state[:,1::2]
odd_entries = num_state[:, 1::2]
else:
odd_entries = num_state[1::2]
assert np.all(odd_entries == 0)
Expand All @@ -92,14 +93,14 @@ def test_reference_squeezed_states(self, setup_backend, r, theta, batched, pure,
even_refs = np.array([np.sqrt(sech(r)) * np.sqrt(factorial(k)) / factorial(k / 2) * (-0.5 * np.exp(1j * theta) * np.tanh(r)) ** (k / 2) for k in range(0, cutoff, 2)])
if batched:
if pure:
even_entries = num_state[:,::2]
even_entries = num_state[:, ::2]
else:
even_entries = num_state[:,::2,::2]
even_entries = num_state[:, ::2, ::2]
even_refs = np.outer(even_refs, np.conj(even_refs))
else:
if pure:
even_entries = num_state[::2]
else:
even_entries = num_state[::2,::2]
even_entries = num_state[::2, ::2]
even_refs = np.outer(even_refs, np.conj(even_refs))
assert np.allclose(even_entries, even_refs, atol=tol, rtol=0.)

0 comments on commit bee96b7

Please sign in to comment.