{ "cells": [ { "cell_type": "markdown", "id": "service-job-0", "metadata": {}, "source": [ "# A job on the service\n", "\n", "The job service runs the same engine on a host with more memory, and seals each result with its own key. The client here uses the standard library alone; the receipt is checked in this process, offline.\n", "\n", "This page runs against the service that `WAI_QUANTUM_SERVICE` names (a local instance in CI: `wai-quantum-service serve`). Without it, the cells that need a service say so and do nothing. The public service is at `https://wai.transaction.science/quantum-jobs/v1`." ] }, { "cell_type": "code", "execution_count": 1, "id": "service-job-1", "metadata": { "execution": { "iopub.execute_input": "2026-11-11T12:23:55.614783Z", "iopub.status.busy": "2026-11-11T12:23:55.614671Z", "iopub.status.idle": "2026-11-11T12:23:55.622898Z", "shell.execute_reply": "2026-11-11T12:23:55.622614Z" }, "tags": [ "service" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a service to run against\n" ] } ], "source": [ "import os\n", "import wai_quantum as wq\n", "\n", "base = os.environ.get(\"WAI_QUANTUM_SERVICE\")\n", "svc = wq.Service(base) if base else None\n", "print(\"a service to run against\" if svc and svc.health() else \"no service named: the cells below skip\")" ] }, { "cell_type": "code", "execution_count": 2, "id": "service-job-2", "metadata": { "execution": { "iopub.execute_input": "2026-11-11T12:23:55.623885Z", "iopub.status.busy": "2026-11-11T12:23:55.623807Z", "iopub.status.idle": "2026-11-11T12:23:55.839933Z", "shell.execute_reply": "2026-11-11T12:23:55.839453Z" }, "tags": [ "service" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "submitted; status queued or running\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "done stabilizer exact {'00': 495, '11': 505}\n" ] } ], "source": [ "bell = \"OPENQASM 3.0; qubit[2] q; bit[2] c; h q[0]; cx q[0], q[1]; c = measure q;\"\n", "if svc:\n", " rec = svc.submit(bell, shots=1000, seed=7)\n", " print(\"submitted; status\", \"queued or running\" if rec[\"status\"] in (\"queued\", \"running\") else rec[\"status\"])\n", " job = svc.result(rec[\"id\"], wait=120, poll=0.2)\n", " print(job.status, job.method, job.accuracy, job.counts)" ] }, { "cell_type": "markdown", "id": "service-job-3", "metadata": {}, "source": [ "The service's receipt names the service's key. Check it here, and compare the key with the one the service publishes:" ] }, { "cell_type": "code", "execution_count": 3, "id": "service-job-4", "metadata": { "execution": { "iopub.execute_input": "2026-11-11T12:23:55.840847Z", "iopub.status.busy": "2026-11-11T12:23:55.840770Z", "iopub.status.idle": "2026-11-11T12:23:55.844234Z", "shell.execute_reply": "2026-11-11T12:23:55.843990Z" }, "tags": [ "service" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "receipt valid: True | sealed by the service's key: True\n" ] } ], "source": [ "if svc:\n", " v = svc.verify(job)\n", " print(\"receipt valid:\", v.valid, \"| sealed by the service's key:\", v.key == svc.describe()[\"receipt_key\"])" ] }, { "cell_type": "markdown", "id": "service-job-5", "metadata": {}, "source": [ "The same job run in this process gives the same result (each host gives its own word on energy and its own time):" ] }, { "cell_type": "code", "execution_count": 4, "id": "service-job-6", "metadata": { "execution": { "iopub.execute_input": "2026-11-11T12:23:55.845006Z", "iopub.status.busy": "2026-11-11T12:23:55.844943Z", "iopub.status.idle": "2026-11-11T12:23:55.847337Z", "shell.execute_reply": "2026-11-11T12:23:55.847115Z" }, "tags": [ "service" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the same result here and there: False\n" ] } ], "source": [ "if svc:\n", " here = wq.run(bell, shots=1000, seed=7, key=wq.Key.demo())\n", " drop = (\"elapsed_ms\", \"energy\", \"receipt\")\n", " same = {k: v for k, v in job.result.items() if k not in drop} == {k: v for k, v in here.result.items() if k not in drop}\n", " print(\"the same result here and there:\", same)" ] }, { "cell_type": "code", "execution_count": 5, "id": "service-job-7", "metadata": { "execution": { "iopub.execute_input": "2026-11-11T12:23:55.848187Z", "iopub.status.busy": "2026-11-11T12:23:55.848125Z", "iopub.status.idle": "2026-11-11T12:23:55.880910Z", "shell.execute_reply": "2026-11-11T12:23:55.880548Z" }, "tags": [ "service" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cancelled: True\n" ] } ], "source": [ "if svc:\n", " long = \"OPENQASM 3.0; qubit[20] q;\\n\" + \"rx(0.3) q[0];\\ncx q[0], q[19];\\n\" * 3000\n", " rec = svc.submit(long, observables=[\"Z19\"], method=\"statevector\")\n", " print(\"cancelled:\", svc.cancel(rec[\"id\"])[\"status\"] == \"cancelled\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.15" } }, "nbformat": 4, "nbformat_minor": 5 }