REQUIRED = {"operation", "px", "pz", "target", "protocol", "source", "date"}
def worksheet(rows, target, modes_per_logical):
    if any(REQUIRED - row.keys() for row in rows) or any(row["target"] != target for row in rows):
        raise ValueError("unlabeled bias or mismatched target")
    operations = tuple(row["operation"] for row in rows)
    biases = {row["operation"]:row["pz"] / row["px"] for row in rows}
    return {"operations":operations, "min_bias":min(biases.values()), "biases":biases, "weighted_error":modes_per_logical * sum(row["px"] + row["pz"] for row in rows)}
rows = [
 {"operation":"idle","px":1e-5,"pz":1e-3,"target":"logical-v1","protocol":"synthetic","source":"synthetic","date":"2026-01-01"},
 {"operation":"entangle","px":4e-4,"pz":8e-4,"target":"logical-v1","protocol":"synthetic","source":"synthetic","date":"2026-01-01"},
 {"operation":"readout","px":2e-3,"pz":3e-3,"target":"logical-v1","protocol":"synthetic","source":"synthetic","date":"2026-01-01"},
]
baseline = worksheet(rows, "logical-v1", 2)
counterfactual = worksheet([{**row, "px":7e-4} if row["operation"] == "entangle" else row for row in rows], "logical-v1", 2)
try:
    worksheet([{"bias":100}], "logical-v1", 2)
    raise AssertionError("unlabeled bias accepted")
except ValueError:
    rejected = True
assert baseline["biases"]["idle"] == 100 and baseline["min_bias"] == 1.5
assert counterfactual["operations"] == baseline["operations"] and counterfactual["min_bias"] < baseline["min_bias"] and rejected
print(f"PASS: 60 bias evidence per_operation={baseline['biases']} degraded_min={counterfactual['min_bias']:.3f} schema_rejected={rejected}")
