2 """Validation: end-to-end workflow plus trace gate. 4 Builds the end-to-end execution gate from: 5 - the emitted canonical workflow contract, 6 - the committed validation workflow baseline bundle, from which the canonical 4q 7 and 6q end-to-end cases are selected, 8 - and the committed validation trace artifact. 10 This layer is intentionally thin: 11 - it freezes one 4q case, one 6q case, and one required trace as the mandatory 13 - it ties those evidence items to the workflow-contract identity, 14 - and it makes missing or malformed required evidence fail explicitly. 17 python benchmarks/density_matrix/workflow_evidence/end_to_end_trace_validation.py 20 from __future__
import annotations
25 from collections
import Counter
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,
39 build_software_metadata,
41 run_validation
as run_workflow_contract_validation,
42 validate_artifact_bundle
as validate_workflow_contract_artifact,
45 SUITE_NAME =
"end_to_end_trace_validation" 46 ARTIFACT_FILENAME =
"end_to_end_trace_bundle.json" 47 DEFAULT_OUTPUT_DIR = WORKFLOW_EVIDENCE_OUTPUT_DIR
48 WORKFLOW_CONTRACT_PATH = DEFAULT_OUTPUT_DIR / WORKFLOW_CONTRACT_ARTIFACT_FILENAME
49 VALIDATION_WORKFLOW_BASELINE_PATH = (
54 /
"validation_evidence" 55 /
"workflow_baseline_bundle.json" 57 VALIDATION_TRACE_ARTIFACT_PATH = (
62 /
"validation_evidence" 63 /
"optimization_trace_4q.json" 65 MANDATORY_END_TO_END_CASE_NAMES = (
"exact_regime_4q_set_00",
"exact_regime_6q_set_00")
66 MANDATORY_END_TO_END_QUBITS = (4, 6)
67 TRACE_CASE_NAME =
"optimization_trace_4q" 68 CANONICAL_END_TO_END_PARAMETER_SET_ID =
"set_00" 69 ARTIFACT_CORE_FIELDS = (
88 return json.loads(path.read_text(encoding=
"utf-8"))
94 validate_workflow_contract_artifact(artifact)
96 _, artifact = run_workflow_contract_validation(verbose=
False)
101 return tuple(workflow_contract[
"thresholds"][
"required_end_to_end_qubits"])
105 return workflow_contract[
"input_contract"][
"execution_modes"][
106 "bounded_optimization_trace" 107 ][
"canonical_trace_case_name"]
112 f
"exact_regime_{qbit_num}q_{CANONICAL_END_TO_END_PARAMETER_SET_ID}" 122 "workflow_id": workflow_contract[
"workflow_id"],
123 "contract_version": workflow_contract[
"contract_version"],
124 "mandatory_end_to_end_case_names": list(mandatory_case_names),
125 "mandatory_end_to_end_qubits": list(mandatory_end_to_end_qubits),
126 "required_trace_case_name": required_trace_case_name,
127 "status_vocabulary": list(STATUS_VOCABULARY),
128 "required_bundle_sources": [
129 workflow_contract[
"suite_name"],
130 "workflow_baseline_validation",
131 "trace_anchor_validation",
137 workflow_thresholds = workflow_bundle[
"thresholds"]
138 contract_thresholds = workflow_contract[
"thresholds"]
140 "absolute_energy_error": contract_thresholds[
"absolute_energy_error"],
141 "rho_is_valid_tol": contract_thresholds[
"rho_is_valid_tol"],
142 "trace_deviation": contract_thresholds[
"trace_deviation"],
143 "observable_imag_abs": contract_thresholds[
"observable_imag_abs"],
144 "required_pass_rate": contract_thresholds[
"required_pass_rate"],
145 "required_end_to_end_qubits": list(
146 contract_thresholds[
"required_end_to_end_qubits"]
149 "workflow_thresholds_match_contract": all(
150 workflow_thresholds[field] == contract_thresholds[field]
152 "absolute_energy_error",
155 "observable_imag_abs",
156 "required_pass_rate",
169 "workflow_completed",
171 "absolute_energy_error",
173 "density_valid_pass",
176 "bridge_supported_pass",
179 "counts_toward_mandatory_baseline",
182 "workflow_evidence_role",
183 "required_workflow_case",
185 missing_fields = [field
for field
in required_fields
if field
not in case]
188 "End-to-end trace case is missing required fields: {}".format(
189 ", ".join(missing_fields)
200 "workflow_completed",
201 "bridge_supported_pass",
206 "energy_improvement",
207 "total_trace_runtime_ms",
208 "process_peak_rss_kb",
211 "workflow_evidence_role",
212 "required_workflow_trace",
215 field
for field
in required_fields
if field
not in trace_artifact
219 "End-to-end trace artifact is missing required fields: {}".format(
220 ", ".join(missing_fields)
226 case_names = [case[
"case_name"]
for case
in cases]
227 counts = Counter(case_names)
228 duplicate_case_names = sorted(
229 case_name
for case_name, count
in counts.items()
if count > 1
231 missing_mandatory_case_names = sorted(
232 set(mandatory_case_names) - set(case_names)
234 unexpected_case_names = sorted(
235 set(case_names) - set(mandatory_case_names)
237 observed_qubits = sorted(case[
"qbit_num"]
for case
in cases)
238 missing_mandatory_qubits = sorted(set(mandatory_qubits) - set(observed_qubits))
239 unexpected_mandatory_qubits = sorted(
240 set(observed_qubits) - set(mandatory_qubits)
242 stable_case_ids_present = (
243 not duplicate_case_names
244 and not missing_mandatory_case_names
245 and not unexpected_case_names
246 and not missing_mandatory_qubits
247 and not unexpected_mandatory_qubits
248 and len(case_names) == len(mandatory_case_names)
251 "observed_case_names": case_names,
252 "observed_qubits": observed_qubits,
253 "missing_mandatory_case_names": missing_mandatory_case_names,
254 "missing_mandatory_qubits": missing_mandatory_qubits,
255 "duplicate_case_names": duplicate_case_names,
256 "unexpected_case_names": unexpected_case_names,
257 "unexpected_mandatory_qubits": unexpected_mandatory_qubits,
258 "stable_case_ids_present": stable_case_ids_present,
264 case[
"workflow_id"] = workflow_contract[
"workflow_id"]
265 case[
"contract_version"] = workflow_contract[
"contract_version"]
266 case[
"workflow_evidence_role"] =
"required_end_to_end" 267 case[
"required_workflow_case"] =
True 272 trace_artifact = dict(trace_artifact)
273 trace_artifact[
"workflow_id"] = workflow_contract[
"workflow_id"]
274 trace_artifact[
"contract_version"] = workflow_contract[
"contract_version"]
275 trace_artifact[
"workflow_evidence_role"] =
"required_trace" 276 trace_artifact[
"required_workflow_trace"] =
True 277 return trace_artifact
284 case
for case
in workflow_bundle[
"cases"]
if case[
"case_name"] == case_name
287 selected_cases.append(
290 return selected_cases
305 cases, mandatory_case_names, mandatory_end_to_end_qubits
308 total_cases = len(cases)
309 passed_end_to_end_cases = sum(
311 case[
"status"] ==
"pass" 312 and case[
"workflow_completed"]
313 and case[
"bridge_supported_pass"]
317 unsupported_cases = sum(case[
"status"] ==
"unsupported" for case
in cases)
318 all_cases_required = all(case[
"support_tier"] ==
"required" for case
in cases)
319 all_cases_mandatory = all(
320 case[
"counts_toward_mandatory_baseline"]
for case
in cases
322 all_cases_match_contract = all(
323 case[
"workflow_id"] == workflow_contract[
"workflow_id"]
324 and case[
"contract_version"] == workflow_contract[
"contract_version"]
327 end_to_end_qubits_match_contract = sorted(
328 case[
"qbit_num"]
for case
in cases
329 ) == sorted(mandatory_end_to_end_qubits)
330 required_trace_present = trace_artifact[
"case_name"] == required_trace_case_name
and bool(
331 trace_artifact[
"required_workflow_trace"]
333 required_trace_completed =
bool(
334 trace_artifact[
"status"] ==
"completed" 335 and trace_artifact[
"workflow_completed"]
337 required_trace_bridge_supported =
bool(
338 trace_artifact[
"bridge_supported_pass"]
340 trace_case_name_matches_contract = (
341 trace_artifact[
"case_name"] == required_trace_case_name
343 trace_matches_contract =
bool(
344 trace_artifact[
"workflow_id"] == workflow_contract[
"workflow_id"]
345 and trace_artifact[
"contract_version"] == workflow_contract[
"contract_version"]
347 end_to_end_gate_completed =
bool(
348 workflow_contract[
"status"] ==
"pass" 349 and case_identity[
"stable_case_ids_present"]
350 and passed_end_to_end_cases == len(mandatory_case_names)
351 and unsupported_cases == 0
352 and all_cases_required
353 and all_cases_mandatory
354 and all_cases_match_contract
355 and end_to_end_qubits_match_contract
356 and required_trace_present
357 and required_trace_completed
358 and required_trace_bridge_supported
359 and trace_case_name_matches_contract
360 and trace_matches_contract
361 and threshold_metadata[
"workflow_thresholds_match_contract"]
365 "suite_name": SUITE_NAME,
366 "status":
"pass" if end_to_end_gate_completed
else "fail",
367 "workflow_id": workflow_contract[
"workflow_id"],
368 "contract_version": workflow_contract[
"contract_version"],
369 "backend": workflow_contract[
"backend"],
370 "reference_backend": workflow_contract[
"reference_backend"],
372 "thresholds": threshold_metadata,
375 "generation_command": (
376 "python benchmarks/density_matrix/" 377 "workflow_evidence/end_to_end_trace_validation.py" 379 "working_directory": str(REPO_ROOT),
381 "workflow_contract_path": str(WORKFLOW_CONTRACT_PATH),
382 "validation_workflow_baseline_path": str(
383 VALIDATION_WORKFLOW_BASELINE_PATH
385 "validation_trace_artifact_path": str(VALIDATION_TRACE_ARTIFACT_PATH),
388 "total_end_to_end_cases": total_cases,
389 "passed_end_to_end_cases": passed_end_to_end_cases,
390 "failed_end_to_end_cases": total_cases - passed_end_to_end_cases,
391 "unsupported_end_to_end_cases": unsupported_cases,
393 "all_cases_required": all_cases_required,
394 "all_cases_count_toward_mandatory_baseline": all_cases_mandatory,
395 "all_cases_match_contract": all_cases_match_contract,
396 "end_to_end_qubits_match_contract": end_to_end_qubits_match_contract,
397 "required_trace_case_name": trace_artifact[
"case_name"],
398 "required_trace_present": required_trace_present,
399 "required_trace_completed": required_trace_completed,
400 "required_trace_bridge_supported": required_trace_bridge_supported,
401 "trace_case_name_matches_contract": trace_case_name_matches_contract,
402 "trace_matches_contract": trace_matches_contract,
403 "workflow_thresholds_match_contract": threshold_metadata[
404 "workflow_thresholds_match_contract" 406 "end_to_end_gate_completed": end_to_end_gate_completed,
408 "required_artifacts": {
409 "workflow_contract": {
410 "suite_name": workflow_contract[
"suite_name"],
411 "status": workflow_contract[
"status"],
412 "workflow_id": workflow_contract[
"workflow_id"],
413 "contract_version": workflow_contract[
"contract_version"],
414 "thresholds": workflow_contract[
"thresholds"],
415 "summary": workflow_contract[
"summary"],
417 "validation_workflow_baseline_reference": {
418 "suite_name": workflow_bundle[
"suite_name"],
419 "status": workflow_bundle[
"status"],
420 "summary": workflow_bundle[
"summary"],
422 "validation_trace_artifact": {
423 "case_name": trace_artifact[
"case_name"],
424 "status": trace_artifact[
"status"],
425 "workflow_completed": trace_artifact[
"workflow_completed"],
429 "trace_artifact": trace_artifact,
436 missing_fields = [field
for field
in ARTIFACT_CORE_FIELDS
if field
not in bundle]
439 "End-to-end trace bundle is missing required fields: {}".format(
440 ", ".join(missing_fields)
444 if bundle[
"workflow_id"] != WORKFLOW_ID:
446 "End-to-end trace bundle has unexpected workflow_id '{}'".format(
447 bundle[
"workflow_id"]
450 if bundle[
"contract_version"] != CONTRACT_VERSION:
452 "End-to-end trace bundle has unexpected contract_version '{}'".format(
453 bundle[
"contract_version"]
457 bundle[
"requirements"][
"mandatory_end_to_end_qubits"]
458 != bundle[
"thresholds"][
"required_end_to_end_qubits"]
461 "End-to-end trace bundle has inconsistent end-to-end qubit requirements" 464 bundle[
"requirements"][
"required_trace_case_name"]
465 != bundle[
"thresholds"][
"required_trace_case_name"]
468 "End-to-end trace bundle has inconsistent trace-case requirements" 470 if bundle[
"summary"][
"stable_case_ids_present"]
is False and bundle[
"status"] ==
"pass":
472 "End-to-end trace bundle cannot pass without stable mandatory case IDs" 475 bundle[
"summary"][
"end_to_end_qubits_match_contract"]
is False 476 and bundle[
"status"] ==
"pass" 479 "End-to-end trace bundle cannot pass without matching workflow-contract end-to-end qubit requirements" 482 bundle[
"summary"][
"trace_case_name_matches_contract"]
is False 483 and bundle[
"status"] ==
"pass" 486 "End-to-end trace bundle cannot pass without matching workflow-contract trace-case requirements" 489 bundle[
"summary"][
"workflow_thresholds_match_contract"]
is False 490 and bundle[
"status"] ==
"pass" 493 "End-to-end trace bundle cannot pass when workflow thresholds drift from workflow-contract metadata" 495 if bundle[
"summary"][
"end_to_end_gate_completed"] != (bundle[
"status"] ==
"pass"):
497 "End-to-end trace bundle end_to_end_gate_completed summary is inconsistent" 503 output_path.parent.mkdir(parents=
True, exist_ok=
True)
504 output_path.write_text(
505 json.dumps(bundle, indent=2, sort_keys=
True) +
"\n", encoding=
"utf-8" 511 workflow_contract_path: Path = WORKFLOW_CONTRACT_PATH,
512 workflow_bundle_path: Path = VALIDATION_WORKFLOW_BASELINE_PATH,
513 trace_artifact_path: Path = VALIDATION_TRACE_ARTIFACT_PATH,
517 workflow_bundle =
_load_json(workflow_bundle_path)
518 trace_artifact =
_load_json(trace_artifact_path)
522 "{} [{}] end_to_end={}/{} trace={}".format(
523 bundle[
"suite_name"],
525 bundle[
"summary"][
"passed_end_to_end_cases"],
526 bundle[
"summary"][
"total_end_to_end_cases"],
527 bundle[
"summary"][
"required_trace_completed"],
530 return workflow_contract, workflow_bundle, trace_artifact, bundle
534 parser = argparse.ArgumentParser(description=__doc__)
538 default=DEFAULT_OUTPUT_DIR,
539 help=
"Directory for the end-to-end trace JSON artifact bundle.",
542 "--workflow-contract-path",
544 default=WORKFLOW_CONTRACT_PATH,
545 help=
"Path to the canonical workflow-contract artifact.",
548 "--workflow-bundle-path",
550 default=VALIDATION_WORKFLOW_BASELINE_PATH,
551 help=
"Path to the committed validation workflow-baseline bundle.",
554 "--trace-artifact-path",
556 default=VALIDATION_TRACE_ARTIFACT_PATH,
557 help=
"Path to the committed validation trace artifact.",
562 help=
"Suppress summary output.",
564 return parser.parse_args()
570 workflow_contract_path=args.workflow_contract_path,
571 workflow_bundle_path=args.workflow_bundle_path,
572 trace_artifact_path=args.trace_artifact_path,
573 verbose=
not args.quiet,
575 output_path = args.output_dir / ARTIFACT_FILENAME
578 "Wrote {} with status {} ({}/{})".format(
581 bundle[
"summary"][
"passed_end_to_end_cases"],
582 bundle[
"summary"][
"total_end_to_end_cases"],
585 if bundle[
"status"] !=
"pass":
589 if __name__ ==
"__main__":
def build_case_identity_summary(cases, mandatory_case_names, mandatory_qubits)
def build_artifact_bundle(workflow_contract, workflow_bundle, trace_artifact)
def get_required_end_to_end_qubits(workflow_contract)
def build_threshold_metadata(workflow_contract, workflow_bundle)
def build_mandatory_end_to_end_case_names(workflow_contract)
def build_requirement_metadata(workflow_contract)
def validate_end_to_end_case_payload(case)
def _enrich_end_to_end_case(case, workflow_contract)
def _extract_mandatory_cases(workflow_bundle, workflow_contract)
def validate_artifact_bundle(bundle)
def _load_workflow_contract
def validate_trace_artifact(trace_artifact)
def get_required_trace_case_name(workflow_contract)
def build_software_metadata()
def _enrich_trace_artifact(trace_artifact, workflow_contract)
def write_artifact_bundle