Example #1
0
    # prepare data
    num_samples = 500
    X, y = datasets.make_moons(n_samples=num_samples,
                               noise=0.05,
                               random_state=0)
    # shift label from {0, 1} to {-1, 1}
    y = y * 2 - np.ones(len(y))
    x_train, x_val, y_train, y_test = train_test_split(X,
                                                       y,
                                                       test_size=0.25,
                                                       random_state=40)

    # build dataset
    dataset = qnn.get_dataset_from_numpy(x_train,
                                         y_train,
                                         val_features=x_val,
                                         val_labels=y_test)

    # customize preprocessing
    preprocessing_params = {"polynomial_degree": 2}

    # customize model
    model_params = {
        "architecture": "qubit",
        "num_q": 5,
        "encoding": "angle",
        "use_early_stopper": True,
        "early_stopper_patience": 20,
        "max_iterations": 10000,
        "interface": "autograd",
        "layer_type": "template",
Example #2
0
    # prepare data
    train_features = np.linspace(-1. * np.pi, 1. * np.pi, 50)
    train_labels = np.asarray((1 + np.sin(train_features)) / 2)
    train_features = train_features / np.pi
    val_features = np.linspace(-1. * np.pi, 1. * np.pi, 50)
    val_labels = np.asarray((1 + np.sin(val_features)) / 2)
    val_features = val_features / np.pi

    train_features = train_features.reshape((len(train_features), 1))
    val_features = val_features.reshape((len(val_features), 1))

    plt.plot(train_features, train_labels)
    plt.savefig("sinusoid.png")

    dataset = qnn.get_dataset_from_numpy(train_features,
                                         train_labels,
                                         val_features=val_features,
                                         val_labels=val_labels)

    # customize postprocessing
    model_params = {
        "architecture": "cv",
        "num_q": 2,
        "encoding": "displacement",
        "snapshot_frequency": 5,
        "prefix": "sinusoid",
        "early_stopper_patience": 50,
        "use_early_stopper": True,
        "num_layers": 5,
        "interface": "autograd",
        "layer_type": "template",
    }
from sklearn import datasets
from sklearn.model_selection import train_test_split

import prevision_quantum_nn as qnn

if __name__ == "__main__":

    # prepare data
    X, y = datasets.make_moons(n_samples=200, noise=0.05, random_state=0)
    x_train, x_val, y_train, y_test = train_test_split(X,
                                                       y,
                                                       test_size=0.25,
                                                       random_state=42)

    dataset = qnn.get_dataset_from_numpy(x_train,
                                         y_train,
                                         x_val=x_val,
                                         y_val=y_test)
    application = qnn.get_application("classification")
    application.solve(dataset)