Example #1
0
    def test_qsvm_setup_data(self, data_preparation_type):
        """ QSVM Setup Data 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')

        data_preparation = self.data_preparation[data_preparation_type]
        try:
            if data_preparation_type == 'wrapped':
                warnings.filterwarnings('ignore', category=DeprecationWarning)
            svm = QSVM(data_preparation)
            if data_preparation_type == 'wrapped':
                warnings.filterwarnings('always', category=DeprecationWarning)

            svm.setup_training_data(self.training_data)
            svm.setup_test_data(self.testing_data)
            quantum_instance = QuantumInstance(backend, seed_transpiler=self.random_seed,
                                               seed_simulator=self.random_seed)
            result = svm.run(quantum_instance)

            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)
        except NameError as ex:
            self.skipTest(str(ex))
Example #2
0
    def test_qsvm_setup_data(self):
        """ QSVM Setup Data 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)

        svm.setup_training_data(self.training_data)
        svm.setup_test_data(self.testing_data)
        quantum_instance = QuantumInstance(backend, seed_transpiler=self.random_seed,
                                           seed_simulator=self.random_seed)
        result = svm.run(quantum_instance)

        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)
Example #3
0
    def test_setup_data(self):
        """Test the setup_*_data methods of QSVM."""
        data_preparation = self.data_preparation
        try:
            svm = QSVM(data_preparation)

            svm.setup_training_data(self.training_data)
            svm.setup_test_data(self.testing_data)
            result = svm.run(self.statevector_simulator)

            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)
        except MissingOptionalLibraryError as ex:
            self.skipTest(str(ex))