Skip to content

Commit

Permalink
55 general losses bobs (#56)
Browse files Browse the repository at this point in the history
* refactor(theboss): Work in progress on refactoring theboss.

* refactor(theboss): Refactor of the boson sampling utilities.

* refactor(theboss): Remove quantum computation utilities tests as they don't test anything now.

* refactor(theboss): Add boson sampling utilities tests file.

* refactor(theboss): Add tests for boson sampling utilities. Fix problems in related files.

* refactor(theboss): Add initial version of fixed non-uniform losses approximation strategy. Black whole project.

* fix(interface): Fix the interface issues by making some of the interfaces more general.

* fix(permanents): Fix classical permanent calculator after the quantum utilities script refactor.

* fix(permanents): Fix the tests after the quantum utilities script refactor.

* refactor(typehints): Fix typehints in most of the files.

* feat(tests): Add test for the non-uniform losses strategies.

* refactor(tests): Add comments and fix typehints in the tests.

* update(version): Update theboss version to 3.0.0

Co-authored-by: Tomasz Rybotycki <rybotycki.tomasz@gmail.com>
  • Loading branch information
Tomev and Tomasz Rybotycki authored Jul 29, 2022
1 parent 56820bb commit 4bbe2bd
Show file tree
Hide file tree
Showing 40 changed files with 2,329 additions and 816 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="2.1.1",
version="3.0.0",
author="Tomasz Rybotycki",
author_email="rybotycki.tomasz+theboss@gmail.com",
long_description=long_description,
Expand Down
41 changes: 31 additions & 10 deletions tests/gcc_based_strategies_tests_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"""

from theboss.boson_sampling_utilities.boson_sampling_utilities import (
calculate_number_of_possible_n_particle_m_mode_output_states,
calculate_number_of_possible_lossy_n_particle_m_mode_output_states,
bosonic_space_dimension,
)
from theboss.distribution_calculators.bs_exact_distribution_with_uniform_losses import (
BSDistributionCalculatorWithFixedLosses,
Expand All @@ -22,22 +21,34 @@


class GCCBasedStrategiesTestsBase(TestBSClassicalSimulationStrategies):
"""
A base class for GCC based tests. It takes care of some boilerplate code.
"""

def _perform_lossless_test(self, strategy: StrategyType = StrategyType.GCC) -> None:
"""
Boilerplate code taking care of performing common part of the uniform losses
tests.
:param strategy:
Strategy to test.
"""
self._strategies_factory.experiment_configuration = (
self._sampling_tvd_experiment_config
)
self._strategies_factory.strategy_type = strategy
distance_experiment_configuration = SamplingAccuracyExperimentConfiguration(
# This exact calculator, when there are no losses, will do the work just fine.
# This exact calculator, when there are no losses, will do the work just
# fine.
exact_calculator=BSDistributionCalculatorWithFixedLosses(
self._sampling_tvd_experiment_config, self._bs_permanent_calculator
),
estimation_calculator=self._generate_frequencies_calculator(
self._strategies_factory.generate_strategy()
),
outcomes_number=calculate_number_of_possible_n_particle_m_mode_output_states(
n=self._sampling_tvd_experiment_config.number_of_particles_left,
m=self._sampling_tvd_experiment_config.number_of_modes,
outcomes_number=bosonic_space_dimension(
particles_number=self._sampling_tvd_experiment_config.number_of_particles_left,
modes_number=self._sampling_tvd_experiment_config.number_of_modes,
),
approximation_tvd_bound=0, # This strategy returns exact solution.
)
Expand All @@ -48,7 +59,15 @@ def _perform_test_for_uniform_losses(
strategy: StrategyType = StrategyType.UNIFORM_LOSSES_GCC,
approximation_bound: int = 0,
) -> None:
"""
Boilerplate code taking care of performing common part of the uniform losses
tests.
:param strategy:
Strategy to test.
:param approximation_bound:
Approximation tvd upperbound.
"""
self._strategies_factory.experiment_configuration = (
self._sampling_tvd_experiment_config
)
Expand All @@ -73,15 +92,17 @@ def _perform_test_for_uniform_losses(
)

distance_experiment_configuration = SamplingAccuracyExperimentConfiguration(
# This exact calculator, when there are no losses, will do the work just fine.
# This exact calculator, when there are no losses, will do the work just
# fine.
exact_calculator=exact_calculator,
estimation_calculator=self._generate_frequencies_calculator(
self._strategies_factory.generate_strategy(),
outcomes=exact_calculator.get_outcomes_in_proper_order(),
),
outcomes_number=calculate_number_of_possible_lossy_n_particle_m_mode_output_states(
n=self._sampling_tvd_experiment_config.initial_number_of_particles,
m=self._sampling_tvd_experiment_config.number_of_modes,
outcomes_number=bosonic_space_dimension(
particles_number=self._sampling_tvd_experiment_config.initial_number_of_particles,
modes_number=self._sampling_tvd_experiment_config.number_of_modes,
losses=True,
),
approximation_tvd_bound=approximation_bound,
)
Expand Down
24 changes: 11 additions & 13 deletions tests/simulation_strategies_tests_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import unittest
from copy import deepcopy
from dataclasses import dataclass
from typing import List
from numpy import array, ndarray, average
from typing import List, Tuple
from numpy import array, average
from numpy.random import randint
from scipy.stats import unitary_group
from theboss.boson_sampling_utilities.permanent_calculators.bs_permanent_calculator_factory import (
Expand All @@ -35,7 +35,7 @@
SimulationStrategyInterface,
)
from theboss.boson_sampling_utilities.boson_sampling_utilities import (
calculate_number_of_possible_n_particle_m_mode_output_states,
bosonic_space_dimension,
)
from tqdm import tqdm

Expand Down Expand Up @@ -78,7 +78,6 @@ def __init__(self, *args, **kwargs):
self._calculator_initial_state = None

def setUp(self) -> None:
print(f"\nIn method {self._testMethodName}. Test start!\n")
self._permutation_matrix = array(
[
[0, 0, 1, 0, 0],
Expand Down Expand Up @@ -108,7 +107,7 @@ def setUp(self) -> None:

self._sampling_tvd_experiment_config = BosonSamplingExperimentConfiguration(
interferometer_matrix=self._permutation_matrix,
initial_state=array(self._distance_calculation_initial_state, dtype=int),
initial_state=self._distance_calculation_initial_state,
initial_number_of_particles=distance_calculation_initial_number_of_particles,
number_of_modes=len(self._distance_calculation_initial_state),
number_of_particles_lost=self._distance_calculation_number_of_particles_lost,
Expand Down Expand Up @@ -136,7 +135,7 @@ def setUp(self) -> None:
haar_random_number_of_particles_lost = 2
self._haar_random_experiment_configuration = BosonSamplingExperimentConfiguration(
interferometer_matrix=array([], dtype=complex),
initial_state=array(self._haar_random_experiment_input_state, dtype=int),
initial_state=self._haar_random_experiment_input_state,
initial_number_of_particles=haar_random_number_of_particles_lost,
number_of_modes=len(self._haar_random_experiment_input_state),
number_of_particles_lost=haar_random_number_of_particles_lost,
Expand All @@ -147,9 +146,6 @@ def setUp(self) -> None:

self._calculator_initial_state = self._distance_calculation_initial_state

def tearDown(self) -> None:
print("\nTest finished!\n")

def _prepare_lossless_distance_experiments_settings_with_binned_inputs(
self,
) -> None:
Expand Down Expand Up @@ -241,7 +237,9 @@ def _check_if_approximation_is_within_bounds(
)

def _generate_frequencies_calculator(
self, strategy: SimulationStrategyInterface, outcomes: List[ndarray] = None
self,
strategy: SimulationStrategyInterface,
outcomes: List[Tuple[int, ...]] = None,
) -> BSSampleBasedDistributionCalculator:
estimated_distribution_calculator = BSSampleBasedDistributionCalculator(
experiment_configuration=self._sampling_tvd_experiment_config,
Expand Down Expand Up @@ -321,9 +319,9 @@ def _prepare_lossless_distance_experiment_settings(self) -> None:
def _test_state_average_probability_for_haar_random_matrices(
self, strategy_factory: SimulationStrategyFactory
) -> None:
number_of_outcomes = calculate_number_of_possible_n_particle_m_mode_output_states(
n=self._haar_random_experiment_configuration.number_of_particles_left,
m=self._haar_random_experiment_configuration.number_of_modes,
number_of_outcomes = bosonic_space_dimension(
particles_number=self._haar_random_experiment_configuration.number_of_particles_left,
modes_number=self._haar_random_experiment_configuration.number_of_modes,
)

error_bound = count_tv_distance_error_bound_of_experiment_results(
Expand Down
4 changes: 3 additions & 1 deletion tests/test_bobs_strategy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
__author__ = "Tomasz Rybotycki"

"""
The aim of this script is to test the BOBS strategy accuracy.
The aim of this script is to test the BOBS strategy accuracy. This script tests
only simulations with uniform or no losses. Nonuniform losses have been placed
in another script.
"""

from tests.gcc_based_strategies_tests_base import GCCBasedStrategiesTestsBase
Expand Down
Loading

0 comments on commit 4bbe2bd

Please sign in to comment.