strange-loop 0.3.0

Hyper-optimized strange loops with temporal consciousness and quantum-classical hybrid computing. NPX: npx strange-loops
Documentation
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! Benchmarks for strange-loop crate

use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use strange_loop::{
    consciousness::ConsciousnessMetrics,
    lipschitz_loop::{LipschitzLoop, LipschitzParams, LoopTopology},
    quantum_container::QuantumContainer,
    strange_attractor::{TemporalAttractor, AttractorConfig, AttractorType},
    temporal_consciousness::{TemporalConsciousness, ConsciousnessConfig},
    types::{StrangeLoop, LoopConfig, ScalarReasoner, SimpleCritic, SafeReflector},
};
use nalgebra::Vector3;
use std::collections::HashMap;

fn bench_strange_loop_convergence(c: &mut Criterion) {
    let mut group = c.benchmark_group("strange_loop_convergence");

    for iterations in [100, 500, 1000, 5000].iter() {
        group.throughput(Throughput::Elements(*iterations as u64));
        group.bench_with_input(
            BenchmarkId::new("scalar_reasoner", iterations),
            iterations,
            |b, &iterations| {
                b.iter(|| {
                    let reasoner = ScalarReasoner::new(0.0, 0.1);
                    let critic = SimpleCritic::new();
                    let reflector = SafeReflector::new();

                    let config = LoopConfig {
                        max_iterations: iterations,
                        max_duration_ns: 1_000_000_000, // 1 second
                        convergence_threshold: 1e-9,
                        lipschitz_constant: 0.9,
                        enable_consciousness: false,
                        enable_quantum: false,
                        enable_simd: true,
                    };

                    let mut strange_loop = StrangeLoop::new(reasoner, critic, reflector, config);
                    let mut context = HashMap::from([("x".to_string(), black_box(10.0))]);

                    black_box(strange_loop.run(&mut context))
                });
            },
        );
    }
    group.finish();
}

fn bench_attractor_dynamics(c: &mut Criterion) {
    let mut group = c.benchmark_group("attractor_dynamics");

    let attractors = [
        ("lorenz", AttractorType::Lorenz { sigma: 10.0, rho: 28.0, beta: 8.0/3.0 }),
        ("rossler", AttractorType::Rossler { a: 0.2, b: 0.2, c: 5.7 }),
        ("chua", AttractorType::Chua { alpha: 15.6, beta: -1.143, gamma: -0.714 }),
    ];

    for (name, attractor_type) in attractors.iter() {
        for steps in [100, 500, 1000, 5000].iter() {
            group.throughput(Throughput::Elements(*steps as u64));
            group.bench_with_input(
                BenchmarkId::new(format!("{}_fixed_step", name), steps),
                steps,
                |b, &steps| {
                    let config = AttractorConfig {
                        attractor_type: attractor_type.clone(),
                        dt_ns: 1000,
                        steps_per_frame: 1,
                        adaptive_stepping: false,
                        tolerance: 1e-6,
                        max_deviation: 50.0,
                    };

                    b.iter(|| {
                        let mut attractor = TemporalAttractor::new(config.clone()).unwrap();
                        for _ in 0..steps {
                            black_box(attractor.step().unwrap());
                        }
                    });
                },
            );

            group.bench_with_input(
                BenchmarkId::new(format!("{}_adaptive_step", name), steps),
                steps,
                |b, &steps| {
                    let config = AttractorConfig {
                        attractor_type: attractor_type.clone(),
                        dt_ns: 1000,
                        steps_per_frame: 1,
                        adaptive_stepping: true,
                        tolerance: 1e-9,
                        max_deviation: 50.0,
                    };

                    b.iter(|| {
                        let mut attractor = TemporalAttractor::new(config.clone()).unwrap();
                        for _ in 0..steps {
                            black_box(attractor.step().unwrap());
                        }
                    });
                },
            );
        }
    }
    group.finish();
}

