uniqc.backend_adapter.task.store module¶
SQLite-backed task storage for UnifiedQuantum.
This module is the single source of truth for task persistence. Both
uniqc.backend_adapter.task_manager and uniqc.backend_adapter.task.persistence delegate to
TaskStore; there is no JSON/JSONL fallback path.
Storage layout:
~/.uniqc/cache/
tasks.sqlite # single database file
Schema versioning (best practices)¶
The database uses SQLite’s built-in metadata slots in the file header rather than a custom bookkeeping table:
PRAGMA application_idholdsAPPLICATION_ID(the 4 bytes"UNIC"). On open we refuse to touch a file whoseapplication_idis set to something else; a zero value means a fresh DB we can stamp.PRAGMA user_versionholds the integer schema version. It is bumped by each migration and used to decide what else to run.
MIGRATIONS is an ordered list of (target_version, migrate_fn)
tuples. To evolve the schema:
Append a new tuple
(N, _apply_vN)whereN = CURRENT_SCHEMA_VERSION + 1.Bump
CURRENT_SCHEMA_VERSIONtoN.Implement
_apply_vN(conn)(DDL or DML on the given connection).
On open, TaskStore iterates the list and runs each outstanding
migration in its own transaction; user_version is stamped as part of
the same transaction so a crashed migration rolls back the version bump
too.
- class uniqc.backend_adapter.task.store.TaskInfo(task_id, backend, status=TaskStatus.PENDING, result=None, shots=1000, submit_time=<factory>, update_time=<factory>, metadata=<factory>, error_message=None, archived_at=None)[source]¶
Bases:
objectInformation about a submitted task.
- Variables:
task_id (str) – Unique identifier for the task.
backend (str) – The backend where the task was submitted.
status (str) – Current status of the task.
result (dict | None) – Task result (if completed).
shots (int) – Number of shots requested.
submit_time (str) – ISO format timestamp of submission.
update_time (str) – ISO format timestamp of last status update.
metadata (dict) – Additional metadata about the task.
error_message (str | None) – When
status == FAILED(or for any non-success terminal state) this carries a human-readable explanation — e.g. the dummy adapter’s stderr or the cloud platform’s error string.Nonewhile the task is still running or on success.archived_at (str | None) – Timestamp when the task was archived;
Noneif still in the livetaskstable.
- Parameters:
- class uniqc.backend_adapter.task.store.TaskShard(uniqc_task_id, shard_index, platform_task_id, backend, circuit_count=1, sub_index_offset=0, status=TaskStatus.PENDING, result=None, error_message=None, submit_time=<factory>, update_time=<factory>)[source]¶
Bases:
objectOne platform-side job belonging to a uniqc-managed task.
A uniqc task is composed of one or more shards, each backed by a single platform-issued task id (
platform_task_id). For single-circuit submissions there is exactly one shard whosecircuit_count == 1; for batched submissions a shard may carry multiple circuits when the underlying platform supports native batch submission. When the user batch exceeds an adapter’smax_native_batch_sizeuniqc transparently slices it into multiple shards.- Variables:
uniqc_task_id (str) – Parent uniqc task id (
uqt_…).shard_index (int) – 0-based index of this shard within the parent.
platform_task_id (str) – Task id assigned by the cloud platform.
backend (str) – Backend label used at submit time (e.g.
"originq:WK_C180").circuit_count (int) – Number of user circuits packed into this shard.
1for non-batched / single-circuit shards.sub_index_offset (int) – Offset of this shard’s first circuit in the user’s original
submit_batchlist. Together withcircuit_countthis is the authoritative source for re-assembling per-circuit results in submission order.status (str) – Status of this shard (independent of sibling shards).
result (Any) – Per-shard adapter result (
dictfor single-circuit shards;list[dict]for native-batch shards). May beNonewhile the shard is still running.error_message (str | None) – Human-readable error if
status == FAILED.submit_time (str) – ISO timestamp when this shard was successfully submitted to the platform.
update_time (str) – ISO timestamp of last status update.
- Parameters:
- class uniqc.backend_adapter.task.store.TaskStatus(*values)[source]¶
-
Enumeration of task statuses.
- CANCELLED = 'cancelled'¶
- FAILED = 'failed'¶
- PENDING = 'pending'¶
- RUNNING = 'running'¶
- SUCCESS = 'success'¶
- class uniqc.backend_adapter.task.store.TaskStore(cache_dir=None)[source]¶
Bases:
objectSQLite-backed storage for task records.
TaskStoreis safe to construct multiple times against the same cache directory; all operations use short-lived connections guarded by a per-instance lock.- Parameters:
cache_dir (Path | str | None) – Directory that holds
tasks.sqlite. Defaults to~/.uniqc/cache/. The directory is created if missing.
- static aggregate_status(shards)[source]¶
Compute parent task status from shards.
Rules (denormalised onto
tasks.status): - no shards yet →pending- any shard non-terminal →running- all shards SUCCESS →success- all terminal AND any FAILED →failed- all CANCELLED (or CANCELLED + SUCCESS only) →cancelledWhen the aggregate is
failedorcancelledcallers may still inspect the individual shards viaget_shards()to discover which ones succeeded.
- clear_all()[source]¶
Delete the on-disk SQLite file (and its WAL/SHM siblings).
Matches the pre-SQLite
clear_cachesemantics: the cache file disappears. A subsequent operation re-initialises the schema.- Return type:
None
- clear_completed(terminal_statuses=('success', 'failed', 'cancelled'))[source]¶
Delete tasks whose status is in
terminal_statuses.
- find_uniqc_id_by_platform_id(platform_task_id, *, backend=None)[source]¶
Return the parent
uniqc_task_idfor a platform id, orNone.
- get_shard_by_platform_id(platform_task_id, *, backend=None)[source]¶
Look up a shard by its platform-issued task id.
When
backendis given, requires a backend match. Returns the first shard found (the index makes lookup O(log n)). If multiple shards share the same platform id across different backends andbackendis not specified, the caller should disambiguate.
- list(*, status=None, backend=None, limit=None, offset=None)[source]¶
List tasks, newest first (by
submit_time).