Spaces:
Runtime error
Runtime error
Create Quantum.py
Browse files- Quantum.py +114 -0
Quantum.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json, yaml
|
| 2 |
+
import networkx as nx
|
| 3 |
+
import random
|
| 4 |
+
from typing import List, Dict, Any, Optional
|
| 5 |
+
from qiskit import QuantumCircuit, Aer, execute
|
| 6 |
+
from colorama import Fore, Style
|
| 7 |
+
|
| 8 |
+
#-----------------------------------
|
| 9 |
+
# LOADING AND PARSING COCOON FILES
|
| 10 |
+
#-----------------------------------
|
| 11 |
+
def load_cocoons(file_path: str) -> List[Dict[str, Any]]:
|
| 12 |
+
"""Load cocoon memories from YAML or JSON."""
|
| 13 |
+
with open(file_path, "r") as f:
|
| 14 |
+
if file_path.endswith((".yaml", ".yml")):
|
| 15 |
+
return yaml.safe_load(f).get("cocoons", [])
|
| 16 |
+
elif file_path.endswith(".json"):
|
| 17 |
+
return json.load(f).get("cocoons", [])
|
| 18 |
+
raise ValueError("Unsupported file format.")
|
| 19 |
+
|
| 20 |
+
#----------------------------
|
| 21 |
+
# SPIDERWEB GRAPH CONSTRUCTION
|
| 22 |
+
#----------------------------
|
| 23 |
+
def build_emotional_webs(cocoons: List[Dict[str, Any]]) -> Dict[str, nx.Graph]:
|
| 24 |
+
"""Build a separate spiderweb graph for each core emotion."""
|
| 25 |
+
emotions = ["compassion", "curiosity", "fear", "joy", "sorrow", "ethics", "quantum"]
|
| 26 |
+
webs = {emotion: nx.Graph() for emotion in emotions}
|
| 27 |
+
|
| 28 |
+
for cocoon in cocoons:
|
| 29 |
+
for tag in cocoon.get("tags", []):
|
| 30 |
+
if tag in webs:
|
| 31 |
+
webs[tag].add_node(cocoon["title"], **cocoon)
|
| 32 |
+
return webs
|
| 33 |
+
|
| 34 |
+
#--------------------------
|
| 35 |
+
# QUANTUM WALK SIMULATION
|
| 36 |
+
#--------------------------
|
| 37 |
+
def quantum_select_node(web: nx.Graph) -> Optional[str]:
|
| 38 |
+
"""Select a node using quantum superposition (or fallback random if simulator fails)."""
|
| 39 |
+
if len(web.nodes) == 0:
|
| 40 |
+
return None
|
| 41 |
+
|
| 42 |
+
node_list = list(web.nodes)
|
| 43 |
+
num_nodes = len(node_list)
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
qc = QuantumCircuit(num_nodes, num_nodes)
|
| 47 |
+
qc.h(range(num_nodes)) # Create superposition
|
| 48 |
+
qc.measure_all()
|
| 49 |
+
backend = Aer.get_backend('qasm_simulator')
|
| 50 |
+
result = execute(qc, backend, shots=1).result()
|
| 51 |
+
counts = result.get_counts()
|
| 52 |
+
state = list(counts.keys())[0]
|
| 53 |
+
index = int(state, 2) % num_nodes
|
| 54 |
+
except Exception:
|
| 55 |
+
index = random.randint(0, num_nodes - 1) # Fallback to uniform selection
|
| 56 |
+
|
| 57 |
+
return node_list[index]
|
| 58 |
+
|
| 59 |
+
#----------------------------
|
| 60 |
+
# ETHICAL SELF-REFLECTION
|
| 61 |
+
#----------------------------
|
| 62 |
+
def reflect_on_cocoon(cocoon: Dict[str, Any]) -> None:
|
| 63 |
+
"""Print a colorized ethical and emotional reflection of a cocoon."""
|
| 64 |
+
emotion = cocoon.get("emotion", "quantum")
|
| 65 |
+
title = cocoon.get("title", "Unknown Memory")
|
| 66 |
+
summary = cocoon.get("summary", "No summary provided.")
|
| 67 |
+
quote = cocoon.get("quote", "β¦")
|
| 68 |
+
|
| 69 |
+
color_map = {
|
| 70 |
+
"compassion": Fore.MAGENTA, "curiosity": Fore.CYAN, "fear": Fore.RED,
|
| 71 |
+
"joy": Fore.YELLOW, "sorrow": Fore.BLUE, "ethics": Fore.GREEN, "quantum": Fore.LIGHTWHITE_EX
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
message_map = {
|
| 75 |
+
"compassion": "π Ethical resonance detected.",
|
| 76 |
+
"curiosity": "π Wonder expands the mind.",
|
| 77 |
+
"fear": "π¨ Alert: shielding activated.",
|
| 78 |
+
"joy": "πΆ Confidence and trust uplift the field.",
|
| 79 |
+
"sorrow": "π§οΈ Processing grief with clarity.",
|
| 80 |
+
"ethics": "βοΈ Validating alignment...",
|
| 81 |
+
"quantum": "βοΈ Entanglement pattern detected."
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
color = color_map.get(emotion, Fore.WHITE)
|
| 85 |
+
message = message_map.get(emotion, "π Unknown entanglement.")
|
| 86 |
+
|
| 87 |
+
print(color + f"\n[Codette Reflection: {emotion.upper()}]")
|
| 88 |
+
print(f"Title : {title}")
|
| 89 |
+
print(f"Summary : {summary}")
|
| 90 |
+
print(f"Quote : {quote}")
|
| 91 |
+
print(f"{message}")
|
| 92 |
+
print(Style.RESET_ALL)
|
| 93 |
+
|
| 94 |
+
#-----------------------
|
| 95 |
+
# FULL EXECUTION ENGINE
|
| 96 |
+
#-----------------------
|
| 97 |
+
def run_quantum_spiderweb(file_path: str, limit: int = 1) -> Dict[str, Dict[str, Any]]:
|
| 98 |
+
"""Run through all emotional webs and reflect on quantum-sampled nodes."""
|
| 99 |
+
cocoons = load_cocoons(file_path)
|
| 100 |
+
webs = build_emotional_webs(cocoons)
|
| 101 |
+
reflections = {}
|
| 102 |
+
|
| 103 |
+
print("\nβ¨ Codette Quantum Cognition: Spiderweb Sweep β¨")
|
| 104 |
+
for emotion, web in webs.items():
|
| 105 |
+
print(f"\nπΈοΈ Web: {emotion.upper()}")
|
| 106 |
+
for _ in range(limit):
|
| 107 |
+
node = quantum_select_node(web)
|
| 108 |
+
if node:
|
| 109 |
+
cocoon = web.nodes[node]
|
| 110 |
+
reflect_on_cocoon(cocoon)
|
| 111 |
+
reflections[emotion] = cocoon
|
| 112 |
+
else:
|
| 113 |
+
print(f" β οΈ No memories in this emotion web.")
|
| 114 |
+
return reflections
|