Skip to content

Commit

Permalink
Merge pull request #401 from Quantum-TII/reuploading_fix
Browse files Browse the repository at this point in the history
Reuploading fix
  • Loading branch information
scarrazza committed Apr 30, 2021
2 parents 757a9a6 + b2ce3c7 commit 463ceeb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
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
15 changes: 10 additions & 5 deletions examples/reuploading_classifier/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# /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',
help="Name of the example", type=str)
Expand All @@ -20,9 +20,13 @@ 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 = {dataset: {}}

try:
parameters = data[dataset][layers]
print('Problem solved before, obtaining parameters from file...')
Expand All @@ -31,6 +35,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.

0 comments on commit 463ceeb

Please sign in to comment.