2 """Validation: unsupported bridge cases. 4 Captures representative unsupported bridge requests as structured artifacts. The 5 goal is to prove that unsupported circuit-source, lowering, and noise-boundary 6 cases fail before execution and surface a stable first unsupported condition. 9 python benchmarks/density_matrix/bridge_scope/unsupported_bridge_validation.py 12 from __future__
import annotations
17 from pathlib
import Path
21 REPO_ROOT = Path(__file__).resolve().parents[3]
22 if str(REPO_ROOT)
not in sys.path:
23 sys.path.insert(0, str(REPO_ROOT))
25 from examples.VQE.shot_noise_measurement
import generate_zz_xx_hamiltonian
26 from benchmarks.density_matrix.workflow_evidence.exact_density_vqe_validation
import (
32 build_software_metadata,
33 build_optimizer_config,
34 build_hamiltonian_metadata,
35 build_initial_parameters,
36 build_open_chain_topology,
38 from squander
import Variational_Quantum_Eigensolver
41 SUITE_NAME =
"unsupported_bridge_validation" 42 ARTIFACT_FILENAME =
"unsupported_bridge_bundle.json" 43 ARTIFACT_CORE_FIELDS = (
56 "expected_status":
"unsupported",
57 "required_categories": [
63 "canonical_bridge_fields": [
65 "unsupported_category",
66 "first_unsupported_condition",
71 "operation_match_pass",
78 if density_noise
is None:
89 vqe = Variational_Quantum_Eigensolver(
93 backend=PRIMARY_BACKEND,
94 density_noise=density_noise,
96 vqe.set_Ansatz(ansatz)
107 unsupported_category: str,
108 first_unsupported_condition: str,
109 bridge_source_type: str,
110 ansatz: str = DEFAULT_ANSATZ,
113 backend=PRIMARY_BACKEND,
116 density_noise=density_noise,
122 "case_name": case_name,
123 "case_kind":
"unsupported_bridge_validation",
125 "bridge_source_type": bridge_source_type,
126 "unsupported_category": unsupported_category,
127 "first_unsupported_condition": first_unsupported_condition,
128 "source_pass":
False,
131 "operation_match_pass":
False,
132 "execution_ready":
False,
142 custom_circuit.add_H(0)
143 custom_circuit.add_CNOT(1, 0)
144 vqe.set_Gate_Structure(custom_circuit)
147 case_name=
"unsupported_bridge_custom_gate_structure_source",
151 purpose=
"Verify a custom manual circuit source fails before bridge execution with a source-category unsupported result.",
152 unsupported_category=
"circuit_source",
153 first_unsupported_condition=
"custom_gate_structure",
154 bridge_source_type=
"custom_gate_structure",
158 vqe.describe_density_bridge()
160 return metadata, runner,
"unsupported circuit source in density backend path: custom_gate_structure" 170 vqe.Generate_Circuit(DEFAULT_LAYERS, DEFAULT_INNER_BLOCKS)
172 vqe.set_Optimized_Parameters(parameters)
175 case_name=
"unsupported_bridge_hea_zyz_lowering",
179 purpose=
"Verify the unsupported HEA_ZYZ lowering path fails before bridge execution.",
180 unsupported_category=
"lowering_path",
181 first_unsupported_condition=
"generated_hea_zyz",
182 bridge_source_type=
"generated_hea_zyz",
187 vqe.describe_density_bridge()
189 return metadata, runner,
"currently supports only the HEA ansatz" 196 "channel":
"local_depolarizing",
198 "after_gate_index": 999,
203 vqe.Generate_Circuit(DEFAULT_LAYERS, DEFAULT_INNER_BLOCKS)
206 case_name=
"unsupported_bridge_after_gate_index",
209 density_noise=density_noise,
210 purpose=
"Verify invalid ordered noise insertion fails before density execution.",
211 unsupported_category=
"noise_insertion",
212 first_unsupported_condition=
"after_gate_index",
213 bridge_source_type=
"generated_hea",
215 metadata[
"source_pass"] =
True 216 metadata[
"gate_pass"] =
True 219 vqe.describe_density_bridge()
221 return metadata, runner,
"after_gate_index exceeds generated gate count" 227 requested_density_noise = [
229 "channel":
"readout_noise",
231 "after_gate_index": 0,
236 case_name=
"unsupported_bridge_noise_channel",
239 density_noise=requested_density_noise,
240 purpose=
"Verify unsupported density-noise channel names fail during bridge configuration.",
241 unsupported_category=
"noise_type",
242 first_unsupported_condition=
"unsupported_density_noise_channel",
243 bridge_source_type=
"unset",
247 Variational_Quantum_Eigensolver(
258 backend=PRIMARY_BACKEND,
259 density_noise=requested_density_noise,
262 return metadata, runner,
"Unsupported density-noise channel" 265 UNSUPPORTED_CASE_BUILDERS = (
266 case_custom_gate_structure_source,
267 case_hea_zyz_lowering,
268 case_invalid_after_gate_index,
269 case_invalid_noise_channel,
274 metadata, runner, expected_error_fragment = case_builder()
277 result = dict(metadata)
281 "unsupported_reason":
"Case executed successfully but was expected to fail.",
282 "error_match_pass":
False,
285 except Exception
as exc:
286 result = dict(metadata)
289 "status":
"unsupported",
290 "unsupported_reason": str(exc),
291 "error_match_pass": expected_error_fragment
in str(exc),
297 " {case_name:<52} category={category:<14} status={status}".format(
298 case_name=result[
"case_name"],
299 category=result[
"unsupported_category"],
300 status=result[
"status"].upper(),
309 print(
" Unsupported Bridge Validation [{}]".format(PRIMARY_BACKEND))
315 unsupported = sum(1
for result
in results
if result[
"status"] ==
"unsupported")
316 error_match = sum(1
for result
in results
if result.get(
"error_match_pass",
False))
319 "suite_name": SUITE_NAME,
320 "status":
"pass" if unsupported == total
and error_match == total
else "fail",
321 "backend": PRIMARY_BACKEND,
325 "total_cases": total,
326 "unsupported_cases": unsupported,
327 "failed_cases": total - unsupported,
328 "error_match_count": error_match,
329 "required_case_count": total,
338 missing_fields = [field
for field
in ARTIFACT_CORE_FIELDS
if field
not in bundle]
341 "Artifact bundle is missing required fields: {}".format(
342 ", ".join(missing_fields)
349 output_path.parent.mkdir(parents=
True, exist_ok=
True)
350 output_path.write_text(json.dumps(bundle, indent=2) +
"\n", encoding=
"utf-8")
354 print(
"\n" +
"=" * 78)
357 for result
in bundle[
"cases"]:
359 if result[
"status"] !=
"unsupported":
360 details.append(
"status")
361 if not result.get(
"error_match_pass",
False):
362 details.append(
"error_match")
364 " {case_name:<52} category={category:<14} status={status}{detail_suffix}".format(
365 case_name=result[
"case_name"],
366 category=result[
"unsupported_category"],
367 status=result[
"status"].upper(),
368 detail_suffix=
"" if not details
else " (" +
",".join(details) +
")",
372 print(
"\n" +
"-" * 78)
374 " Total: {unsupported}/{total} representative unsupported bridge cases behaved as expected".format(
375 unsupported=bundle[
"summary"][
"unsupported_cases"],
376 total=bundle[
"summary"][
"total_cases"],
379 if bundle[
"status"] ==
"pass":
380 print(
"\n ALL TESTS PASSED - Unsupported bridge validation gate is closed.")
382 print(
"\n Some representative unsupported bridge cases did not behave as expected.")
387 parser = argparse.ArgumentParser(description=__doc__)
392 help=
"Optional directory for the unsupported-bridge JSON artifact bundle.",
394 args = parser.parse_args()
400 if args.output_dir
is not None:
403 if bundle[
"status"] !=
"pass":
407 if __name__ ==
"__main__":
def case_custom_gate_structure_source()
def validate_artifact_bundle(bundle)
def build_optimizer_config()
def build_open_chain_topology
generate_zz_xx_hamiltonian
def build_initial_parameters
def write_artifact_bundle
def case_invalid_noise_channel()
def build_artifact_bundle(results)
def build_software_metadata()
def case_invalid_after_gate_index()
def build_requirement_metadata()
def run_validation(verbose=True)
def print_summary(bundle)
def build_hamiltonian_metadata()
def case_hea_zyz_lowering()
def capture_unsupported_case(case_builder, verbose=True)