Ejemplo n.º 1
0
    def test_two_mode_squeezed_state_torontonian(self):
        """Test the sampling routines by comparing the photon number frequencies and the exact
        probability distribution of a two mode squeezed vacuum state
        """
        n_samples = 1000
        mean_n = 1.0
        r = np.arcsinh(np.sqrt(mean_n))
        c = np.cosh(2 * r)
        s = np.sinh(2 * r)
        sigma = np.array([[c, s, 0, 0], [s, c, 0, 0], [0, 0, c, -s],
                          [0, 0, -s, c]])

        samples = torontonian_sample_state(sigma, samples=n_samples)
        assert np.all(samples[:, 0] == samples[:, 1])

        samples1d = samples[:, 0]
        bins = np.arange(0, max(samples1d), 1)
        (freq, _) = np.histogram(samples1d, bins=bins)
        rel_freq = freq / n_samples

        probs = np.empty([2])
        probs[0] = 1.0 / (1.0 + mean_n)
        probs[1] = 1.0 - probs[0]
        assert np.allclose(
            rel_freq,
            probs[0:-1],
            atol=rel_tol / np.sqrt(n_samples),
            rtol=rel_tol / np.sqrt(n_samples),
        )
Ejemplo n.º 2
0
    def measure_threshold(self, modes, shots=1, select=None):
        if shots != 1:
            if select is not None:
                raise NotImplementedError(
                    "Gaussian backend currently does not support "
                    "postselection")
            warnings.warn(
                "Cannot simulate non-Gaussian states. "
                "Conditional state after Threshold measurement has not been updated."
            )

        mu = self.circuit.mean
        cov = self.circuit.scovmatxp()
        # check we are sampling from a gaussian state with zero mean
        if not allclose(mu, zeros_like(mu)):
            raise NotImplementedError(
                "Threshold measurement is only supported for "
                "Gaussian states with zero mean")
        x_idxs = array(modes)
        p_idxs = x_idxs + len(mu)
        modes_idxs = concatenate([x_idxs, p_idxs])
        reduced_cov = cov[ix_(modes_idxs, modes_idxs)]
        samples = torontonian_sample_state(reduced_cov, shots)
        # for backward compatibility with previous measurement behaviour,
        # if only one shot, then we drop the shots axis
        if shots == 1:
            samples = samples.reshape((len(modes), ))
        return samples
Ejemplo n.º 3
0
 def test_torontonian_state_lowmax(self, max_photons):
     """test sampling never exceeds max photons for torontonian"""
     m = 0.432
     phi = 0.546
     V = TMS_cov(np.arcsinh(m), phi)
     res = torontonian_sample_state(V, samples=10, max_photons=max_photons)
     assert np.max(res) <= max_photons
Ejemplo n.º 4
0
    def measure_threshold(self, modes, shots=1, select=None, **kwargs):
        if shots != 1:
            if select is not None:
                raise NotImplementedError(
                    "Gaussian backend currently does not support "
                    "postselection")
            warnings.warn(
                "Cannot simulate non-Gaussian states. "
                "Conditional state after Threshold measurement has not been updated."
            )

        mu = self.circuit.mean
        cov = self.circuit.scovmatxp()
        mean = self.circuit.smeanxp()
        # check we are sampling from a gaussian state with zero mean

        x_idxs = array(modes)
        p_idxs = x_idxs + len(mu)
        modes_idxs = concatenate([x_idxs, p_idxs])
        reduced_cov = cov[ix_(modes_idxs, modes_idxs)]
        reduced_mean = mean[modes_idxs]
        samples = torontonian_sample_state(mu=reduced_mean,
                                           cov=reduced_cov,
                                           samples=shots)

        return samples
Ejemplo n.º 5
0
    def test_single_coherent_state_torontonian(self):
        """Test the sampling routines by comparing the photon number frequencies and the exact
        probability distribution of a single mode squeezed vacuum state
        """
        n_samples = 10000

        sigma = np.array([[1.0, 0.0], [0.0, 1.0]])
        mu = np.array([1.0, 1.0])
        samples = torontonian_sample_state(sigma, samples=n_samples, mu=mu)

        samples_list = list(samples)

        rel_freq = np.array([samples_list.count(0),
                             samples_list.count(1)]) / n_samples
        x2 = np.array([np.exp(-0.5), 1 - np.exp(-0.5)])
        assert np.allclose(rel_freq,
                           x2,
                           atol=rel_tol / np.sqrt(n_samples),
                           rtol=rel_tol / np.sqrt(n_samples))
Ejemplo n.º 6
0
    def test_single_squeezed_state_torontonian(self):
        """Test the sampling routines by comparing the photon number frequencies and the exact
        probability distribution of a single mode squeezed vacuum state
        """
        n_samples = 10000
        mean_n = 1.0
        r = np.arcsinh(np.sqrt(mean_n))
        sigma = np.array([[np.exp(2 * r), 0.0], [0.0, np.exp(-2 * r)]])
        samples = torontonian_sample_state(sigma, samples=n_samples)
        samples_list = list(samples)

        rel_freq = np.array([samples_list.count(0),
                             samples_list.count(1)]) / n_samples
        x2 = np.empty([2])

        x2[0] = 1.0 / np.sqrt(1.0 + mean_n)
        x2[1] = 1.0 - x2[0]
        assert np.allclose(rel_freq,
                           x2,
                           atol=rel_tol / np.sqrt(n_samples),
                           rtol=rel_tol / np.sqrt(n_samples))
Ejemplo n.º 7
0
 def test_torontonian_samples_nans(self):
     """test exception is raised if not a numpy array"""
     with pytest.raises(ValueError,
                        match="Covariance matrix must not contain NaNs."):
         torontonian_sample_state(np.array([[0, 5], [0, np.NaN]]),
                                  samples=20)
Ejemplo n.º 8
0
 def test_torontonian_samples_nonsquare(self):
     """test exception is raised if not a numpy array"""
     with pytest.raises(ValueError,
                        match="Covariance matrix must be square."):
         torontonian_sample_state(np.array([[0, 5, 3], [0, 1, 2]]),
                                  samples=20)
Ejemplo n.º 9
0
 def test_torontonian_samples_nonnumpy(self):
     """test exception is raised if not a numpy array"""
     with pytest.raises(TypeError):
         torontonian_sample_state(5, samples=20)