Skip to content

Use tracker context in kernel models demo #992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions demonstrations/tutorial_kernel_based_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,13 @@ def kernel_matrix(A, B):
# Training the SVM optimizes internal parameters that basically
# weigh kernel functions.
# It is a breeze in scikit-learn, which is designed
# as a high-level machine learning library:
# as a high-level machine learning library (we'll put this in a
# ``qml.Tracker`` context so that we can
# track device executions):
#

svm = SVC(kernel=kernel_matrix).fit(X_train, y_train)
with qml.Tracker(dev_kernel) as tracker:
svm = SVC(kernel=kernel_matrix).fit(X_train, y_train)


######################################################################
Expand All @@ -307,7 +310,7 @@ def kernel_matrix(A, B):
# How many times was the quantum device evaluated?
#

dev_kernel.num_executions
tracker.totals['executions']


######################################################################
Expand Down Expand Up @@ -485,7 +488,9 @@ def quantum_model_predict(X_pred, trained_params, trained_bias):
n_layers = 2
batch_size = 20
steps = 100
trained_params, trained_bias, loss_history = quantum_model_train(n_layers, steps, batch_size)

with qml.Tracker(dev_var) as tracker:
trained_params, trained_bias, loss_history = quantum_model_train(n_layers, steps, batch_size)

pred_test = quantum_model_predict(X_test, trained_params, trained_bias)
print("accuracy on test set:", accuracy_score(pred_test, y_test))
Expand All @@ -506,7 +511,7 @@ def quantum_model_predict(X_pred, trained_params, trained_bias):
# How often was the device executed?
#

dev_var.num_executions
tracker.totals['executions']


######################################################################
Expand Down Expand Up @@ -688,4 +693,4 @@ def model_evals_nn(n_data, n_params, n_steps, split, batch_size):
##############################################################################
# About the author
# ----------------
# .. include:: ../_static/authors/maria_schuld.txt
# .. include:: ../_static/authors/maria_schuld.txt
Loading