def test_qgan_training_run_algo_numpy(self): """ qgan training run algo numpy test """ # Set number of qubits per data dimension as list of k qubit values[#q_0,...,#q_k-1] num_qubits = [2] # Batch size batch_size = 100 # Set number of training epochs num_epochs = 5 _qgan = QGAN( self._real_data, self._bounds, num_qubits, batch_size, num_epochs, discriminator=NumPyDiscriminator(n_features=len(num_qubits)), snapshot_dir=None) _qgan.seed = self.seed _qgan.set_generator() trained_statevector = _qgan.run( QuantumInstance(BasicAer.get_backend('statevector_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed)) trained_qasm = _qgan.run( QuantumInstance(BasicAer.get_backend('qasm_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed)) self.assertAlmostEqual(trained_qasm['rel_entr'], trained_statevector['rel_entr'], delta=0.1)
def test_qgan_training_run_algo_numpy_multivariate(self): """Test QGAN training using a NumPy discriminator, for multivariate distributions.""" # Set number of qubits per data dimension as list of k qubit values[#q_0,...,#q_k-1] num_qubits = [1, 2] # Batch size batch_size = 100 # Set number of training epochs num_epochs = 5 # Reshape data in a multi-variate fashion # (two independent identically distributed variables, # each represented by half of the generated samples) real_data = self._real_data.reshape((-1, 2)) bounds = [self._bounds, self._bounds] _qgan = QGAN(real_data, bounds, num_qubits, batch_size, num_epochs, discriminator=NumPyDiscriminator(n_features=len(num_qubits)), snapshot_dir=None) _qgan.seed = self.seed _qgan.set_generator() trained_statevector = _qgan.run( QuantumInstance(BasicAer.get_backend('statevector_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed)) trained_qasm = _qgan.run(QuantumInstance(BasicAer.get_backend('qasm_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed)) self.assertAlmostEqual(trained_qasm['rel_entr'], trained_statevector['rel_entr'], delta=0.1)
def test_qgan_save_model(self): """Test the QGAN functionality to store the current model.""" # Set number of qubits per data dimension as list of k qubit values[#q_0,...,#q_k-1] num_qubits = [2] # Batch size batch_size = 100 # Set number of training epochs num_epochs = 5 with tempfile.TemporaryDirectory() as tmpdirname: _qgan = QGAN(self._real_data, self._bounds, num_qubits, batch_size, num_epochs, discriminator=NumPyDiscriminator(n_features=len(num_qubits)), snapshot_dir=tmpdirname) _qgan.seed = self.seed _qgan.set_generator() trained_statevector = _qgan.run( QuantumInstance(BasicAer.get_backend('statevector_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed)) trained_qasm = _qgan.run(QuantumInstance(BasicAer.get_backend('qasm_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed)) self.assertAlmostEqual(trained_qasm['rel_entr'], trained_statevector['rel_entr'], delta=0.1)
def test_qgan_training_run_algo_torch(self): """Test QGAN training using a PyTorch discriminator.""" try: # Set number of qubits per data dimension as list of k qubit values[#q_0,...,#q_k-1] num_qubits = [2] # Batch size batch_size = 100 # Set number of training epochs num_epochs = 5 _qgan = QGAN(self._real_data, self._bounds, num_qubits, batch_size, num_epochs, discriminator=PyTorchDiscriminator(n_features=len(num_qubits)), snapshot_dir=None) _qgan.seed = self.seed _qgan.set_generator() trained_statevector = _qgan.run(QuantumInstance( BasicAer.get_backend('statevector_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed)) trained_qasm = _qgan.run(QuantumInstance(BasicAer.get_backend('qasm_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed)) self.assertAlmostEqual(trained_qasm['rel_entr'], trained_statevector['rel_entr'], delta=0.1) except MissingOptionalLibraryError: self.skipTest('pytorch not installed, skipping test')
print(type(reshaped_data)) #reshaped_norm_data = [t+1 for t in reshaped_data] num_qubits = [4] k = len(num_qubits) # Set number of training epochs # Note: The algorithm's runtime can be shortened by reducing the number of training epochs. num_epochs = 10 # Batch size # Initialize qGAN qgan = QGAN(reshaped_data, bounds=bounds, num_qubits=num_qubits, batch_size=1, num_epochs=num_epochs, snapshot_dir="data") print("QGAN set") qgan.seed = 1 # Set quantum instance to run the quantum generator quantum_instance = QuantumInstance( backend=BasicAer.get_backend('statevector_simulator')) print("quantum_instance set") # Set entangler map entangler_map = [[0, 1]] # Set an initial state for the generator circuit init_dist = UniformDistribution(sum(num_qubits), low=bounds[0], high=bounds[1]) q = QuantumRegister(sum(num_qubits), name='q') qc = QuantumCircuit(q) init_dist.build(qc, q) init_distribution = Custom(num_qubits=sum(num_qubits), circuit=qc)
def QGAN_method(kk, num_qubit, epochs, batch, bound, snap, data): start = time.time() real_data = data # In[41]: # Number training data samples N = 1000 # Load data samples from log-normal distribution with mean=1 and standard deviation=1 mu = 1 sigma = 1 # real_data = np.random.lognormal(mean = mu, sigma=sigma, size=N) # print(real_data) # Set the data resolution # Set upper and lower data values as list of k min/max data values [[min_0,max_0],...,[min_k-1,max_k-1]] bounds = np.array([0, bound]) # Set number of qubits per data dimension as list of k qubit values[#q_0,...,#q_k-1] num_qubits = [num_qubit] k = kk # In[52]: # Set number of training epochs # Note: The algorithm's runtime can be shortened by reducing the number of training epochs. num_epochs = epochs # Batch size batch_size = batch # Initialize qGAN qgan = QGAN(real_data, bounds, num_qubits, batch_size, num_epochs, snapshot_dir=snap) qgan.seed = 1 # Set quantum instance to run the quantum generator quantum_instance = QuantumInstance( backend=BasicAer.get_backend('statevector_simulator')) # Set entangler map entangler_map = [[0, 1]] # Set an initial state for the generator circuit init_dist = UniformDistribution(sum(num_qubits), low=bounds[0], high=bounds[1]) q = QuantumRegister(sum(num_qubits), name='q') qc = QuantumCircuit(q) init_dist.build(qc, q) init_distribution = Custom(num_qubits=sum(num_qubits), circuit=qc) var_form = RY(int(np.sum(num_qubits)), depth=k, initial_state=init_distribution, entangler_map=entangler_map, entanglement_gate='cz') # Set generator's initial parameters init_params = aqua_globals.random.rand( var_form._num_parameters) * 2 * np.pi # Set generator circuit g_circuit = UnivariateVariationalDistribution(int(sum(num_qubits)), var_form, init_params, low=bounds[0], high=bounds[1]) # Set quantum generator qgan.set_generator(generator_circuit=g_circuit) # Set classical discriminator neural network discriminator = NumPyDiscriminator(len(num_qubits)) qgan.set_discriminator(discriminator) # In[53]: # Run qGAN qgan.run(quantum_instance) # Runtime end = time.time() print('qGAN training runtime: ', (end - start) / 60., ' min') return qgan