Esempio n. 1
0
    def _build_plan(self, plan_factory, device_params, alpha, beta, seed):
        plan = plan_factory()

        bijection = philox(64, 2)

        # Keeping the kernel the same so it can be cached.
        # The seed will be passed as the computation parameter instead.
        keygen = KeyGenerator.create(bijection, seed=numpy.int32(0))

        sampler = normal_bm(bijection, numpy.float64)

        squeezing = plan.persistent_array(self._system.squeezing)
        decoherence = plan.persistent_array(self._system.decoherence)

        plan.kernel_call(TEMPLATE.get_def("generate_input_state"),
                         [alpha, beta, squeezing, decoherence, seed],
                         kernel_name="generate",
                         global_size=alpha.shape,
                         render_kwds=dict(
                             system=self._system,
                             representation=self._representation,
                             Representation=Representation,
                             bijection=bijection,
                             keygen=keygen,
                             sampler=sampler,
                             ordering=ordering,
                             exp=functions.exp(numpy.float64),
                             mul_cr=functions.mul(numpy.complex128,
                                                  numpy.float64),
                             add_cc=functions.add(numpy.complex128,
                                                  numpy.complex128),
                         ))

        return plan
Esempio n. 2
0
def test_computation_general(thr_and_double):

    size = 10000
    batch = 101

    thr, double = thr_and_double
    dtype = numpy.float64 if double else numpy.float32
    mean, std = -2, 10
    bijection = philox(64, 4)
    sampler = normal_bm(bijection, dtype, mean=mean, std=std)

    rng = CBRNG(Type(dtype, shape=(batch, size)), 1, sampler)
    check_computation(thr, rng, mean=mean, std=std)
Esempio n. 3
0
def test_computation_general(thr_and_double):

    size = 10000
    batch = 101

    thr, double = thr_and_double
    dtype = numpy.float64 if double else numpy.float32
    mean, std = -2, 10
    bijection = philox(64, 4)
    sampler = normal_bm(bijection, dtype, mean=mean, std=std)

    rng = CBRNG(Type(dtype, shape=(batch, size)), 1, sampler)
    check_computation(thr, rng, mean=mean, std=std)
Esempio n. 4
0
 def get_sampler(self, double):
     dtype = numpy.complex128 if double else numpy.complex64
     return normal_bm(self.bijection, dtype, mean=self.mean, std=self.std)
Esempio n. 5
0
 def get_sampler(self, double):
     dtype = numpy.float64 if double else numpy.float32
     return normal_bm(self.bijection, dtype, mean=self.mean, std=self.std)
Esempio n. 6
0
    def _build_plan(self, plan_factory, device_params, alpha, beta, alpha_i,
                    beta_i, seed):
        plan = plan_factory()

        system = self._system
        representation = self._representation

        unitary = plan.persistent_array(self._system.unitary)

        needs_noise_matrix = representation != Representation.POSITIVE_P and system.needs_noise_matrix(
        )

        mmul = MatrixMul(alpha, unitary, transposed_b=True)

        if not needs_noise_matrix:

            # TODO: this could be sped up for repr != POSITIVE_P,
            # since in that case alpha == conj(beta), and we don't need to do two multuplications.

            mmul_beta = MatrixMul(beta, unitary, transposed_b=True)
            trf_conj = self._make_trf_conj()
            mmul_beta.parameter.matrix_b.connect(trf_conj,
                                                 trf_conj.output,
                                                 matrix_b_p=trf_conj.input)

            plan.computation_call(mmul, alpha, alpha_i, unitary)
            plan.computation_call(mmul_beta, beta, beta_i, unitary)

        else:

            noise_matrix = system.noise_matrix()
            noise_matrix_dev = plan.persistent_array(noise_matrix)

            # If we're here, it's not positive-P, and alpha == conj(beta).
            # This means we can just calculate alpha, and then build beta from it.

            w = plan.temp_array_like(alpha)
            temp_alpha = plan.temp_array_like(alpha)

            plan.computation_call(mmul, temp_alpha, alpha_i, unitary)

            bijection = philox(64, 2)

            # Keeping the kernel the same so it can be cached.
            # The seed will be passed as the computation parameter instead.
            keygen = KeyGenerator.create(bijection, seed=numpy.int32(0))

            sampler = normal_bm(bijection, numpy.float64)

            plan.kernel_call(TEMPLATE.get_def("generate_apply_matrix_noise"),
                             [w, seed],
                             kernel_name="generate_apply_matrix_noise",
                             global_size=alpha.shape,
                             render_kwds=dict(
                                 bijection=bijection,
                                 keygen=keygen,
                                 sampler=sampler,
                                 mul_cr=functions.mul(numpy.complex128,
                                                      numpy.float64),
                                 add_cc=functions.add(numpy.complex128,
                                                      numpy.complex128),
                             ))

            noise = plan.temp_array_like(alpha)
            plan.computation_call(mmul, noise, w, noise_matrix_dev)

            plan.kernel_call(TEMPLATE.get_def("add_noise"),
                             [alpha, beta, temp_alpha, noise],
                             kernel_name="add_noise",
                             global_size=alpha.shape,
                             render_kwds=dict(
                                 add=functions.add(numpy.complex128,
                                                   numpy.complex128),
                                 conj=functions.conj(numpy.complex128)))

        return plan
Esempio n. 7
0
 def get_sampler(self, bijection, double):
     dtype = numpy.complex128 if double else numpy.complex64
     return normal_bm(bijection, dtype, mean=self.mean, std=self.std)
Esempio n. 8
0
 def get_sampler(self, bijection, double):
     dtype = numpy.float64 if double else numpy.float32
     return normal_bm(bijection, dtype, mean=self.mean, std=self.std)