|
|
| """run_eval.py — CLI entrypoint for the NEXUSMON Quantum Bridge.
|
|
|
| Called by the Express quantum.ts route via execSync.
|
| Usage:
|
| py -3.11 run_eval.py evaluate [0.7,0.3] 1024
|
| py -3.11 run_eval.py classify [0.5,0.8,0.2,0.9] "[[0.1,0.2,0.3,0.4],[0.5,0.6,0.7,0.8]]"
|
| py -3.11 run_eval.py kernel [0.5,0.3] [0.8,0.6]
|
| """
|
|
|
| import json
|
| import sys
|
| import os
|
|
|
|
|
| SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
| if SCRIPT_DIR not in sys.path:
|
| sys.path.insert(0, SCRIPT_DIR)
|
|
|
|
|
| def cmd_evaluate(claim_hash: list[float], shots: int) -> dict:
|
| """Run truth-gate circuit evaluation."""
|
| from qiskit_bridge import QuantumCognitionRouter
|
| import asyncio
|
|
|
| router = QuantumCognitionRouter()
|
| result = asyncio.run(router.evaluate_truth(claim_hash, shots))
|
| return result
|
|
|
|
|
| def cmd_classify(features: list[float], weights: list[list[float]]) -> dict:
|
| """Run PennyLane QML classification."""
|
| from pennylane_qml import QuantumMLRouter
|
|
|
| router = QuantumMLRouter()
|
| result = router.classify(features, weights)
|
| return result
|
|
|
|
|
| def cmd_kernel(x1: list[float], x2: list[float]) -> dict:
|
| """Compute quantum kernel."""
|
| from pennylane_qml import QuantumMLRouter
|
|
|
| router = QuantumMLRouter()
|
| result = router.kernel(x1, x2)
|
| return result
|
|
|
|
|
| def main():
|
| if len(sys.argv) < 2:
|
| print(json.dumps({"ok": False, "error": "Usage: run_eval.py <evaluate|classify|kernel> [args...]"}))
|
| sys.exit(1)
|
|
|
| command = sys.argv[1]
|
|
|
| try:
|
| if command == "evaluate":
|
| if len(sys.argv) < 4:
|
| raise ValueError("evaluate needs claim_hash and shots")
|
| claim_hash = json.loads(sys.argv[2])
|
| shots = int(sys.argv[3])
|
| result = cmd_evaluate(claim_hash, shots)
|
| elif command == "classify":
|
| if len(sys.argv) < 3:
|
| raise ValueError("classify needs features")
|
| features = json.loads(sys.argv[2])
|
| weights = json.loads(sys.argv[3]) if len(sys.argv) > 3 else []
|
| result = cmd_classify(features, weights)
|
| elif command == "kernel":
|
| if len(sys.argv) < 4:
|
| raise ValueError("kernel needs x1 and x2")
|
| x1 = json.loads(sys.argv[2])
|
| x2 = json.loads(sys.argv[3])
|
| result = cmd_kernel(x1, x2)
|
| else:
|
| result = {"ok": False, "error": f"Unknown command: {command}"}
|
|
|
| print(json.dumps(result))
|
|
|
| except Exception as e:
|
| print(json.dumps({"ok": False, "error": str(e), "degraded": True}))
|
|
|
|
|
| if __name__ == "__main__":
|
| main() |