Skip to content
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

Reuploading fix #401

Merged
merged 7 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions examples/reuploading_classifier/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def create_target(name):
* np.sqrt(2 / 3)], dtype=complex),
np.array([1 / np.sqrt(3), np.exp(-1j * 2 * np.pi / 3) * np.sqrt(2 / 3)], dtype=complex)]

else:
raise NotImplementedError('This dataset is not implemented')

return targets


Expand Down
20 changes: 13 additions & 7 deletions examples/reuploading_classifier/main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# /usr/bin/env python
import datasets as ds
import numpy as np
from qlassifier import single_qubit_classifier
import pickle
import argparse

#TODO: fix issue with .pkl

parser = argparse.ArgumentParser()
parser.add_argument("--dataset", default='tricrown',
parser.add_argument("--dataset", default='circle',
help="Name of the example", type=str)
parser.add_argument("--layers", default=10, help="Number of layers.", type=int)
parser.add_argument("--layers", default=2, help="Number of layers.", type=int)


def main(dataset, layers):
Expand All @@ -20,9 +20,14 @@ def main(dataset, layers):
layers (int): Number of layers to use in the classifier
"""
ql = single_qubit_classifier(dataset, layers) # Define classifier
with open('saved_parameters.pkl', 'rb') as f:
# Load previous results. Have we ever run these problem?
data = pickle.load(f)
try:
with open('saved_parameters.pkl', 'rb') as f:
# Load previous results. Have we ever run these problem?
data = pickle.load(f)
except:
data = {}
data[dataset] = {}
AdrianPerezSalinas marked this conversation as resolved.
Show resolved Hide resolved

try:
parameters = data[dataset][layers]
print('Problem solved before, obtaining parameters from file...')
Expand All @@ -31,6 +36,7 @@ def main(dataset, layers):
print('Problem never solved, finding optimal parameters...')
result, parameters = ql.minimize(
method='l-bfgs-b', options={'disp': True})

data[dataset][layers] = parameters
with open('saved_parameters.pkl', 'wb') as f:
pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
Expand Down
Binary file modified examples/reuploading_classifier/saved_parameters.pkl
Binary file not shown.