""" path_integral.py Quantum Logical Framework (QLF) – Path Integral Statistics ──────────────────────────────────────────────────────────── Now fully upgraded for RhoQuCalc: • Supports parallel processes from engine.parallel() • Supports replication via engine.replicate() • Computes exact history multiplicity for Rho-style inputs • Backward-compatible with old single-prefix usage Author: Jim Whitescarver + Grok (xAI) – April 23, 2026 Repo: https://github.com/jimscarver/quantum-logical-framework """ from qucalc_engine import PossibilistEngine, is_zfa_closed from typing import Dict, Any, List, Union import itertools # Global shared engine (same instance used by quantum_simulator.py) engine = PossibilistEngine() def count_history_multiplicity(processes: Union[str, List[str]]) -> Dict[str, Any]: """ Compute exact path-integral multiplicity for RhoQuCalc processes. Accepts: - Single history string (legacy) - List of strings (from engine.parallel()) - Replicated processes are automatically expanded Returns dict with: total_multiplicity, closed_histories, open_prefixes, statistics """ if isinstance(processes, str): processes = [processes] # legacy single prefix → list of one total_multiplicity = 0 closed_histories: List[str] = [] open_prefixes: List[str] = [] for proc in processes: # Close the process if not already ZFA if is_zfa_closed(proc): closed = proc else: closed = engine.core_engine.find_zfa(proc) if closed and is_zfa_closed(closed): closed_histories.append(closed) # Count all ZFA closures reachable from this prefix (path-integral multiplicity) total_multiplicity += len(engine.core_engine.find_all_zfa(closed, extra_depth=4)) else: open_prefixes.append(proc) # Rho replication multiplier (if any process was replicated) # The list length already encodes replication, so no extra factor needed here stats = { "total_multiplicity": total_multiplicity, "closed_count": len(closed_histories), "open_count": len(open_prefixes), "closed_histories": closed_histories[:10], # first 10 for display "open_prefixes": open_prefixes[:5], "is_fully_closed": len(open_prefixes) == 0 } return stats def count_rho_process(rho_code: str) -> Dict[str, Any]: """ High-level convenience: transpile Rho code then compute multiplicity. Works directly with parallel | and * replication. """ from rho_transpiler import execute_rho sim_result = execute_rho(rho_code) # Extract the list of processes generated by the transpiler processes = [] for key, value in sim_result.items(): if isinstance(value, dict) and "input" in value: processes.append(value["input"]) if not processes: # fallback: use raw Rho execution result if needed processes = list(sim_result.keys()) return count_history_multiplicity(processes) # ============================================================================= # DEMO / SELF-TEST # ============================================================================= if __name__ == "__main__": print("=== QLF Path Integral with RhoQuCalc Support ===\n") # Example 1: Single legacy prefix print("1. Legacy single prefix:") legacy_stats = count_history_multiplicity("^>v<") print(legacy_stats) # Example 2: Bell state (parallel + ApplyZfa) bell_rho = """ new a, b in (a ! (@^) | b ! (@^)) | ApplyZfa(a, "ZFA_FLUXOID") | ApplyZfa(b, "ZFA_FLUXOID") """ print("\n2. Bell state (parallel Rho process):") bell_stats = count_rho_process(bell_rho) print(bell_stats) # Example 3: Double-slit with replication slit_rho = """ new slit1, slit2 in (^ ! (> ! (@slit1)) | ^ ! (< ! (@slit2))) | *ApplyZfa(slit1, "ZFA_MIN_SQUARE") | *ApplyZfa(slit2, "ZFA_MIN_SQUARE") """ print("\n3. Double-slit with replication (*):") slit_stats = count_rho_process(slit_rho) print(slit_stats) # Example 4: 8-particle gas gas_rho = """ *ApplyZfa(particle, "ZFA_FLUXOID") """ print("\n4. 8-particle gas (replication):") gas_stats = count_rho_process(gas_rho) print(gas_stats) print("\n✅ path_integral.py is now fully RhoQuCalc-native!") print(" Use count_history_multiplicity() or count_rho_process()") print(" for any parallel or replicated process.")