示例#1
0
def getUf(n, reload, v):
    """
    We implemented a method to save our generated matrices to save compute time.
    In doing so, we saved our U_f functions in the directory 'uf/{algo}/'.

    getUf allows you to either load a matrix U_f if it exists or generate a new matrix that is balanced or constant

    **oracle.uf is a wrapper function in our oracle.py file (Generates a bit mapping and a U_f matrix)**

    Args:
        n: number of bits
        balanced: balance flag
        reload: flag to reload U_f
        v: verbose flag

    Returns:
        Uf matrix for loaded or generated constant or balanced
    """
    path = f'uf/bv/bv{n}.npy'
    (a, b) = load_ab_lists(n, reload)
    if v:
        print(f'a = {a}, b = {b}')

    if not os.path.exists(path) or reload:
        U_f = oracle.uf(n, 1, oracle.Algos.BV, func=(a, b))
        if v:
            print("New matrix U_f saved to path: " + path, flush=True)
        np.save(path, U_f)
    else:
        U_f = Operator(np.load(path))
    if v:
        print(f'Matrix U_f is: \n{U_f}\n', flush=True)

    return U_f
示例#2
0
def getUf(n, f_type, reload, v):
    """
    We implemented a method to save our generated matrices to save compute time.
    In doing so, we saved our U_f functions in the directory 'uf/{algo}/'.

    getUf allows you to either load a matrix U_f if it exists or generate a new matrix that is balanced or constant

    **oracle.uf is a wrapper function in our oracle.py file (Generates a bit mapping and a U_f matrix)**

    Args:
        n: number of bits
        balanced: balance flag
        reload: flag to reload U_f
        v: verbose flag

    Returns:
        Uf matrix for loaded or generated constant or balanced
    """
    path = f'uf/dj/{"const" if f_type == oracle.DJ.CONSTANT else "bal"}{n}.npy'
    if not os.path.exists(
            path
    ) or reload:  # If path to oracle doesn't exist or reload flag => generate new U_f
        U_f = oracle.uf(n, 1, oracle.Algos.DJ, f_type)
        if v:
            print("New matrix U_f saved to path: " + path, flush=True)
        np.save(path, U_f)
    else:
        U_f = Operator(np.load(path))
    if v:
        print(f'Matrix U_f is: \n{U_f}\n', flush=True)

    return U_f
def getZf(n, reload):
    if not reload:
        path = f'uf/grover/grover{n}.npy'
        Z_f = np.load(path)
    else:
        Z_f = oracle.uf(n, 1, algo=oracle.Algos.GROVER)

    return Z_f
示例#4
0
def get_zf(n, reload, verbose):
    path = f'uf/grover/grover{n}.npy'
    if not reload and os.path.exists(path):
        Z_f = np.load(path)
    else:
        if verbose:
            print(f'Getting new Zf .. ', end='', flush=True)
        Z_f = oracle.uf(n, 1, algo=oracle.Algos.GROVER)
        if verbose:
            print('done')
    return Z_f
示例#5
0
    def __getUf(self, n, reload, v):
        """
        We implemented a method to save our generated matrices to save compute time. 
        In doing so, we saved our U_f functions in the directory 'uf/{algo}/'. 

        __getUf allows you to either load a corresponding matrix Uf if it exists or load a new one with desired alpha

        **oracle.uf is a wrapper function in our oracle.py file (Generates a bit mapping and a U_f matrix)**

        Args: 
            n: number of bits
            reload: flag to reload a_list
            v: verbose flag

        Returns:
            Uf matrix for loaded or generated alpha and beta
        """
        path = 'uf/bv/bv{}.npy'.format(n)
        self.a = self.__load_a_list(
            n, reload)  # Load or generate random alpha value
        self.b = random.randint(0, 1)  # Generate random beta value
        if v:
            print("Alpha is as follows: ")
            print(self.a)
            print()
            print("Beta is as follows: ")
            print(self.b)
            print()
        if not os.path.exists(
                path
        ) or reload:  # If path to oracle doesn't exist or reload flag => generate new U_f
            U_f = oracle.uf(n, 1, algo=oracle.Algos.BV, func=(self.a, self.b))
            if v:
                print("New matrix U_f saved to path: " + path)
            np.save(path, U_f)
        else:
            U_f = np.load(path)
        if v:
            print("Matrix U_f is as follows: ")
            print(U_f)
            print()
        return U_f
示例#6
0
    def __getUf(self, n, balanced, reload, v):
        """
        We implemented a method to save our generated matrices to save compute time. 
        In doing so, we saved our U_f functions in the directory 'uf/{algo}/'. 

        __getUf allows you to either load a corresponding matrix Uf if it exists or load a new matrix that is balanced or constant

        **oracle.uf is a wrapper function in our oracle.py file (Generates a bit mapping and a U_f matrix)**

        Args: 
            n: number of bits
            balanced: balance flag
            reload: flag to reload U_f
            v: verbose flag

        Returns:
            Uf matrix for loaded or generated constant or balanced
        """
        path = 'uf/dj/{}{}.npy'.format('bal' if balanced else 'const', n)
        if not os.path.exists(
                path
        ) or reload:  # If path to oracle doesn't exist or reload flag => generate new U_f
            U_f = oracle.uf(
                n, 1, oracle.Algos.DJ,
                oracle.DJ.BALANCED if balanced else oracle.DJ.CONSTANT)
            if v:
                print("New matrix U_f saved to path: " + path)
            np.save(path, U_f)
        else:
            U_f = np.load(path)
        if v:
            print("Matrix U_f is as follows: ")
            print(U_f)
            print()

        return U_f