Example #1
0
    def test_kron(self, shape_a, shape_b, sparse):
        """ Tests kronecker product """
        np.random.seed()

        a_np = np.random.random(shape_a)
        b_np = np.random.random(shape_b)
        expected = np.kron(a_np, b_np)

        if sparse:
            a_np = sp.csr_matrix(a_np)
            b_np = sp.csr_matrix(b_np)

        b0 = np.random.randint(1, a_np.shape[0] + 1)
        b1 = np.random.randint(1, a_np.shape[1] + 1)
        b2 = np.random.randint(1, b_np.shape[0] + 1)
        b3 = np.random.randint(1, b_np.shape[1] + 1)

        a = ds.array(a_np, (b0, b1))
        b = ds.array(b_np, (b2, b3))

        b4 = np.random.randint(1, (b0 * b2) + 1)
        b5 = np.random.randint(1, (b1 * b3) + 1)

        computed = ds.kron(a, b, (b4, b5))

        self.assertTrue(_validate_array(computed))

        computed = computed.collect(False)

        # convert to ndarray because there is no kron for sparse matrices in
        # scipy
        if a._sparse:
            computed = computed.toarray()

        self.assertTrue(_equal_arrays(expected, computed))
Example #2
0
    def test_kron_regular(self, a_shape, a_bsize, b_shape, b_bsize):
        """ Tests kron when blocks of b are all equal """
        a = ds.random_array(a_shape, a_bsize)
        b = ds.random_array(b_shape, b_bsize)

        computed = ds.kron(a, b)
        expected = np.kron(a.collect(), b.collect())

        self.assertTrue(_validate_array(computed))
        self.assertTrue(_equal_arrays(computed.collect(), expected))
Example #3
0
    def build_gate(self, gate, pos):
        bs = self._hilbert_size // max_par
        gates = [
            np.identity(pow(base, pos[0])), gate,
            np.identity(pow(base, self._N - pos[1] - 1))
        ]

        #gate[0]
        bsize_x = gates[0].shape[0] // max_par
        if (bsize_x < 1):
            bsize_x = 1
        bsize_y = gates[0].shape[1] // max_par
        if (bsize_y < 1):
            bsize_y = 1
        bsize = (bsize_x, bsize_y)
        g_0 = ds.array(gates[0], block_size=bsize)
        #gate[1]
        bsize_x = gates[1].shape[0] // max_par
        if (bsize_x < 1):
            bsize_x = 1
        bsize_y = gates[1].shape[1] // max_par
        if (bsize_y < 1):
            bsize_y = 1
        bsize = (bsize_x, bsize_y)
        g_0 = ds.kron(g_0, ds.array(gates[1], block_size=bsize))

        #gate[2]
        bsize_x = gates[2].shape[0] // max_par
        if (bsize_x < 1):
            bsize_x = 1
        bsize_y = gates[2].shape[1] // max_par
        if (bsize_y < 1):
            bsize_y = 1
        bsize = (bsize_x, bsize_y)
        g_0 = ds.kron(g_0,
                      ds.array(gates[2], block_size=bsize),
                      block_size=(bs, bs))

        return g_0