fn bench_quantum_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("quantum_operations");

    for qubits in [2, 3, 4, 5].iter() {
        group.throughput(Throughput::Elements(1000));
        group.bench_with_input(
            BenchmarkId::new("measurements", qubits),
            qubits,
            |b, &qubits| {
                b.iter(|| {
                    let mut quantum = QuantumContainer::new(qubits);

                    // Create uniform superposition
                    let num_states = 1 << qubits;
                    let probabilities = vec![1.0 / num_states as f64; num_states];
                    quantum.create_superposition_from_classical(&probabilities).unwrap();

                    // Perform measurements
                    for _ in 0..1000 {
                        black_box(quantum.measure());
                    }
                });
            },
        );

        group.bench_with_input(
            BenchmarkId::new("gate_operations", qubits),
            qubits,
            |b, &qubits| {
                use strange_loop::quantum_container::Gate;

                b.iter(|| {
                    let mut quantum = QuantumContainer::new(qubits);

                    // Apply gates to all qubits
                    for qubit in 0..qubits {
                        black_box(quantum.apply_gate(qubit, Gate::H).unwrap());
                        black_box(quantum.apply_gate(qubit, Gate::RZ(0.5)).unwrap());
                    }
                });
            },
        );
    }
    group.finish();
}

fn bench_lipschitz_loops(c: &mut Criterion) {
    let mut group = c.benchmark_group("lipschitz_loops");

    let topologies = [
        ("fixed_point", LoopTopology::FixedPoint),
        ("newton", LoopTopology::Newton),
        ("accelerated", LoopTopology::Accelerated),
        ("conjugate_gradient", LoopTopology::ConjugateGradient),
    ];

    for (name, topology) in topologies.iter() {
        group.bench_function(name, |b| {
            let params = LipschitzParams {
                lipschitz_constant: 0.8,
                tolerance: 1e-9,
                max_iterations: 1000,
                adaptive_estimation: true,
                damping: 0.99,
            };

            b.iter(|| {
                let mut loop_solver = LipschitzLoop::new(params.clone(), topology.clone()).unwrap();

                // Simple contractive function
                let function = |x: Vector3<f64>| black_box(0.7 * x);
                let initial_state = Vector3::new(10.0, 10.0, 10.0);

                black_box(loop_solver.execute(function, initial_state))
            });
        });
    }
    group.finish();
}

fn bench_consciousness_evolution(c: &mut Criterion) {
    let mut group = c.benchmark_group("consciousness_evolution");

    let configs = [
        ("minimal", ConsciousnessConfig {
            enable_quantum: false,
            enable_attractors: false,
            enable_lipschitz: false,
            enable_self_modification: false,
            phi_elements: 2,
            max_evolution_iterations: 100,
            ..ConsciousnessConfig::default()
        }),
        ("quantum_only", ConsciousnessConfig {
            enable_quantum: true,
            enable_attractors: false,
            enable_lipschitz: false,
            enable_self_modification: false,
            phi_elements: 4,
            max_evolution_iterations: 100,
            ..ConsciousnessConfig::default()
        }),
        ("full_system", ConsciousnessConfig {
            enable_quantum: true,
            enable_attractors: true,
            enable_lipschitz: true,
            enable_self_modification: false, // Disable for consistent benchmarking
            phi_elements: 6,
            max_evolution_iterations: 100,
            ..ConsciousnessConfig::default()
        }),
    ];

    for (name, config) in configs.iter() {
        for iterations in [10, 25, 50, 100].iter() {
            group.throughput(Throughput::Elements(*iterations as u64));
            group.bench_with_input(
                BenchmarkId::new(name, iterations),
                iterations,
                |b, &iterations| {
                    b.iter(|| {
                        let mut consciousness = TemporalConsciousness::new(config.clone()).unwrap();
                        black_box(consciousness.evolve_consciousness(iterations).unwrap())
                    });
                },
            );
        }
    }
    group.finish();
}

fn bench_phi_calculation(c: &mut Criterion) {
    let mut group = c.benchmark_group("phi_calculation");

    for elements in [4, 6, 8, 10, 12].iter() {
        group.throughput(Throughput::Elements(*elements as u64));
        group.bench_with_input(
            BenchmarkId::new("integrated_information", elements),
            elements,
            |b, &elements| {
                b.iter(|| {
                    let mut metrics = ConsciousnessMetrics::new();
                    let connections = elements * (elements - 1) / 2;
                    let coupling_strength = 0.8;

                    black_box(metrics.calculate_phi(
                        black_box(elements),
                        black_box(connections),
                        black_box(coupling_strength)
                    ))
                });
            },
        );
    }
    group.finish();
}

