Skip to content

Commit

Permalink
Merge pull request #81 from Zhaoyilunnn/master
Browse files Browse the repository at this point in the history
fix response handling in `Task.send` and add ut in workflow
  • Loading branch information
chensgit169 authored Sep 13, 2023
2 parents d6ea733 + 56a1767 commit bbc3a2b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
17 changes: 10 additions & 7 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Build

on:
on:
push:
branches:
- "master"
Expand All @@ -12,7 +12,7 @@ on:
workflow_dispatch:



jobs:
build_wheels:
name: Build python wheels
Expand All @@ -31,23 +31,26 @@ jobs:
- os-arch: "macosx_arm64"
os: "macos-11"
runs-on: ${{ matrix.os }}

env:
CIBW_BUILD: ${{ matrix.cibw-python }}-${{ matrix.os-arch }}
PYTHON: ${{ matrix.python-version }}
TWINE_USERNAME: "__token__"

steps:
- uses: actions/checkout@v3

# Used to host cibuildwheel
- uses: actions/setup-python@v3
- name: Install dependence
run: python -m pip install pybind11 cibuildwheel scikit-build twine
run: python -m pip install pybind11 cibuildwheel scikit-build twine pytest

- name: Build wheels
run: python -m cibuildwheel --output-dir dist


- name: Run unit tests
run: pip install . && pytest tests/

- name: Publish package
run: python -m twine upload dist/*.whl
if: ${{ contains(github.ref, '/tags/') }}
Expand Down
29 changes: 14 additions & 15 deletions quafu/tasks/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import copy
import logging
from typing import Dict, List, Tuple
from typing import Dict, List, Optional, Tuple
from urllib import parse

import numpy as np
Expand Down Expand Up @@ -42,7 +42,7 @@ class Task(object):
"""

def __init__(self, user: User = None):
def __init__(self, user: Optional[User] = None):
self.user = User() if user is None else user

self.shots = 1000
Expand Down Expand Up @@ -242,28 +242,27 @@ def send(self,
) # type: requests.models.Response

# TODO: completing status code checks
# assert response.ok
if not response.ok:
logging.warning("Received a non-200 response from the server.\n")
if response.status_code == 502:
logging.critical(
"Received a 502 Bad Gateway response. Please try again later.\n"
"If there is persistent failure, please report it on our github page."
)
raise UserError()
logging.critical("Received a 502 Bad Gateway response. Please try again later.\n"
"If there is persistent failure, please report it on our github page.")
raise UserError('502 Bad Gateway response')
else:
res_dict = response.json()

if response.status_code in [201, 205]:
res_dict = response.json() # type: dict
quafu_status = res_dict['status']
if quafu_status in [201, 205]:
raise UserError(res_dict["message"])
elif response.status_code == 5001:
elif quafu_status == 5001:
raise CircuitError(res_dict["message"])
elif response.status_code == 5003:
elif quafu_status == 5003:
raise ServerError(res_dict["message"])
elif response.status_code == 5004:
elif quafu_status == 5004:
raise CompileError(res_dict["message"])
else:
task_id = res_dict["task_id"]

if not (group in self.submit_history):
if group not in self.submit_history:
self.submit_history[group] = [task_id]
else:
self.submit_history[group].append(task_id)
Expand Down
3 changes: 2 additions & 1 deletion quafu/users/userapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import os
from typing import Optional

import requests

Expand All @@ -28,7 +29,7 @@ class User(object):
exec_async_api = "qbackend/scq_kit_asyc/"
exec_recall_api = "qbackend/scq_task_recall/"

def __init__(self, api_token: str = None, token_dir: str = None):
def __init__(self, api_token: Optional[str] = None, token_dir: Optional[str] = None):
"""
Initialize user account and load backend information.
Expand Down
7 changes: 5 additions & 2 deletions tests/quafu/algorithms/estimator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@
class TestEstimator:
"""Test class of Estimator"""

@patch("quafu.tasks.tasks.Task.send")
def test_run(self, mock_send):
@patch("quafu.users.userapi.User._load_account_token", autospec=True)
@patch("quafu.users.userapi.User.get_available_backends", autospec=True)
@patch("quafu.tasks.tasks.Task.send", autospec=True)
def test_run(self, mock_send, mock_backends, mock_load_account):
"""Test Estimator.run"""
mock_send.return_value = TEST_EXE_RES
mock_backends.return_value = {"ScQ-P10": None}
circ = QuantumCircuit(5)

for i in range(5):
Expand Down

0 comments on commit bbc3a2b

Please sign in to comment.