def test_qsvm_kernel_binary_directly(self): ref_kernel_training = np.array( [[1., 0.85366667, 0.12341667, 0.36408333], [0.85366667, 1., 0.11141667, 0.45491667], [0.12341667, 0.11141667, 1., 0.667], [0.36408333, 0.45491667, 0.667, 1.]]) ref_kernel_testing = np.array( [[0.14316667, 0.18208333, 0.4785, 0.14441667], [0.33608333, 0.3765, 0.02316667, 0.15858333]]) ref_alpha = np.array([0.36064489, 1.49204209, 0.0264953, 1.82619169]) ref_bias = np.array([-0.03380763]) ref_support_vectors = np.array([[2.95309709, 2.51327412], [3.14159265, 4.08407045], [4.08407045, 2.26194671], [4.46106157, 2.38761042]]) backend = get_aer_backend('qasm_simulator') num_qubits = 2 feature_map = SecondOrderExpansion(num_qubits=num_qubits, depth=2, entangler_map={0: [1]}) svm = QSVMKernel(feature_map, self.training_data, self.testing_data, None) svm.random_seed = self.random_seed run_config = RunConfig(shots=self.shots, max_credits=10, memory=False, seed=self.random_seed) quantum_instance = QuantumInstance(backend, run_config, seed_mapper=self.random_seed) result = svm.run(quantum_instance) np.testing.assert_array_almost_equal(result['kernel_matrix_training'], ref_kernel_training, decimal=1) np.testing.assert_array_almost_equal(result['kernel_matrix_testing'], ref_kernel_testing, decimal=1) self.assertEqual(len(result['svm']['support_vectors']), 4) np.testing.assert_array_almost_equal(result['svm']['support_vectors'], ref_support_vectors, decimal=4) np.testing.assert_array_almost_equal(result['svm']['alphas'], ref_alpha, decimal=4) np.testing.assert_array_almost_equal(result['svm']['bias'], ref_bias, decimal=4) self.assertEqual(result['testing_accuracy'], 0.5)
def test_qsvm_kernel_binary_directly_statevector(self): ref_kernel_testing = np.array( [[0.1443953, 0.18170069, 0.47479649, 0.14691763], [0.33041779, 0.37663733, 0.02115561, 0.16106199]]) ref_support_vectors = np.array([[2.95309709, 2.51327412], [3.14159265, 4.08407045], [4.08407045, 2.26194671], [4.46106157, 2.38761042]]) backend = get_aer_backend('statevector_simulator') num_qubits = 2 feature_map = SecondOrderExpansion(num_qubits=num_qubits, depth=2, entangler_map={0: [1]}) svm = QSVMKernel(feature_map, self.training_data, self.testing_data, None) svm.random_seed = self.random_seed quantum_instance = QuantumInstance(backend, seed_mapper=self.random_seed) result = svm.run(quantum_instance) ori_alphas = result['svm']['alphas'] np.testing.assert_array_almost_equal(result['kernel_matrix_testing'], ref_kernel_testing, decimal=4) self.assertEqual(len(result['svm']['support_vectors']), 4) np.testing.assert_array_almost_equal(result['svm']['support_vectors'], ref_support_vectors, decimal=4) self.assertEqual(result['testing_accuracy'], 0.5) file_path = self._get_resource_path('qsvm_kernel_test.npz') svm.save_model(file_path) self.assertTrue(os.path.exists(file_path)) loaded_svm = QSVMKernel(feature_map, self.training_data, None, None) loaded_svm.load_model(file_path) np.testing.assert_array_almost_equal( loaded_svm.ret['svm']['support_vectors'], ref_support_vectors, decimal=4) np.testing.assert_array_almost_equal(loaded_svm.ret['svm']['alphas'], ori_alphas, decimal=4) loaded_test_acc = loaded_svm.test(svm.test_dataset[0], svm.test_dataset[1], quantum_instance) self.assertEqual(result['testing_accuracy'], loaded_test_acc) np.testing.assert_array_almost_equal( loaded_svm.ret['kernel_matrix_testing'], ref_kernel_testing, decimal=4) if os.path.exists(file_path): try: os.remove(file_path) except: pass