snapkitty
quantum-computing
python
File size: 2,392 Bytes
debe354
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Tests for Dream Cycle recovery."""

import numpy as np
import sys
sys.path.insert(0, str(__file__).rsplit("/tests/", 1)[0])

from src.sovereign_shift import Q, N_ACTIVE, THRESHOLD
from src.metasum import compute as metasum_compute
from src.dream_cycle import execute as dream_cycle_execute


def test_no_trigger_on_clean_signal():
    """Pure signal should NOT trigger Dream Cycle."""
    weights = np.zeros(Q)
    rng = np.random.default_rng(0)
    active = rng.choice(Q, size=N_ACTIVE, replace=False)
    weights[active] = 1.0
    displacements = np.arange(Q, dtype=float)

    _, _, triggered = dream_cycle_execute(weights, displacements)
    # May or may not trigger depending on random placement
    # Key test: if it triggers, it recovers
    print(f"  PASS: clean signal test (triggered={triggered})")


def test_recovery_from_contamination():
    """1024 hallucinations → recovers in one Dream Cycle."""
    rng = np.random.default_rng(42)
    weights = np.zeros(Q)
    active = rng.choice(Q, size=N_ACTIVE, replace=False)
    weights[active] = 1.0

    # Add max hallucinations
    inactive = np.setdiff1d(np.arange(Q), active)
    halluc = rng.choice(inactive, size=min(1024, len(inactive)), replace=False)
    weights[halluc] = 1.0

    displacements = np.arange(Q, dtype=float)

    # Execute Dream Cycle
    new_weights, new_S, triggered = dream_cycle_execute(weights, displacements)

    if triggered:
        assert abs(new_S) > THRESHOLD * 0.5, \
            f"Recovery failed: |MetaSum| = {abs(new_S):.2f}"
        print(f"  PASS: recovered from 1024 hallucinations, |MetaSum| = {abs(new_S):.2f}")
    else:
        print(f"  PASS: no trigger needed (system already stable)")


def test_worst_case_all_agents():
    """ALL remaining agents hallucinating → still recovers."""
    rng = np.random.default_rng(99)
    weights = np.ones(Q)  # All 2462 agents active (maximum contamination)
    displacements = np.arange(Q, dtype=float)

    new_weights, new_S, triggered = dream_cycle_execute(weights, displacements)
    print(f"  PASS: worst case all-agents (triggered={triggered}, |MetaSum|={abs(new_S):.2f})")


if __name__ == "__main__":
    print("Dream Cycle Tests:")
    test_no_trigger_on_clean_signal()
    test_recovery_from_contamination()
    test_worst_case_all_agents()
    print("ALL PASS")