Exemple #1
0
    def test_single_pm_graphs(self, parallel):
        """Tests that the number of photons is the same for modes i and n-i
        in the special case of a graph with one single perfect matching
        """
        n = 10  # size of the graph
        approx_samples = 1000  # number of samples in the hafnian approximation
        A = np.eye(n)[::-1]
        n_mean = 2
        nr_samples = 10
        samples = hafnian_sample_graph(
            A,
            n_mean,
            cutoff=5,
            approx=True,
            approx_samples=approx_samples,
            samples=nr_samples,
            parallel=parallel,
        )

        test_passed = True
        for i in range(nr_samples):
            s = samples[i]
            for k in range(len(s) // 2):
                if s[k] + s[-(k + 1)] % 2 == 1:
                    test_passed = False

        assert test_passed
Exemple #2
0
    def test_probability_vacuum(self):
        """Tests that the probability of zero photons is correct"""

        n = 4  # n is the size of the graph
        approx_samples = 1000  # number of samples in the hafnian approximation
        A = np.random.binomial(1, 0.5, (n, n))

        A = np.triu(A)
        A = A + np.transpose(A)
        n_mean = 1.0
        Q = gen_Qmat_from_graph(A, n_mean)
        prob0 = np.abs(1 / (np.sqrt(np.linalg.det(Q))))

        nr_samples = 100
        samples = hafnian_sample_graph(A,
                                       n_mean,
                                       cutoff=5,
                                       approx=True,
                                       approx_samples=approx_samples,
                                       samples=nr_samples)
        nr_zeros = 0

        for i in range(nr_samples):
            photons = np.sum(samples[i])
            if photons == 0:
                nr_zeros += 1

        prob0_estimate = nr_zeros / nr_samples
        # allowed error in estimation
        delta = 0.2

        assert np.abs(prob0 - prob0_estimate) < delta
Exemple #3
0
 def test_hafnian_sample_graph(self):
     """Test hafnian sampling from a graph"""
     A = np.array([[0, 3.0 + 4j], [3.0 + 4j, 0]])
     n_samples = 1000
     mean_n = 0.5
     samples = hafnian_sample_graph(A, mean_n, samples=n_samples)
     approx_mean_n = np.sum(samples) / n_samples
     assert np.allclose(mean_n, approx_mean_n, rtol=2e-1)