Repository navigation
Expand file tree
/
Copy pathexperiments.py
More file actions
118 lines (95 loc) · 4.77 KB
/
Copy pathexperiments.py
File metadata and controls
118 lines (95 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""
experiments.py -- simulator-first experiment briefs (drafts).
A research request ("compare two error-mitigation methods on a shallow
circuit") becomes a brief that someone could reproduce and critique:
question what is being tested
hypothesis what outcome would support or weaken it
system circuit family / system under test
variables what is varied (at least one)
metric how outcomes are measured (METRICS, or your own)
controls seeds, shots, noise model -- what is held fixed
Every brief is a draft with execution fixed to a LOCAL SIMULATOR and
hardware DISABLED: nothing here runs, submits or spends anything. The
brief is the plan a later, separately reviewed run must follow; a model
never claims a result from it.
"""
import json
import os
import time
STORE = os.path.join(os.path.expanduser("~"), ".osiris", "experiments.json")
METRICS = ("expectation-value error vs. exact", "state fidelity", "success probability",
"variance across seeds", "runtime / cost")
def _load():
try:
with open(STORE, encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, dict) and isinstance(data.get("briefs"), list):
return data
except (OSError, ValueError):
pass
return {"next_id": 1, "briefs": []}
def _save(data):
os.makedirs(os.path.dirname(STORE), exist_ok=True)
tmp = STORE + ".tmp"
with open(tmp, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
os.replace(tmp, STORE)
def new_brief(question="", hypothesis="", system="", variables=(), metric="", controls=()):
clean = lambda s: " ".join(str(s).split())
return {"question": clean(question), "hypothesis": clean(hypothesis), "system": clean(system),
"variables": [clean(v) for v in variables if str(v).strip()], "metric": clean(metric),
"controls": [clean(c) for c in controls if str(c).strip()],
"execution": "local simulator only", "hardware": "disabled -- needs a separate reviewed request"}
# field -> (check on a brief, what is missing). problems() and the form's
# per-answer check (field_problem) use the same rules.
RULES = {
"question": (lambda b: len(b["question"].split()) >= 6,
"Question: state what is being tested in a sentence (6+ words)."),
"hypothesis": (lambda b: len(b["hypothesis"].split()) >= 6,
"Hypothesis: say what outcome would support or weaken it (6+ words)."),
"system": (lambda b: len(b["system"]) >= 3,
"System: name the circuit family or system under test."),
"variables": (lambda b: bool(b["variables"]), "Variables: name at least one thing that is varied."),
"metric": (lambda b: bool(b["metric"]), "Metric: pick how outcomes are measured."),
"controls": (lambda b: any(w in " ".join(b["controls"]).lower() for w in ("seed", "shot", "noise")),
"Controls: fix at least a seed, a shot count or a noise model, or runs cannot be compared."),
}
EXAMPLES = {
"question": "Does zero-noise extrapolation reduce expectation-value error on a 3-qubit GHZ circuit?",
"hypothesis": "ZNE gives lower mean error than no mitigation at the same shot count.",
"system": "3-qubit GHZ circuit, depth 2-10",
"variables": "mitigation method (none, ZNE) -- then circuit depth",
"metric": "type a number from the list above",
"controls": "seed 7 -- then 4000 shots -- then depolarizing noise p=0.01",
}
def problems(b):
"""What keeps a brief from being reproducible ([] = complete)."""
return [msg for check, msg in RULES.values() if not check(b)]
def field_problem(field, value):
"""RULES[field]'s message if this one answer fails it, else None -- so the
form can say so at once (on-device, "screenshots", "demo" and "r" were
only flagged after all six answers, 2026-09-24)."""
check, msg = RULES[field]
return None if check(new_brief(**{field: value})) else msg
def save(b):
data = _load()
b = dict(b, id=data["next_id"], status="draft", created=time.strftime("%Y-%m-%dT%H:%M:%S"))
data["briefs"].append(b)
data["next_id"] += 1
_save(data)
return b
def briefs():
return _load()["briefs"]
def readiness(brief_or_id, **kwargs):
"""Run the simulator-first hardware-readiness gate on a saved brief ID or brief dict."""
from osiris_cli import quantum_readiness
if isinstance(brief_or_id, int):
matches = [b for b in briefs() if b.get("id") == brief_or_id]
if not matches:
raise ValueError(f"experiment brief #{brief_or_id} not found")
target = matches[0]
elif isinstance(brief_or_id, dict):
target = brief_or_id
else:
raise ValueError("brief_or_id must be an integer brief ID or brief dict")
return quantum_readiness.assess_experiment_brief(target, **kwargs)