-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from chensgit169/master
user guide doc is to be updated
- Loading branch information
Showing
7 changed files
with
100 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
QUAFU_URL = "https://quafu.baqis.ac.cn/" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,17 @@ | ||
from ..exceptions import QuafuError | ||
|
||
|
||
class UserError(QuafuError): | ||
pass | ||
|
||
|
||
class APITokenNotFound(UserError): | ||
pass | ||
|
||
|
||
class InvalidAPIToken(UserError): | ||
pass | ||
|
||
|
||
class BackendNotAvailable(UserError): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,98 @@ | ||
from ..utils.platform import get_homedir | ||
import json | ||
import os | ||
|
||
import requests | ||
import json | ||
from urllib import parse | ||
|
||
from .exceptions import UserError | ||
from ..utils.platform import get_homedir | ||
|
||
|
||
class User(object): | ||
def __init__(self): | ||
self.apitoken = "" | ||
self._url = "http://quafu.baqis.ac.cn/" | ||
|
||
if self.check_account_data(): | ||
self.load_account() | ||
|
||
self.backends_api = "qbackend/get_backends/" | ||
self.chip_api = "qbackend/scq_get_chip_info/" | ||
self.exec_api = "qbackend/scq_kit/" | ||
self.exec_async_api = "qbackend/scq_kit_asyc/" | ||
self.exec_recall_api = "qbackend/scq_task_recall/" | ||
self.priority = 2 | ||
url = "https://quafu.baqis.ac.cn/" | ||
backends_api = url + "qbackend/get_backends/" | ||
chip_api = url + "qbackend/scq_get_chip_info/" | ||
exec_api = url + "qbackend/scq_kit/" | ||
exec_async_api = url + "qbackend/scq_kit_asyc/" | ||
exec_recall_api = url + "qbackend/scq_task_recall/" | ||
|
||
def check_account_data(self): | ||
homedir = get_homedir() | ||
file_dir = homedir + "/.quafu/" | ||
if not os.path.exists(file_dir): | ||
print("Your user information is not configured. Remember to configure it by User.save_apitoken(<your api_token>)") | ||
return False | ||
return True | ||
# TODO: Check if the user's basic data file is formated | ||
def __init__(self, api_token: str = None, token_dir: str = None): | ||
""" | ||
Initialize user account and load backend information. | ||
:param api_token: if provided | ||
:param token_dir: where api token is found or saved | ||
""" | ||
self._available_backends = {} | ||
|
||
def save_apitoken(self, apitoken): | ||
if token_dir is None: | ||
self.token_dir = get_homedir() + "/.quafu/" | ||
else: | ||
self.token_dir = token_dir | ||
|
||
if api_token is None: | ||
self.api_token = self._load_account_token() | ||
else: | ||
self.api_token = api_token | ||
|
||
self.priority = 2 | ||
|
||
def save_apitoken(self, apitoken=None): | ||
""" | ||
Save your apitoken associate your Quafu account. | ||
Save api-token associate your Quafu account. | ||
""" | ||
self.apitoken = apitoken | ||
homedir = get_homedir() | ||
file_dir = homedir + "/.quafu/" | ||
if apitoken is not None: | ||
import warnings | ||
warnings.warn("The argument 'apitoken' in this function will be deprecated " | ||
"in the future, please set api token by providing 'api_token' " | ||
"or 'token_dir' when initialize User()." | ||
) | ||
self.api_token = apitoken | ||
|
||
file_dir = self.token_dir | ||
if not os.path.exists(file_dir): | ||
os.mkdir(file_dir) | ||
with open(file_dir + "api", "w") as f: | ||
f.write(self.apitoken+"\n") | ||
f.write("http://quafu.baqis.ac.cn/") | ||
f.write(self.api_token + "\n") | ||
f.write(self.url) | ||
|
||
def load_account(self): | ||
def _load_account_token(self): | ||
""" | ||
Load Quafu account. | ||
Load Quafu account, only api at present. | ||
TODO: expand to load more user information | ||
""" | ||
homedir = get_homedir() | ||
file_dir = homedir + "/.quafu/" | ||
try: | ||
f = open(file_dir + "api", "r") | ||
data = f.readlines() | ||
token = data[0].strip("\n") | ||
url = data[1].strip("\n") | ||
self.apitoken = token | ||
self._url = url | ||
return token, url | ||
except: | ||
raise UserError("User configure error. Please set up your token.") | ||
|
||
def get_backends_info(self): | ||
f = open(self.token_dir + "api", "r") | ||
token = f.readline() | ||
except FileNotFoundError: | ||
raise UserError(f"API token file not found at: '{self.token_dir}'. " | ||
"Please set up by providing api_token/token_dir when initializing User.") | ||
return token | ||
|
||
def _get_backends_info(self): | ||
""" | ||
Get available backends information | ||
""" | ||
|
||
backends_info = requests.post(url=self._url+self.backends_api, headers={"api_token" : self.apitoken}) | ||
backends_info = requests.post(url=self.backends_api, headers={"api_token": self.api_token}) | ||
backends_info_dict = json.loads(backends_info.text) | ||
if backends_info_dict["status"] == 201: | ||
raise UserError(backends_info_dict["message"]) | ||
else: | ||
return backends_info_dict["data"] | ||
|
||
def get_available_backends(self, print_info=True): | ||
""" | ||
Get available backends | ||
""" | ||
from quafu.backends.backends import Backend | ||
backends_info = self.get_backends_info() | ||
self._available_backends = {info["system_name"]:Backend(info) for info in backends_info} | ||
backends_info = self._get_backends_info() | ||
self._available_backends = {info["system_name"]: Backend(info) for info in backends_info} | ||
|
||
if print_info: | ||
print((" "*5).join(["system_name".ljust(10), "qubits".ljust(10), "status".ljust(10)])) | ||
print((" " * 5).join(["system_name".ljust(10), "qubits".ljust(10), "status".ljust(10)])) | ||
for backend in self._available_backends.values(): | ||
print((" "*5).join([backend.name.ljust(10), str(backend.qubit_num).ljust(10), backend.status.ljust(10)])) | ||
print((" " * 5).join( | ||
[backend.name.ljust(10), str(backend.qubit_num).ljust(10), backend.status.ljust(10)])) | ||
|
||
return self._available_backends |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
import os | ||
import sys | ||
from ..exceptions import QuafuError | ||
|
||
|
||
def get_homedir(): | ||
if sys.platform == 'win32': | ||
homedir = os.environ['USERPROFILE'] | ||
elif sys.platform == 'linux' or sys.platform == 'darwin': | ||
homedir = os.environ['HOME'] | ||
|
||
else: | ||
raise QuafuError(f'unsupported platform:{sys.platform}. ' | ||
f'You may raise a request issue on github.') | ||
return homedir | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters