def test_save_and_load_model(self): """Test saving and loading a model with the VQC.""" data_preparation = self.data_preparation wavefunction = self.ryrz_wavefunction vqc = VQC(self.spsa, data_preparation, wavefunction, self.training_data, self.testing_data) result = vqc.run(self.qasm_simulator) with self.subTest( msg='check optimal params, training loss and testing accuracy' ): np.testing.assert_array_almost_equal(result['opt_params'], self.ref_opt_params, decimal=4) np.testing.assert_array_almost_equal(result['training_loss'], self.ref_train_loss, decimal=8) self.assertEqual(0.5, result['testing_accuracy']) file_path = self.get_resource_path('vqc_test.npz') vqc.save_model(file_path) with self.subTest(msg='assert saved file exists'): self.assertTrue(os.path.exists(file_path)) loaded_vqc = VQC(self.spsa, data_preparation, wavefunction, self.training_data, None) loaded_vqc.load_model(file_path) loaded_test_acc = loaded_vqc.test(vqc.test_dataset[0], vqc.test_dataset[1], self.qasm_simulator) with self.subTest( msg= 'check optimal parameters and testing accuracy of loaded model' ): np.testing.assert_array_almost_equal(loaded_vqc.ret['opt_params'], self.ref_opt_params, decimal=4) self.assertEqual(result['testing_accuracy'], loaded_test_acc) predicted_probs, predicted_labels = loaded_vqc.predict( self.testing_data['A'], self.qasm_simulator) with self.subTest(msg='check probs and labels of predicted labels'): np.testing.assert_array_almost_equal(predicted_probs, self.ref_prediction_a_probs, decimal=8) np.testing.assert_array_equal(predicted_labels, self.ref_prediction_a_label) if os.path.exists(file_path): try: os.remove(file_path) except Exception: # pylint: disable=broad-except pass
def test_save_and_load_model(self): """ save and load model test """ np.random.seed(self.random_seed) aqua_globals.random_seed = self.random_seed backend = BasicAer.get_backend('qasm_simulator') num_qubits = 2 optimizer = SPSA(max_trials=10, save_steps=1, c0=4.0, skip_calibration=True) feature_map = SecondOrderExpansion(feature_dimension=num_qubits, depth=2) var_form = RYRZ(num_qubits=num_qubits, depth=3) vqc = VQC(optimizer, feature_map, var_form, self.training_data, self.testing_data) quantum_instance = QuantumInstance(backend, shots=1024, seed_simulator=self.random_seed, seed_transpiler=self.random_seed) result = vqc.run(quantum_instance) np.testing.assert_array_almost_equal(result['opt_params'], self.ref_opt_params, decimal=4) np.testing.assert_array_almost_equal(result['training_loss'], self.ref_train_loss, decimal=8) self.assertEqual(1.0, result['testing_accuracy']) file_path = self._get_resource_path('vqc_test.npz') vqc.save_model(file_path) self.assertTrue(os.path.exists(file_path)) loaded_vqc = VQC(optimizer, feature_map, var_form, self.training_data, None) loaded_vqc.load_model(file_path) np.testing.assert_array_almost_equal( loaded_vqc.ret['opt_params'], self.ref_opt_params, decimal=4) loaded_test_acc = loaded_vqc.test(vqc.test_dataset[0], vqc.test_dataset[1], quantum_instance) self.assertEqual(result['testing_accuracy'], loaded_test_acc) predicted_probs, predicted_labels = loaded_vqc.predict(self.testing_data['A'], quantum_instance) np.testing.assert_array_almost_equal(predicted_probs, self.ref_prediction_a_probs, decimal=8) np.testing.assert_array_equal(predicted_labels, self.ref_prediction_a_label) if quantum_instance.has_circuit_caching: self.assertLess(quantum_instance._circuit_cache.misses, 3) if os.path.exists(file_path): try: os.remove(file_path) except Exception: # pylint: disable=broad-except pass
def test_save_and_load_model(self, mode): """ save and load model test """ aqua_globals.random_seed = self.seed backend = BasicAer.get_backend('qasm_simulator') optimizer = SPSA(max_trials=10, save_steps=1, c0=4.0, skip_calibration=True) data_preparation = self.data_preparation[mode] wavefunction = self.ryrz_wavefunction[mode] if mode == 'wrapped': warnings.filterwarnings('ignore', category=DeprecationWarning) # set up algorithm vqc = VQC(optimizer, data_preparation, wavefunction, self.training_data, self.testing_data) quantum_instance = QuantumInstance(backend, shots=1024, seed_simulator=self.seed, seed_transpiler=self.seed) result = vqc.run(quantum_instance) np.testing.assert_array_almost_equal(result['opt_params'], self.ref_opt_params[mode], decimal=4) np.testing.assert_array_almost_equal(result['training_loss'], self.ref_train_loss[mode], decimal=8) self.assertEqual(1.0 if mode == 'wrapped' else 0.5, result['testing_accuracy']) file_path = self.get_resource_path('vqc_test.npz') vqc.save_model(file_path) self.assertTrue(os.path.exists(file_path)) loaded_vqc = VQC(optimizer, data_preparation, wavefunction, self.training_data, None) if mode == 'wrapped': warnings.filterwarnings('always', category=DeprecationWarning) loaded_vqc.load_model(file_path) np.testing.assert_array_almost_equal(loaded_vqc.ret['opt_params'], self.ref_opt_params[mode], decimal=4) loaded_test_acc = loaded_vqc.test(vqc.test_dataset[0], vqc.test_dataset[1], quantum_instance) self.assertEqual(result['testing_accuracy'], loaded_test_acc) predicted_probs, predicted_labels = loaded_vqc.predict( self.testing_data['A'], quantum_instance) np.testing.assert_array_almost_equal(predicted_probs, self.ref_prediction_a_probs[mode], decimal=8) np.testing.assert_array_equal(predicted_labels, self.ref_prediction_a_label[mode]) if os.path.exists(file_path): try: os.remove(file_path) except Exception: # pylint: disable=broad-except pass
def test_save_and_load_model(self, use_circuits): """ save and load model test """ aqua_globals.random_seed = self.seed backend = BasicAer.get_backend('qasm_simulator') num_qubits = 2 optimizer = SPSA(max_trials=10, save_steps=1, c0=4.0, skip_calibration=True) feature_map = SecondOrderExpansion(feature_dimension=num_qubits, depth=2) var_form = RYRZ(num_qubits=num_qubits, depth=3) # convert to circuit if circuits should be used if use_circuits: x = ParameterVector('x', feature_map.feature_dimension) feature_map = feature_map.construct_circuit(x) theta = ParameterVector('theta', var_form.num_parameters) var_form = var_form.construct_circuit(theta) # set up algorithm vqc = VQC(optimizer, feature_map, var_form, self.training_data, self.testing_data) # sort parameters for reproducibility if use_circuits: vqc._feature_map_params = list(x) vqc._var_form_params = list(theta) quantum_instance = QuantumInstance(backend, shots=1024, seed_simulator=self.seed, seed_transpiler=self.seed) result = vqc.run(quantum_instance) np.testing.assert_array_almost_equal(result['opt_params'], self.ref_opt_params, decimal=4) np.testing.assert_array_almost_equal(result['training_loss'], self.ref_train_loss, decimal=8) self.assertEqual(1.0, result['testing_accuracy']) file_path = self.get_resource_path('vqc_test.npz') vqc.save_model(file_path) self.assertTrue(os.path.exists(file_path)) loaded_vqc = VQC(optimizer, feature_map, var_form, self.training_data, None) # sort parameters for reproducibility if use_circuits: loaded_vqc._feature_map_params = list(x) loaded_vqc._var_form_params = list(theta) loaded_vqc.load_model(file_path) np.testing.assert_array_almost_equal(loaded_vqc.ret['opt_params'], self.ref_opt_params, decimal=4) loaded_test_acc = loaded_vqc.test(vqc.test_dataset[0], vqc.test_dataset[1], quantum_instance) self.assertEqual(result['testing_accuracy'], loaded_test_acc) predicted_probs, predicted_labels = loaded_vqc.predict( self.testing_data['A'], quantum_instance) np.testing.assert_array_almost_equal(predicted_probs, self.ref_prediction_a_probs, decimal=8) np.testing.assert_array_equal(predicted_labels, self.ref_prediction_a_label) if os.path.exists(file_path): try: os.remove(file_path) except Exception: # pylint: disable=broad-except pass