Skip to content

Commit

Permalink
fix backend retry
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed May 27, 2024
1 parent cf901d2 commit 0ede116
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 35 deletions.
11 changes: 8 additions & 3 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"myst_nb",
]

#"sphinx_rtd_dark_mode",
#"sphinx_rtd_theme",
# "sphinx_rtd_dark_mode",
# "sphinx_rtd_theme",
jupyter_execute_notebooks = "off"
myst_enable_extensions = ["colon_fence"]

Expand Down Expand Up @@ -68,7 +68,12 @@
"navbar_start": ["navbar-logo", "navbar-version"],
"navbar_align": "content",
"header_links_before_dropdown": 5,
"secondary_sidebar_items": ["page-toc", "searchbox", "edit-this-page", "sourcelink"],
"secondary_sidebar_items": [
"page-toc",
"searchbox",
"edit-this-page",
"sourcelink",
],
"use_edit_page_button": True,
"analytics": {"google_analytics_id": "G-JJNSHE8EFK"},
"external_links": [
Expand Down
33 changes: 25 additions & 8 deletions docs/source/docs/qiskit_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"After that, install dqpu and qiskit:\n",
"\n",
"```bash\n",
"pip install qiskit dqpu\n",
"pip install qiskit dqpu pylatexenc\n",
"```"
]
},
Expand All @@ -51,7 +51,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -68,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 2,
"metadata": {},
"outputs": [
{
Expand All @@ -78,7 +78,7 @@
"<Figure size 287.294x284.278 with 1 Axes>"
]
},
"execution_count": 17,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -99,9 +99,26 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"asyncio.run() cannot be called from a running event loop\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/dakk/.pyenv/versions/3.10.13/envs/dqpu2/lib/python3.10/site-packages/dqpu-0.2.2-py3.10.egg/dqpu/blockchain/near.py:67: RuntimeWarning: coroutine 'NearBlockchain.load_account.<locals>.v' was never awaited\n",
" return loop.run_until_complete(v())\n",
"RuntimeWarning: Enable tracemalloc to get the object allocation traceback\n"
]
}
],
"source": [
"backend = DQPUBackend()\n",
"backend.load_account(\"dqpu_alice.testnet\")"
Expand All @@ -116,7 +133,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -133,7 +150,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand Down
2 changes: 1 addition & 1 deletion dqpu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# limitations under the License.


__version__ = "0.2.6"
__version__ = "0.2.7"
16 changes: 11 additions & 5 deletions dqpu/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import json
import tempfile

from ..blockchain import repeat_until_done


def submit_job(nb, ipfs, qasm_data, num_qubits, depth, options):
fp = tempfile.NamedTemporaryFile(mode="w", delete=False)
Expand All @@ -23,20 +25,24 @@ def submit_job(nb, ipfs, qasm_data, num_qubits, depth, options):

job_file = ipfs.upload(fp.name)

nb.submit_job(num_qubits, depth, options["shots"], job_file, options["reward"])
return nb.get_latest_jobs()[-1]["id"]
repeat_until_done(
lambda: nb.submit_job(
num_qubits, depth, options["shots"], job_file, options["reward"]
)
)
return repeat_until_done(lambda: nb.get_latest_jobs())[-1]["id"]


def job_status(nb, jid):
return nb.get_job_status(jid)
return repeat_until_done(lambda: nb.get_job_status(jid))


def job_remove(nb, jid):
return nb.remove_job(jid)
return repeat_until_done(lambda: nb.remove_job(jid))


def job_result(nb, ipfs, jid):
j = nb.get_job(jid)
j = repeat_until_done(lambda: nb.get_job(jid))
data = json.loads(ipfs.get(j["result_file"]))
trap_list = json.loads(ipfs.get(j["trap_file"]))
return data, trap_list
1 change: 1 addition & 0 deletions dqpu/blockchain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from .blockchain import repeat_until_done # noqa: F401
from .ipfs_gateway import IPFSGateway, start_ipfs_daemon, stop_ipfs_daemon # noqa: F401
from .near import NearBlockchain, from_near, to_near # noqa: F401
12 changes: 12 additions & 0 deletions dqpu/blockchain/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import time


def repeat_until_done(f, n_iterations=10, wait_time=5):
try:
return f()
except Exception as e:
print(e)
print(f"Function call failed, retrying in {wait_time} seconds ({n_iterations})")
time.sleep(wait_time)
return repeat_until_done(f, n_iterations - 1, wait_time)


class Blockchain:
"""Abstract blockchain provider"""
Expand Down
2 changes: 1 addition & 1 deletion dqpu/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def cli(): # noqa: C901
print(f"Circuit file is {job_file}")

shots = random.choice([256, 512, 1024, 2048, 4096, 8192, 16384])

# sh_fact = (shots / 16384) * nq
# reward = random.randint(int(0.5 * sh_fact), int(1.1 * sh_fact)) / 1000.0
reward = nq * nq / 600000
Expand Down
10 changes: 8 additions & 2 deletions dqpu/samplernode.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@

from requests.exceptions import ReadTimeout

from .blockchain import IPFSGateway, NearBlockchain, from_near, to_near
from .blockchain import (
IPFSGateway,
NearBlockchain,
from_near,
repeat_until_done,
to_near,
)
from .cli import default_parser
from .sampler import SAMPLERS
from .utils import create_dqpu_dirs, repeat_until_done
from .utils import create_dqpu_dirs


def filter_jobs(jobs, args):
Expand Down
11 changes: 0 additions & 11 deletions dqpu/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@
# limitations under the License.

import os
import time


def repeat_until_done(f, n_iterations=10, wait_time=5):
try:
return f()
except Exception as e:
print(e)
print(f"Function call failed, retrying in {wait_time} seconds ({n_iterations})")
time.sleep(wait_time)
return repeat_until_done(f, n_iterations - 1, wait_time)


def create_dqpu_dirs(): # noqa: C901
Expand Down
4 changes: 2 additions & 2 deletions dqpu/verifiernode.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

from requests.exceptions import ReadTimeout

from .blockchain import IPFSGateway, NearBlockchain
from .blockchain import IPFSGateway, NearBlockchain, repeat_until_done
from .cli import default_parser
from .q import Circuit
from .utils import create_dqpu_dirs, repeat_until_done
from .utils import create_dqpu_dirs
from .verifier import BasicTrapper # BasicTrapInfo,


Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import dqpu

REQUIREMENTS = [
#"dask[distributed]==2024.4.2",
# "dask[distributed]==2024.4.2",
"scipy==1.13.0",
"matplotlib",
"qiskit==1.0.2",
Expand All @@ -32,7 +32,7 @@
"pydantic",
"nest_asyncio",
"pyqrack",
"qiskit-qrack-provider>=0.11.0"
"qiskit-qrack-provider>=0.11.0",
]

setup(
Expand Down

0 comments on commit 0ede116

Please sign in to comment.