Ejemplo n.º 1
0
            variance = b.variance_pixels()
            order = np.argsort(variance)[::-1]
            is_order_calculated = True
        order_now = copy.copy(order.reshape(-1, )[:num_features])
    else:
        print('Generate random sequence of the measurements')

    print('Generate samples and calculate PSNR for the dataset')
    if var_name is 'which_class':
        b.select_samples([para['class']])
    if samples is 'all':
        samples = list(range(b.images.shape[1]))
    for n in range(samples.__len__()):
        if select_way is 'RandomMeasure':
            order_now = np.random.permutation(a.length)[:num_features]
        img = b.images[order_now, n]
        img_new = a.generate_features(img, pos=order_now, f_max=1, f_min=0,
                                      is_display=False, way=generate_way)
        psnr_av[t] += (skimage.measure.compare_psnr(b.images[:, n], img_new) / samples.__len__())
        mse_av[t] += (mean_squared_error(b.images[:, n], img_new) / samples.__len__())
        ssim[t] += (skimage.measure.compare_ssim(
            b.images[:, n], img_new, data_range=1) / samples.__len__())
    print('Average PSNR = ' + str(psnr_av[t]))
file_name_fixed = os.path.basename(__file__)[:-3] + para['dataset']
output_txt(psnr_av, filename='PSNR'+file_name_fixed)
output_txt(mse_av, filename='MSE'+file_name_fixed)
output_txt(ssim, filename='SSIM'+file_name_fixed)
print('All simulations completed')


Ejemplo n.º 2
0
para_dmps['num_theta'] = 1

a = MpsOpenBoundaryClass(length, d, chi, spin='half', way='svd', ini_way='r')
a.central_orthogonalization(0, normalize=True)

a.calculate_entanglement_spectrum()
a.calculate_entanglement_entropy()
ent0_mid = a.ent[round(a.length / 2)]
fid0 = a.fidelity_log_to_product_state()
print('Mid entanglement entropy and fid0 = %.12g, %.12g' % (ent0_mid, fid0))

fid, _, _, mpo, _ = deep_mps_qubit(a, para_dmps)

fid = fid[1:]
# plot(fid)
output_txt(fid.reshape(-1, ), pre_fix + 'fid')

fid_prod0 = np.zeros((para_dmps['num_layers'], ))
for n in range(para_dmps['num_layers']):
    mps_ini = open_mps_product_state_spin_up(a.length, a.mps[0].shape[1])[0]
    fid_prod0[n] = fidelities_to_original_state(a, mps_ini, mpo[:n + 1],
                                                para_dmps['chi_overlap'])[-1]
# plot(fid1)
output_txt(fid_prod0.reshape(-1, ), pre_fix + 'fid_prod0')

# fid_recover = np.zeros((para_dmps['num_layers'], ))
# for n in range(1, para_dmps['num_layers']+1):
#     mps = copy.deepcopy(a)
#     for nn in range(n):
#         mps_data = act_umpo_on_mps(mps.mps, mpo[nn])
#         mps.input_mps(mps_data, if_deepcopy=False)
Ejemplo n.º 3
0
from algorithms.TNmachineLearningAlgo import pca
from library.BasicFunctions import output_txt
import numpy as np
import skimage

# num_f = [1] + list(range(4, 104, 4))
num_f = [80]
psnr = np.zeros(num_f.__len__())
dataset = 'fashion_mnist'

classes = [3]
a = MachineLearningFeatureMap(2, dataset)
a.identify_dataset()
a.load_data()
a.select_samples(classes)

b = MachineLearningFeatureMap(2, dataset)
b.identify_dataset()
b.load_data(file_sample='t10k-images.idx3-ubyte',
            file_label='t10k-labels.idx1-ubyte')
b.select_samples(classes)
for t in range(num_f.__len__()):
    _, _, u = pca(a.images, num_f[t])
    imgs1 = (b.images.T.dot(u).dot(u.T)).T
    for n in range(imgs1.shape[1]):
        psnr[t] += (skimage.measure.compare_psnr(b.images[:, n], imgs1[:, n]) /
                    imgs1.shape[1])
    # a.input_data(images=imgs1, labels=np.arange(imgs1.shape[1]))
    # a.show_image(list(np.random.permutation(imgs1.shape[1])[:25]))
