Skip to content

Commit

Permalink
fix(tvd): Fix problem with computing tvd with dicts. (#57)
Browse files Browse the repository at this point in the history
* fix(tvd): Fix problem with computing tvd with dicts.

* fix(tests): Fix nonuniform losses exact simulation with BOBS test.

Co-authored-by: Tomasz Rybotycki <rybotycki.tomasz@gmail.com>
  • Loading branch information
Tomev and Tomasz Rybotycki authored Aug 1, 2022
1 parent 4bbe2bd commit e297681
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="theboss",
version="3.0.0",
version="3.0.1",
author="Tomasz Rybotycki",
author_email="rybotycki.tomasz+theboss@gmail.com",
long_description=long_description,
Expand Down
45 changes: 22 additions & 23 deletions tests/test_nonuniform_losses_simulation_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from theboss.simulation_strategies.simulation_strategy_factory import (
StrategyType,
SimulationStrategyFactory,
SimulationStrategyInterface,
)


Expand All @@ -54,6 +53,7 @@ def setUp(self) -> None:
# Interferometer matrix preparation.
self._blocks_transmissivities: List[float] = [0.5, 0.2]
block_matrices: List[ndarray] = [unitary_group.rvs(self._m) for _ in range(2)]

self._interferometer_matrix: ndarray = self._get_interferometer_matrix(
block_matrices
)
Expand All @@ -67,22 +67,19 @@ def setUp(self) -> None:

self._config: BosonSamplingExperimentConfiguration
self._config = BosonSamplingExperimentConfiguration(
self._interferometer_matrix,
self._initial_state,
self._m * 2,
self._m * 2,
0,
self._m * 2,
interferometer_matrix=self._interferometer_matrix,
initial_state=self._initial_state,
initial_number_of_particles=self._m * 2,
number_of_modes=self._m * 2,
number_of_particles_lost=0,
number_of_particles_left=self._m * 2,
hierarchy_level=2 * self._m,
)
self._permanent_calculator: RyserPermanentCalculator = RyserPermanentCalculator(
self._interferometer_matrix, self._initial_state, None
)
self._strategy_factory = SimulationStrategyFactory(
experiment_configuration=self._config,
bs_permanent_calculator=self._permanent_calculator,
)

self._strategy: SimulationStrategyInterface
self._strategy_type: StrategyType = StrategyType.BOBS

def _get_samples_number(self, modes_number: int) -> int:
"""
Expand Down Expand Up @@ -240,7 +237,7 @@ def _compute_bobs_approximation_tvd_bound(self):
n = 2 * self._m

bound = pow(eta_eff, 2) / 2
bound *= n - self._strategy_factory._experiment_configuration.hierarchy_level
bound *= n - self._config.hierarchy_level
bound += eta_eff * (1 - eta_eff) / 2

return bound
Expand All @@ -254,11 +251,16 @@ def _perform_accuracy_test(self, approximation_distance_bound: float = 0) -> Non
:return:
"""
# Prepare strategy.
self._strategy = self._strategy_factory.generate_strategy()
strategy_factory: SimulationStrategyFactory = SimulationStrategyFactory(
experiment_configuration=self._config,
bs_permanent_calculator=self._permanent_calculator,
strategy_type=self._strategy_type,
)
strategy = strategy_factory.generate_strategy()

# Get samples.
samples_number: int = self._get_samples_number(self._m)
samples = self._strategy.simulate(self._initial_state, samples_number)
samples = strategy.simulate(self._initial_state, samples_number)

# Get counts.
counts: DefaultDict[Tuple[int, ...], int] = defaultdict(lambda: 0)
Expand All @@ -276,33 +278,31 @@ def _perform_accuracy_test(self, approximation_distance_bound: float = 0) -> Non
self.assertTrue(
tvd < self._desired_statistical_accuracy + approximation_distance_bound,
f"TVD ({tvd}) is greater than expected ("
f"{self._desired_statistical_accuracy + approximation_distance_bound})!",
f"{self._desired_statistical_accuracy + approximation_distance_bound})!\n\n"
f"Tested matrix was: {self._interferometer_matrix}",
)

def test_lossy_net_gcc_accuracy(self) -> None:
"""
Test accuracy of the non-uniform lossy net GCC Strategy in the presence of the
non-uniform losses.
"""
self._strategy_factory.strategy_type = StrategyType.LOSSY_NET_GCC
self._strategy_type = StrategyType.LOSSY_NET_GCC
self._perform_accuracy_test()

def test_exact_bobs_accuracy(self) -> None:
"""
Test accuracy of the general BOBS Strategy in the presence of the non-uniform
losses without approximations.
"""
self._strategy_factory.strategy_type = StrategyType.BOBS
self._strategy_factory.experiment_configuration.hierarchy_level = self._m
self._perform_accuracy_test()

def test_small_approximation_bobs_accuracy(self) -> None:
"""
Test accuracy of the general BOBS strategy in the presence of the non-uniform
losses with small approximation.
"""
self._strategy_factory.strategy_type = StrategyType.BOBS
self._strategy_factory.experiment_configuration.hierarchy_level = 2
self._config.hierarchy_level = 2
approximation_bound: float = self._compute_bobs_approximation_tvd_bound()
self._perform_accuracy_test(approximation_bound)

Expand All @@ -311,7 +311,6 @@ def test_high_approximation_bobs_accuracy(self) -> None:
Test accuracy of the general BOBS strategy in the presence of the non-uniform
losses with high approximation.
"""
self._strategy_factory.strategy_type = StrategyType.BOBS
self._strategy_factory.experiment_configuration.hierarchy_level = 1
self._config.hierarchy_level = 1
approximation_bound: float = self._compute_bobs_approximation_tvd_bound()
self._perform_accuracy_test(approximation_bound)
3 changes: 3 additions & 0 deletions theboss/quantum_computations_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def count_total_variation_distance_dicts(
distribution1: DefaultDict[Tuple[int, ...], float] = defaultdict(lambda: 0)
distribution2: DefaultDict[Tuple[int, ...], float] = defaultdict(lambda: 0)

distribution1.update(distribution_1)
distribution2.update(distribution_2)

# Compute the tvd.
return 0.5 * sum([abs(distribution1[key] - distribution2[key]) for key in keys])

Expand Down

0 comments on commit e297681

Please sign in to comment.