def unit_tests(n): if not os.path.exists('uf'): os.mkdir('uf') if not os.path.exists('uf/dj'): os.mkdir('uf/dj') SAVEPATH = 'uf/dj/' CONSTPATH = 'const' + str(n) BALPATH = 'bal' + str(n) if not os.path.exists(SAVEPATH + CONSTPATH + '.npy'): const_mapping = oracle.init_bit_mapping(n, algo=oracle.Algos.DJ, func=oracle.DJ.CONSTANT) assert is_bal_or_const(const_mapping, oracle.DJ.CONSTANT) U_const_f = oracle.gen_matrix(const_mapping, n, 1) else: U_const_f = np.load(SAVEPATH + CONSTPATH + '.npy') if not os.path.exists(SAVEPATH + BALPATH + '.npy'): balanced_mapping = oracle.init_bit_mapping(n, algo=oracle.Algos.DJ, func=oracle.DJ.BALANCED) assert is_bal_or_const(balanced_mapping, oracle.DJ.BALANCED) U_bal_f = oracle.gen_matrix(balanced_mapping, n, 1) else: U_bal_f = np.load(SAVEPATH + BALPATH + '.npy') assert is_unitary(U_const_f) if not os.path.exists(SAVEPATH + CONSTPATH + '.npy'): np.save(SAVEPATH + CONSTPATH, U_const_f) assert is_unitary(U_bal_f) if not os.path.exists(SAVEPATH + BALPATH + '.npy'): np.save(SAVEPATH + BALPATH, U_bal_f)
def getUf(n, reload): path = f'uf/simon/simon{n}.npy' SAVEDIR = 'uf/simon/' SLIST = 's_list.npy' if not reload and os.path.exists(path): U_f = np.load(path) s_list = np.load(SAVEDIR + SLIST, allow_pickle=True).item() s = s_list[n] else: SAVEDIR = 'uf/simon/' SLIST = 's_list.npy' mapping = oracle.init_bit_mapping(n, algo=oracle.Algos.SIMON) s = get_s(mapping) assert is_valid(s, mapping) if os.path.exists(SAVEDIR + SLIST): s_list = np.load(SAVEDIR + SLIST, allow_pickle=True).item() else: s_list = {} s_list[n] = s np.save(SAVEDIR + SLIST, s_list, allow_pickle=True) U_f = oracle.gen_matrix(mapping, n, n) return U_f, s
def unit_tests(n): if not os.path.exists('uf/bv'): os.makedirs('uf/bv') SAVEPATH = 'uf/bv/' FILEPATH = 'bv' + str(n) SPATH = 'a_dict' if not os.path.exists(SAVEPATH + FILEPATH + '.npy'): if os.path.exists(SAVEPATH + SPATH + '.npy'): a_list = np.load(SAVEPATH + SPATH + '.npy', allow_pickle=True).item() else: a_list = {} a = a_list[n] if n in a_list.keys() else np.random.randint( low=0, high=2, size=n) a_list[n] = a np.save(SAVEPATH + SPATH, a_list, allow_pickle=True) f_mapping = oracle.init_bit_mapping(n, algo=oracle.Algos.BV, func=(a, 0)) assert is_correct(f_mapping, a) U_f = oracle.gen_matrix(f_mapping, n, 1) else: U_f = np.load(SAVEPATH + FILEPATH + '.npy') assert is_unitary(U_f) if not os.path.exists(SAVEPATH + FILEPATH + '.npy'): np.save(SAVEPATH + FILEPATH, U_f)
def unit_tests(n): if not os.path.exists('uf'): os.mkdir('uf') if not os.path.exists('uf/simon'): os.mkdir('uf/simon') SAVEDIR = 'uf/simon/' FILEPATH = 'simon' + str(n) SPATH = 's_dict' if not os.path.exists(SAVEDIR + FILEPATH + '.npy'): mapping = oracle.init_bit_mapping(n, algo=oracle.Algos.SIMON) assert is_correct(mapping) U_f = oracle.gen_matrix(mapping, n, n) s = get_s(mapping) if os.path.exists(SAVEDIR + SPATH + '.npy'): s_list = np.load(SAVEDIR + SPATH + '.npy', allow_pickle=True).item() else: s_list = {} s_list[n] = s np.save(SAVEDIR + SPATH, s_list, allow_pickle=True) else: U_f = np.load(SAVEDIR + FILEPATH + '.npy') assert is_unitary(U_f) if not os.path.exists(SAVEDIR + FILEPATH + '.npy'): np.save(SAVEDIR + FILEPATH, U_f)
def getUf(n, reload): if not reload: path = f'uf/simon/simon{n}.npy' U_f = np.load(path) else: SAVEDIR = 'uf/simon/' SPATH = 's_dict.npy' f = oracle.init_bit_mapping(n, algo=oracle.Algos.SIMON) xa = list(f)[0] xb = xa fa = f[xa] for x, fx in f.items(): if fx == fa and x != xa: xb = x break s = f'{int(xa, 2) ^ int(xb, 2):0{len(fa)}b}' if os.path.exists(SAVEDIR + SPATH): s_list = np.load(SAVEDIR + SPATH, allow_pickle=True).item() else: s_list = {} s_list[n] = s np.save(SAVEDIR + SPATH, s_list, allow_pickle=True) U_f = oracle.gen_matrix(f, n, n) return U_f
def unit_tests(n): if not os.path.exists('uf'): os.mkdir('uf') if not os.path.exists('uf/grover'): os.mkdir('uf/grover') SAVEDIR = 'uf/grover/' FILEPATH = f'{SAVEDIR}grover{str(n)}.npy' FPATH = f'{SAVEDIR}f_dict.npy' if not os.path.exists(FILEPATH): mapping = oracle.init_bit_mapping(n, algo=oracle.Algos.GROVER) U_f = oracle.gen_matrix(mapping, n, 1) if os.path.exists(FPATH): f_dict = np.load(FPATH, allow_pickle=True).item() else: f_dict = {} f_dict[n] = mapping np.save(FPATH, f_dict, allow_pickle=True) else: U_f = np.load(FILEPATH) assert is_unitary(U_f) if not os.path.exists(FILEPATH): np.save(FILEPATH, U_f)
def unit_tests(n): if not os.path.exists('uf/bv'): os.makedirs('uf/bv') SAVEPATH = 'uf/bv/' FILEPATH = f'bv{n}' APATH = 'a_dict' BPATH = 'b_dict' if not os.path.exists(f'{SAVEPATH}{FILEPATH}.npy'): if os.path.exists(f'{SAVEPATH}{APATH}.npy'): a_list = np.load(f'{SAVEPATH}{APATH}.npy', allow_pickle=True).item() else: a_list = {} if os.path.exists(f'{SAVEPATH}{BPATH}.npy'): b_list = np.load(f'{SAVEPATH}{BPATH}.npy', allow_pickle=True).item() else: b_list = {} a = a_list[n] if n in a_list.keys() else np.random.randint( low=0, high=2, size=n) b = b_list[n] if n in b_list.keys() else np.random.randint(low=0, high=2) a_list[n] = a b_list[n] = b np.save(f'{SAVEPATH}{APATH}.npy', a_list, allow_pickle=True) np.save(f'{SAVEPATH}{BPATH}.npy', b_list, allow_pickle=True) print( f'Generating bit mapping for {n}-qubit BV with a={"".join(str(i) for i in a)} and b={b} .. ', end='', flush=True) mapping = oracle.init_bit_mapping(n, algo=oracle.Algos.BV, func=(a, b)) print('done') print(f'Generating U_f from bit mapping .. ', end='', flush=True) U_f = oracle.gen_matrix(mapping, n, 1) print('done') else: print( f'Loading U_f for {n}-qubit BV from {SAVEPATH}{FILEPATH}.npy .. ', end='', flush=True) U_f = Operator(np.load(f'{SAVEPATH}{FILEPATH}.npy')) print('done') assert U_f.is_unitary() if not os.path.exists(f'{SAVEPATH}{FILEPATH}.npy'): print(f'Saving U_f for {n}-qubit BV to {SAVEPATH}{FILEPATH}.npy .. ', end='', flush=True) np.save(f'{SAVEPATH}{FILEPATH}', U_f.data) print('done') print('')
def unit_test(n): if not os.path.exists('uf/grover'): os.makedirs('uf/grover') if not os.path.exists(f'{SAVEDIR}grover{str(n)}.npy'): print(f'Generating bit mapping for {n}-qubit Grover .. ', end='', flush=True) mapping = oracle.init_bit_mapping(n, algo=oracle.Algos.GROVER) print('done') if os.path.exists(f'{SAVEDIR}{FLIST}.npy'): print(f'Loading f_list for Grover from {SAVEDIR}{FLIST}.npy .. ', end='', flush=True) f_list = np.load(f'{SAVEDIR}{FLIST}.npy', allow_pickle=True).item() print('done') else: f_list = {} f_list[n] = mapping print(f'Saving f_list for Grover to {SAVEDIR}{FLIST}.npy .. ', end='', flush=True) np.save(f'{SAVEDIR}{FLIST}.npy', f_list, allow_pickle=True) print('done') print(f'Generating oracle matrix for {n}-qubit Grover .. ', end='', flush=True) U_f = oracle.gen_matrix(mapping, n, 1) print('done') else: print( f'Loading U_f for {n}-qubit Grover from {SAVEDIR}grover{str(n)}.npy .. ', end='', flush=True) U_f = Operator(np.load(f'{SAVEDIR}grover{str(n)}.npy')) print("done") print(f'Check if U_f is unitary .. ', end='', flush=True) assert U_f.is_unitary() print('done') if not os.path.exists(f'{SAVEDIR}grover{str(n)}.npy'): print( f'Saving U_f for {n}-qubit Grover from {SAVEDIR}grover{str(n)}.npy .. ', end='', flush=True) np.save(f'{SAVEDIR}grover{str(n)}.npy', U_f.data) print("done")
def unit_test(n, s=None): if not os.path.exists('uf/simon'): os.makedirs('uf/simon') if not os.path.exists(f'{SAVEDIR}simon{str(n)}.npy') or s is not None: print(f'Generating bit mapping for {n}-qubit Simon .. ', end='', flush=True) mapping = oracle.init_bit_mapping(n, algo=oracle.Algos.SIMON, s=s) print('done') print(f'Generating s value for newly generated {n}-bit mapping Simon .. ', end='', flush=True) s = get_s(mapping) print('done') print(f'Checking validity of s value .. ', end='', flush=True) assert is_valid(s, mapping) print('done') if os.path.exists(f'{SAVEDIR}{SLIST}.npy'): print(f'Loading s_list for Simon from {SAVEDIR}{SLIST}.npy .. ', end='', flush=True) s_list = np.load(f'{SAVEDIR}{SLIST}.npy', allow_pickle=True).item() print('done') else: s_list={} s_list[n] = s print(f'Saving s_list for Simon to {SAVEDIR}{SLIST}.npy .. ', end='', flush=True) np.save(f'{SAVEDIR}{SLIST}.npy', s_list, allow_pickle=True) print('done') print(f'Generating oracle matrix for {n}-qubit Simon .. ', end='', flush=True) U_f = oracle.gen_matrix(mapping, n, n) print('done') else: print(f'Loading U_f for {n}-qubit Simon from {SAVEDIR}simon{str(n)}.npy .. ', end='', flush=True) U_f = Operator(np.load(f'{SAVEDIR}simon{str(n)}.npy')) print("done") print(f'Check if U_f is unitary .. ', end='', flush=True) assert U_f.is_unitary() print('done') print(f'Saving U_f for {n}-qubit Simon from {SAVEDIR}simon{str(n)}.npy .. ', end='', flush=True) np.save(f'{SAVEDIR}simon{str(n)}.npy', U_f.data) print("done")
def unit_tests(n): if not os.path.exists('uf'): os.mkdir('uf') if not os.path.exists('uf/dj'): os.mkdir('uf/dj') SAVEPATH = 'uf/dj/' CONSTPATH = f'const{n}' BALPATH = f'bal{n}' if not os.path.exists(f'{SAVEPATH}{CONSTPATH}.npy'): print(f'Generating bit mapping for {n}-qubit constant DJ .. ', end='', flush=True) const_mapping = oracle.init_bit_mapping(n, algo=oracle.Algos.DJ, func=oracle.DJ.CONSTANT) print('done') assert is_bal_or_const(const_mapping, oracle.DJ.CONSTANT) print(f'Generating U_f from bit mapping .. ', end='', flush=True) U_const_f = oracle.gen_matrix(const_mapping, n, 1) print('done') else: print( f'Loading U_f for {n}-qubit constant DJ from {SAVEPATH}{CONSTPATH}.npy .. ', end='', flush=True) U_const_f = Operator(np.load(f'{SAVEPATH}{CONSTPATH}.npy')) print('done') if not os.path.exists(f'{SAVEPATH}{BALPATH}.npy'): print(f'Generating bit mapping for {n}-qubit balanced DJ .. ', end='', flush=True) balanced_mapping = oracle.init_bit_mapping(n, algo=oracle.Algos.DJ, func=oracle.DJ.BALANCED) print('done') assert is_bal_or_const(balanced_mapping, oracle.DJ.BALANCED) print(f'Generating U_f from bit mapping .. ', end='', flush=True) U_bal_f = oracle.gen_matrix(balanced_mapping, n, 1) print('done') else: print( f'Loading U_f for {n}-qubit balanced DJ from {SAVEPATH}{BALPATH}.npy .. ', end='', flush=True) U_bal_f = Operator(np.load(f'{SAVEPATH}{BALPATH}.npy')) print('done') assert U_const_f.is_unitary() if not os.path.exists(f'{SAVEPATH}{CONSTPATH}.npy'): print( f'Saving U_f for {n}-qubit constant DJ to {SAVEPATH}{CONSTPATH}.npy .. ', end='', flush=True) np.save(SAVEPATH + CONSTPATH, U_const_f.data) print('done') assert U_bal_f.is_unitary() if not os.path.exists(f'{SAVEPATH}{BALPATH}.npy'): print( f'Saving U_f for {n}-qubit balanced DJ to {SAVEPATH}{BALPATH}.npy .. ', end='', flush=True) np.save(SAVEPATH + BALPATH, U_bal_f.data) print('done') print('')