output_txt(psnr, filename='PSNR_pca' + dataset)
Ejemplo n.º 4
0
    else:
        ob, a, info, para_dmrg = dmrg_finite_size(para_dmrg)
        save_pr(para_dmrg['data_path'], para_dmrg['data_exp'] + '.pr', (ob, a, info, para_dmrg),
                ('ob', 'a', 'info', 'para'))
    print('Energy per site = ' + str(ob['e_per_site']))

    a.calculate_entanglement_spectrum()
    a.calculate_entanglement_entropy()
    ent0_mid[n] = a.ent[round(a.length/2)]
    fid_ini[n] = a.fidelity_log_to_product_state()
    # print('Mid entanglement entropy and fid0 = %.12g, %.12g' % (ent0_mid, fid_ini))

    save_path = path.join(para_dmrg['project_path'], 'data_dMPS/')
    save_exp = 'UMPO_layer' + str(para_dmps['num_layers']) + para_dmrg['data_exp'] + '.pr'
    if path.isfile(path.join(save_path, save_exp)):
        print('Load existing MPO data ...')
        mpo, fid_tmp = load_pr(path.join(save_path, save_exp), ['mpo', 'fid'])
    else:
        fid_tmp, _, _, mpo, _ = deep_mps_qubit(a, para_dmps)
        save_pr(save_path, save_exp, [mpo, fid_tmp], ['mpo', 'fid'])
    fid[n] = fid_tmp[-1]

    mps_ini = open_mps_product_state_spin_up(a.length, a.mps[0].shape[1])[0]
    fid_prod0[n] = fidelities_to_original_state(a, mps_ini, mpo, para_dmps['chi_overlap'])[-1]

output_txt(fid.reshape(-1, ), pre_fix + 'fid')
output_txt(fid_prod0.reshape(-1, ), pre_fix + 'fid_prod0')
output_txt(ent0_mid.reshape(-1, ), pre_fix + 'ent0_mid')
output_txt(fid_ini.reshape(-1, ), pre_fix + 'fid_ini')

Ejemplo n.º 5
0
para['hx'] = 0.5
para['hz'] = 0
para['l'] = 18  # number of physical sites in the bulk
para['tau'] = 1e-5
para['bound_cond'] = 'open'

n_var = var.__len__()
e_error = np.zeros((n_var, 1))
corr_xx = np.zeros((n_var, para['l_phys'] - 1))
corr_zz = np.zeros((n_var, para['l_phys'] - 1))

for n in range(n_var):
    exec('para[\'' + var_name + '\'] = ' + str(var[n]))
    para = make_para_consistent_ed(para)
    para['pos4corr'] = np.hstack((np.ones(
        (para['l'] - 1, 1), dtype=int), np.arange(2, para['l'] - 1,
                                                  dtype=int).reshape(-1, 1)))
    bath, solver, ob0, ob = qes_1d_ed(para)
    e_error[n] = abs(-0.318309886183529 - ob['e_site'])
    if n == 0:
        corr_xx = np.array(ob['corr_xx']).reshape(-1, 1)
        corr_zz = np.array(ob['corr_zz']).reshape(-1, 1)
    else:
        corr_xx = np.hstack((corr_xx, np.array(ob['corr_xx']).reshape(-1, 1)))
        corr_zz = np.hstack((corr_zz, np.array(ob['corr_zz']).reshape(-1, 1)))

output_txt(e_error0, 'e_error0')
output_txt(e_error, 'e_error')
output_txt(abs(corr_xx), 'corr_xx')
output_txt(abs(corr_zz), 'corr_zz')
Ejemplo n.º 6
0
else:
    ob, a, info, para = dmrg_finite_size(para_dmrg)
    save_pr(para['data_path'], para['data_exp'] + '.pr', (ob, a, info, para),
            ('ob', 'a', 'info', 'para'))
print('Energy per site = ' + str(ob['e_per_site']))
a.calculate_entanglement_spectrum()
a.calculate_entanglement_entropy()

fid, ent, lm_mid, mpo, mps = deep_mps_qubit(a, para_dmps)

lm = [a.lm[round(a.length / 2)].reshape(-1, 1)]
lm_mat = np.ones((lm_mid[-1].size, lm_mid.__len__() + 1))
lm_mat[:a.lm[round(a.length / 2)].size, 0] = a.lm[round(a.length / 2)]
for n in range(lm_mid.__len__()):
    lm_mat[:lm_mid[n].size, n + 1] = lm_mid[n]
