open-standards/wai/quantum-conformance/receipts/generate.py
Transaction Science 1cf0076e86 Receipts corpus: qudit jobs, and wasmQ and qudit jobs the job service made, judged alike by both verifiers
quantum-conformance/receipts/ had no qudit job and no wasmQ job a service sealed. It gains
eight made receipts, each pinned in made.lock.json and named in MADE:

- cli-qudit-qutrit-bell, -ququarts-phase, -six-levels, -qubits-ghz: `wai-quantum qudit run
  --demo-key --json` (wai.quantum.qudit/1): a qutrit Bell pair with its amplitudes, two
  ququarts under a controlled sum, a phase and a Fourier power, two qudits of six levels
  (F_2 x F_3), and a GHZ state as a qudit job of dimension 2;
- service-wasmq-surface3-decode64, service-wasmq2-t-toffoli, service-wasmq-fault-fuel and
  service-qudit-qutrit-bell: POST /v1/wasmq and POST /v1/qudit on a job service built from
  this checkout (Linux x86_64), each the service's job report sealed with its key; the
  wasmQ/2 run's energy.run the processor packages' reading over the worker's window
  (attribution none), the fault sealed.

make.sh makes them (the service's when WAI_QSVC_BIN names its binary). Both verifiers (the
command line's `verify` and receipt_verify.py) give each new receipt no reason, and
receipt_differential.py --reference agrees three ways on all 421 receipts and on every one
of the 33,184 one-member changes of the 111 valid ones; the plants wasmq-unchecked,
landscape-unchecked and lenient-key are each caught. generate.py --check and --self-test,
and receipt_verify.py --self-test (846 checks), pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-10 23:18:39 -05:00

1534 lines
114 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""generate.py: the crafted half of the receipt corpus (README.md).
python3 -I generate.py # writes jobs/crafted-*.json and manifest.json
python3 -I generate.py --check # fails unless both are what it would write
The corpus has two halves. The made half (`jobs/cli-*`, `jobs/service-*`,
`jobs/scripted-*`, `jobs/fixture-*`) was sealed by the command line, the job
service, the provider clients and earlier engines, and is kept as it was made:
nothing here writes it. The crafted half is made here from the made half, each
receipt changed in one way and, where the change is inside what is sealed,
sealed again with the published demo key (seed 42 repeated, the key
`wai-quantum --demo-key` signs with), so that the one rule it breaks is the
only thing wrong with it. The manifest names, for every receipt in both halves,
the reasons a verifier must give (wai/extensions/quantum-providers.md
section 8.4), each written here by hand from that section: none is copied from
a verifier's output.
To seal, it needs BLAKE3, the RFC 8785 text and Ed25519 signing. BLAKE3 is the
integer-only referee's (../tools/referee.py, which shares no code with the
engine); the other two are written below. Neither verifier is imported: the
two verifiers judge what this writes, and must agree with each other and with
the manifest.
Standard library only, Python 3.9+.
"""
import copy
import hashlib
import importlib.util
import json
import math
import os
import struct
import sys
from decimal import Decimal
HERE = os.path.dirname(os.path.abspath(__file__))
JOBS = os.path.join(HERE, "jobs")
sys.dont_write_bytecode = True
_spec = importlib.util.spec_from_file_location("referee", os.path.join(HERE, "..", "tools", "referee.py"))
_referee = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(_referee)
blake3 = _referee.blake3
# ------------------------------------------------------------------ JSON text
class Raw(object):
"""A number written as this literal, whatever value it stands for."""
def __init__(self, literal, value):
self.literal, self.value = literal, value
def es_number(x):
"""ECMAScript's Number::toString of the double x (section 8.2)."""
x = float(x)
if x == 0:
return "0"
sign = "-" if x < 0 else ""
t = Decimal(repr(abs(x))).as_tuple()
digits = "".join(map(str, t.digits)).rstrip("0") or "0"
k = len(digits)
n = len("".join(map(str, t.digits))) + t.exponent
if k <= n <= 21:
body = digits + "0" * (n - k)
elif 0 < n <= 21:
body = digits[:n] + "." + digits[n:]
elif -6 < n <= 0:
body = "0." + "0" * (-n) + digits
else:
e = n - 1
body = digits[0] + ("." + digits[1:] if k > 1 else "") + "e" + ("+" if e >= 0 else "-") + str(abs(e))
return sign + body
def first_number(v):
"""A number as the first form's writer wrote it (section 8.2)."""
if isinstance(v, bool):
raise TypeError
if isinstance(v, int) and (0 <= v < 2 ** 64 or -2 ** 63 <= v < 0):
return str(v)
x = float(v)
if x == 0:
return "-0.0" if str(x).startswith("-") else "0.0"
sign = "-" if x < 0 else ""
t = Decimal(repr(abs(x))).as_tuple()
digits = "".join(map(str, t.digits)).rstrip("0") or "0"
k = len(digits)
n = len("".join(map(str, t.digits))) + t.exponent
if k <= n <= 16:
body = digits + "0" * (n - k) + ".0"
elif 0 < n <= 16:
body = digits[:n] + "." + digits[n:]
elif -5 < n <= 0:
body = "0." + "0" * (-n) + digits
else:
e = n - 1
body = digits[0] + ("." + digits[1:] if k > 1 else "") + "e" + ("-" if e < 0 else "+") + str(abs(e))
return sign + body
_SHORT = {'"': '\\"', "\\": "\\\\", "\b": "\\b", "\t": "\\t", "\n": "\\n", "\f": "\\f", "\r": "\\r"}
def string_text(s):
out = ['"']
for c in s:
if c in _SHORT:
out.append(_SHORT[c])
elif ord(c) < 0x20:
out.append("\\u%04x" % ord(c))
else:
out.append(c)
out.append('"')
return "".join(out)
def utf16(s):
return s.encode("utf-16-be")
def text(v, form="canonical"):
"""The canonical text of v, or its first-form text (members in order)."""
if v is None:
return "null"
if v is True:
return "true"
if v is False:
return "false"
if isinstance(v, Raw):
return text(v.value, form)
if isinstance(v, (int, float)):
return es_number(v) if form == "canonical" else first_number(v)
if isinstance(v, str):
return string_text(v)
if isinstance(v, list):
return "[" + ",".join(text(x, form) for x in v) + "]"
keys = sorted(v, key=utf16) if form == "canonical" else list(v)
return "{" + ",".join(string_text(k) + ":" + text(v[k], form) for k in keys) + "}"
def dump(v, indent=0):
"""A readable file's text of v: two-space indents, Raw literals as given."""
pad, inner = " " * indent, " " * (indent + 1)
if isinstance(v, Raw):
return v.literal
if isinstance(v, dict):
if not v:
return "{}"
return "{\n" + ",\n".join(inner + json.dumps(k, ensure_ascii=False) + ": " + dump(x, indent + 1) for k, x in v.items()) + "\n" + pad + "}"
if isinstance(v, list):
if not v:
return "[]"
return "[\n" + ",\n".join(inner + dump(x, indent + 1) for x in v) + "\n" + pad + "]"
if isinstance(v, float) and v == 0 and str(v).startswith("-"):
return "-0.0"
return json.dumps(v, ensure_ascii=False)
# ---------------------------------------------------------------- Ed25519 (RFC 8032)
P = 2 ** 255 - 19
L = 2 ** 252 + 27742317777372353535851937790883648493
D = -121665 * pow(121666, P - 2, P) % P
SQRT_M1 = pow(2, (P - 1) // 4, P)
def _add(a, b):
x1, y1, z1, t1 = a
x2, y2, z2, t2 = b
A, B = (y1 - x1) * (y2 - x2) % P, (y1 + x1) * (y2 + x2) % P
C, E = t1 * 2 * D * t2 % P, z1 * 2 * z2 % P
e, f, g, h = B - A, E - C, E + C, B + A
return (e * f % P, g * h % P, f * g % P, e * h % P)
def _mul(s, pt):
q = (0, 1, 1, 0)
while s:
if s & 1:
q = _add(q, pt)
pt = _add(pt, pt)
s >>= 1
return q
def _encode(pt):
x, y, z, _ = pt
zi = pow(z, P - 2, P)
x, y = x * zi % P, y * zi % P
return (y | ((x & 1) << 255)).to_bytes(32, "little")
def _decode(b):
y = int.from_bytes(b, "little") & ((1 << 255) - 1)
sign = b[31] >> 7
if y >= P:
return None
xx = (y * y - 1) * pow(D * y * y + 1, P - 2, P) % P
x = pow(xx, (P + 3) // 8, P)
if (x * x - xx) % P:
x = x * SQRT_M1 % P
if (x * x - xx) % P:
return None
if x == 0 and sign:
return None
if x & 1 != sign:
x = P - x
return (x, y, 1, x * y % P)
BASE = _decode((4 * pow(5, P - 2, P) % P).to_bytes(32, "little"))
def public_key(seed):
h = hashlib.sha512(seed).digest()
a = int.from_bytes(h[:32], "little")
a = (a & ((1 << 254) - 8)) | (1 << 254)
return _encode(_mul(a, BASE))
def sign(seed, msg):
h = hashlib.sha512(seed).digest()
a = int.from_bytes(h[:32], "little")
a = (a & ((1 << 254) - 8)) | (1 << 254)
A = _encode(_mul(a, BASE))
r = int.from_bytes(hashlib.sha512(h[32:] + msg).digest(), "little") % L
R = _encode(_mul(r, BASE))
k = int.from_bytes(hashlib.sha512(R + A + msg).digest(), "little") % L
return R + ((r + k * a) % L).to_bytes(32, "little")
DEMO = bytes([42] * 32)
OTHER = bytes([43] * 32)
IDENTITY = bytes([1] + [0] * 31)
# A point of order 8.
ORDER8 = bytes.fromhex("c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a")
# --------------------------------------------------------------------- sealing
DOMAIN = b"wai:quantum-service-receipt"
def digest(request, result, form="canonical"):
q = text(request, form).encode("utf-8")
t = text({k: v for k, v in result.items() if k != "receipt"}, form).encode("utf-8")
return blake3(DOMAIN + bytes([2 if form == "canonical" else 1]) + struct.pack(">Q", len(q)) + q + struct.pack(">Q", len(t)) + t)
def seal(body, seed=DEMO, form="canonical"):
"""body with its result sealed again by seed's key, in form."""
d = digest(body["request"], body["result"], form)
receipt = {"alg": "ed25519", "canon": "rfc8785", "key": public_key(seed).hex(), "digest": d.hex(), "signature": sign(seed, d).hex()}
if form != "canonical":
del receipt["canon"]
out = copy.deepcopy(body)
out["result"] = {k: v for k, v in out["result"].items() if k != "receipt"}
out["result"]["receipt"] = receipt
return out
MERKLE_LEAF = b"moq-jwp:merkle-leaf\x01"
MERKLE_NODE = b"moq-jwp:merkle-node\x01"
def merkle_levels(leaves):
"""Each level of the tree over leaves (section 8.6), the leaves' hashes first."""
level = [blake3(MERKLE_LEAF + x) for x in leaves]
levels = [level]
while len(level) > 1:
if len(level) % 2:
level = level + [level[-1]]
levels[-1] = level
level = [blake3(MERKLE_NODE + level[i] + level[i + 1]) for i in range(0, len(level), 2)]
levels.append(level)
return levels
def set_leaf(item):
"""An item's leaf (section 8.6): the digest of its request and its result, or its error."""
if "result" in item:
return digest(item["request"], item["result"])
return digest(item["request"], {"error": item["error"]})
def seal_set(body, seed=DEMO):
"""A set with its root and paths made from its items, and its receipt over
its request and its head, sealed again."""
out = copy.deepcopy(body)
r = out["result"]
levels = merkle_levels([set_leaf(it) for it in r["items"]])
r["root"] = levels[-1][0].hex()
for i, it in enumerate(r["items"]):
path, at = [], i
for level in levels[:-1]:
path.append(level[at ^ 1].hex())
at //= 2
it["path"] = path
head = {k: v for k, v in r.items() if k not in ("items", "receipt")}
d = digest(out["request"], head)
r["receipt"] = {"alg": "ed25519", "canon": "rfc8785", "key": public_key(seed).hex(), "digest": d.hex(), "signature": sign(seed, d).hex()}
return out
def coin_split(n):
"""Section 8.7's m(n): what a fair coin's split of n leaves between its sides on average."""
if n == 0:
return 0.0
j = (n - 1) // 2
if j < 4096:
m = 1.0
for i in range(j):
m = m * float(2 * i + 3) / float(2 * i + 2)
return m
jj = float(j)
c = (1.0 - 1.0 / (8.0 * jj) + 1.0 / (128.0 * jj * jj) + 5.0 / (1024.0 * jj * jj * jj)) / math.sqrt(math.pi * jj)
return (2.0 * jj + 1.0) * c
def _count(v):
"""A count's value (quantum-wasmq section 6.5): a whole number from 0 to 2^53 - 1."""
if isinstance(v, Raw):
v = v.value
if isinstance(v, bool) or not isinstance(v, (int, float)) or v != v or v in (float("inf"), float("-inf")):
return None
return int(v) if v == int(v) and 0 <= v <= 2 ** 53 - 1 else None
def bare_transcript(r):
"""The transcript_blake3 of a wasmQ result in which no call returned (quantum-wasmq
section 6.5, rule 35, which the second version keeps, section 11.6): the prefix and,
after a fault, its shot, reason and fuel. None when a call returned, or its callouts
are no count."""
if not isinstance(r, dict) or r.get("format") not in ("wai.quantum.wasmq.result/1", "wai.quantum.wasmq.result/2"):
return None
fault = r.get("fault") if r.get("status") == "fault" and isinstance(r.get("fault"), dict) else None
in_call = 1 if fault is not None and fault.get("phase") == "call" else 0
k = _count(r.get("callouts"))
if k is None or k > in_call:
return None
data = b"wai:wasmq-transcript\x01"
if fault is not None:
reason = fault.get("reason")
data += b"fault" + struct.pack("<Q", _count(fault.get("shot")) or 0) + (reason.encode("utf-8") if isinstance(reason, str) else b"") + struct.pack("<Q", _count(fault.get("fuel")) or 0)
return blake3(data).hex()
def retranscribed(before, b):
"""b, a wasmQ job changed from before, its transcript made again as the writer makes it
when no call returned, unless the change was to the transcript itself."""
r = b.get("result")
if isinstance(r, dict) and r.get("transcript_blake3") == before.get("result", {}).get("transcript_blake3"):
t = bare_transcript(r)
if t is not None:
r["transcript_blake3"] = t
return b
def edited(body, edit, form="canonical"):
"""body changed by edit (which may change request, result or both), sealed again; a
wasmQ job in which no call returned keeps the transcript its writer would give it."""
b = copy.deepcopy(body)
edit(b)
return seal(retranscribed(body, b), form=form)
def rx(body, edit):
"""body with only its receipt changed by edit: nothing sealed again."""
b = copy.deepcopy(body)
edit(b["result"]["receipt"])
return b
def load(name):
with open(os.path.join(JOBS, name), encoding="utf-8") as f:
return json.load(f)
# --------------------------------------------------------------------- the cases
def edited_set(body, edit):
"""A set's result changed by edit, nothing sealed again."""
b = copy.deepcopy(body)
edit(b["result"])
return b
def edited_set_head(body, edit):
"""A set's head changed by edit and the set's receipt sealed again over it,
its root and paths left as they are."""
b = copy.deepcopy(body)
edit(b["result"])
head = {k: v for k, v in b["result"].items() if k not in ("items", "receipt")}
d = digest(b["request"], head)
b["result"]["receipt"] = {"alg": "ed25519", "canon": "rfc8785", "key": public_key(DEMO).hex(), "digest": d.hex(), "signature": sign(DEMO, d).hex()}
return b
def cases():
"""(file name, the text, the reasons, what it is), every crafted receipt."""
run = load("cli-run-observables.json")
prov = load("cli-provider-reference.json")
priced = load("cli-provider-reference-priced.json")
legacy = load("fixture-provider-job-legacy.json")
sampler = load("scripted-sampler-v2.json")
prx = load("scripted-prx-circuits.json")
turns = load("scripted-native-turns.json")
sim = load("scripted-cloud-tasks-simulator.json")
qpu = load("scripted-cloud-tasks-processor.json")
progs = load("cli-set-programs.json")
sweep = load("cli-set-sweep.json")
out = []
def add(name, body_or_text, reasons, what):
t = body_or_text if isinstance(body_or_text, (str, bytes)) else dump(body_or_text) + "\n"
out.append(("crafted-" + name + ".json", t, sorted(reasons), what))
# --- the seal's form (section 8.2): one spelling of each member
add("seal-alg-other", rx(run, lambda r: r.update(alg="rsa-pss")), ["receipt_form"], "an alg other than ed25519")
add("seal-canon-null", rx(run, lambda r: r.update(canon=None)), ["receipt_form"], "canon present and null: no form")
add("seal-canon-other", rx(run, lambda r: r.update(canon="jcs")), ["receipt_form"], "canon naming no form")
add("seal-key-upper", rx(run, lambda r: r.update(key=r["key"].upper())), ["receipt_form"], "the key in upper-case hex: a second spelling of one key")
def plus(r):
d = r["digest"]
i = next(i for i in range(0, 64, 2) if d[i] == "0")
r["digest"] = d[:i] + "+" + d[i + 1:]
if any(run["result"]["receipt"]["digest"][i] == "0" for i in range(0, 64, 2)):
add("seal-digest-plus", rx(run, plus), ["receipt_form"], "a + where a hex digit belongs, which a lenient hex reader takes")
add("seal-key-two-byte-char", rx(run, lambda r: r.update(key="aé" + r["key"][3:])), ["receipt_form"], "a two-byte character among the key's hex digits")
add("seal-signature-short", rx(run, lambda r: r.update(signature=r["signature"][:-2])), ["receipt_form"], "a signature one byte short")
add("seal-key-missing", rx(run, lambda r: r.pop("key")), ["receipt_form"], "no key")
b = copy.deepcopy(run)
b["result"]["receipt"] = "VALID"
add("seal-receipt-string", b, ["receipt_form"], "a receipt that is not an object")
# --- the signature, strictly
forged = copy.deepcopy(run)
forged["result"]["expectations"][0]["value"] = -1.0
d = digest(forged["request"], forged["result"])
forged["result"]["receipt"] = {"alg": "ed25519", "canon": "rfc8785", "key": IDENTITY.hex(), "digest": d.hex(), "signature": (IDENTITY + bytes(32)).hex()}
add("seal-weak-key-forgery", forged, ["bad_key"], "the identity as the key and as R, S = 0: a library's lenient check takes it for any digest, so any content verifies")
any_digest = copy.deepcopy(forged)
B = _encode(BASE)
any_digest["result"]["receipt"]["signature"] = (B + (1).to_bytes(32, "little")).hex()
add("seal-weak-key-any-digest", any_digest, ["bad_key"], "the identity as the key, R = [1]B, S = 1: [S]B = R + [k]A for every digest, R of full order")
alias = bytes([0xEE] + [0xFF] * 30 + [0x7F])
add("seal-key-noncanonical", rx(run, lambda r: r.update(key=alias.hex())), ["bad_key"], "a key whose y is p + 1: the identity, spelt a second way")
y = 2
while _decode(y.to_bytes(32, "little")) is not None:
y += 1
add("seal-key-off-curve", rx(run, lambda r: r.update(key=y.to_bytes(32, "little").hex())), ["bad_key"], "a key that is no point on the curve")
mixed = _encode(_add(_decode(public_key(DEMO)), _decode(ORDER8)))
add("seal-key-mixed-order", rx(run, lambda r: r.update(key=mixed.hex())), ["bad_key"], "the demo key plus a point of order 8: not of small order, outside the prime-order subgroup")
def s_plus_l(r):
sig = bytes.fromhex(r["signature"])
s = int.from_bytes(sig[32:], "little") + L
r["signature"] = (sig[:32] + s.to_bytes(32, "little")).hex()
add("seal-s-plus-l", rx(run, s_plus_l), ["bad_signature"], "S + l: a second spelling of the same signature")
# S made for this R by the holder of the key, so that [S]B = R + [k]A holds:
# only the rule on R can refuse it. a is the key's scalar; R is the identity
# (spelt one way or the other), so [S]B = [k]A is S = k·a.
def equation_holds(r_bytes):
def edit(r):
h = hashlib.sha512(DEMO).digest()
a = int.from_bytes(h[:32], "little")
a &= (1 << 254) - 8
a |= 1 << 254
k = int.from_bytes(hashlib.sha512(r_bytes + bytes.fromhex(r["key"]) + bytes.fromhex(r["digest"])).digest(), "little") % L
r["signature"] = (r_bytes + ((k * a) % L).to_bytes(32, "little")).hex()
return edit
add("seal-r-noncanonical", rx(run, equation_holds(alias)), ["bad_signature"], "R spelt with y of p or more")
add("seal-r-small-order", rx(run, equation_holds(IDENTITY)), ["bad_signature"], "R the identity, S made so that the equation holds: a lax check takes it")
add("seal-signature-other-key", rx(run, lambda r: r.update(signature=sign(OTHER, bytes.fromhex(r["digest"])).hex())), ["bad_signature"], "a signature by another key")
stale = copy.deepcopy(run)
stale["result"]["expectations"][0]["value"] = 0.5
add("seal-digest-stale", stale, ["digest_mismatch"], "the result changed after it was sealed: the digest alone")
add("seal-digest-other", rx(run, lambda r: r.update(digest=blake3(b"another job").hex())), ["bad_signature", "digest_mismatch"], "a digest of something else, the signature over the first")
# --- the first form
first = seal(run, form="first")
add("form-first", first, [], "a receipt of the first form, sealed over the compact text in the document's order")
exps = copy.deepcopy(run)
exps["result"]["x"] = [1e16, 1.5e300, 1e-7, 2.5e-6, 1e15, 0.00001]
add("form-first-exponents", seal(exps, form="first"), [], "a first-form receipt with numbers its writer wrote with an exponent, signed both ways (1e+16, 1e-7)")
big_first = copy.deepcopy(run)
big_first["request"]["seed"] = 2 ** 53 + 1
add("form-first-big-integer", seal(big_first, form="first"), [], "a first-form receipt sealing 2^53 + 1 by its digits, as its writer wrote it: not inexact")
past = copy.deepcopy(run)
past["request"]["seed"] = Raw("18446744073709551617", float(2 ** 64))
add("form-first-past-its-integers", seal(past, form="first"), ["inexact_number"], "a first-form receipt with 2^64 + 1 written: past the integers its writer kept by their digits, it sealed the double 2^64")
past["request"]["seed"] = Raw("18446744073709551616", float(2 ** 64))
add("form-first-past-its-integers-exact", seal(past, form="first"), [], "the same with 2^64 written, which the double holds")
unformed = rx(seal(big_first, form="first"), lambda r: r.update(alg="rsa-pss"))
unformed["request"]["seed"] = Raw("9007199254740993", 2 ** 53 + 1)
add("form-none-inexact", unformed, ["inexact_number", "receipt_form"], "a receipt with no canon and another alg names no form, so it is not of the first form, and 2^53 + 1 is inexact")
add("form-first-relabelled", rx(first, lambda r: r.update(canon="rfc8785")), ["digest_mismatch"], "a first-form receipt relabelled as canonical")
add("form-canonical-relabelled", rx(run, lambda r: r.pop("canon")), ["digest_mismatch"], "a canonical receipt relabelled as the first form")
reordered = copy.deepcopy(first)
keys = list(reordered["result"])
reordered["result"] = {k: reordered["result"][k] for k in reversed(keys[:-1])}
reordered["result"]["receipt"] = first["result"]["receipt"]
add("form-first-reordered", reordered, ["digest_mismatch"], "a first-form receipt with its result's members reordered")
respelt = copy.deepcopy(first)
respelt["result"]["expectations"][0]["value"] = Raw("1", 1)
add("form-first-respelt", respelt, ["digest_mismatch"], "a first-form receipt with 1.0 written 1, which its writer wrote 1.0")
# --- the canonical text: what any reader and writer may do changes nothing
spelt = copy.deepcopy(run)
spelt["request"] = dict(reversed(list(spelt["request"].items())))
spelt["request"]["shots"] = Raw("1.0e3", 1000)
spelt["result"]["expectations"][0]["value"] = Raw("1", 1)
t = dump(spelt) + "\n"
t = t.replace('"OPENQASM', '"\\u004fPENQASM', 1).replace('"qubits"', '"\\u0071ubits"', 1)
add("canon-respelt", t, [], "members reordered, numbers written otherwise (1.0e3, 1), names and strings with escapes")
strings = {"request": {"qasm": "OPENQASM 3.0;", "\U0001F600": "a key above the BMP", "": "sorts after it in UTF-16", "z": "tab\tnewline\ncontrol\u0001 slash/ del\u007f sep
 nonchar￿ quote\" backslash\\"},
"result": {"method": "statevector", "accuracy": {"class": "exact"}}}
add("canon-strings", seal(strings), [], "strings and names that RFC 8785 writes its own way: UTF-16 order, escapes, characters left as they are")
numbers = {"request": {"qasm": "OPENQASM 3.0;"},
"result": {"method": "statevector", "accuracy": {"class": "exact"},
"x": [1e21, 1e-7, 1e-6, 0.1 + 0.2, 5e-324, 1.7976931348623157e308, -0.0, 123456789012345680000.0, 2 ** 53, 2 ** 64, 0, -1, 1e300, 4.35, 0.000001234]}}
sealed = seal(numbers)
sealed["result"]["x"][1] = Raw("0.0000001", 1e-7)
sealed["result"]["x"][9] = Raw("18446744073709551616", 2 ** 64)
add("canon-numbers", sealed, [], "numbers at the edges of ECMAScript's layout, and 2^64, a whole number a double holds")
tiny = seal({"request": {"qasm": "OPENQASM 3.0;"}, "result": {"method": "statevector", "accuracy": {"class": "exact"}, "x": 0}})
tiny["result"]["x"] = Raw("1e-400", 0)
add("canon-underflow", tiny, [], "1e-400 reads as zero, as sealed")
near = seal({"request": {"qasm": "OPENQASM 3.0;"}, "result": {"method": "statevector", "accuracy": {"class": "exact"}, "x": 1.7976931348623157e308}})
near["result"]["x"] = Raw("1.7976931348623158e308", 1.7976931348623157e308)
add("canon-near-max", near, [], "a number just below the limit, which rounds to the largest double")
inexact = seal({"request": {"qasm": "OPENQASM 3.0;"}, "result": {"method": "statevector", "accuracy": {"class": "exact"}, "x": 2 ** 53}})
inexact["result"]["x"] = Raw("9007199254740993", 2 ** 53)
add("number-inexact", inexact, ["inexact_number"], "2^53 + 1 written, 2^53 sealed: the text says more than the seal")
big = seal({"request": {"qasm": "OPENQASM 3.0;", "seed": 2 ** 64}, "result": {"method": "statevector", "accuracy": {"class": "exact"}}})
big["request"]["seed"] = Raw("18446744073709551617", 2 ** 64)
add("number-inexact-unsealed", rx(run, lambda r: r.update(x=Raw("9007199254740993", 2 ** 53 + 1))), [], "2^53 + 1 in a member of the receipt, which nothing seals and a verifier ignores")
add("number-inexact-past-u64", big, ["inexact_number"], "2^64 + 1 in the request, 2^64 sealed")
# --- reading (section 8.2), each a text
base = dump(run) + "\n"
add("read-duplicate", base.replace('"qubits": ', '"qubits": 7,\n "qubits": ', 1), ["unreadable"], "a member of the result named twice")
add("read-duplicate-unsealed", base.replace('"status": "done"', '"status": "failed",\n "status": "done"', 1), ["unreadable"], "a member of the unsealed envelope named twice: still unreadable")
add("read-duplicate-escaped", base.replace('"qubits": ', '"qubit\\u0073": 7,\n "qubits": ', 1), ["unreadable"], "two names that are one once their escapes are read")
add("read-lone-surrogate", base.replace('"OPENQASM', '"\\ud800OPENQASM', 1), ["unreadable"], "an escaped high surrogate with no low one")
add("read-lone-low-surrogate", base.replace('"OPENQASM', '"\\udc00OPENQASM', 1), ["unreadable"], "an escaped low surrogate alone")
add("read-byte-order-mark", "" + base, ["unreadable"], "a byte order mark")
add("read-raw-control", base.replace('"OPENQASM', '"\tOPENQASM', 1), ["unreadable"], "a tab not escaped inside a string")
add("read-number-overflow", base.replace('"status": "done"', '"status": "done",\n "x": 1e400', 1), ["unreadable"], "a number no double reaches, in the unsealed envelope")
limit = "179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497792"
add("read-number-at-limit", base.replace('"status": "done"', '"status": "done",\n "x": ' + limit, 1), ["unreadable"], "2^1024 - 2^970 exactly, which rounds to infinity")
deep = {"request": {"qasm": "OPENQASM 3.0;", "nest": []}, "result": {"method": "statevector", "accuracy": {"class": "exact"}}}
n = deep["request"]["nest"]
for _ in range(124):
n.append([])
n = n[0]
add("read-depth-127", seal(deep), [], "arrays and objects 127 deep, the most a reader takes")
n.append([])
add("read-depth-128", seal(deep), ["unreadable"], "128 deep")
add("read-trailing", base + "{}\n", ["unreadable"], "a second value after the first")
add("read-not-json", "signature VALID\n", ["unreadable"], "not JSON")
add("read-not-utf8", base.encode("utf-8").replace(b"OPENQASM", b"\xffPENQASM", 1), ["unreadable"], "a byte that is not UTF-8")
add("job-array", "[]\n", ["not_a_job"], "a value that is not an object")
b = copy.deepcopy(run)
del b["request"]
add("job-no-request", b, ["not_a_job"], "no request")
b = copy.deepcopy(run)
b["request"] = "OPENQASM 3.0;"
add("job-request-string", b, ["not_a_job"], "a request that is not an object")
b = copy.deepcopy(run)
del b["result"]["receipt"]
add("job-no-receipt", b, ["no_receipt"], "a result with no receipt")
b = copy.deepcopy(run)
b.update(status="failed", method="a different method", why="anything at all", id="j-another")
add("envelope-changed", b, [], "the unsealed envelope changed: nothing a verifier takes")
# --- a provider's result (section 8.4), each sealed again so one rule breaks
def at(path, value):
def f(b):
o = b
for k in path[:-1]:
o = o[k]
o[path[-1]] = value
return f
def drop(path):
def f(b):
o = b
for k in path[:-1]:
o = o[k]
del o[path[-1]]
return f
R = ("result",)
add("provider-measured-simulator", edited(prov, at(R + ("accuracy", "class"), "measured")), ["provider_class"], "a simulator's stated result labelled measured")
add("provider-approximate-false", edited(prov, at(R + ("accuracy", "approximate"), False)), ["provider_class"], "approximate given and not true")
def silicon_approximate(b):
b["result"]["provider"]["provenance"] = "silicon"
b["result"]["accuracy"]["class"] = "measured"
b["result"]["accuracy"]["approximate"] = True
add("provider-approximate-processor", edited(prov, silicon_approximate), ["provider_class"], "a processor's counts labelled approximate")
add("provider-provenance-unknown", edited(prov, at(R + ("provider", "provenance"), "fpga")), ["provider_class", "provider_members"], "a provenance that is none of silicon, simulator or emulator")
add("provider-approximate-simulator", edited(prov, at(R + ("accuracy", "approximate"), True)), [], "a simulator's result its provider states is approximate")
add("provider-request-provider", edited(prov, at(("request", "provider"), "another")), ["request_provider"], "the request names another provider")
add("provider-request-shots", edited(prov, at(("request", "shots"), 301)), ["request_shots"], "the request asks for other shots")
add("provider-request-shots-default", edited(prov, drop(("request", "shots"))), ["request_shots"], "the request gives no shots (1024), the result others")
def recount(f):
def g(b):
f(b["result"])
return g
add("provider-counts-sum", edited(prov, recount(lambda r: r["counts"].update({k: v + 1 for k, v in list(r["counts"].items())[:1]}))), ["counts"], "counts that do not sum to the shots")
def wider(r):
k, v = next(iter(r["counts"].items()))
del r["counts"][k]
r["counts"]["0" + k] = v
add("provider-counts-width", edited(prov, recount(wider)), ["counts"], "outcomes of two lengths")
add("provider-counts-zero", edited(prov, recount(lambda r: r["counts"].update({"01": 0}))), ["counts"], "an outcome listed with a count of 0")
def hidden(r):
k, v = next(iter(r["counts"].items()))
r["counts"][k] = 1
r["omitted_shots"] = v - 1
# Section 8.4: only a count's value has a ceiling, and a sum is exact.
add("provider-whole-past-safe", edited(prov, at(R + ("job", "polls"), 2 ** 53)), [], "2^53 polls: a whole number has no ceiling but a count's")
add("provider-whole-spelt-large", edited(prov, at(R + ("provider", "qubits"), 1e30)), [], "1e30 qubits: whole, and absurd, and no rule's to refuse")
def past_safe(count):
def edit(r):
r["request"]["shots"] = 2 ** 53
r["result"]["shots"] = 2 ** 53
r["result"]["counts"] = {"00": count, "11": 2 ** 53 - count} if count < 2 ** 53 else {"00": count}
r["result"]["distinct_outcomes"] = len(r["result"]["counts"])
r["result"]["omitted_shots"] = 0
return edit
add("provider-shots-past-safe", edited(prov, past_safe(2 ** 53 - 1)), [], "2^53 shots, counted within a count's ceiling")
add("provider-count-past-safe", edited(prov, past_safe(2 ** 53)), ["counts"], "a count of 2^53, past its ceiling")
def same_strings(r):
r["request"]["shots"] = "300"
r["result"]["shots"] = "300"
add("provider-shots-same-strings", edited(prov, same_strings), ["counts"], "equal strings are the request's shots, and a string is no shots")
add("provider-counts-hidden", edited(prov, recount(hidden)), ["counts"], "shots left out while fewer than 65,536 outcomes are listed")
add("provider-counts-distinct", edited(prov, recount(lambda r: r.update(distinct_outcomes=r["distinct_outcomes"] + 1))), ["counts"], "distinct_outcomes not the outcomes listed")
# Section 8.1: the shots a device kept, where it kept another number than asked (a
# post-selected readout discards some): the counts are of those.
def kept(n, accepted=None):
def edit(r):
first = next(iter(r["counts"]))
r["counts"][first] -= n
r["accepted_shots"] = r["shots"] - n if accepted is None else accepted
return edit
add("provider-accepted-shots", edited(sampler, recount(kept(27))), [], "a processor that kept 27 fewer shots than asked, its counts of those it kept (a post-selected readout)")
add("provider-accepted-shots-not-the-counts", edited(sampler, recount(kept(27, sampler["result"]["shots"] - 26))), ["counts"], "accepted_shots one more than the counts' sum")
add("provider-accepted-shots-counts-of-every-shot", edited(sampler, recount(lambda r: r.update(accepted_shots=r["shots"] - 1))), ["counts"], "counts of every shot asked beside an accepted_shots of one fewer")
add("provider-accepted-shots-null", edited(sampler, recount(lambda r: r.update(accepted_shots=None))), ["counts"], "accepted_shots null: present, and no whole number")
add("provider-accepted-shots-more", edited(sampler, recount(lambda r: (r["counts"].update({next(iter(r["counts"])): r["counts"][next(iter(r["counts"]))] + 24}), r.update(accepted_shots=r["shots"] + 24)))), [], "a device that ran 24 more shots than asked, its counts of all it ran")
add("provider-members-describe", edited(prov, at(R + ("provider", "describe"), prov["result"]["provider"]["describe"].upper())), ["provider_members"], "the describe hash in upper case")
add("provider-members-no-qasm3", edited(prov, drop(R + ("compiled", "qasm3"))), ["provider_members"], "no compiled program's text")
add("provider-members-energy", edited(prov, at(R + ("energy",), {"joules": 1})), ["provider_members"], "energy as a figure measured here")
add("provider-members-kwh-unlabelled", edited(prov, at(R + ("energy", "service_kwh"), 0.001)), ["provider_members"], "the provider's energy figure not labelled as its own")
add("provider-members-no-schema", edited(prov, drop(R + ("schema",))), ["provider_members"], "no schema")
add("provider-members-no-charged", edited(prov, drop(R + ("cost", "charged"))), ["provider_members"], "no charged")
add("provider-members-basis", edited(prov, at(R + ("provider", "basis"), "gpizz")), ["provider_members"], "the provider's basis not the program's")
add("provider-counts-source-other", edited(prov, at(R + ("counts_source",), "estimated")), ["counts_source"], "a counts_source of no value")
add("provider-execution-default", edited(prov, at(R + ("execution", "twirling"), "default")), ["execution"], "a provider saying default, which only a client says")
add("provider-execution-verbatim-remapped", edited(prov, at(R + ("execution", "remapping"), "on")), ["execution"], "run as written and remapped")
add("provider-execution-sent", edited(prov, at(R + ("execution", "sent"), {"fields": [], "settings": {}})), ["execution"], "sent, which only a client states")
add("provider-execution-member", edited(prov, at(R + ("execution", "routing"), "off")), ["execution"], "a member execution does not have")
nobody = {"stated_by": "nobody", "mitigation": "unknown", "twirling": "unknown", "decoupling": "unknown", "remapping": "unknown", "optimisation": "unknown", "verbatim": None}
add("provider-execution-nobody", edited(prov, at(R + ("execution",), nobody)), [], "a provider that states nothing: no one's word")
client = {"stated_by": "client", "sent": {"fields": ["op", "program", "protocol", "shots"], "settings": {}}, "mitigation": "default", "twirling": "default", "decoupling": "default", "remapping": "default", "optimisation": "default", "verbatim": False}
add("provider-execution-client-open", edited(prov, at(R + ("execution",), client)), ["execution"], "the open protocol's client claiming to state what was set")
add("provider-compiled-program", edited(prov, at(R + ("compiled", "qasm3"), prov["result"]["compiled"]["qasm3"] + "// another program\n")), ["compiled_program"], "a program's text that is not the text its hash names")
add("provider-cost-free-confirmed", edited(prov, at(R + ("cost", "confirmed"), True)), ["cost_confirmation"], "a free run that claims a confirmation")
add("provider-cost-unconfirmed", edited(prov, at(R + ("cost", "free"), False)), ["cost_confirmation"], "a run that was not free, with no confirmation")
# Section 8.1: a provider's own record takes the job's submit as the confirmation of a run
# that is not free, and says so; of no other run, and in no other words.
add("provider-cost-confirmed-by-submit", edited(priced, at(R + ("cost", "confirmed_by"), "submit")), [], "a provider's own record of a run that was not free: it took the submit as the confirmation")
add("provider-cost-confirmed-by-free", edited(prov, at(R + ("cost", "confirmed_by"), "submit")), ["cost_confirmation"], "a free run that says a submit confirmed it")
add("provider-cost-confirmed-by-other", edited(priced, at(R + ("cost", "confirmed_by"), "person")), ["cost_confirmation"], "confirmed_by of no value")
add("provider-cost-confirmed-by-null", edited(priced, at(R + ("cost", "confirmed_by"), None)), ["cost_confirmation"], "confirmed_by present and null")
add("provider-stated-by-a-null", edited(legacy, at(R + ("counts_source",), None)), ["counts_source", "execution", "provider_class", "provider_members"], "an earlier-form result given counts_source null: stated by its members, whatever their values")
add("earlier-request-shots", edited(legacy, at(("request", "shots"), 65)), ["request_shots"], "an earlier-form result whose request asks other shots")
add("earlier-approximate", edited(legacy, at(R + ("accuracy", "approximate"), True)), ["provider_class"], "approximate on an earlier-form result")
add("earlier-simulated", edited(legacy, at(R + ("accuracy", "class"), "simulated")), ["provider_class"], "the earlier form labelled simulated, which no earlier engine wrote")
# --- what each built-in kind records (section 8.3)
add("kind-sampler-twirling-default", edited(sampler, at(R + ("execution", "twirling"), "default")), ["execution"], "sampler-v2 not stating the twirling it sends off")
add("kind-prx-shots", edited(prx, at(R + ("counts_source",), "shots")), ["counts_source"], "prx-circuits claiming shot records it does not read")
add("kind-prx-twirling-off", edited(prx, at(R + ("execution", "twirling"), "off")), ["execution"], "prx-circuits claiming a setting it does not send")
add("kind-turns-shots", edited(turns, at(R + ("counts_source",), "shots")), ["counts_source"], "native-turns claiming shots: its counts are always rebuilt from probabilities")
add("kind-tasks-vendor-counts", edited(qpu, at(R + ("counts_source",), "vendor_counts")), ["counts_source"], "cloud-tasks claiming the provider's own counts")
add("kind-tasks-simulator-boxed", edited(sim, at(R + ("execution",), {k: v for k, v in qpu["result"]["execution"].items()})), ["execution"], "a simulator's task stating the processor's verbatim box")
def nobody(r):
r["result"]["execution"] = dict({k: "unknown" for k in ("mitigation", "twirling", "decoupling", "remapping", "optimisation")}, stated_by="nobody", verbatim=None)
r["result"]["compiled"].pop("qubit_names")
# Section 8.4: compiled.qubit_names.
add("kind-tasks-no-names", edited(qpu, drop(R + ("compiled", "qubit_names"))), ["provider_members"], "cloud-tasks, stated by its client, not saying which of the device's qubits its indices were")
add("kind-prx-no-names", edited(prx, drop(R + ("compiled", "qubit_names"))), ["provider_members"], "prx-circuits, stated by its client, not saying which of the device's qubits its indices were")
add("kind-tasks-names-null", edited(qpu, at(R + ("compiled", "qubit_names"), None)), ["provider_members"], "null is given, and is no object")
add("kind-tasks-name-leading-zero", edited(qpu, at(R + ("compiled", "qubit_names"), dict(qpu["result"]["compiled"]["qubit_names"], **{"01": "x"}))), ["provider_members"], "an index with a leading zero")
add("kind-sampler-names-short", edited(sampler, at(R + ("compiled", "qubit_names"), {})), ["provider_members"], "names given where they are optional, and not of every index of the layouts")
add("kind-prx-nobody", edited(prx, nobody), [], "a job recorded before its client said what it sent: stated by nobody, on a kind with rows")
add("kind-tasks-processor-unboxed", edited(qpu, at(R + ("execution", "verbatim"), False)), ["execution"], "a processor's task not stating its verbatim box")
# Section 8.4: every index qasm3 acts on is named.
def acts_on_one_more(r):
c = r["result"]["compiled"]
q = max(int(k) for k in c["qubit_names"]) + 1
c["qasm3"] = c["qasm3"] + "x $%d;\n" % q
c["program"] = blake3(c["qasm3"].encode("utf-8")).hex()
add("kind-tasks-names-short-of-text", edited(qpu, acts_on_one_more), ["provider_members"], "qasm3 acts on a qubit its names do not name, outside both layouts")
# --- a whole double past 2^53, as RFC 8785 writes it (section 8.2)
def big_job(spelling):
# Each spelling stands for the double it reads as (section 8.2).
return {"request": {"qasm": "OPENQASM 3.0;", "observables": [{"pauli": "Z0", "coeff": Raw(spelling, float(spelling))}]},
"result": {"method": "statevector", "accuracy": {"class": "exact"}, "x": Raw(spelling, float(spelling))}}
add("number-canonical-spelling", seal(big_job("1152921504606847000")), [], "2^60 written as RFC 8785 writes it, its shortest digits and zeros: the double's own spelling")
add("number-exact-spelling", seal(big_job("1152921504606846976")), [], "2^60 written by its exact digits")
add("number-exponent-spelling", seal(big_job("1.152921504606847e+18")), [], "2^60 written with an exponent, as Python writes it")
add("number-near-canonical", seal(big_job("1152921504606847001")), ["inexact_number"], "digits one past the double's canonical text: another spelling of an inexact whole number")
add("number-padded-past", seal(big_job("1152921504606847000000")), ["inexact_number"], "the digits padded past 10^21, where RFC 8785 writes an exponent: not its spelling")
# --- sets (section 8.6)
S = ("result",)
add("set-error-item", seal_set(edited_set(progs, lambda r: (r["items"][1].pop("result"), r["items"][1].update(error="it did not run")))), [], "a set whose second item failed: its error is its leaf")
add("set-item-changed", seal_set(edited_set(progs, lambda r: r["items"][0]["result"]["counts"].update({"00": r["items"][0]["result"]["counts"].get("00", 0) + 1}))), ["set_item"], "an item's counts changed after it was sealed, the set sealed again over it")
add("set-root-other", edited_set_head(progs, lambda r: r.update(root="ab" * 32)), ["set_root"], "a root that is no tree's of these items, sealed by the set")
add("set-path-other", edited_set(progs, lambda r: r["items"][0]["path"].__setitem__(0, "cd" * 32)), ["set_root"], "an item's path changed: a path is not sealed, and must lead to the root")
add("set-count-other", edited_set_head(progs, lambda r: r.update(count=r["count"] + 1)), ["set_items"], "a count other than the items'")
add("set-index-swapped", edited_set(progs, lambda r: (r["items"][0].update(index=1), r["items"][1].update(index=0))), ["set_items"], "items whose index is not their place")
def other_shots(r):
it = r["items"][1]
it["request"]["shots"] = 65
sealed = seal({"request": it["request"], "result": it["result"]})
it["result"] = sealed["result"]
add("set-item-not-given", seal_set(edited_set(progs, other_shots)), ["set_items"], "an item sealed alone, and not the item the request's programs give")
add("set-head-changed", edited_set(progs, lambda r: r.update(elapsed_ms=r["elapsed_ms"] + 1)), ["digest_mismatch"], "the head changed after the set was sealed")
def failed_inexact(r):
it = r["items"][1]
it.pop("result")
it["error"] = "it did not run"
it["request"]["seed"] = Raw("9007199254740993", 2 ** 53 + 1)
add("set-error-item-inexact", seal_set(edited_set(progs, failed_inexact)), ["set_item", "set_items"], "a failed item whose request writes a number its leaf seals as another: no item's number escapes")
def first_form_set(body):
"""A set sealed in the first form: its receipt over its request and head by its writer's text."""
b = copy.deepcopy(body)
head = {k: v for k, v in b["result"].items() if k not in ("items", "receipt")}
d = digest(b["request"], head, form="first")
b["result"]["receipt"] = {"alg": "ed25519", "key": public_key(DEMO).hex(), "digest": d.hex(), "signature": sign(DEMO, d).hex()}
return b
def failed_by_digits(r):
it = r["items"][1]
it.pop("result")
it["error"] = "it did not run"
failed = seal_set(edited_set(progs, failed_by_digits))
add("set-first-form-error-item", first_form_set(failed), [], "a set of the first form, an item failed: valid")
by_digits = copy.deepcopy(failed)
by_digits["result"]["items"][1]["request"]["seed"] = Raw("9007199254740993", float(2 ** 53 + 1))
add("set-first-form-error-item-inexact", first_form_set(seal_set(by_digits)), ["set_item", "set_items"], "a set of the first form whose failed item writes 2^53 + 1 by its digits: its leaf, the canonical form's digest, seals the double")
def stranger(r):
it = r["items"][0]
it["result"] = seal({"request": it["request"], "result": it["result"]}, seed=OTHER)["result"]
add("set-item-other-key", seal_set(edited_set(progs, stranger)), ["set_item"], "an item sealed by a key other than the set's")
add("set-empty", edited_set_head(progs, lambda r: (r["items"].clear(), r.update(count=0, root="00" * 32))), ["set_items"], "a set of no items, its head sealed so")
add("set-sweep-short", seal_set(edited_set(sweep, lambda r: (r["items"].pop(), r.update(count=len(r["items"]))))), ["set_items"], "a sweep missing its last point, counted and sealed so")
big_sweep = copy.deepcopy(sweep)
big_sweep["request"]["sweep"]["theta"] = [Raw("9007199254740993", 2 ** 53 + 1)] + big_sweep["request"]["sweep"]["theta"][1:]
add("set-sweep-inexact", seal_set(big_sweep), ["inexact_number"], "a sweep's value no double holds, in the set's own request")
# --- another method claims a provider's class
add("method-measured", edited(run, at(R + ("accuracy", "class"), "measured")), ["provider_class"], "the engine's own result labelled measured")
add("method-simulated", edited(run, at(R + ("accuracy", "class"), "simulated")), ["provider_class"], "the engine's own result labelled simulated")
add("method-class-unknown", edited(run, at(R + ("accuracy", "class"), "quasi-exact")), [], "a class a verifier does not know: not a reason, since a later version may add one")
# --- a wasmQ job (quantum-wasmq section 6.5): each sealed again, one rule broken
wrun = load("cli-wasmq-run.json")
wfault = load("cli-wasmq-fault.json")
wjob = load("cli-wasmq-state-job.json")
C = ("request", "wasmq")
def fewer(k, by=1):
def f(b):
counts = b["result"]["counts"]
most = max(counts, key=lambda o: counts[o])
counts[most] -= by
if k:
b["result"]["shots_run"] -= by
return f
def both(name, value):
def f(b):
b["request"]["wasmq"]["run"][name] = value
b["result"][name] = value
return f
def budget(name, value):
def f(b):
b["request"]["wasmq"]["run"]["fuel"][name] = value
b["result"]["fuel"]["budget"][name] = value
return f
add("wasmq-counts-short", edited(wrun, fewer(False)), ["wasmq_form"], "a wasmQ run whose counts add up to one shot fewer than it ran (rule 19)")
add("wasmq-ok-before-all-shots", edited(wrun, fewer(True)), ["wasmq_form"], "a wasmQ run that is ok after 31 of its 32 shots (rule 18)")
add("wasmq-fuel-unaccounted", edited(wrun, lambda b: b["result"]["fuel"].update(instances=b["result"]["fuel"]["instances"] + 1)), ["wasmq_form"], "a wasmQ run whose instances and calls do not add up to the fuel it used (rule 15)")
add("wasmq-fuel-past-job-budget", edited(wfault, budget("per_job", 100)), ["wasmq_form"], "a wasmQ fault that used 400 fuel under a run's budget of 100 (rule 13)")
add("wasmq-instances-past-budget", edited(wjob, budget("per_instance", 8000)), ["wasmq_form"], "one instance for the whole run, given more fuel for its seed, init and prior than three stretches of 8000 (rule 17)")
add("wasmq-fault-unnamed", edited(wfault, lambda b: (b["result"]["fault"].update(reason="out_of_fuel"), b["result"]["fault"].pop("budget"))), ["wasmq_form"], "a wasmQ fault of a reason the profile does not name (rule 18)")
add("wasmq-result-member-added", edited(wrun, at(R + ("note",), "a member the profile does not write")), ["wasmq_form"], "a wasmQ result with a member the profile does not write (rule 9)")
add("wasmq-contract-method", edited(wrun, both("method", "statevector")), ["wasmq_form"], "a wasmQ contract and result of a method the profile does not have (rule 8)")
add("wasmq-seed-past-exact", edited(wrun, both("seed", 2 ** 53)), ["wasmq_form"], "a wasmQ seed of 2^53: a double holds it, so it is no inexact number, and the profile allows at most 2^53 - 1 (rules 1, 8)")
add("wasmq-modules-other", edited(wrun, lambda b: b["result"]["modules"][0].update(blake3="ab" * 32)), ["wasmq_form"], "a wasmQ result naming a module the contract does not pin (rule 16)")
add("wasmq-transcript-upper", edited(wrun, lambda b: b["result"].update(transcript_blake3=b["result"]["transcript_blake3"].upper())), ["wasmq_form"], "a wasmQ transcript digest in upper case (rule 12)")
add("wasmq-first-form", seal(wrun, form="first"), ["wasmq_form"], "a wasmQ job sealed in the first form, older than the profile: the seal holds, and no host wrote it (rule 10)")
def uncounted(b):
b["result"].update(callouts=0)
b["result"]["fuel"].update(callouts={}, instances=400)
add("wasmq-call-fault-uncounted", edited(wfault, uncounted), ["wasmq_form"], "a call that stopped the run, which the fuel counts no call of: the stopped call is one of the calls made (rule 18)")
def past_largest(b):
b["result"].update(callouts=2)
b["result"]["fuel"]["callouts"]["f"].update(calls=2, fuel=401, max=399)
b["result"]["fuel"]["used"] = 401
add("wasmq-call-fault-past-largest", edited(wfault, past_largest), ["wasmq_form"], "a call that stopped after 400 fuel, where the most one call of it used is 399 (rule 18)")
# Each rule the corpus held no receipt for, broken alone (rule 1 cannot be: every place a
# number may be has a rule of its own that holds it within 2^53 - 1).
wrunf = load("cli-wasmq-fault-run.json")
winit = load("cli-wasmq-fault-init.json")
wprior = load("cli-wasmq-fault-prior.json")
def each(*edits):
def f(b):
for e in edits:
e(b)
return f
def later_shot(shot, state=None, instances=1, fuel=1, callouts=None):
# The init fault moved to `shot`, after that many shots of one call each.
def f(b):
n = shot if callouts is None else callouts
if state:
b["request"]["wasmq"]["run"]["state"] = state
b["result"].update(shots_run=shot, counts={"0": shot} if shot else {}, callouts=n)
b["result"]["fuel"].update(instances=instances, callouts={"f": {"calls": n, "fuel": n, "max": 1}} if n else {}, used=instances + n)
b["result"]["fault"].update(shot=shot, fuel=fuel)
return f
def second_module(fault_module):
def f(b):
c = b["request"]["wasmq"]
c["modules"].append({"name": "m2", "blake3": "cd" * 32, "bytes": 57, "priors": []})
c["callouts"].append({"extern": "g", "module": 1, "export": "g"})
b["result"]["modules"].append({"name": "m2", "blake3": "cd" * 32})
b["result"]["fault"]["module"] = fault_module
return f
def shots(n):
def f(b):
b["request"]["wasmq"]["run"]["shots"] = b["result"]["shots"] = n
return f
add("wasmq-request-member-added", edited(wrun, at(("request", "x"), 1)), ["wasmq_form"], "a wasmQ request with a member the profile does not write (rule 2)")
add("wasmq-contract-member-added", edited(wrun, at(C + ("x",), 1)), ["wasmq_form"], "a wasmQ contract with a member section 2.2 does not list (rule 3)")
add("wasmq-contract-profile", edited(wrun, at(C + ("profile",), "wai.quantum.wasmq/2")), ["wasmq_form"], "a wasmQ contract of another profile (rule 4)")
add("wasmq-program-empty", edited(wrun, at(C + ("program", "bytes"), 0)), ["wasmq_form"], "a wasmQ contract whose program is 0 bytes long (rule 5)")
add("wasmq-module-empty", edited(wrun, lambda b: b["request"]["wasmq"]["modules"][0].update(bytes=0)), ["wasmq_form"], "a wasmQ contract whose module is 0 bytes long (rule 6)")
add("wasmq-callouts-twice", edited(wrun, lambda b: b["request"]["wasmq"]["callouts"].append({"extern": "decode64", "module": 0, "export": "x"})), ["wasmq_form"], "a wasmQ contract with two callouts of one extern (rule 7)")
add("wasmq-result-seed", edited(wrun, lambda b: b["result"].update(seed=b["result"]["seed"] + 1)), ["wasmq_form"], "a wasmQ result whose seed is not its contract's (rule 11)")
add("wasmq-call-past-per-call", edited(wrun, budget("per_call", 21750)), ["wasmq_form"], "a wasmQ run whose largest call used 21751 fuel under a budget of 21750 a call (rule 14)")
add("wasmq-calls-of-no-fuel", edited(wrun, lambda b: (b["result"]["fuel"]["callouts"]["decode64"].update(fuel=0, max=0), b["result"]["fuel"].update(used=b["result"]["fuel"]["instances"]))), ["wasmq_form"], "32 calls that returned having used no fuel, where entering a function weighs 1 (rule 20)")
add("wasmq-priors-unpassed", edited(wrun, lambda b: b["result"]["fuel"].update(instances=32 * 90 - 1, used=32 * 90 - 1 + 588348)), ["wasmq_form"], "32 shots of instances passed a prior of 87 bytes in less fuel than its bytes and its two calls take, cabi_realloc 2 at least and wasmq-prior 1 (rule 21)")
add("wasmq-call-fault-other-module", edited(wfault, second_module("m2")), ["wasmq_form"], "a call of a callout in the first module, whose fault names the second (rule 22)")
add("wasmq-prior-fault-no-prior", edited(winit, at(R + ("fault", "phase"), "prior")), ["wasmq_form"], "a fault while a prior is passed, in a module with no prior (rule 23)")
add("wasmq-bad-result-in-init", edited(winit, each(at(R + ("fault", "reason"), "bad_result"), at(R + ("fault", "fuel"), 2), at(R + ("fuel", "instances"), 2), at(R + ("fuel", "used"), 2))), ["wasmq_form"], "a bad_result from an init, which takes no result (rule 24)")
add("wasmq-lifecycle-fuel-past-instances", edited(winit, later_shot(1, instances=1, fuel=2)), ["wasmq_form"], "an init fault in shot 1 that used 2 fuel, where the instances used 1, shot 0's init 1 of it at least (rule 25)")
add("wasmq-job-state-later-instance", edited(winit, later_shot(2, state="job", instances=2)), ["wasmq_form"], "a run of one instance for the run whose instance fails in shot 2 (rule 26)")
add("wasmq-first-instance-after-call", edited(winit, later_shot(0, callouts=1)), ["wasmq_form"], "the first shot's instance failing after a call was made, where no shot ran its steps (rules 27 and 32)")
add("wasmq-fuel-fault-under-limit", edited(wfault, lambda b: (b["result"]["fuel"]["callouts"]["f"].update(fuel=10, max=10), b["result"]["fuel"].update(used=10), b["result"]["fault"].update(fuel=10))), ["wasmq_form"], "a call's fuel fault after 10 fuel, under its limit of 400 (rule 28)")
add("wasmq-run-budget-not-spent", edited(wrunf, budget("per_job", 60)), ["wasmq_form"], "a fault of the run's budget with 50 of its 60 used (rule 28)")
add("wasmq-results-past-bits", edited(wrunf, each(shots(2 ** 20), lambda b: b["result"].update(counts={"0" * 257: 2}))), ["wasmq_form"], "2^20 shots of 257 bits, past the 2^28 bits a run's results hold (rule 29)")
add("wasmq-work-past-bound", edited(wrun, budget("per_job", 2 ** 39)), ["wasmq_form"], "a run given 2^39 fuel beside a module's code, past the 2^42 operations a run asks (rule 30)")
add("wasmq-frames-past-envelope", edited(wprior, lambda b: b["request"]["wasmq"]["modules"][0]["priors"][0].update(bytes=2 ** 27)), ["wasmq_form"], "a prior of 2^27 bytes beside a program and a module, more than an envelope holds (rule 31)")
add("wasmq-fault-null", edited(wrun, at(R + ("fault",), None)), ["wasmq_form"], "an ok result with a fault member whose value is null: a member is present whatever its value (rule 18)")
# Round 5's rules, each broken alone.
add("wasmq-no-qubit", edited(wrun, at(R + ("qubits",), 0)), ["wasmq_form"], "a wasmQ result of a program of no qubit, which no program a reader reads is (rule 12)")
def calls_at_their_largest(b):
b["result"]["fuel"]["callouts"]["decode64"].update(fuel=21751)
b["result"]["fuel"]["used"] = b["result"]["fuel"]["instances"] + 21751
add("wasmq-calls-under-largest", edited(wrun, calls_at_their_largest), ["wasmq_form"], "32 calls in 21751 fuel, where the most one of them used is 21751 and each other used 1 at least (rule 20)")
def calls_past_steps(b):
n = 32 * 2 ** 21 + 1
b["result"]["callouts"] = n
b["result"]["fuel"]["callouts"]["decode64"].update(calls=n, fuel=n, max=1)
b["result"]["fuel"]["used"] = b["result"]["fuel"]["instances"] + n
add("wasmq-calls-past-steps", edited(wrun, calls_past_steps), ["wasmq_form"], "32 shots that made 2^21 calls each and one more, where a shot runs at most 2^21 steps (rule 32)")
add("wasmq-prior-past-instance-budget", edited(wrun, each(budget("per_instance", 89), lambda b: b["result"]["fuel"].update(instances=32 * 90, used=32 * 90 + 588348))), ["wasmq_form"], "a prior of 87 bytes passed whole in one stretch, which takes 90 at least, under a lifecycle call's budget of 89 (rule 33)")
add("wasmq-trap-of-no-fuel", edited(winit, at(R + ("fault", "fuel"), 0)), ["wasmq_form"], "an init that trapped after no fuel, where its entry is charged before it runs (rule 34)")
add("wasmq-transcript-of-no-call", edited(winit, at(R + ("transcript_blake3",), "ab" * 32)), ["wasmq_form"], "a fault before any call whose transcript is not the run's end alone (rule 35)")
respelt = copy.deepcopy(wrun)
respelt["request"]["wasmq"]["run"]["shots"] = Raw("32.0", 32)
respelt["result"]["shots"] = Raw("3.2e1", 32)
add("wasmq-respelt", (dump(respelt) + "\n").replace('"state": ', '"\\u0073tate": ', 1), [], "a wasmQ job with its shots written 32.0 and 3.2e1 and a member of its contract named with an escape: the same sealed content")
moved = copy.deepcopy(wfault)
moved.update(status="done", method="statevector", id="j-another")
add("wasmq-envelope-changed", moved, [], "a wasmQ fault whose unsealed body says done, another method and an id: a note beside the verdict, not a reason")
# The second version (quantum-wasmq section 11.6), each sealed again: the rules it reads
# with its own names, and rule 36, its energy; and the unmetered form, which keeps them.
w2 = load("cli-wasmq2-t-toffoli.json")
w2s = load("cli-wasmq2-stabilizer.json")
E2 = ("result", "energy")
first_callout = lambda b: sorted(b["result"]["fuel"]["callouts"])[0]
add("wasmq2-unmetered", edited(w2, at(E2 + ("run",), {"unmetered": "the runner read no energy meter for this run"})), [], "a second-version run whose energy is the unmetered form with its reason: a figure (section 11.5)")
add("wasmq2-reading-of-zero", edited(w2, at(E2 + ("run",), {"joules_micro": 0, "energy_provenance": "OnChipCounter", "energy_uncertainty": {"relative_ppm": 1, "window_us": 1}})), ["wasmq_form"], "a second-version run whose energy reads zero joules: a run that read nothing is unmetered (rule 36)")
add("wasmq2-unmetered-saying-nothing", edited(w2, at(E2 + ("run",), {"unmetered": ""})), ["wasmq_form"], "a second-version run unmetered with no reason (rule 36)")
add("wasmq2-estimate-as-reading", edited(w2, at(E2 + ("run",), {"joules_micro": 1200, "energy_provenance": "Estimator", "energy_uncertainty": {"relative_ppm": 241, "window_us": 9000}})), ["wasmq_form"], "a second-version run whose figure is an estimate, which is no reading (rule 36)")
add("wasmq2-below-the-floor", edited(w2, at(E2 + ("run",), {"joules_micro": 1200, "energy_provenance": "OnChipCounter", "energy_uncertainty": {"relative_ppm": 240, "window_us": 9000}})), ["wasmq_form"], "a reading of 1200 microjoules declared at 240 ppm, below the 288 676 its rounding gives (rule 36)")
add("wasmq2-callout-without-figure", edited(w2, lambda b: b["result"]["energy"]["callouts"].pop(first_callout(b))), ["wasmq_form"], "a second-version run with no figure for a callout it called (rule 36)")
add("wasmq2-first-version-energy", edited(w2, at(E2, {"unmetered": "the runner reads no energy meter; fuel is the profile's measure of work"})), ["wasmq_form"], "a second-version run with the first version's energy (rule 36)")
add("wasmq2-profile-first", edited(w2, at(C + ("profile",), "wai.quantum.wasmq/1")), ["wasmq_form"], "a second-version result under a first-version contract (rule 4)")
add("wasmq2-method-neither", edited(w2, lambda b: (b["request"]["wasmq"]["run"].update(method="statevector"), b["result"].update(method="statevector"))), ["wasmq_form"], "a second-version run of a method neither version has (rule 8)")
add("wasmq2-accuracy-of-the-other-method", edited(w2, at(R + ("accuracy",), {"class": "exact"})), ["wasmq_form"], "an exact run that states the tableau's accuracy (rule 11)")
add("wasmq2-exact-past-26-qubits", edited(w2, at(R + ("qubits",), 27)), ["wasmq_form"], "the exact method on 27 qubits, past the 26 its state holds (rule 12)")
add("wasmq2-stabilizer-at-27-qubits", edited(w2s, at(R + ("qubits",), 27)), [], "the stabilizer method on 27 qubits: the exact method's bound is its own")
out.extend(contract_cases(run, at, drop))
return out
def contract_cases(run, at, drop):
"""Section 8.7: a job under a contract, each rule broken once and sealed again, and
what a host may state and still be valid. Each expectation is written from 8.7."""
local = load("cli-contract-local.json")
auto = load("cli-contract-auto.json")
routed = load("cli-contract-routed.json")
named = load("cli-contract-provider-priced.json")
out = []
def add(name, body, reasons, what):
out.append(("crafted-contract-" + name + ".json", dump(body) + "\n", sorted(reasons), what))
R, P = ("result",), ("result", "planner")
def route(name, f):
def g(b):
f(next(r for r in b["result"]["planner"]["routes"] if r["route"] == name))
return g
def both(*fs):
def g(b):
for f in fs:
f(b)
return g
def met(r):
r["outcome"] = "met"
r.pop("rejected", None)
# --- the contract and the reasoning's form
add("form-member", edited(local, at(("request", "contract", "deadline"), 1)), ["contract_form"], "a member no contract has: read as absent, a requirement would drop in silence")
add("form-exactness", edited(local, at(("request", "contract", "exactness"), "nearly")), ["contract_form"], "an exactness that is no class")
add("form-cost", edited(local, at(("request", "contract", "max_cost", "amount"), "1e3")), ["contract_form"], "a ceiling's amount that is not in decimals")
add("form-cost-long", edited(local, at(("request", "contract", "max_cost", "amount"), "1" * 65)), ["contract_form"], "a ceiling's amount of 65 characters: at most 64")
add("planner-missing", edited(local, drop(P)), ["planner_form"], "a contract job sealed without its reasoning")
add("planner-format", edited(local, at(P + ("format",), "wai.quantum.planner/2")), ["planner_form"], "a reasoning of another format")
add("planner-wanted", edited(local, at(P + ("wanted",), "heuristic")), ["planner_form"], "a class wanted that is not the contract's")
add("planner-wanted-from", edited(auto, at(P + ("wanted_from",), "contract")), ["planner_form"], "a class said to be the contract's, where the contract names none and the job keeps auto's")
add("planner-on-plain", edited(run, at(R + ("planner",), local["result"]["planner"])), ["planner_form"], "a job with no contract that carries a reasoning")
add("shadow-on-plain", edited(run, at(R + ("shadow",), {"ran": False, "code": "none", "why": "no provider answered"})), ["shadow"], "a job with no contract that carries a shadow")
# --- each route's outcome and the choice, recomputed
add("route-claimed", edited(local, route("method:mps", met)), ["planner_route"], "a heuristic route said to meet exact")
add("route-code", edited(local, route("method:mps", lambda r: r["rejected"].update(code="deadline"))), ["planner_route"], "a route rejected for a reason the rules do not give it")
def dearer(b):
route("method:stabilizer", lambda r: r.update(outcome="met"))(b)
route("method:statevector", lambda r: r.update(outcome="chosen"))(b)
b["result"]["planner"]["choice"] = "method:statevector"
b["result"]["method"] = "statevector"
add("choice-dearer", edited(local, dearer), ["planner_choice"], "another route that meets the contract chosen and run, where the rule picks the cheaper")
add("choice-not-run", edited(local, at(R + ("method",), "statevector")), ["planner_choice"], "the route chosen is not what ran")
add("stable-digest", edited(local, at(P + ("stable", "digest"), "00" * 32)), ["planner_stable"], "a digest that is not what the routes' prices give")
add("stable-choice", edited(local, at(P + ("stable", "choice"), False)), ["planner_stable"], "a choice said to depend on the host, where it depends on nothing of it")
add("route-price-work", edited(local, route("method:stabilizer", lambda r: r["price"].update(work=r["price"]["work"] + 1))), ["planner_stable"], "a price changed that the digest covers and the order does not: the rule still picks the route")
add("route-price-memory", edited(local, route("method:stabilizer", lambda r: r["price"].update(memory_bytes=1 << 29))), ["planner_choice", "planner_stable"], "a price changed that the digest covers and the order reads: another route that meets the contract now comes first")
# --- what the contract kept, and the class given
add("kept-false", edited(local, at(P + ("kept", "deadline", "kept"), False)), ["planner_kept"], "a deadline said missed that was kept")
add("kept-dropped", edited(local, drop(P + ("kept", "cost"))), ["planner_kept"], "a ceiling the contract gives, and kept does not state")
def late(b):
b["result"]["elapsed_ms"] = 61000
b["result"]["planner"]["kept"]["deadline"]["elapsed_ms"] = 61000
b["result"]["planner"]["kept"]["deadline"]["kept"] = False
add("kept-missed-stated", edited(local, late), [], "a deadline missed and said so: an answer all the same, and valid")
add("class-short", edited(local, at(R + ("accuracy", "class"), "estimate")), ["contract_class"], "a result whose class does not meet the class wanted")
add("shadow-on-local", edited(local, at(R + ("shadow",), {"ran": False, "code": "none", "why": "no provider answered"})), ["shadow"], "a local route's result that carries a shadow")
# --- a provider's answer and its shadow
S = R + ("shadow",)
add("shadow-missing", edited(routed, drop(S)), ["shadow"], "a provider's answer without its shadow")
add("shadow-plan-missing", edited(routed, drop(P + ("shadow",))), ["shadow"], "a provider's answer whose reasoning planned no shadow")
add("shadow-numerator", edited(routed, lambda b: b["result"]["shadow"]["distance"].update(numerator=b["result"]["shadow"]["distance"]["numerator"] + 1)), ["shadow"], "a distance that is not what the two lists of counts give")
add("shadow-not-computed", edited(routed, at(S + ("distance",), {"computed": False, "why": "said so"})), ["shadow"], "a distance not computed, with every shot listed on both sides")
def reseeded(b):
for p in (b["result"]["shadow"]["request"], b["result"]["planner"]["shadow"]["request"]):
p["seed"] = 4
add("shadow-seed", edited(routed, reseeded), ["shadow"], "a shadow sampled on the job's own stream, not one of its own")
add("shadow-said-none", edited(routed, at(S, {"ran": False, "code": "none", "why": "said none"})), ["shadow"], "a shadow said not to have run, where its plan had a route")
add("shadow-omitted-spelt", edited(routed, at(S + ("omitted_shots",), 0.0)), [], "a shadow's omitted_shots of 0 written as 0.0: 0 by value, and valid")
add("route-provider-claimed", edited(routed, route("provider:priced", met)), ["planner_route"], "a provider that costs money, with no ceiling to hold it to, said to meet the contract")
add("provider-not-chosen", edited(routed, at(R + ("provider", "name"), "priced")), ["planner_choice", "request_provider"], "an answer from a provider the planner did not choose")
add("providers-not-allowed", edited(routed, drop(("request", "contract", "providers"))), ["planner_choice", "planner_route", "request_provider"], "a provider's answer to a contract that does not allow one")
add("at-least-false", edited(routed, route("provider:priced", lambda r: r["price"]["money"].update(at_least=False))), ["planner_form"], "at_least given and not true")
add("at-least-claimed", edited(named, route("provider:priced", lambda r: r["price"]["money"].update(at_least=True))), ["planner_choice", "planner_route"], "a cost that is the least it can come to, under the ceiling, said to meet it")
def charged_over(b):
b["result"]["cost"]["charged"]["amount"] = "1.5"
b["result"]["planner"]["kept"]["cost"]["charged"]["amount"] = "1.5"
add("charged-over", edited(named, charged_over), ["planner_kept"], "charged past the ceiling, and kept said to hold")
def over(b):
charged_over(b)
b["result"]["planner"]["kept"]["cost"]["kept"] = False
add("charged-over-stated", edited(named, over), [], "charged past the ceiling, and said so: valid")
add("kept-charged-other", edited(named, at(P + ("kept", "cost", "charged", "amount"), "0.01")), ["planner_kept"], "kept stating a charge that is not the result's: within the ceiling, and not what was charged")
add("kept-member-extra", edited(local, at(P + ("kept", "deadline", "note"), "on time")), ["planner_kept"], "a member of kept that its table does not give")
add("kept-elapsed-other", edited(local, lambda b: b["result"]["planner"]["kept"]["deadline"].update(elapsed_ms=b["result"]["elapsed_ms"] + 1)), ["planner_kept"], "a method's time in kept that is not the run's own elapsed_ms, within the deadline all the same")
# --- what the job names, the question it asks, and what auto picks (review: auto verifiable)
add("auto-other", edited(auto, at(P + ("auto",), "method:statevector")), ["planner_form"], "auto said to pick a route the ranks do not")
add("auto-rank-lowered", edited(auto, route("method:statevector", lambda r: r.update(auto_rank=5))), ["planner_form"], "a route's rank in auto's order lowered, so that auto would pick it")
add("auto-rank-other", edited(auto, route("method:mps", lambda r: r.update(auto_rank=65))), ["planner_form"], "a rank that is not the method's, on a route that does not win by it")
add("auto-rank-missing", edited(auto, route("method:statevector", lambda r: r.pop("auto_rank"))), ["planner_form"], "a local route without its rank in auto's order")
add("question-other", edited(auto, at(P + ("question",), "amplitudes")), ["planner_form"], "a question the request does not ask")
add("named-other", edited(named, at(P + ("named",), None)), ["planner_form"], "a provider the request names, said to be named by nothing")
# --- a provider under a contract (review: providers default to never, J070)
add("provider-named-not-allowed", edited(named, drop(("request", "contract", "providers"))), ["contract_provider", "planner_choice", "planner_route"], "a provider named under a contract that does not allow one: never is the default")
add("named-approximate", edited(named, route("provider:priced", lambda r: r.update(approximate=True))), [], "a provider named, its simulator stated approximate, under a contract that names no class: it keeps the named route's class, and the rule against an approximate simulator holds only a class the contract names")
add("approximate-rejected", edited(routed, route("provider:priced", lambda r: (r.update(approximate=True), r["rejected"].update(code="class")))), [], "a simulator stated approximate, where the contract wants simulated: rejected for its class, before its cost is weighed")
# --- the bounds of a price (review: seconds and amounts a verifier reads whole)
add("price-seconds-past", edited(routed, route("provider:priced", lambda r: r["price"]["time"].update(seconds=1e13))), ["planner_form"], "a time past 10^12 seconds, which a host states as unknown")
add("price-amount-long", edited(routed, route("provider:priced", lambda r: r["price"]["money"].update(amount="1" * 65))), ["planner_form"], "an amount of 65 characters, which a host states as unknown")
add("local-money", edited(local, route("method:statevector", lambda r: r["price"].update(money={"free": False, "amount": "0", "currency": "USD"}))), ["planner_form"], "a method here priced in money: a local route is free")
# --- the rules' edges, each a route's code the rule gives it
sv_bytes = lambda b: b["result"]["planner"]["host"]["job_bytes"]
add("memory-at-cap", edited(local, lambda b: route("method:statevector", lambda r: r["price"].update(memory_bytes=sv_bytes(b)))(b)), ["planner_stable"], "a route that needs exactly the job's memory meets it; the digest covers memory, and is not the routes' now")
def unknown_time(r):
r["price"]["time"] = {"seconds": None, "why": "said unknown"}
r["outcome"] = "rejected"
r["rejected"] = {"code": "deadline_unknown", "why": "its time is not known, and the contract gives a deadline"}
add("deadline-unknown", edited(local, route("method:cutrank", unknown_time)), ["planner_stable"], "a route whose time is unknown under a deadline, rejected for it; the digest covers the time, and is not the routes' now")
def currency(b):
b["request"]["contract"]["max_cost"]["currency"] = "EUR"
add("currency-other", edited(named, currency), ["planner_choice", "planner_kept", "planner_route"], "a ceiling in another currency than the estimate's: no route meets it, and kept states another ceiling")
def joules(b):
b["request"]["contract"]["max_joules"] = 2
for r in b["result"]["planner"]["routes"]:
if r["price"] is not None:
r["price"]["energy"] = {"joules": 1.5, "basis": "an estimate", "watch": "a host's live reading"}
b["result"]["energy"] = {"joules_micro": 2000000}
b["result"]["planner"]["kept"]["energy"] = {"max_joules": 2, "joules": 2, "kept": True}
b["result"]["planner"]["stable"]["choice"] = False
add("energy-at-ceiling", edited(local, joules), [], "an energy ceiling met exactly by a figure sealed after the run, every route watched: kept, and the choice depends on the host")
add("energy-stable-claimed", edited(local, both(joules, at(P + ("stable", "choice"), True))), ["planner_stable"], "the same, the choice said to depend on nothing of the host")
def unwatched(outcome, code=None):
def f(r):
r["price"]["energy"] = {"joules": 0.5, "basis": "an estimate", "unwatched": "nothing reads it"}
r["outcome"] = outcome
r.pop("rejected", None)
if code:
r["rejected"] = {"code": code, "why": "said so"}
return f
add("energy-unwatched", edited(local, both(joules, route("method:statevector", unwatched("rejected", "energy_unwatched")))), [], "a route nothing can read while it runs, under a ceiling: refused, though its estimate is under it")
add("energy-unwatched-claimed", edited(local, both(joules, route("method:statevector", unwatched("met")))), ["planner_route"], "a route nothing can stop, said to meet a ceiling")
add("energy-over-estimate", edited(local, both(joules, route("method:statevector", lambda r: (r["price"]["energy"].update(joules=3), r.update(outcome="rejected"), r.update(rejected={"code": "energy", "why": "said so"}))))), [], "an estimate clearly past the ceiling refuses a watched route")
add("energy-estimate-at-ceiling", edited(local, both(joules, route("method:statevector", lambda r: r["price"]["energy"].update(joules=2)))), [], "an estimate exactly at the ceiling is not past it, and refuses nothing")
add("energy-watch-form", edited(local, both(joules, route("method:statevector", lambda r: r["price"]["energy"].update(watch=True)))), ["planner_form"], "a watch that is no sentence")
def stopped(b):
joules(b)
for k in ("counts", "distinct_outcomes", "omitted_shots", "outcome_dimension"):
b["result"].pop(k, None)
b["result"]["accuracy"] = {"class": "none", "note": "stopped at its ceiling"}
b["result"]["stopped"] = {"at": "max_joules", "joules": 2.5, "max_joules": 2, "elapsed_ms": 0, "read": "a host's live reading"}
b["result"]["elapsed_ms"] = 0
b["result"]["energy"] = {"unmetered": "stopped"}
b["result"]["planner"]["kept"]["energy"] = {"max_joules": 2, "joules": 2.5, "kept": False}
add("stopped", edited(local, stopped), [], "a run stopped at its ceiling by a live reading: no answer, and a receipt that says so")
add("stopped-kept-claimed", edited(local, both(stopped, at(P + ("kept", "energy", "kept"), True))), ["planner_kept"], "a stopped run said to have kept its ceiling")
add("stopped-under", edited(local, both(stopped, at(R + ("stopped", "joules"), 1.5), at(P + ("kept", "energy", "joules"), 1.5))), ["stopped"], "a run said stopped under its ceiling")
add("stopped-with-counts", edited(local, both(stopped, at(R + ("counts",), {"00": 100}))), ["stopped"], "a stopped run that gives an answer")
add("stopped-whole-below", edited(local, both(stopped, at(R + ("energy",), {"joules_micro": 1}))), ["stopped"], "a stopped run whose whole figure is less than what stopped it")
add("stopped-whole-above", edited(local, both(stopped, at(R + ("energy",), {"joules_micro": 2600000}))), [], "a stopped run whose whole figure is a little past what stopped it")
add("stopped-elapsed-fraction", edited(local, both(stopped, at(R + ("stopped", "elapsed_ms"), 1.5), at(R + ("elapsed_ms",), 1.5), at(P + ("kept", "deadline", "elapsed_ms"), 1.5))), ["planner_kept", "stopped"], "a stop at a time that is no whole number, the result's the same (a deadline's kept time is whole too)")
add("stopped-elapsed-other", edited(local, both(stopped, at(R + ("stopped", "elapsed_ms"), 99999))), ["stopped"], "a stop at another time than the result's")
add("stopped-with-verdict", edited(local, both(stopped, at(R + ("verdict",), {"holds": True}))), ["stopped"], "a stopped run that gives a verdict, an answer by another name")
add("stopped-with-outputs", edited(local, both(stopped, at(R + ("outputs",), {"x": 1}))), ["stopped"], "a stopped run that gives outputs")
add("stopped-other-ceiling", edited(local, both(stopped, at(R + ("stopped", "max_joules"), 9))), ["stopped"], "a stop at a ceiling the contract does not give")
add("stopped-at-ceiling", edited(local, both(stopped, at(R + ("stopped", "joules"), 2), at(P + ("kept", "energy", "joules"), 2))), [], "a reading exactly at the ceiling stops a run")
add("stopped-class", edited(local, both(stopped, at(R + ("accuracy", "class"), "exact"))), ["stopped"], "a stopped run that claims a class")
add("stopped-on-provider", edited(local, both(stopped, at(R + ("method",), "provider"))), ["contract_form", "counts", "planner_choice", "provider_class", "request_provider", "request_shots", "stopped"], "a stop on a provider's run, which nothing here reads: and a local job's request and route, which no provider's result can have, in the earlier form")
add("stopped-read-form", edited(local, both(stopped, at(R + ("stopped", "read"), 7))), ["stopped"], "a stop that does not say what read it")
add("stopped-elapsed-form", edited(local, both(stopped, at(R + ("stopped", "elapsed_ms"), 1.5))), ["stopped"], "a stop at a time that is no whole number")
add("stopped-extra-member", edited(local, both(stopped, at(R + ("stopped", "note"), "more"))), ["stopped"], "a stop with a member its form does not have")
add("stopped-at-other", edited(local, both(stopped, at(R + ("stopped", "at"), "deadline"))), ["stopped"], "a stop at something other than the energy ceiling")
add("energy-watch-both", edited(local, both(joules, route("method:statevector", lambda r: r["price"]["energy"].update(unwatched="and yet")))), ["planner_form"], "a route said both to be read and to have no reading")
add("stopped-on-plain", edited(run, at(R + ("stopped",), {"at": "max_joules"})), ["stopped"], "a job with no contract said to have been stopped")
# --- what a route states of itself (the verification of the review)
add("route-class-gpu", edited(local, route("method:statevector_single", lambda r: r.update({"class": "exact"}))), ["planner_route"], "the GPU's single precision stated exact: a class its method does not have")
add("route-class-by-job", edited(routed, route("method:stabilizer", lambda r: r.update({"class": "estimate"}))), ["planner_route", "planner_stable"], "the stabilizer stated estimate, on a job with no noise and no target")
add("route-host-dependent", edited(local, route("method:trajectories", lambda r: r.update(host_dependent=True))), ["planner_route", "planner_stable"], "a built-in on no GPU said to depend on the host, which takes it out of the digest")
add("route-provider-host-independent", edited(routed, route("provider:priced", lambda r: r.update(host_dependent=False))), ["planner_route"], "a provider's route said not to depend on the host")
add("result-class-foreign", edited(local, at(R + ("accuracy", "class"), "bit-exact")), ["contract_class"], "the stabilizer's result stated bit-exact: it meets what is wanted, and its method has no such class")
add("price-memory-null", edited(local, route("method:statevector", lambda r: r["price"].update(memory_bytes=None))), ["planner_form"], "a method's memory stated null, which passes the memory rule and wins its tie")
add("price-provider-memory", edited(routed, route("provider:priced", lambda r: r["price"].update(memory_bytes=1024))), ["planner_form"], "a provider's route stating a memory")
add("host-providers-other", edited(routed, at(P + ("host", "providers"), ["someone-else"])), ["planner_form"], "host.providers that are not the provider routes")
# --- the shadow, a method's run of exactly the job's program (review: steered, suppressed, unverifiable)
add("shadow-route-missing", edited(routed, drop(S + ("route",))), ["shadow"], "a shadow that names no route")
add("shadow-plan-route-missing", edited(routed, drop(P + ("shadow", "route"))), ["shadow"], "a shadow planned with no route, where null would say why")
add("shadow-plan-route-absent", edited(routed, both(at(P + ("shadow",), {"code": "none", "why": "said none"}), at(S, {"ran": False, "code": "none", "why": "said none"}))), ["shadow"], "a plan with neither a route nor null, and a shadow said not to have run")
def provider_shadow(b):
for side in (b["result"]["planner"]["shadow"], b["result"]["shadow"]):
side["route"] = "provider:ref"
side["request"].pop("method")
add("shadow-route-provider", edited(routed, provider_shadow), ["shadow"], "a provider as its own shadow")
def noisy(b):
for side in (b["result"]["planner"]["shadow"], b["result"]["shadow"]):
side["request"]["noise"] = {"depolarizing_2q": 0.2}
add("shadow-request-extra", edited(routed, noisy), ["shadow"], "a shadow run under a noise model the job never asked for, on both sides")
add("shadow-plan-code", edited(routed, both(at(P + ("shadow",), {"route": None, "code": "failed", "why": "said so"}), at(S, {"ran": False, "code": "failed", "why": "said so"}))), ["shadow"], "a shadow planned with a code no plan has")
add("shadow-plan-class", edited(routed, at(P + ("shadow", "class"), "heuristic")), ["shadow"], "a shadow planned at a class its route is not listed with")
add("shadow-plan-budget", edited(routed, at(P + ("shadow", "budget_seconds"), 299)), ["shadow"], "a budget that is not what the contract leaves")
add("shadow-plan-price-over", edited(routed, at(P + ("shadow", "price", "time", "seconds"), 301)), ["shadow"], "a shadow planned at a time past its budget")
def fraction(b):
c = b["result"]["shadow"]["counts"]
k = sorted(c)[0]
c[k] = c[k] - 0.5
c[sorted(c)[1]] += 0.5
add("shadow-count-fraction", edited(routed, fraction), ["shadow"], "a shadow's counts that are not whole, summing to the shots all the same")
add("shadow-count-sum", edited(routed, lambda b: b["result"]["shadow"]["counts"].update({sorted(b["result"]["shadow"]["counts"])[0]: b["result"]["shadow"]["counts"][sorted(b["result"]["shadow"]["counts"])[0]] + 1})), ["shadow"], "a shadow's counts that sum to more than its shots")
add("shadow-count-short", edited(routed, both(at(S + ("omitted_shots",), 1), at(S + ("distance",), {"computed": False, "why": "a shot is omitted"}))), ["shadow"], "a shot said omitted beside counts that are all the shots: they sum to one more, and the distance, not computed, is no help")
add("shadow-distinct", edited(routed, at(S + ("distinct_outcomes",), 7)), ["shadow"], "a shadow that says more outcomes than it lists, none omitted")
def by_mps(b):
route("method:mps", lambda r: None)(b)
b["result"]["planner"]["shadow"].update({"route": "method:mps", "class": "heuristic"})
b["result"]["planner"]["shadow"]["request"]["method"] = "mps"
b["result"]["shadow"]["route"] = "method:mps"
b["result"]["shadow"]["request"]["method"] = "mps"
add("shadow-class-foreign", edited(routed, by_mps), ["shadow"], "a shadow run by mps stated bit-exact: it meets its plan, and mps has no such class")
add("shadow-bound-other", edited(routed, lambda b: b["result"]["shadow"]["distance"].update(sampling_tvd_bound=0.01)), ["shadow"], "a bound on the distance the shots alone leave that is not what the counts give")
# --- what ran is the route chosen (the third check)
def paid_estimate(b):
b["result"]["cost"] = {"free": False, "estimate": {"free": False, "amount": "50", "currency": "USD", "unit": "job", "seconds": 0.0}, "charged": None, "confirmed": True}
add("cost-route-free-run-paid", edited(routed, paid_estimate), ["planner_choice"], "a run sealed at 50 USD on a route the reasoning calls free: not the route chosen")
def sealed_free(b):
b["result"]["cost"] = {"free": True, "estimate": {"free": True, "seconds": 0.0}, "charged": None, "confirmed": False}
b["result"]["planner"]["kept"]["cost"]["charged"] = None
add("cost-route-paid-run-free", edited(named, sealed_free), ["planner_choice"], "a paid route chosen, its run sealed as free and unconfirmed")
add("cost-amount-other", edited(named, at(R + ("cost", "estimate", "amount"), "0.08")), ["planner_choice"], "a run sealed at another estimate than its route's")
def unknown_sealed_free(b):
del b["request"]["contract"]["max_cost"]
del b["result"]["planner"]["kept"]["cost"]
route("provider:priced", lambda r: r["price"].update(money={"free": False, "unknown": "the provider gives no estimate"}))(b)
b["result"]["cost"] = {"free": True, "estimate": {"free": True, "seconds": 0.0}, "charged": None, "confirmed": False}
add("cost-unknown-run-free", edited(named, unknown_sealed_free), ["planner_choice"], "a route whose cost is not known, its run sealed as free and unconfirmed")
def least_dropped(b):
del b["request"]["contract"]["max_cost"]
del b["result"]["planner"]["kept"]["cost"]
route("provider:priced", lambda r: r["price"]["money"].update(at_least=True))(b)
add("cost-at-least-dropped", edited(named, least_dropped), ["planner_choice"], "a route's estimate the least it can come to, its run's estimate sealed as the whole")
add("route-device-other", edited(routed, route("provider:ref", lambda r: r.update(device="reference:line-6"))), ["planner_choice"], "the route chosen names another device than the one that ran")
add("route-provenance-class", edited(routed, route("provider:priced", lambda r: r.update(provenance="silicon"))), ["planner_route"], "a provider's route whose class is not its provenance's")
def shadow_width(b):
s = b["result"]["shadow"]
s["counts"] = dict(("0" + k, v) for k, v in s["counts"].items())
a, c = b["result"]["counts"], s["counts"]
shots = sum(a.values())
keys = sorted(set(a) | set(c))
root = 0.0
for k in keys:
root += math.sqrt(float(a.get(k, 0)) * float(c.get(k, 0)))
f = 0.0
for k in keys:
f += coin_split(a.get(k, 0) + c.get(k, 0))
s["distance"] = {"computed": True, "numerator": sum(abs(a.get(k, 0) - c.get(k, 0)) for k in keys), "denominator": 2 * shots,
"total_variation": sum(abs(a.get(k, 0) - c.get(k, 0)) for k in keys) / (2 * shots),
"hellinger_fidelity": root * root / (float(shots) * float(shots)), "sampling_tvd_bound": min(1.0, 0.5 * f / shots)}
add("shadow-width-other", edited(routed, shadow_width), ["shadow"], "a shadow whose outcomes are wider than the job's, its distance what they give")
add("route-mps-exact", edited(local, route("method:mps", lambda r: (r.update({"class": "exact", "outcome": "met"}), r.pop("rejected", None)))), ["planner_route", "planner_stable"], "the matrix product state's route stated exact, which a route of it never is")
def registered(b):
rs = b["result"]["planner"]["routes"]
turbo = copy.deepcopy(next(r for r in rs if r["route"] == "method:exact"))
turbo.update({"route": "method:turbo", "method": "turbo"})
rs.append(turbo)
add("route-registered-host-independent", edited(local, registered), ["planner_route", "planner_stable"], "a method the host registered, stated to depend on nothing of the host")
def host_failed(b):
b["result"]["shadow"] = {"ran": False, "code": "failed", "why": "the host says it failed"}
add("shadow-said-failed", edited(routed, host_failed), [], "a shadow planned and said to have failed: valid to read, and run again by --recheck")
# --- an energy-landscape job (quantum-landscape section 5.4): each sealed again, one
# thing changed. A rule's number is in what each is for.
lrun = load("cli-landscape-run.json")
lsettle = load("cli-landscape-settle.json")
lreplay = load("cli-landscape-replay.json")
lsweep = load("cli-landscape-sweep.json")
lcompare = load("cli-landscape-compare.json")
lprov = load("cli-landscape-provider.json")
lpriced = load("cli-landscape-provider-priced.json")
LF = ["landscape_form"]
def add(name, body_or_text, reasons, what):
t = body_or_text if isinstance(body_or_text, (str, bytes)) else dump(body_or_text) + "\n"
out.append(("crafted-" + name + ".json", t, sorted(reasons), what))
def board(parity="bit-exact", **extra):
"""The answer's member made another's: an FPGA board the host certified within a
capacity, its audit beside it where its grade is bit-exact."""
def f(b):
r = b["result"]
m = {"id": "fpga:board-1", "class": "fpga", "kernel": r["member"]["kernel"], "parity": parity, "capacity": {"cells": 64}}
m.update(extra)
r["member"] = m
if parity == "bit-exact":
r["audit"] = {"checked": [{"seed": r["seed"], "settle": "whole"} if r["mode"] == "settle" else {"seed": r["seed"], "chains": [0]}]}
return f
def member(**changes):
def f(b):
b["result"]["member"].update(changes)
return f
def res(**changes):
def f(b):
b["result"].update(changes)
return f
def req(**changes):
def f(b):
b["request"].update(changes)
return f
def drop(*names):
def f(b):
for n in names:
b["result"].pop(n, None)
return f
add("landscape-board-certified", edited(lrun, board()), [], "an energy-landscape run from a board the host certified bit-exact within a capacity of 64 cells, its audit beside it: every rule holds")
add("landscape-board-settle-certified", edited(lsettle, board()), [], "a settle from the same board, its audit the reference's whole settle: every rule holds")
add("landscape-first-form", seal(lrun, form="first"), LF, "an energy-landscape run sealed in the first form, older than the extension: the seal holds, and no host wrote it (rule 1)")
add("landscape-schema-other", edited(lrun, res(schema="wai.quantum.job-result/1")), LF, "an energy-landscape answer named as a circuit job's result (rule 2)")
add("landscape-engine-other", edited(lrun, res(engine={"name": "another", "version": "1"})), LF, "an energy-landscape answer naming another engine (rule 2)")
add("landscape-request-seed-text", edited(lrun, req(seed="7x")), LF, "a request whose seed is no whole number (rule 3)")
add("landscape-request-temperature-exponent", edited(lrun, req(temperature="1e2")), LF, "a request whose temperature is written with an exponent (rule 3)")
add("landscape-request-parity-other", edited(lrun, req(parity="exact")), LF, "a request whose parity is neither bit-exact nor statistical (rule 3)")
add("landscape-no-operation", edited(lrun, drop("state")), LF, "an energy-landscape answer with none of lines, rows and state: of no operation (rule 4)")
add("landscape-label-empty", edited(lrun, res(label="")), LF, "an empty label (rule 5)")
add("landscape-member-added", edited(lrun, member(note="a member section 5.4 does not name")), LF, "a member holding a member it does not name (rule 6)")
add("landscape-member-id-class", edited(lrun, board(id="gpu:board-1")), LF, "a member whose id names another class than its own (rule 6)")
add("landscape-reference-graded", edited(lrun, member(parity="bit-exact")), LF, "the reference stated bit-exact, not reference (rule 6), with no audit (rule 9)")
add("landscape-board-reference", edited(lrun, board(parity="reference")), LF, "a board stated at the reference's own parity (rule 6)")
add("landscape-settle-statistical", edited(lsettle, board(parity="statistical")), LF, "a settle kernel graded statistical, which only a sampling kernel can be (rule 6)")
add("landscape-compare-kernels", edited(lcompare, lambda b: b["result"]["member"]["kernels"].append({"kernel": "wai.quantum.landscape/settle", "parity": "reference"})), LF, "a comparison whose member names the settle kernel beside the sampling kernel (rule 6)")
add("landscape-demoted-graded", edited(lrun, board(demoted="its audit found other bits")), LF, "a member the audit demoted, still stated bit-exact (rule 7)")
add("landscape-demoted-uncertified", edited(lrun, board(parity="uncertified", demoted="its audit found other bits")), [], "a member the audit demoted, stated uncertified, as section 7.3 says: every rule holds")
add("landscape-capacity-under-floor", edited(lrun, board(capacity={"cells": 31})), LF, "a board graded bit-exact within 31 cells, under the floor of 32 (rule 8)")
add("landscape-capacity-side-under-floor", edited(lrun, board(capacity={"cells": 64, "above_zero": {"field_bound": 40}})), LF, "a board graded bit-exact with a field bound of 40 above temperature 0, under the floor of 41 (rule 8)")
add("landscape-capacity-uncertified-small", edited(lrun, board(parity="uncertified", capacity={"cells": 8})), [], "a board run by name, uncertified, within 8 cells: no grade, so no floor (rule 8 holds)")
add("landscape-capacity-on-reference", edited(lrun, member(capacity={"cells": 64})), LF, "the reference stating a capacity, which it declares none of (rule 8)")
add("landscape-capacity-unknown-number", edited(lrun, board(capacity={"cells": 64, "qubits": 3})), LF, "a capacity naming a number section 5 does not have (rule 8)")
add("landscape-bit-exact-no-audit", edited(lrun, (lambda f: lambda b: (f(b), b["result"].pop("audit")))(board())), LF, "a board graded bit-exact whose answer carries no audit (rule 9)")
add("landscape-audit-on-reference", edited(lrun, res(audit={"checked": [{"seed": "7", "chains": [0]}]})), LF, "an audit on the reference's own answer, which nothing audits (rule 9)")
add("landscape-digest-other", edited(lrun, res(program_digest="ab" * 32)), LF, "a program digest that is not the request's program's (rule 10)")
add("landscape-seed-other", edited(lrun, res(seed="8")), LF, "an answer's seed that is not the request's (rule 10)")
add("landscape-mode-kernel", edited(lrun, res(mode="settle")), LF, "a sampling run stated as a settle: the kernel and the temperature say otherwise (rule 10)")
add("landscape-state-hash-other", edited(lrun, res(state_hash="0" * 16)), LF, "a state hash that is not the state's (rule 10)")
add("landscape-replay-other", edited(lrun, res(replay="0" * 64)), LF, "a replay digest that is not the one the program, the plan, the state and the energy give (rule 10)")
add("landscape-energy-lowered", edited(lrun, lambda b: b["result"].update(energy=b["result"]["energy"] - 1)), LF, "an energy lowered by one, the replay digest left: the digest binds the energy (rule 10)")
add("landscape-joules-measured", edited(lrun, lambda b: b["result"]["joules"].update(kind="measured")), LF, "joules stated as measured: they are priced, always (section 4.4, rule 10)")
add("landscape-reached-wrong", edited(lrun, res(reached=False)), LF, "the lowest energy said not reached where the energy is the truth's lowest (rule 10)")
add("landscape-variables-other", edited(lrun, lambda b: b["result"].update(variables=b["result"]["variables"] + 1)), LF, "as many variables stated as the state has cells and one more (rule 10)")
add("landscape-replay-line-dropped", edited(lreplay, lambda b: b["result"].update(lines="".join(b["result"]["lines"].splitlines(True)[:-1]))), LF, "a replay of four seeds with three lines (rule 11)")
add("landscape-replay-seeds-other", edited(lreplay, lambda b: b["request"].update(seeds=5)), LF, "a replay asked for five seeds, answering four (rule 11)")
add("landscape-sweep-row-parity", edited(lsweep, lambda b: b["result"]["rows"][0].update(parity="bit-exact")), LF, "a sweep's row stating another parity than the member's for its kernel (rule 12)")
add("landscape-compare-classical-parity", edited(lcompare, lambda b: b["result"]["rows"][-1].update(parity="reference")), LF, "a classical method's row stating a parity: it ran on no member (rule 12)")
add("landscape-local-provider-members", edited(lrun, res(job={"id": "ref-1", "polls": 0})), LF, "a job run here, carrying a provider's job (rule 13)")
add("landscape-provider-name-other", edited(lprov, lambda b: b["result"]["provider"].update(name="other")), LF, "a provider's job naming another provider than its request (rule 13)")
add("landscape-provider-unrequested", edited(lprov, lambda b: b["request"].pop("provider")), LF, "a provider's members on a job whose request names no provider (rule 13)")
add("landscape-provider-unconfirmed", edited(lpriced, lambda b: b["result"]["cost"].update(confirmed=False)), LF, "a run that was not free, not confirmed: no client sends one (rule 13)")
add("landscape-provider-kind-builtin", edited(lprov, lambda b: b["result"]["provider"].update(kind="sampler-v2")), LF, "a provider's job of a built-in kind, which carries no energy-landscape job (rule 13)")
add("landscape-tampered", (lambda b: (b["result"].update(state="0" * len(b["result"]["state"])), b)[1])(copy.deepcopy(lrun)), ["digest_mismatch", "landscape_form"], "an energy-landscape run whose state was changed after it was sealed: the seal, and its hash and replay digest (rule 10)")
return out
# --------------------------------------------------------------------- the made half
MADE = {
"cli-run-observables.json": ("the command line, on this machine, with --demo-key: a Bell pair's expectation values", []),
"cli-run-statevector.json": ("the command line: a three-qubit circuit's counts by the state vector", []),
"cli-run-trajectories.json": ("the command line: noisy trajectories, an estimate with its error", []),
"cli-run-mps.json": ("the command line: thirty qubits as a matrix product state", []),
"cli-compile.json": ("the command line: a compile, its certificate and its receipt", []),
"cli-provider-reference.json": ("the command line, on the reference provider over a local process: a simulator's stated result", []),
"cli-provider-reference-priced.json": ("the command line, on the reference provider with a price, confirmed with --yes", []),
"cli-contract-local.json": ("the command line under a contract: exact, a deadline and a ceiling, the cheapest route here chosen and its reasoning sealed", []),
"cli-contract-auto.json": ("the command line under a contract that names no class: auto's pick kept, held to a deadline", []),
"cli-contract-routed.json": ("the command line under a contract that allows the person's providers and wants a simulator's counts: the free one chosen, a classical method's counts and their distance beside its own", []),
"cli-contract-provider-priced.json": ("the command line on a provider it names, allowed by its contract and under a ceiling, confirmed with --yes: the estimate held to the ceiling, the charge to it after, and the shadow", []),
"cli-contract-routed-seed.json": ("the same routed job with the seed 2^53 - 2: its shadow's seed, the request's plus 2^32, wraps at 2^53", []),
"cli-contract-any.json": ("the command line under a contract that takes any class, the person's providers allowed: a free method here and a free provider tie on money and on time to the millisecond, and the method here comes first", []),
"cli-contract-bounded.json": ("the command line under a contract that wants bounded: a single-precision route does not meet it, and the state vector is chosen", []),
"cli-contract-fabric-cpu.json": ("the command line on the CPU it asks for, under a deadline: each route that runs on an accelerator rejected for it", []),
"cli-contract-target.json": ("the command line on a device target, the person's providers allowed beside it, under a deadline: the target's method is named, and the providers are not", []),
"cli-contract-pulse.json": ("the command line, a pulse program under a contract that wants bounded and a deadline: its one route", []),
"service-run.json": ("the free job service, signed with its key", []),
"service-run-stabilizer.json": ("the free job service: a stabilizer job with observables", []),
"scripted-sampler-v2.json": ("sampler-v2 through a service that follows its contract", []),
"scripted-sampler-v2-post-selected.json": ("sampler-v2 on a device that answered 13 of its 400 shots fewer, as a post-selected readout does: its counts sealed as the 387 shots it kept (accepted_shots)", []),
"scripted-prx-circuits.json": ("prx-circuits through a service that follows its contract", []),
"scripted-contract-prx-circuits.json": ("prx-circuits named under a contract that wants a device's counts, confirmed: its counts, the planner's reasoning, and a classical method's counts and their distance beside them", []),
"scripted-native-turns.json": ("native-turns, its simulator, through a service that follows its contract", []),
"scripted-native-turns-noise.json": ("native-turns, its simulator under a device's noise model", []),
"scripted-cloud-tasks-processor.json": ("cloud-tasks on a processor: a verbatim box, rows of shots", []),
"scripted-cloud-tasks-processor-probabilities.json": ("cloud-tasks on a processor whose result holds probabilities: counts rebuilt", []),
"scripted-cloud-tasks-simulator.json": ("cloud-tasks on its simulator: simulated", []),
"fixture-job-legacy.json": ("a run sealed before results named their engine and schema (the engine's pinned fixture)", []),
"fixture-provider-job-legacy.json": ("a provider job of the earlier form: a simulator's counts sealed as measured, read as simulated", []),
"fixture-compile-legacy.json": ("a compile as an earlier page sealed it (the engine's pinned fixture)", []),
"cli-set-programs.json": ("the command line: a set of two programs, one receipt over both and each its own (section 8.6)", []),
"cli-set-sweep.json": ("the command line: a sweep of an input at three points, sealed as a set", []),
"first-form-service-run.json": ("the job service as it was on 18 October 2026, commit 72d18742d, sealing with the first form's own writer: counts", []),
"first-form-service-observables.json": ("the same service, the same writer: expectation values, doubles in its own text", []),
"cli-wasmq-run.json": ("the command line: a wasmQ run, a surface-code round decoded by WebAssembly between its gates, 32 shots", []),
"cli-wasmq-state-job.json": ("the command line: a wasmQ run of one instance for the whole run, its decoder reading a prior", []),
"cli-wasmq-fault.json": ("the command line: a wasmQ run that stopped in a call past the call's fuel budget", []),
"cli-wasmq-fault-run.json": ("the command line: a wasmQ run that stopped in its third shot when the run's fuel ran out", []),
"cli-wasmq-fault-init.json": ("the command line: a wasmQ run that stopped in an instance's init", []),
"cli-wasmq-fault-prior.json": ("the command line: a wasmQ run that stopped while a module read its prior", []),
"cli-landscape-run.json": ("the command line: an energy-landscape run (wai.quantum.landscape/1), sampling from the default start, every arrangement checked", []),
"cli-landscape-settle.json": ("the command line: an energy-landscape settle, the deterministic kernel, one end of a chain pinned", []),
"cli-landscape-quench.json": ("the command line: an energy-landscape quench, chains at temperature 0", []),
"cli-landscape-replay.json": ("the command line: an energy-landscape replay of four seeds, one line each", []),
"cli-landscape-sweep.json": ("the command line: an energy-landscape sweep, the program at each temperature", []),
"cli-landscape-compare.json": ("the command line: an energy-landscape comparison with three classical methods, on a program that fails on purpose", []),
"cli-landscape-provider.json": ("the command line, an energy-landscape run on the reference provider over a local process (quantum-providers section 11): the provider, its job and its cost added", []),
"cli-landscape-provider-priced.json": ("the same on the reference provider with a price, confirmed with --yes: charged", []),
"cli-wasmq2-t-toffoli.json": ("the command line: a wasmQ/2 run by the exact method, h, t, h and a Toffoli under two set controls, its energy as the host read it", []),
"cli-wasmq2-feedback-rotation.json": ("the command line: a wasmQ/2 run whose callout turns a measured bit into three eighths of a turn, applied as its bits' phases", []),
"cli-wasmq2-angle-arguments.json": ("the command line: a wasmQ/2 run with an angle[8] known on reading and an angle[4] of measured bits, given back by the module", []),
"cli-wasmq2-rotations-and-a-reset.json": ("the command line: a wasmQ/2 run of a random reset and each kind of gate the exact method takes", []),
"cli-wasmq2-stabilizer.json": ("the command line: a wasmQ/2 run of a Clifford program by the stabilizer method", []),
"cli-wasmq2-exact-of-a-clifford-program.json": ("the command line: the same Clifford program by the exact method, the same counts", []),
"cli-qudit-qutrit-bell.json": ("the command line: a qudit job (wai.quantum.qudit/1, quantum-sim section 9.7), a qutrit Bell pair by a Fourier gate and a controlled sum, its three amplitudes asked for as integers", []),
"cli-qudit-ququarts-phase.json": ("the command line: two ququarts, a controlled sum to the third power, a phase on each level and a Fourier gate to the third power", []),
"cli-qudit-six-levels.json": ("the command line: two qudits of six levels (F_2 x F_3), a Fourier gate, a shift, a controlled sum and a clock, two amplitudes asked for", []),
"cli-qudit-qubits-ghz.json": ("the command line: a qudit job of dimension 2, three qubits in a GHZ state by a Fourier gate and two controlled sums", []),
"service-wasmq-surface3-decode64.json": ("a job service built from this checkout (POST /v1/wasmq), sealed with its key: the surface-code round run as a job, the service's report around it, its own members unsealed", []),
"service-wasmq2-t-toffoli.json": ("the same service: a wasmQ/2 run by the exact method, its energy what the service read of the worker that ran it", []),
"service-wasmq-fault-fuel.json": ("the same service: a wasmQ run that stopped in a call past the call's fuel budget, the fault sealed in the job", []),
"service-qudit-qutrit-bell.json": ("the same service (POST /v1/qudit): a qutrit Bell pair run as a job, its energy what the service read of its worker", []),
}
def manifest(crafted):
entries = []
for name in sorted(MADE):
what, reasons = MADE[name]
entries.append({"file": "jobs/" + name, "reasons": reasons, "made_by": "made", "what": what})
for name, _, reasons, what in crafted:
entries.append({"file": "jobs/" + name, "reasons": reasons, "made_by": "crafted", "what": what})
return {
"about": "Job receipts and the reasons a verifier must give for each (wai/extensions/quantum-providers.md section 8.4): valid when there are none. made: sealed by the command line, the job service, the provider clients or an earlier engine, kept as made. crafted: made by generate.py from those, each changed in one way and sealed again with the published demo key where the change is inside the seal. Every expectation is written by hand from section 8.4.",
"entries": entries,
}
LOCK = os.path.join(HERE, "made.lock.json")
def lock_of():
try:
with open(LOCK, encoding="utf-8") as f:
return json.load(f)
except OSError:
return {}
def digest_of(name):
with open(os.path.join(JOBS, name), "rb") as f:
return hashlib.sha256(f.read()).hexdigest()
def write_lock(lock):
with open(LOCK, "w", encoding="utf-8") as f:
f.write(json.dumps(lock, indent=2, sort_keys=True) + "\n")
def check_made():
"""A made receipt is kept: named in MADE, in jobs/, and the bytes pinned."""
lock, problems = lock_of(), []
for name in sorted(set(MADE) | set(lock)):
if name not in MADE:
problems.append("%s is pinned, and no longer in MADE: a made receipt is kept" % name)
elif name not in lock:
problems.append("%s is in MADE, and not pinned: generate.py --pin %s" % (name, name))
elif not os.path.exists(os.path.join(JOBS, name)):
problems.append("%s is pinned, and gone from jobs/: a made receipt is kept" % name)
elif digest_of(name) != lock[name]:
problems.append("%s has changed since it was pinned: a made receipt is kept as it was made, and a new one takes a new name" % name)
on_disk = {n for n in os.listdir(JOBS) if n.endswith(".json") and not n.startswith("crafted-")}
for name in sorted(on_disk - set(MADE)):
problems.append("%s is in jobs/, and not in MADE" % name)
return problems
def pin(names):
lock = lock_of()
for name in names:
if name in lock:
sys.exit("generate.py --pin: %s is pinned already: a made receipt is kept as it was made" % name)
if name not in MADE:
sys.exit("generate.py --pin: %s is not in MADE" % name)
lock[name] = digest_of(name)
write_lock(lock)
print("generate.py --pin: %d pinned, %d in all" % (len(names), len(lock)))
def self_test():
"""The pins can fail: a made receipt dropped, changed, or unnamed, in a copy."""
global JOBS, LOCK, MADE
import shutil
import tempfile
jobs, lock, made = JOBS, LOCK, MADE
failures = 0
with tempfile.TemporaryDirectory() as tmp:
try:
JOBS, LOCK = os.path.join(tmp, "jobs"), os.path.join(tmp, "made.lock.json")
os.mkdir(JOBS)
for name in made:
shutil.copy(os.path.join(jobs, name), JOBS)
shutil.copy(lock, LOCK)
name = sorted(made)[0]
def expect(what, want):
nonlocal failures
problems = check_made()
held = any(want in p for p in problems)
print(("PASS " if held else "FAIL ") + what)
failures += not held
if check_made():
print("FAIL the copy holds as it is: %s" % check_made())
failures += 1
with open(os.path.join(JOBS, name), "ab") as f:
f.write(b" ")
expect("a made receipt changed is found", "has changed")
os.remove(os.path.join(JOBS, name))
expect("a made receipt dropped is found", "gone from jobs/")
MADE = {k: v for k, v in made.items() if k != name}
expect("a made receipt dropped from MADE too is found", "no longer in MADE")
MADE = dict(made)
shutil.copy(os.path.join(jobs, name), JOBS)
with open(os.path.join(JOBS, "made-by-no-one.json"), "wb") as f:
f.write(b"{}")
expect("a receipt in jobs/ that MADE does not name is found", "not in MADE")
os.remove(os.path.join(JOBS, "made-by-no-one.json"))
MADE = dict(made, **{"new.json": ("a new one", [])})
expect("a name in MADE that is not pinned is found", "not pinned")
finally:
JOBS, LOCK, MADE = jobs, lock, made
if failures:
sys.exit(1)
print("generate.py --self-test: the pins can fail, each way")
def main():
if sys.argv[1:2] == ["--pin"]:
return pin(sys.argv[2:])
if sys.argv[1:] == ["--self-test"]:
return self_test()
check = "--check" in sys.argv[1:]
problems = check_made()
if problems:
for p in problems:
print("generate.py: " + p)
sys.exit(1)
crafted = cases()
want = {os.path.join(JOBS, name): (t if isinstance(t, bytes) else t.encode("utf-8")) for name, t, _, _ in crafted}
want[os.path.join(HERE, "manifest.json")] = (json.dumps(manifest(crafted), indent=2, ensure_ascii=False) + "\n").encode("utf-8")
stale = [n for n in os.listdir(JOBS) if n.startswith("crafted-") and os.path.join(JOBS, n) not in want]
bad = []
for path, data in sorted(want.items()):
if check:
try:
with open(path, "rb") as f:
if f.read() != data:
bad.append(path)
except OSError:
bad.append(path)
else:
with open(path, "wb") as f:
f.write(data)
if check:
for p in bad + [os.path.join(JOBS, n) for n in stale]:
print("generate.py --check: %s is not what generate.py writes" % os.path.relpath(p, HERE))
if bad or stale:
sys.exit(1)
print("generate.py --check: %d crafted receipts and the manifest are as written" % len(crafted))
else:
for n in stale:
os.remove(os.path.join(JOBS, n))
print("generate.py: wrote %d crafted receipts and the manifest" % len(crafted))
if __name__ == "__main__":
main()