def test_orbits(self, dim):
        """Test if function returns all the integer partitions of 5. This test does not
        require ``similarity.orbits`` to return the orbits in any specified order."""
        partition = all_orbits[dim]
        orb = similarity.orbits(dim)

        assert sorted(partition) == sorted(orb)
예제 #2
0
    def get_event_probability_exact(self, event: tuple):
        """
        :param event: a specification of an event. Tuple of length 2
        :return: the exact probability of the given event
        """
        photons, max_photons_per_mode = event
        prob = 0
        for orbit in sfs.orbits(photons):
            if max(orbit) <= max_photons_per_mode:
                prob += self.get_orbit_probability_exact(orbit)

        return prob
예제 #3
0
    def get_orbit_feature_vector(self, max_photons: int, mc=False, samples: int = 1000):
        """
        Calculate and return a feature vector, based on orbit probabilities

        :param max_photons: max number of photons in a detection event
        :param mc: if True, uses monte carlo simulation to. If False, calculates exact
        :param samples: number of samples for monte carlo simulation
        :return: feature vector comprised of orbit probabilities
        """
        orbit_prob = partial(self.get_orbit_probability_mc, samples=samples) if mc else self.get_orbit_probability_exact

        return [orbit_prob(orbit)
                for n in range(max_photons + 1)
                for orbit in sfs.orbits(n)]
 def test_orbit_sorted(self, dim):
     """Test if function generates orbits that are lists sorted in descending order."""
     assert all([o == sorted(o, reverse=True) for o in similarity.orbits(dim)])
 def test_orbit_sum(self, dim):
     """Test if function generates orbits that are lists that sum to ``dim``."""
     assert all([sum(o) == dim for o in similarity.orbits(dim)])
예제 #6
0
##############################################################################
# Here, ``[1, 1]`` means that two photons were detected, each in a separate mode. Other samples
# can be randomly generated from the ``[1, 1]`` orbit using:

print(similarity.orbit_to_sample([1, 1], modes=m0.modes))

##############################################################################
# Orbits provide a useful way to coarse-grain the samples from GBS into outcomes that are
# statistically more likely to be observed. However, we are interested in coarse-graining further
# into *events*, which correspond to a combination of orbits with the same photon number such
# that the number of photons counted in each mode does not exceed a fixed value
# ``max_count_per_mode``. To understand this, let's look at all of the orbits with a photon
# number of 5:

print(list(similarity.orbits(5)))

##############################################################################
# All 5-photon samples belong to one of the orbits above. A 5-photon event with
# ``max_count_per_mode = 3`` means that we include the orbits: ``[[1, 1, 1, 1, 1], [2, 1, 1, 1],
# [3, 1, 1], [2, 2, 1], [3, 2]]`` and ignore the orbits ``[[4, 1], [5]]``. For example,
# the sample ``[0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 3, 0, 0, 0]`` is a 5-photon event:

print(
    similarity.sample_to_event(
        [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 3, 0, 0, 0], 3))

##############################################################################
# Samples with more than ``max_count_per_mode`` in any mode are not counted as part of the event:

print(