Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
workflow_interpretation_validation.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 """Validation: workflow interpretation guardrails.
3 
4 Builds the workflow-interpretation layer from:
5 - the emitted workflow-contract bundle,
6 - the emitted end-to-end trace bundle,
7 - the emitted matrix baseline bundle,
8 - the emitted unsupported-workflow bundle,
9 - and the committed optional evidence bundle.
10 
11 This layer is intentionally thin:
12 - it computes the main workflow claim only from mandatory complete supported
13  evidence,
14 - it keeps optional evidence explicitly supplemental,
15 - it keeps unsupported/deferred evidence explicitly negative,
16 - and it treats missing mandatory evidence as incomplete rather than partial
17  success.
18 
19 Run with:
20  python benchmarks/density_matrix/workflow_evidence/workflow_interpretation_validation.py
21 """
22 
23 from __future__ import annotations
24 
25 import argparse
26 import json
27 import sys
28 from pathlib import Path
29 
30 REPO_ROOT = Path(__file__).resolve().parents[3]
31 if str(REPO_ROOT) not in sys.path:
32  sys.path.insert(0, str(REPO_ROOT))
33 
34 from benchmarks.density_matrix.workflow_evidence.workflow_contract_validation import (
35  ARTIFACT_FILENAME as WORKFLOW_CONTRACT_ARTIFACT_FILENAME,
36  CONTRACT_VERSION,
37  DEFAULT_OUTPUT_DIR as WORKFLOW_EVIDENCE_OUTPUT_DIR,
38  WORKFLOW_ID,
39  build_software_metadata,
40  get_git_revision,
41  run_validation as run_workflow_contract_validation,
42  validate_artifact_bundle as validate_workflow_contract_artifact,
43 )
44 from benchmarks.density_matrix.workflow_evidence.end_to_end_trace_validation import (
45  ARTIFACT_FILENAME as END_TO_END_TRACE_ARTIFACT_FILENAME,
46  run_validation as run_end_to_end_trace_validation,
47  validate_artifact_bundle as validate_end_to_end_trace_artifact,
48 )
49 from benchmarks.density_matrix.workflow_evidence.matrix_baseline_validation import (
50  ARTIFACT_FILENAME as MATRIX_BASELINE_ARTIFACT_FILENAME,
51  run_validation as run_matrix_baseline_validation,
52  validate_artifact_bundle as validate_matrix_baseline_artifact,
53 )
54 from benchmarks.density_matrix.workflow_evidence.unsupported_workflow_validation import (
55  ARTIFACT_FILENAME as UNSUPPORTED_WORKFLOW_ARTIFACT_FILENAME,
56  run_validation as run_unsupported_workflow_validation,
57  validate_artifact_bundle as validate_unsupported_workflow_artifact,
58 )
59 
60 SUITE_NAME = "workflow_interpretation_validation"
61 ARTIFACT_FILENAME = "workflow_interpretation_bundle.json"
62 DEFAULT_OUTPUT_DIR = WORKFLOW_EVIDENCE_OUTPUT_DIR
63 WORKFLOW_CONTRACT_PATH = DEFAULT_OUTPUT_DIR / WORKFLOW_CONTRACT_ARTIFACT_FILENAME
64 END_TO_END_TRACE_BUNDLE_PATH = DEFAULT_OUTPUT_DIR / END_TO_END_TRACE_ARTIFACT_FILENAME
65 MATRIX_BASELINE_BUNDLE_PATH = DEFAULT_OUTPUT_DIR / MATRIX_BASELINE_ARTIFACT_FILENAME
66 UNSUPPORTED_WORKFLOW_BUNDLE_PATH = (
67  DEFAULT_OUTPUT_DIR / UNSUPPORTED_WORKFLOW_ARTIFACT_FILENAME
68 )
69 OPTIONAL_NOISE_BUNDLE_PATH = (
70  REPO_ROOT
71  / "benchmarks"
72  / "density_matrix"
73  / "artifacts"
74  / "noise_support"
75  / "optional_noise_classification_bundle.json"
76 )
77 ARTIFACT_CORE_FIELDS = (
78  "suite_name",
79  "status",
80  "workflow_id",
81  "contract_version",
82  "backend",
83  "reference_backend",
84  "requirements",
85  "thresholds",
86  "software",
87  "provenance",
88  "summary",
89  "required_artifacts",
90 )
91 
92 
93 def _load_json(path: Path):
94  return json.loads(path.read_text(encoding="utf-8"))
95 
96 
97 def _load_workflow_contract(path: Path = WORKFLOW_CONTRACT_PATH):
98  if path.exists():
99  artifact = _load_json(path)
100  validate_workflow_contract_artifact(artifact)
101  return artifact
102  _, artifact = run_workflow_contract_validation(verbose=False)
103  return artifact
104 
105 
106 def _load_end_to_end_trace_bundle(path: Path = END_TO_END_TRACE_BUNDLE_PATH):
107  if path.exists():
108  artifact = _load_json(path)
109  validate_end_to_end_trace_artifact(artifact)
110  return artifact
111  _, _, _, artifact = run_end_to_end_trace_validation(verbose=False)
112  return artifact
113 
114 
115 def _load_matrix_baseline_bundle(path: Path = MATRIX_BASELINE_BUNDLE_PATH):
116  if path.exists():
117  artifact = _load_json(path)
118  validate_matrix_baseline_artifact(artifact)
119  return artifact
120  _, _, _, artifact = run_matrix_baseline_validation(verbose=False)
121  return artifact
122 
123 
124 def _load_unsupported_workflow_bundle(path: Path = UNSUPPORTED_WORKFLOW_BUNDLE_PATH):
125  if path.exists():
126  artifact = _load_json(path)
127  validate_unsupported_workflow_artifact(artifact)
128  return artifact
129  *_, artifact = run_unsupported_workflow_validation(verbose=False)
130  return artifact
131 
132 
133 def build_requirement_metadata(workflow_contract):
134  return {
135  "workflow_id": workflow_contract["workflow_id"],
136  "contract_version": workflow_contract["contract_version"],
137  "main_claim_rule": (
138  "Only mandatory, complete, supported evidence may close the main "
139  "workflow claim."
140  ),
141  "excluded_evidence_classes": [
142  "optional",
143  "deferred",
144  "unsupported",
145  "incomplete",
146  ],
147  "required_bundle_sources": [
148  "workflow_contract_validation",
149  "end_to_end_trace_validation",
150  "matrix_baseline_validation",
151  "unsupported_workflow_validation",
152  "optional_noise_classification",
153  ],
154  }
155 
156 
158  workflow_contract,
159  end_to_end_trace_bundle,
160  matrix_baseline_bundle,
161  unsupported_workflow_bundle,
162  optional_noise_bundle,
163 ):
164  incomplete_mandatory_artifacts = []
165  mandatory_artifacts = {
166  "workflow_contract": workflow_contract,
167  "end_to_end_trace_reference": end_to_end_trace_bundle,
168  "matrix_baseline_reference": matrix_baseline_bundle,
169  "unsupported_workflow_reference": unsupported_workflow_bundle,
170  }
171  for artifact_id, artifact in mandatory_artifacts.items():
172  if artifact["status"] != "pass":
173  incomplete_mandatory_artifacts.append(artifact_id)
174 
175  workflow_contract_complete = bool(
176  workflow_contract["summary"].get("contract_sections_complete", False)
177  )
178  end_to_end_gate_complete = bool(
179  end_to_end_trace_bundle["summary"].get("end_to_end_gate_completed", False)
180  )
181  matrix_baseline_gate_complete = bool(
182  matrix_baseline_bundle["summary"].get("matrix_gate_completed", False)
183  )
184  unsupported_workflow_gate_complete = bool(
185  unsupported_workflow_bundle["summary"].get("unsupported_gate_completed", False)
186  )
187  unsupported_case_field_alignment = (
188  unsupported_workflow_bundle["requirements"]["required_case_fields"]
189  == workflow_contract["output_contract"]["required_unsupported_case_fields"]
190  )
191  mandatory_artifacts_complete = bool(
192  not incomplete_mandatory_artifacts
193  and workflow_contract_complete
194  and end_to_end_gate_complete
195  and matrix_baseline_gate_complete
196  and unsupported_workflow_gate_complete
197  )
198  optional_evidence_supplemental = bool(
199  optional_noise_bundle["status"] == "pass"
200  and optional_noise_bundle["summary"][
201  "optional_cases_count_toward_mandatory_baseline"
202  ]
203  == 0
204  )
205  unsupported_evidence_negative_only = bool(
206  unsupported_workflow_bundle["status"] == "pass"
207  and unsupported_workflow_bundle["summary"]["unsupported_status_cases"]
208  == unsupported_workflow_bundle["summary"]["total_cases"]
209  and unsupported_workflow_bundle["summary"]["mandatory_baseline_case_count"]
210  == 0
211  and unsupported_case_field_alignment
212  )
213  main_workflow_claim_completed = bool(
214  mandatory_artifacts_complete
215  and optional_evidence_supplemental
216  and unsupported_evidence_negative_only
217  )
218 
219  bundle = {
220  "suite_name": SUITE_NAME,
221  "status": "pass" if main_workflow_claim_completed else "fail",
222  "workflow_id": workflow_contract["workflow_id"],
223  "contract_version": workflow_contract["contract_version"],
224  "backend": workflow_contract["backend"],
225  "reference_backend": workflow_contract["reference_backend"],
226  "requirements": build_requirement_metadata(workflow_contract),
227  "thresholds": {
228  "mandatory_completion_rule": "all_mandatory_artifacts_pass",
229  "optional_cases_count_toward_mandatory_baseline": 0,
230  "mandatory_baseline_case_count_for_negative_evidence": 0,
231  },
232  "software": build_software_metadata(),
233  "provenance": {
234  "generation_command": (
235  "python benchmarks/density_matrix/"
236  "workflow_evidence/workflow_interpretation_validation.py"
237  ),
238  "working_directory": str(REPO_ROOT),
239  "git_revision": get_git_revision(),
240  "workflow_contract_path": str(WORKFLOW_CONTRACT_PATH),
241  "end_to_end_trace_bundle_path": str(END_TO_END_TRACE_BUNDLE_PATH),
242  "matrix_baseline_bundle_path": str(MATRIX_BASELINE_BUNDLE_PATH),
243  "unsupported_workflow_bundle_path": str(
244  UNSUPPORTED_WORKFLOW_BUNDLE_PATH
245  ),
246  "optional_noise_bundle_path": str(OPTIONAL_NOISE_BUNDLE_PATH),
247  },
248  "summary": {
249  "mandatory_artifacts": list(mandatory_artifacts.keys()),
250  "incomplete_mandatory_artifacts": incomplete_mandatory_artifacts,
251  "workflow_contract_complete": workflow_contract_complete,
252  "end_to_end_gate_complete": end_to_end_gate_complete,
253  "matrix_baseline_gate_complete": matrix_baseline_gate_complete,
254  "unsupported_workflow_gate_complete": unsupported_workflow_gate_complete,
255  "unsupported_case_field_alignment": unsupported_case_field_alignment,
256  "mandatory_artifacts_complete": mandatory_artifacts_complete,
257  "optional_cases": optional_noise_bundle["summary"]["optional_cases"],
258  "optional_passed_cases": optional_noise_bundle["summary"][
259  "optional_passed_cases"
260  ],
261  "optional_cases_count_toward_mandatory_baseline": optional_noise_bundle[
262  "summary"
263  ]["optional_cases_count_toward_mandatory_baseline"],
264  "optional_evidence_supplemental": optional_evidence_supplemental,
265  "unsupported_status_cases": unsupported_workflow_bundle["summary"][
266  "unsupported_status_cases"
267  ],
268  "unsupported_cases": unsupported_workflow_bundle["summary"][
269  "unsupported_cases"
270  ],
271  "deferred_cases": unsupported_workflow_bundle["summary"]["deferred_cases"],
272  "unsupported_evidence_negative_only": unsupported_evidence_negative_only,
273  "main_workflow_claim_completed": main_workflow_claim_completed,
274  },
275  "required_artifacts": {
276  "workflow_contract": {
277  "suite_name": workflow_contract["suite_name"],
278  "status": workflow_contract["status"],
279  "summary": workflow_contract["summary"],
280  },
281  "end_to_end_trace_reference": {
282  "suite_name": end_to_end_trace_bundle["suite_name"],
283  "status": end_to_end_trace_bundle["status"],
284  "summary": end_to_end_trace_bundle["summary"],
285  },
286  "matrix_baseline_reference": {
287  "suite_name": matrix_baseline_bundle["suite_name"],
288  "status": matrix_baseline_bundle["status"],
289  "summary": matrix_baseline_bundle["summary"],
290  },
291  "unsupported_workflow_reference": {
292  "suite_name": unsupported_workflow_bundle["suite_name"],
293  "status": unsupported_workflow_bundle["status"],
294  "summary": unsupported_workflow_bundle["summary"],
295  },
296  "optional_noise_reference": {
297  "suite_name": optional_noise_bundle["suite_name"],
298  "status": optional_noise_bundle["status"],
299  "summary": optional_noise_bundle["summary"],
300  },
301  },
302  }
304  return bundle
305 
306 
308  missing_fields = [field for field in ARTIFACT_CORE_FIELDS if field not in bundle]
309  if missing_fields:
310  raise ValueError(
311  "Workflow-interpretation bundle is missing required fields: {}".format(
312  ", ".join(missing_fields)
313  )
314  )
315 
316  if bundle["workflow_id"] != WORKFLOW_ID:
317  raise ValueError(
318  "Workflow-interpretation bundle has unexpected workflow_id '{}'".format(
319  bundle["workflow_id"]
320  )
321  )
322  if bundle["contract_version"] != CONTRACT_VERSION:
323  raise ValueError(
324  "Workflow-interpretation bundle has unexpected contract_version '{}'".format(
325  bundle["contract_version"]
326  )
327  )
328  if bundle["summary"]["main_workflow_claim_completed"] != (bundle["status"] == "pass"):
329  raise ValueError(
330  "Workflow-interpretation bundle main_workflow_claim_completed summary is inconsistent"
331  )
332 
333 
334 def write_artifact_bundle(output_path: Path, bundle):
336  output_path.parent.mkdir(parents=True, exist_ok=True)
337  output_path.write_text(
338  json.dumps(bundle, indent=2, sort_keys=True) + "\n", encoding="utf-8"
339  )
340 
341 
342 def run_validation(
343  *,
344  workflow_contract_path: Path = WORKFLOW_CONTRACT_PATH,
345  end_to_end_trace_bundle_path: Path = END_TO_END_TRACE_BUNDLE_PATH,
346  matrix_baseline_bundle_path: Path = MATRIX_BASELINE_BUNDLE_PATH,
347  unsupported_workflow_bundle_path: Path = UNSUPPORTED_WORKFLOW_BUNDLE_PATH,
348  optional_noise_bundle_path: Path = OPTIONAL_NOISE_BUNDLE_PATH,
349  verbose=False,
350 ):
351  workflow_contract = _load_workflow_contract(workflow_contract_path)
352  end_to_end_trace_bundle = _load_end_to_end_trace_bundle(
353  end_to_end_trace_bundle_path
354  )
355  matrix_baseline_bundle = _load_matrix_baseline_bundle(matrix_baseline_bundle_path)
356  unsupported_workflow_bundle = _load_unsupported_workflow_bundle(
357  unsupported_workflow_bundle_path
358  )
359  optional_noise_bundle = _load_json(optional_noise_bundle_path)
360  bundle = build_artifact_bundle(
361  workflow_contract,
362  end_to_end_trace_bundle,
363  matrix_baseline_bundle,
364  unsupported_workflow_bundle,
365  optional_noise_bundle,
366  )
367  if verbose:
368  print(
369  "{} [{}] mandatory_complete={} optional_supplemental={} unsupported_negative_only={}".format(
370  bundle["suite_name"],
371  bundle["status"],
372  bundle["summary"]["mandatory_artifacts_complete"],
373  bundle["summary"]["optional_evidence_supplemental"],
374  bundle["summary"]["unsupported_evidence_negative_only"],
375  )
376  )
377  return (
378  workflow_contract,
379  end_to_end_trace_bundle,
380  matrix_baseline_bundle,
381  unsupported_workflow_bundle,
382  optional_noise_bundle,
383  bundle,
384  )
385 
386 
388  parser = argparse.ArgumentParser(description=__doc__)
389  parser.add_argument(
390  "--output-dir",
391  type=Path,
392  default=DEFAULT_OUTPUT_DIR,
393  help="Directory for the workflow-interpretation JSON artifact bundle.",
394  )
395  parser.add_argument(
396  "--workflow-contract-path",
397  type=Path,
398  default=WORKFLOW_CONTRACT_PATH,
399  help="Path to the canonical workflow-contract artifact.",
400  )
401  parser.add_argument(
402  "--end-to-end-trace-bundle-path",
403  type=Path,
404  default=END_TO_END_TRACE_BUNDLE_PATH,
405  help="Path to the end-to-end trace bundle.",
406  )
407  parser.add_argument(
408  "--matrix-baseline-bundle-path",
409  type=Path,
410  default=MATRIX_BASELINE_BUNDLE_PATH,
411  help="Path to the matrix-baseline bundle.",
412  )
413  parser.add_argument(
414  "--unsupported-workflow-bundle-path",
415  type=Path,
416  default=UNSUPPORTED_WORKFLOW_BUNDLE_PATH,
417  help="Path to the unsupported-workflow bundle.",
418  )
419  parser.add_argument(
420  "--optional-noise-bundle-path",
421  type=Path,
422  default=OPTIONAL_NOISE_BUNDLE_PATH,
423  help="Path to the committed optional evidence bundle.",
424  )
425  parser.add_argument(
426  "--quiet",
427  action="store_true",
428  help="Suppress summary output.",
429  )
430  return parser.parse_args()
431 
432 
433 def main():
434  args = parse_args()
435  *_, bundle = run_validation(
436  workflow_contract_path=args.workflow_contract_path,
437  end_to_end_trace_bundle_path=args.end_to_end_trace_bundle_path,
438  matrix_baseline_bundle_path=args.matrix_baseline_bundle_path,
439  unsupported_workflow_bundle_path=args.unsupported_workflow_bundle_path,
440  optional_noise_bundle_path=args.optional_noise_bundle_path,
441  verbose=not args.quiet,
442  )
443  output_path = args.output_dir / ARTIFACT_FILENAME
444  write_artifact_bundle(output_path, bundle)
445  print(
446  "Wrote {} with status {} ({})".format(
447  output_path,
448  bundle["status"],
449  bundle["summary"]["main_workflow_claim_completed"],
450  )
451  )
452  if bundle["status"] != "pass":
453  raise SystemExit(1)
454 
455 
456 if __name__ == "__main__":
457  main()
def build_software_metadata()
def build_artifact_bundle(workflow_contract, end_to_end_trace_bundle, matrix_baseline_bundle, unsupported_workflow_bundle, optional_noise_bundle)