예제 #1
0
    def runTest(self):
        from sklearn.datasets import load_iris

        X, y = load_iris(True)
        X = numpy.asarray([x[0:2] for x, yy in zip(X, y) if yy != 2])
        y = numpy.asarray([yy for x, yy in zip(X, y) if yy != 2])

        preprocessing_pipeline = Pipeline([('scaler', StandardScaler()),
                                           ('l2norm',
                                            Normalizer(norm='l2', copy=True))])
        X = preprocessing_pipeline.fit_transform(X, y)
        X_train, X_test, y_train, y_test = train_test_split(X,
                                                            y,
                                                            test_size=0.10,
                                                            random_state=42)

        initial_state_builder = QmlGenericStateCircuitBuilder(
            MöttönenStatePreparation())

        execution_backend = qiskit.Aer.get_backend(
            'qasm_simulator')  # type: BaseBackend
        qml = QmlHadamardNeighborClassifier(
            backend=execution_backend,
            shots=8192,
            classifier_circuit_factory=initial_state_builder,
            feature_map=NormedAmplitudeEncoding())

        qml.fit(X_train, y_train)
        prediction = qml.predict(X_test)

        self.assertEqual(len(prediction), len(y_test))
        self.assertListEqual(prediction, list(y_test))

        for i in range(len(qml.last_predict_p_acc)):
            self.assertAlmostEqual(qml.last_predict_p_acc[i],
                                   QmlHadamardNeighborClassifier.p_acc_theory(
                                       X_train, y_train, X_test[i]),
                                   delta=0.05)

        for i in range(len(qml.last_predict_probability)):
            predicted_label = prediction[i]
            self.assertAlmostEqual(
                qml.last_predict_probability[i],
                QmlHadamardNeighborClassifier.p_label_theory(
                    X_train, y_train, X_test[i], predicted_label),
                delta=0.05)
예제 #2
0
    def runTest(self):
        log.info(
            "Testing 'QmlHadamardNeighborClassifier' with CNOT Preparation.")
        execution_backend = qiskit.Aer.get_backend(
            'qasm_simulator')  # type: BaseBackend

        X_train = numpy.asarray([[1.0, 1.0], [-1.0, 1.0], [-1.0, -1.0],
                                 [1.0, -1.0]])
        y_train = [0, 1, 0, 1]

        X_test = numpy.asarray([[0.2, 0.4], [0.4, -0.8]])
        y_test = [0, 1]

        class MyFeatureMap(FeatureMap):
            def map(self, input_vector: list) -> sparse.dok_matrix:
                result = sparse.dok_matrix((4, 1))
                index = 0
                if input_vector[0] > 0 and input_vector[1] > 0:
                    index = 0
                if input_vector[0] < 0 < input_vector[1]:
                    index = 1
                if input_vector[0] < 0 and input_vector[1] < 0:
                    index = 2
                if input_vector[0] > 0 > input_vector[1]:
                    index = 3
                result[index, 0] = 1.0
                return result

        feature_map = MyFeatureMap()

        initial_state_builder = QmlBinaryDataStateCircuitBuilder(CCXToffoli())

        qml = QmlHadamardNeighborClassifier(
            backend=execution_backend,
            shots=100 * 8192,
            feature_map=feature_map,
            classifier_circuit_factory=initial_state_builder)

        qml.fit(X_train, y_train)

        prediction = qml.predict(X_test)

        self.assertEqual(len(prediction), len(y_test))
        self.assertListEqual(prediction, y_test)
예제 #3
0
    def runTest(self):
        log.info(
            "Testing 'QmlHadamardNeighborClassifier' with Möttönen Preparation."
        )
        execution_backend = qiskit.Aer.get_backend(
            'qasm_simulator')  # type: BaseBackend

        classifier_state_factory = QmlGenericStateCircuitBuilder(
            MöttönenStatePreparation())

        qml = QmlHadamardNeighborClassifier(
            feature_map=NormedAmplitudeEncoding(),
            classifier_circuit_factory=classifier_state_factory,
            backend=execution_backend,
            shots=100 * 8192)

        from sklearn.datasets import load_wine
        from sklearn.decomposition import PCA
        from sklearn.preprocessing import StandardScaler, Normalizer
        from sklearn.pipeline import Pipeline

        X, y = load_wine(True)

        preprocessing_pipeline = Pipeline([('scaler', StandardScaler()),
                                           ('pca2', PCA(n_components=2)),
                                           ('l2norm',
                                            Normalizer(norm='l2', copy=True))])
        X = preprocessing_pipeline.fit_transform(X, y)

        X_train = X[[33, 88, 144]]
        y_train = y[[33, 88, 144]]

        X_test = X[[28, 140]]
        y_test = y[[28, 140]]

        qml.fit(X_train, y_train)
        prediction = qml.predict(X_test)

        self.assertEqual(len(prediction), len(y_test))
        self.assertListEqual(prediction, list(y_test))