Exemplo n.º 1
0
               'task': 'supervised',
               'loss': myloss,
               'optimizer': 'SGD',
               'init_learning_rate': 0.5,
               'decay': 0.01,
               'log_every': 10,
               'warm_start': False #Set this to True after first run
               }

# Create the learner
learner = CircuitLearner(hyperparams=hyperparams)

# Train the learner
learner.train_circuit(X=X_train, Y=Y_train, steps=steps, batch_size=batch_size)

# Evaluate the score of a test set
test_score = learner.score_circuit(X=X_test, Y=Y_test,
                                   outputs_to_predictions=outputs_to_predictions)
# The score_circuit() function returns a dictionary of different metrics. We select the accuracy and loss.
print("\nAccuracy on test set: {}".format(test_score['accuracy']))
print("Loss on test set: {}".format(test_score['loss']))

# Predict the labels of the new inputs
predictions = learner.run_circuit(X=X_pred,
                                  outputs_to_predictions=outputs_to_predictions)
print("\nPredictions for new inputs: ", predictions['outputs'])




Exemplo n.º 2
0
X_train = np.array([[0, 1], [0, 2], [0, 3], [0, 4]])

# Set the hyperparameters of the model and the training algorithm
hyperparams = {
    'circuit': circuit,
    'init_circuit_params': my_params,
    'task': 'unsupervised',
    'optimizer': 'Nelder-Mead',
    'loss': myloss,
    'regularizer': myregularizer,
    'regularization_strength': 0.1,
    'print_log': True,
    'log_every': 100
}

# Create the learner
learner = CircuitLearner(hyperparams=hyperparams)

# Train the learner
learner.train_circuit(X=X_train, steps=500)

# Get the final distribution, which is the circuit output
outcomes = learner.run_circuit()
final_distribution = outcomes['outputs']

# Use a helper function to sample fock states from this state.
# They should show a similar distribution to the training data
for i in range(10):
    sample = sample_from_distribution(distribution=final_distribution)
    print("Fock state sample {}:{}".format(i, sample))