output_txt(np.log10(lm_mat), 'lm')
gap = np.log(lm_mat[1, :]) - np.log(lm_mat[0, :])
output_txt(gap, 'gap')

ent_av = [np.average(a.ent)]
for n in range(ent.__len__()):
    ent_av.append(np.average(ent[n]))
output_txt(ent_av, 'ent_av')

pos_mid = round(a.length / 2)
ent_mid = [a.ent[pos_mid]]
for n in range(ent.__len__()):
    ent_mid.append(ent[n][pos_mid])
output_txt(ent_mid, 'ent_mid')

fid = fid[1:]
Ejemplo n.º 7
0
    print('theta = ' + str(para_dmps['theta']))
    fid_tmp, _, _, mpo, _ = deep_mps_qubit(a, para_dmps)
    print('Fidelity = ' + str(fid_tmp))
    fid[tt] = fid_tmp[-1]
    # mps_ini = open_mps_product_state_spin_up(a.length, a.mps[0].shape[1])[0]
    # fid_prod0[tt] = fidelities_to_original_state(a, mps_ini, mpo,
    #                                              para_dmps['chi_overlap'])[-1]
    # mps = copy.deepcopy(a)
    # for n in range(para_dmps['num_layers']):
    #     mps_data = act_umpo_on_mps(mps.mps, mpo[n])
    #     mps.input_mps(mps_data, if_deepcopy=False)
    #     mps.orthogonalize_mps(mps.length - 1, 0, normalize=True, is_trun=False)
    #     mps.center = 0
    #     mps.orthogonalize_mps(0, mps.length - 1, normalize=True, is_trun=True,
    #                           chi=para_dmps['chi_overlap'])
    #     mps.center = mps.length - 1
    # fid_recover[tt] = fidelities_to_original_state(a, mps.mps, mpo, para_dmps['chi_overlap'])[-1]

output_txt(fid.reshape(-1, ), 'fid')
# output_txt(fid_prod0.reshape(-1, ), 'fid_prod0')
# output_txt(fid_recover.reshape(-1, ), 'fid_recover')

# exp = 'plot(np.arange(ent[0].size),'
# for n in range(0, ent.__len__(), 2):
#     exp += 'ent[' + str(n) + '],'
# exp = exp[:-1] + ')'
# exec(exp)

# ent = np.hstack(ent)
# output_txt(ent, 'ent')
Ejemplo n.º 8
0
# calculate entanglement properties and fidelity
a.calculate_entanglement_spectrum()
a.calculate_entanglement_entropy()
ent0_mid = a.ent[round(a.length / 2)]
fid0 = a.fidelity_log_to_product_state()
print('Mid entanglement entropy and fid0 = %.12g, %.12g' % (ent0_mid, fid0))

# calculate the MPUs
save_path = path.join(para_dmrg['project_path'], 'data_dMPS/')
save_exp = 'UMPO_layer' + str(
    para_dmps['num_layers']) + para_dmrg['data_exp'] + '.pr'
if path.isfile(path.join(save_path, save_exp)):
    print('Load existing MPO data ...')
    mpo, fid = load_pr(path.join(save_path, save_exp), ['mpo', 'fid'])
else:
    fid, _, _, mpo, _ = deep_mps_qubit(a, para_dmps, para_dmrg)
    save_pr(save_path, save_exp, [mpo, fid], ['mpo', 'fid'])

fid = fid[1:]  # |<0|(|U_dag\psi>)|
output_txt(fid.reshape(-1, ), pre_fix + 'fid')  # save results as txt file

fid_prod0 = np.zeros((para_dmps['num_layers'], ))  # # |(<0|U)|GS>)|
for n in range(para_dmps['num_layers']):
    mps_ini = open_mps_product_state_spin_up(a.length, a.mps[0].shape[1])[0]
    fid_prod0[n] = fidelities_to_original_state(a, mps_ini, mpo[:n + 1],
                                                para_dmps['chi_overlap'])[-1]
# plot(fid1)
output_txt(fid_prod0.reshape(-1, ),
           pre_fix + 'fid_prod0')  # save results as txt file