Beispiel #1
0
def scal_prod_parity(i, j):
    """Return sign of entry i, j of parity-adjusted Hadamard matrix  

    Then such a matrix M has entries 

         M[i,j] = (-1) ** scal_prod_parity(i, j) .
    """
    return bitparity(i & j) ^ (bitparity(i) & bitparity(j))
Beispiel #2
0
    def make_parity_masks(self):
        """Create parity masks

        self.PARITY_MASK[0] and self.PARITY_MASK[1] select the 
        entries v[j] of a vector stored in a single variable, 
        where index j has even and odd parity, respectively.
        """
        parity = sum((bitparity(i) << i for i in range(self.INT_FIELDS)))
        mask0 = self.smask(self.P, ~parity)
        mask1 = self.smask(self.P, parity)
        self.PARITY_MASK = [mask0, mask1]
Beispiel #3
0
def gate_ctrl_not_testdata():
    for cols, data, v1, v2 in gate_ctrl_not_data:
        yield QStateMatrix(0, cols, data), v1, v2
    for cols in list(range(6)):
        for r in range(cols + 3):
            for i in range(3):
                vc = randint(0, (1 << cols) - 1)
                v = randint(0, (1 << cols) - 1)
                while bitparity(vc & v):
                    v = randint(0, (1 << cols) - 1)
                m = qs_rand_matrix(0, cols, r)
                yield m, vc, v
Beispiel #4
0
def make_suboctad_table():
    """Make table for conversion from suboctad to bit vector

    Let o be an octad. Let b_0,...,b_7 be the elements of octad 
    o in natural order. Let x = sum 2**i * x_i, i = 0,...,7.
    Then sum (x_i * b_i) is a suboctad as defined in function
    mat24_suboctad_to_cocode() in module mat24_functions.c.  
    In that function we also define a natural numbering of the
    suboctads of an octad. Entry x of following table is the
    number of the suboctad sum (x_i * b_i) for 0 <= x < 128.
    Note that bit b_7 can be computed from bits b_0,...,b_6
    as the even parity bit.
    """
    return [((i >> 1) ^ -bitparity(i)) & 0x3f for i in range(128)]
Beispiel #5
0
    def merge_parities(self, dest, var0, var1, parity=0):
        """Merge entries of dest from var0 and var1

        Here dest, var0, and var1 are vectors stored in a
        single variable.

        If the integer 'parity' has even bit parity then the
        entry var0[i] copied to dest[i] if i has even parity
        and the entry var1[i] is copied to dest[i] if i has
        odd parity. If the integer 'parity' has odd parity, 
        the roles of var0 and var1 are echanged.
        """
        mask0, mask1 = self.PARITY_MASK
        if bitparity(parity) == 1:
            mask0, mask1 = mask1, mask0
        s = dest.assign((var0 & mask0) | (var1 & mask1))
        self.matrix_code += s
        self.n_operations += 3
        self.n_code_lines += 1
Beispiel #6
0
def hadamard_matrix(lg_n):
    """Return a 2**lg_n times 2**lg_n Hadamard matrix"""
    n = 1 << lg_n
    f = lambda i, j: bitparity(i & j)
    return pm_mat_from_function(f, n)