def positive_wavefunction_data(gpu, num_hidden): with open( os.path.join(__tests_location__, "data", "test_grad_data.pkl"), "rb" ) as f: test_data = pickle.load(f) qucumber.set_random_seed(SEED, cpu=True, gpu=gpu, quiet=True) data = torch.tensor(test_data["tfim1d"]["train_samples"], dtype=torch.double) target_psi = torch.tensor(test_data["tfim1d"]["target_psi"], dtype=torch.double) num_visible = data.shape[-1] nn_state = PositiveWaveFunction(num_visible, num_hidden, gpu=gpu) PGU = PosGradsUtils(nn_state) data = data.to(device=nn_state.device) vis = nn_state.generate_hilbert_space(num_visible) target_psi = target_psi.to(device=nn_state.device) PositiveWaveFunctionFixture = namedtuple( "PositiveWaveFunctionFixture", ["data", "target_psi", "grad_utils", "nn_state", "vis"], ) return PositiveWaveFunctionFixture( data=data, target_psi=target_psi, grad_utils=PGU, nn_state=nn_state, vis=vis )
nh = nv * 10 nn_state = PositiveWaveFunction(num_visible=nv, num_hidden=nh, gpu=False) def psi_coefficient(nn_state, space, A, **kwargs): norm = nn_state.compute_normalization(space).sqrt_() return A * nn_state.psi(space)[0][0] / norm pbs = 100 nbs = pbs epochs = 500 lr = 0.005 k = 10 period = 50 space = nn_state.generate_hilbert_space() callbacks = [ MetricEvaluator( period, { "Fidelity": ts.fidelity, "KL": ts.KL, "A_Ψrbm_0": psi_coefficient }, target=true_psi, verbose=True, space=space, A=1.0, ) ]
def test_trainingpositive(): print("Positive WaveFunction") print("---------------------") train_samples_path = os.path.join( __tests_location__, "..", "examples", "Tutorial1_TrainPosRealWaveFunction", "tfim1d_data.txt", ) psi_path = os.path.join( __tests_location__, "..", "examples", "Tutorial1_TrainPosRealWaveFunction", "tfim1d_psi.txt", ) train_samples, target_psi = data.load_data(train_samples_path, psi_path) nv = nh = train_samples.shape[-1] fidelities = [] KLs = [] epochs = 5 batch_size = 100 num_chains = 200 CD = 10 lr = 0.1 log_every = 5 print("Training 10 times and checking fidelity and KL at 5 epochs...\n") for i in range(10): print("Iteration: ", i + 1) nn_state = PositiveWaveFunction(num_visible=nv, num_hidden=nh, gpu=False) space = nn_state.generate_hilbert_space(nv) callbacks = [ MetricEvaluator( log_every, { "Fidelity": ts.fidelity, "KL": ts.KL }, target_psi=target_psi, space=space, verbose=True, ) ] initialize_posreal_params(nn_state) nn_state.fit( data=train_samples, epochs=epochs, pos_batch_size=batch_size, neg_batch_size=num_chains, k=CD, lr=lr, time=True, progbar=False, callbacks=callbacks, ) fidelities.append(ts.fidelity(nn_state, target_psi, space)) KLs.append(ts.KL(nn_state, target_psi, space)) print("\nStatistics") print("----------") print( "Fidelity: ", np.average(fidelities), "+/-", np.std(fidelities) / np.sqrt(len(fidelities)), "\n", ) print("KL: ", np.average(KLs), "+/-", np.std(KLs) / np.sqrt(len(KLs)), "\n") assert abs(np.average(fidelities) - 0.85) < 0.02 assert abs(np.average(KLs) - 0.29) < 0.05 assert (np.std(fidelities) / np.sqrt(len(fidelities))) < 0.01 assert (np.std(KLs) / np.sqrt(len(KLs))) < 0.01
def quantum_state_training_data(request): nn_state_type = request.param if nn_state_type == PositiveWaveFunction: root = os.path.join( request.fspath.dirname, "..", "examples", "Tutorial1_TrainPosRealWaveFunction", ) train_samples, target = data.load_data( tr_samples_path=os.path.join(root, "tfim1d_data.txt"), tr_psi_path=os.path.join(root, "tfim1d_psi.txt"), ) train_bases, bases = None, None nn_state = PositiveWaveFunction(num_visible=train_samples.shape[-1], gpu=False) batch_size, num_chains = 100, 200 fid_target, kl_target = 0.85, 0.29 reinit_params_fn = initialize_posreal_params elif nn_state_type == ComplexWaveFunction: root = os.path.join( request.fspath.dirname, "..", "examples", "Tutorial2_TrainComplexWaveFunction", ) train_samples, target, train_bases, bases = data.load_data( tr_samples_path=os.path.join(root, "qubits_train.txt"), tr_psi_path=os.path.join(root, "qubits_psi.txt"), tr_bases_path=os.path.join(root, "qubits_train_bases.txt"), bases_path=os.path.join(root, "qubits_bases.txt"), ) nn_state = ComplexWaveFunction(num_visible=train_samples.shape[-1], gpu=False) batch_size, num_chains = 50, 10 fid_target, kl_target = 0.38, 0.33 reinit_params_fn = initialize_complex_params elif nn_state_type == DensityMatrix: root = os.path.join(request.fspath.dirname, "..", "examples", "Tutorial3_TrainDensityMatrix") train_samples, target, train_bases, bases = data.load_data_DM( tr_samples_path=os.path.join(root, "N2_W_state_100_samples_data.txt"), tr_mtx_real_path=os.path.join(root, "N2_W_state_target_real.txt"), tr_mtx_imag_path=os.path.join(root, "N2_W_state_target_imag.txt"), tr_bases_path=os.path.join(root, "N2_W_state_100_samples_bases.txt"), bases_path=os.path.join(root, "N2_IC_bases.txt"), ) nn_state = DensityMatrix(num_visible=train_samples.shape[-1], gpu=False) batch_size, num_chains = 100, 10 fid_target, kl_target = 0.45, 0.42 def reinit_params_fn(request, nn_state): nn_state.reinitialize_parameters() else: raise ValueError( f"invalid test config: {nn_state_type} is not a valid quantum state type" ) return { "nn_state": nn_state, "data": train_samples, "input_bases": train_bases, "target": target, "bases": bases, "epochs": 5, "pos_batch_size": batch_size, "neg_batch_size": num_chains, "k": 10, "lr": 0.1, "space": nn_state.generate_hilbert_space(), "fid_target": fid_target, "kl_target": kl_target, "reinit_params_fn": reinit_params_fn, }