2 """Validation: fixed-parameter matrix baseline. 4 Builds the fixed-parameter matrix gate from: 5 - the emitted canonical workflow contract, 6 - the emitted end-to-end trace bundle, 7 - and the committed validation workflow baseline bundle, which already contains 8 the rich 4/6/8/10 fixed-parameter matrix evidence. 10 This layer is intentionally thin: 11 - it preserves one stable exact-regime matrix identity, 12 - it rebinds matrix cases to the canonical workflow ID and version, 13 - and it fails explicitly when required matrix identity or 10-qubit anchor 14 evidence is incomplete. 17 python benchmarks/density_matrix/workflow_evidence/matrix_baseline_validation.py 20 from __future__
import annotations
25 from collections
import Counter, defaultdict
26 from pathlib
import Path
28 REPO_ROOT = Path(__file__).resolve().parents[3]
29 if str(REPO_ROOT)
not in sys.path:
30 sys.path.insert(0, str(REPO_ROOT))
32 from benchmarks.density_matrix.workflow_evidence.workflow_contract_validation
import (
33 ARTIFACT_FILENAME
as WORKFLOW_CONTRACT_ARTIFACT_FILENAME,
35 DEFAULT_OUTPUT_DIR
as WORKFLOW_EVIDENCE_OUTPUT_DIR,
38 build_software_metadata,
40 run_validation
as run_workflow_contract_validation,
41 validate_artifact_bundle
as validate_workflow_contract_artifact,
43 from benchmarks.density_matrix.workflow_evidence.end_to_end_trace_validation
import (
44 ARTIFACT_FILENAME
as END_TO_END_TRACE_ARTIFACT_FILENAME,
45 run_validation
as run_end_to_end_trace_validation,
46 validate_artifact_bundle
as validate_end_to_end_trace_artifact,
49 SUITE_NAME =
"matrix_baseline_validation" 50 ARTIFACT_FILENAME =
"matrix_baseline_bundle.json" 51 DEFAULT_OUTPUT_DIR = WORKFLOW_EVIDENCE_OUTPUT_DIR
52 WORKFLOW_CONTRACT_PATH = DEFAULT_OUTPUT_DIR / WORKFLOW_CONTRACT_ARTIFACT_FILENAME
53 END_TO_END_TRACE_BUNDLE_PATH = DEFAULT_OUTPUT_DIR / END_TO_END_TRACE_ARTIFACT_FILENAME
54 VALIDATION_WORKFLOW_BASELINE_PATH = (
59 /
"validation_evidence" 60 /
"workflow_baseline_bundle.json" 62 MANDATORY_WORKFLOW_QUBITS = (4, 6, 8, 10)
63 PARAMETER_SET_COUNT = 10
64 ARTIFACT_CORE_FIELDS = (
82 return json.loads(path.read_text(encoding=
"utf-8"))
88 validate_workflow_contract_artifact(artifact)
90 _, artifact = run_workflow_contract_validation(verbose=
False)
97 validate_end_to_end_trace_artifact(artifact)
99 _, _, _, artifact = run_end_to_end_trace_validation(verbose=
False)
104 return tuple(workflow_contract[
"thresholds"][
"required_workflow_qubits"])
108 return int(workflow_contract[
"thresholds"][
"fixed_parameter_sets_per_size"])
112 return int(workflow_contract[
"thresholds"][
"documented_anchor_qubit"])
116 requirements = validation_workflow_baseline_bundle[
"requirements"]
118 "workflow_id": workflow_contract[
"workflow_id"],
119 "contract_version": workflow_contract[
"contract_version"],
124 "mandatory_parameter_set_ids": list(requirements[
"mandatory_parameter_set_ids"]),
125 "required_case_names": list(requirements[
"mandatory_case_names"]),
127 "required_bundle_sources": [
128 workflow_contract[
"suite_name"],
129 "end_to_end_trace_validation",
130 "workflow_baseline_validation",
136 contract_thresholds = workflow_contract[
"thresholds"]
137 workflow_thresholds = validation_workflow_baseline_bundle[
"thresholds"]
139 "absolute_energy_error",
142 "observable_imag_abs",
143 "required_pass_rate",
144 "required_workflow_qubits",
145 "fixed_parameter_sets_per_size",
146 "documented_anchor_qubit",
149 field: contract_thresholds[field]
for field
in threshold_fields
151 workflow_thresholds[field] == contract_thresholds[field]
152 for field
in threshold_fields
153 if field
in workflow_thresholds
164 "workflow_completed",
166 "absolute_energy_error",
168 "density_valid_pass",
171 "bridge_supported_pass",
174 "counts_toward_mandatory_baseline",
177 "workflow_evidence_role",
178 "required_matrix_case",
180 missing_fields = [field
for field
in required_fields
if field
not in case]
183 "Matrix baseline case is missing required fields: {}".format(
184 ", ".join(missing_fields)
190 expected_case_names = set(requirements[
"required_case_names"])
191 expected_parameter_set_ids = set(requirements[
"mandatory_parameter_set_ids"])
193 observed_case_names = [case[
"case_name"]
for case
in cases]
194 case_counts = Counter(observed_case_names)
195 duplicate_case_names = sorted(
196 case_name
for case_name, count
in case_counts.items()
if count > 1
198 missing_mandatory_case_names = sorted(expected_case_names - set(observed_case_names))
199 unexpected_case_names = sorted(set(observed_case_names) - expected_case_names)
201 parameter_sets_per_qbit = defaultdict(list)
203 parameter_sets_per_qbit[case[
"qbit_num"]].append(case[
"parameter_set_id"])
205 missing_parameter_set_ids_by_qbit = {}
206 duplicate_parameter_set_ids_by_qbit = {}
208 for qbit_num
in requirements[
"mandatory_workflow_qubits"]:
209 observed_ids = parameter_sets_per_qbit.get(qbit_num, [])
210 counts = Counter(observed_ids)
211 duplicate_ids = sorted(
213 for parameter_set_id, count
in counts.items()
216 missing_ids = sorted(expected_parameter_set_ids - set(observed_ids))
217 duplicate_parameter_set_ids_by_qbit[str(qbit_num)] = duplicate_ids
218 missing_parameter_set_ids_by_qbit[str(qbit_num)] = missing_ids
219 cases_per_qbit[str(qbit_num)] = len(observed_ids)
221 stable_case_ids_present = (
222 not missing_mandatory_case_names
223 and not duplicate_case_names
224 and not unexpected_case_names
225 and len(observed_case_names) == len(expected_case_names)
227 stable_parameter_set_ids_present = all(
228 not missing_parameter_set_ids_by_qbit[str(qbit_num)]
229 and not duplicate_parameter_set_ids_by_qbit[str(qbit_num)]
230 for qbit_num
in requirements[
"mandatory_workflow_qubits"]
234 "observed_case_names": observed_case_names,
235 "missing_mandatory_case_names": missing_mandatory_case_names,
236 "duplicate_case_names": duplicate_case_names,
237 "unexpected_case_names": unexpected_case_names,
238 "stable_case_ids_present": stable_case_ids_present,
239 "cases_per_qbit": cases_per_qbit,
240 "missing_parameter_set_ids_by_qbit": missing_parameter_set_ids_by_qbit,
241 "duplicate_parameter_set_ids_by_qbit": duplicate_parameter_set_ids_by_qbit,
242 "stable_parameter_set_ids_present": stable_parameter_set_ids_present,
248 case[
"workflow_id"] = workflow_contract[
"workflow_id"]
249 case[
"contract_version"] = workflow_contract[
"contract_version"]
250 case[
"workflow_evidence_role"] =
"required_fixed_parameter_matrix" 251 case[
"required_matrix_case"] =
True 257 end_to_end_trace_bundle,
258 validation_workflow_baseline_bundle,
261 validation_workflow_baseline_bundle, workflow_contract
264 workflow_contract, validation_workflow_baseline_bundle
268 for case
in validation_workflow_baseline_bundle[
"cases"]
274 total_cases = len(cases)
275 passed_cases = sum(case[
"status"] ==
"pass" for case
in cases)
276 unsupported_cases = sum(case[
"status"] ==
"unsupported" for case
in cases)
277 bridge_supported_cases = sum(
278 case.get(
"bridge_supported_pass",
False)
for case
in cases
280 all_cases_required = all(case[
"support_tier"] ==
"required" for case
in cases)
281 all_cases_mandatory = all(
282 case[
"counts_toward_mandatory_baseline"]
for case
in cases
284 all_cases_match_contract = all(
285 case[
"workflow_id"] == workflow_contract[
"workflow_id"]
286 and case[
"contract_version"] == workflow_contract[
"contract_version"]
289 workflow_inventory_matches_contract =
bool(
290 validation_workflow_baseline_bundle[
"requirements"][
"mandatory_workflow_qubits"]
291 == requirements[
"mandatory_workflow_qubits"]
292 and validation_workflow_baseline_bundle[
"requirements"][
293 "fixed_parameter_sets_per_size" 295 == requirements[
"fixed_parameter_sets_per_size"]
297 documented_10q_anchor_present = any(
298 case[
"qbit_num"] == requirements[
"documented_anchor_qubit"]
for case
in cases
300 matrix_gate_completed =
bool(
301 workflow_contract[
"status"] ==
"pass" 302 and end_to_end_trace_bundle[
"status"] ==
"pass" 303 and validation_workflow_baseline_bundle[
"status"] ==
"pass" 304 and case_identity[
"stable_case_ids_present"]
305 and case_identity[
"stable_parameter_set_ids_present"]
306 and passed_cases == total_cases
307 and unsupported_cases == 0
308 and bridge_supported_cases == total_cases
309 and all_cases_required
310 and all_cases_mandatory
311 and all_cases_match_contract
312 and workflow_inventory_matches_contract
313 and workflow_thresholds_match_contract
314 and documented_10q_anchor_present
318 "suite_name": SUITE_NAME,
319 "status":
"pass" if matrix_gate_completed
else "fail",
320 "workflow_id": workflow_contract[
"workflow_id"],
321 "contract_version": workflow_contract[
"contract_version"],
322 "backend": workflow_contract[
"backend"],
323 "reference_backend": workflow_contract[
"reference_backend"],
324 "requirements": requirements,
325 "thresholds": threshold_metadata,
328 "generation_command": (
329 "python benchmarks/density_matrix/" 330 "workflow_evidence/matrix_baseline_validation.py" 332 "working_directory": str(REPO_ROOT),
334 "workflow_contract_path": str(WORKFLOW_CONTRACT_PATH),
335 "end_to_end_trace_bundle_path": str(END_TO_END_TRACE_BUNDLE_PATH),
336 "validation_workflow_baseline_path": str(
337 VALIDATION_WORKFLOW_BASELINE_PATH
341 "total_cases": total_cases,
342 "passed_cases": passed_cases,
343 "failed_cases": total_cases - passed_cases,
344 "unsupported_cases": unsupported_cases,
345 "bridge_supported_cases": bridge_supported_cases,
346 "required_cases": total_cases,
347 "required_passed_cases": passed_cases,
348 "required_pass_rate": (passed_cases / total_cases)
if total_cases
else 0.0,
349 "documented_10q_anchor_present": documented_10q_anchor_present,
350 "workflow_inventory_matches_contract": workflow_inventory_matches_contract,
351 "workflow_thresholds_match_contract": workflow_thresholds_match_contract,
352 "all_cases_required": all_cases_required,
353 "all_cases_count_toward_mandatory_baseline": all_cases_mandatory,
354 "all_cases_match_contract": all_cases_match_contract,
356 "matrix_gate_completed": matrix_gate_completed,
358 "required_artifacts": {
359 "workflow_contract": {
360 "suite_name": workflow_contract[
"suite_name"],
361 "status": workflow_contract[
"status"],
362 "workflow_id": workflow_contract[
"workflow_id"],
363 "contract_version": workflow_contract[
"contract_version"],
364 "thresholds": workflow_contract[
"thresholds"],
365 "summary": workflow_contract[
"summary"],
367 "end_to_end_trace_reference": {
368 "suite_name": end_to_end_trace_bundle[
"suite_name"],
369 "status": end_to_end_trace_bundle[
"status"],
370 "summary": end_to_end_trace_bundle[
"summary"],
372 "validation_workflow_baseline_reference": {
373 "suite_name": validation_workflow_baseline_bundle[
"suite_name"],
374 "status": validation_workflow_baseline_bundle[
"status"],
375 "summary": validation_workflow_baseline_bundle[
"summary"],
385 missing_fields = [field
for field
in ARTIFACT_CORE_FIELDS
if field
not in bundle]
388 "Matrix baseline bundle is missing required fields: {}".format(
389 ", ".join(missing_fields)
393 if bundle[
"workflow_id"] != WORKFLOW_ID:
395 "Matrix baseline bundle has unexpected workflow_id '{}'".format(
396 bundle[
"workflow_id"]
399 if bundle[
"contract_version"] != CONTRACT_VERSION:
401 "Matrix baseline bundle has unexpected contract_version '{}'".format(
402 bundle[
"contract_version"]
406 bundle[
"requirements"][
"mandatory_workflow_qubits"]
407 != bundle[
"thresholds"][
"required_workflow_qubits"]
410 "Matrix baseline bundle has inconsistent workflow-qubit requirements" 413 bundle[
"requirements"][
"fixed_parameter_sets_per_size"]
414 != bundle[
"thresholds"][
"fixed_parameter_sets_per_size"]
417 "Matrix baseline bundle has inconsistent parameter-set-count requirements" 420 bundle[
"requirements"][
"documented_anchor_qubit"]
421 != bundle[
"thresholds"][
"documented_anchor_qubit"]
424 "Matrix baseline bundle has inconsistent documented-anchor requirements" 426 if bundle[
"summary"][
"matrix_gate_completed"] != (bundle[
"status"] ==
"pass"):
428 "Matrix baseline bundle matrix_gate_completed summary is inconsistent" 430 for case
in bundle[
"cases"]:
432 if case[
"workflow_id"] != bundle[
"workflow_id"]:
434 "Matrix baseline case '{}' does not match bundle workflow_id".format(
438 if case[
"contract_version"] != bundle[
"contract_version"]:
440 "Matrix baseline case '{}' does not match bundle contract_version".format(
444 if not bundle[
"summary"][
"all_cases_match_contract"]
and bundle[
"status"] ==
"pass":
446 "Matrix baseline bundle cannot pass when matrix cases do not match the canonical contract" 449 bundle[
"summary"][
"workflow_inventory_matches_contract"]
is False 450 and bundle[
"status"] ==
"pass" 453 "Matrix baseline bundle cannot pass when matrix inventory drifts from workflow-contract metadata" 456 bundle[
"summary"][
"workflow_thresholds_match_contract"]
is False 457 and bundle[
"status"] ==
"pass" 460 "Matrix baseline bundle cannot pass when matrix thresholds drift from workflow-contract metadata" 466 output_path.parent.mkdir(parents=
True, exist_ok=
True)
467 output_path.write_text(
468 json.dumps(bundle, indent=2, sort_keys=
True) +
"\n", encoding=
"utf-8" 474 workflow_contract_path: Path = WORKFLOW_CONTRACT_PATH,
475 end_to_end_trace_bundle_path: Path = END_TO_END_TRACE_BUNDLE_PATH,
476 validation_workflow_baseline_path: Path = VALIDATION_WORKFLOW_BASELINE_PATH,
481 end_to_end_trace_bundle_path
483 validation_workflow_baseline_bundle =
_load_json(
484 validation_workflow_baseline_path
488 end_to_end_trace_bundle,
489 validation_workflow_baseline_bundle,
493 "{} [{}] matrix={}/{} 10q_anchor={}".format(
494 bundle[
"suite_name"],
496 bundle[
"summary"][
"required_passed_cases"],
497 bundle[
"summary"][
"required_cases"],
498 bundle[
"summary"][
"documented_10q_anchor_present"],
503 end_to_end_trace_bundle,
504 validation_workflow_baseline_bundle,
510 parser = argparse.ArgumentParser(description=__doc__)
514 default=DEFAULT_OUTPUT_DIR,
515 help=
"Directory for the matrix-baseline JSON artifact bundle.",
518 "--workflow-contract-path",
520 default=WORKFLOW_CONTRACT_PATH,
521 help=
"Path to the canonical workflow-contract artifact.",
524 "--end-to-end-trace-bundle-path",
526 default=END_TO_END_TRACE_BUNDLE_PATH,
527 help=
"Path to the end-to-end trace bundle.",
530 "--validation-workflow-baseline-path",
532 default=VALIDATION_WORKFLOW_BASELINE_PATH,
533 help=
"Path to the committed validation workflow-baseline bundle.",
538 help=
"Suppress summary output.",
540 return parser.parse_args()
546 workflow_contract_path=args.workflow_contract_path,
547 end_to_end_trace_bundle_path=args.end_to_end_trace_bundle_path,
548 validation_workflow_baseline_path=args.validation_workflow_baseline_path,
549 verbose=
not args.quiet,
551 output_path = args.output_dir / ARTIFACT_FILENAME
554 "Wrote {} with status {} ({}/{})".format(
557 bundle[
"summary"][
"required_passed_cases"],
558 bundle[
"summary"][
"required_cases"],
561 if bundle[
"status"] !=
"pass":
565 if __name__ ==
"__main__":
def validate_artifact_bundle(bundle)
def build_artifact_bundle(workflow_contract, end_to_end_trace_bundle, validation_workflow_baseline_bundle)
def validate_case_payload(case)
def _enrich_case(case, workflow_contract)
def _load_workflow_contract
def get_documented_anchor_qubit(workflow_contract)
def write_artifact_bundle
def build_threshold_metadata(workflow_contract, validation_workflow_baseline_bundle)
def get_required_workflow_qubits(workflow_contract)
def build_software_metadata()
def build_case_identity_summary(cases, requirements)
def get_required_parameter_set_count(workflow_contract)
def build_requirement_metadata(validation_workflow_baseline_bundle, workflow_contract)
def _load_end_to_end_trace_bundle