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)

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

        qc = initial_state_builder.build_circuit(
            "test", [feature_map.map(e) for e in X_train], y_train,
            feature_map.map(X_test[0]))
        statevector_backend = qiskit.Aer.get_backend(
            'statevector_simulator')  # type: BaseBackend
        job = qiskit.execute(qc, statevector_backend, shots=1)  # type: BaseJob
        simulator_state_vector = job.result().get_statevector()
        input_state_vector = initial_state_builder.get_last_state_vector()

        for i, s in zip(input_state_vector.toarray(), simulator_state_vector):
            log.debug("{:.4f} == {:.4f}".format(i[0], s))
            self.assertAlmostEqual(i[0], s, places=4)
Esempio n. 2
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)
        y_predict, y_test = predict(qml)

        predictions_match = [p == l for p, l in zip(y_predict, y_test)]
        self.assertTrue(all(predictions_match))

        self.assertEqual(len(qml.last_predict_probability), 2)

        input_1_probability = qml.last_predict_probability[0]
        input_2_probability = qml.last_predict_probability[1]

        self.assertAlmostEqual(input_1_probability, 0.629, delta=0.02)
        self.assertAlmostEqual(input_2_probability, 0.547, delta=0.02)
Esempio n. 3
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)
Esempio n. 4
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))