Beispiel #1
0
    def test_qsvm_binary_directly_statevector(self):
        """ QSVM Binary Directly Statevector test """
        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 = BasicAer.get_backend('statevector_simulator')
        num_qubits = 2
        feature_map = SecondOrderExpansion(feature_dimension=num_qubits,
                                           depth=2,
                                           entangler_map=[[0, 1]])
        svm = QSVM(feature_map, self.training_data, self.testing_data, None)

        quantum_instance = QuantumInstance(backend, seed_transpiler=self.random_seed,
                                           seed_simulator=self.random_seed)
        file_path = self.get_resource_path('qsvm_test.npz')
        try:
            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)

            svm.save_model(file_path)

            self.assertTrue(os.path.exists(file_path))

            loaded_svm = QSVM(feature_map)
            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)
        except NameError as ex:
            self.skipTest(str(ex))
        finally:
            if os.path.exists(file_path):
                try:
                    os.remove(file_path)
                except Exception:  # pylint: disable=broad-except
                    pass
Beispiel #2
0
    def test_binary_directly_statevector(self):
        """Test QSVM on binary classification on BasicAer's statevector simulator.

        Also tests saving and loading models."""
        data_preparation = self.data_preparation
        svm = QSVM(data_preparation, self.training_data, self.testing_data,
                   None)

        file_path = self.get_resource_path('qsvm_test.npz')
        try:
            result = svm.run(self.statevector_simulator)

            ori_alphas = result['svm']['alphas']

            np.testing.assert_array_almost_equal(
                result['kernel_matrix_testing'],
                self.ref_kernel_testing['statevector'],
                decimal=4)

            self.assertEqual(len(result['svm']['support_vectors']), 4)
            np.testing.assert_array_almost_equal(
                result['svm']['support_vectors'],
                self.ref_support_vectors,
                decimal=4)

            self.assertEqual(result['testing_accuracy'], 0.5)

            svm.save_model(file_path)

            self.assertTrue(os.path.exists(file_path))

            loaded_svm = QSVM(data_preparation)
            loaded_svm.load_model(file_path)

            np.testing.assert_array_almost_equal(
                loaded_svm.ret['svm']['support_vectors'],
                self.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],
                                              self.statevector_simulator)
            self.assertEqual(result['testing_accuracy'], loaded_test_acc)

            np.testing.assert_array_almost_equal(
                loaded_svm.ret['kernel_matrix_testing'],
                self.ref_kernel_testing['statevector'],
                decimal=4)
        except MissingOptionalLibraryError as ex:
            self.skipTest(str(ex))
        finally:
            if os.path.exists(file_path):
                try:
                    os.remove(file_path)
                except Exception:  # pylint: disable=broad-except
                    pass