fn bench_memory_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("memory_operations");

    // Benchmark temporal pattern storage
    group.bench_function("temporal_pattern_storage", |b| {
        b.iter(|| {
            let config = ConsciousnessConfig::default();
            let mut consciousness = TemporalConsciousness::new(config).unwrap();

            // Generate patterns
            for i in 0..black_box(1000) {
                consciousness.evolve_consciousness(1).unwrap();
            }

            black_box(consciousness.temporal_patterns().len())
        });
    });

    // Benchmark attractor trajectory storage
    group.bench_function("attractor_trajectory", |b| {
        b.iter(|| {
            let config = AttractorConfig::default();
            let mut attractor = TemporalAttractor::new(config).unwrap();
            attractor.set_max_trajectory_length(1000);

            for _ in 0..black_box(1000) {
                attractor.step().unwrap();
            }

            black_box(attractor.trajectory().len())
        });
    });

    group.finish();
}

fn bench_simd_optimizations(c: &mut Criterion) {
    let mut group = c.benchmark_group("simd_optimizations");

    // Benchmark with and without SIMD (if available)
    group.bench_function("vector_operations_scalar", |b| {
        b.iter(|| {
            let vectors: Vec<Vector3<f64>> = (0..1000)
                .map(|i| Vector3::new(i as f64, (i*2) as f64, (i*3) as f64))
                .collect();

            let mut result = Vector3::zeros();
            for v in &vectors {
                result += v;
            }

            black_box(result)
        });
    });

    #[cfg(feature = "simd")]
    group.bench_function("vector_operations_simd", |b| {
        use wide::f64x4;

        b.iter(|| {
            let data: Vec<f64> = (0..4000)
                .map(|i| i as f64)
                .collect();

            let mut sum = f64x4::ZERO;
            for chunk in data.chunks_exact(4) {
                let vec = f64x4::new([chunk[0], chunk[1], chunk[2], chunk[3]]);
                sum += vec;
            }

            black_box(sum.to_array())
        });
    });

    group.finish();
}

fn bench_concurrent_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("concurrent_operations");

    group.bench_function("parallel_consciousness_evolution", |b| {
        use rayon::prelude::*;

        b.iter(|| {
            let configs: Vec<_> = (0..4)
                .map(|_| ConsciousnessConfig {
                    max_evolution_iterations: 25,
                    ..ConsciousnessConfig::default()
                })
                .collect();

            let results: Vec<_> = configs.into_par_iter()
                .map(|config| {
                    let mut consciousness = TemporalConsciousness::new(config).unwrap();
                    consciousness.evolve_consciousness(25).unwrap()
                })
                .collect();

            black_box(results)
        });
    });

    group.bench_function("parallel_attractor_computation", |b| {
        use rayon::prelude::*;

        b.iter(|| {
            let configs: Vec<_> = (0..4)
                .map(|_| AttractorConfig::default())
                .collect();

            let results: Vec<_> = configs.into_par_iter()
                .map(|config| {
                    let mut attractor = TemporalAttractor::new(config).unwrap();
                    for _ in 0..100 {
                        attractor.step().unwrap();
                    }
                    attractor.state()
                })
                .collect();

            black_box(results)
        });
    });

    group.finish();
}

fn bench_error_handling(c: &mut Criterion) {
    let mut group = c.benchmark_group("error_handling");

    group.bench_function("error_creation_and_propagation", |b| {
        use strange_loop::error::LoopError;

        b.iter(|| {
            // Test error creation performance
            let errors = vec![
                LoopError::convergence_failure(1000),
                LoopError::timeout(1_000_000),
                LoopError::lipschitz_violation(1.2, 1.0),
                LoopError::invalid_policy("test error".to_string()),
                LoopError::quantum_error("quantum test".to_string()),
            ];

            // Test error checking
            for error in &errors {
                black_box(error.is_recoverable());
                black_box(error.is_fatal());
            }

            black_box(errors)
        });
    });

    group.finish();
}

criterion_group!(
    benches,
    bench_strange_loop_convergence,
    bench_attractor_dynamics,
    bench_quantum_operations,
    bench_lipschitz_loops,
    bench_consciousness_evolution,
    bench_phi_calculation,
    bench_memory_operations,
    bench_simd_optimizations,
    bench_concurrent_operations,
    bench_error_handling,
);

criterion_main!(benches);