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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
//! Quantum Walks - Graph Exploration and Mixing
//!
//! Discrete quantum walks on graphs with coin and position registers.
//! Used for search, mixing, and quantum speedups.
//!
//! Walk types:
//! - Coined walks: explicit coin flip register
//! - Adjacency matrix walks: directly on graph Laplacian

use crate::{AlgorithmError, AlgorithmResult};
use std::collections::HashMap;

/// Graph representation for walks
#[derive(Debug, Clone)]
pub struct Graph {
    /// Vertex count
    pub vertices: usize,

    /// Adjacency list
    pub edges: Vec<Vec<usize>>,
}

impl Graph {
    /// Create new graph
    pub fn new(vertices: usize) -> Self {
        Graph {
            vertices,
            edges: vec![Vec::new(); vertices],
        }
    }

    /// Add undirected edge
    pub fn add_edge(&mut self, u: usize, v: usize) -> AlgorithmResult<()> {
        if u >= self.vertices || v >= self.vertices {
            return Err(AlgorithmError::InvalidGraph("Vertex out of range".to_string()));
        }

        if u == v {
            return Err(AlgorithmError::InvalidGraph("No self-loops".to_string()));
        }

        if !self.edges[u].contains(&v) {
            self.edges[u].push(v);
        }
        if !self.edges[v].contains(&u) {
            self.edges[v].push(u);
        }

        Ok(())
    }

    /// Get neighbors of vertex
    pub fn neighbors(&self, v: usize) -> AlgorithmResult<Vec<usize>> {
        if v >= self.vertices {
            return Err(AlgorithmError::InvalidGraph("Vertex out of range".to_string()));
        }
        Ok(self.edges[v].clone())
    }

    /// Get degree of vertex
    pub fn degree(&self, v: usize) -> AlgorithmResult<usize> {
        if v >= self.vertices {
            return Err(AlgorithmError::InvalidGraph("Vertex out of range".to_string()));
        }
        Ok(self.edges[v].len())
    }

    /// Check if regular (all vertices same degree)
    pub fn is_regular(&self) -> AlgorithmResult<bool> {
        if self.vertices == 0 {
            return Ok(true);
        }

        let d = self.degree(0)?;
        for v in 0..self.vertices {
            if self.degree(v)? != d {
                return Ok(false);
            }
        }
        Ok(true)
    }
}

/// Coined quantum walk state
#[derive(Debug, Clone)]
pub struct CoinedWalkState {
    /// Position probability distribution (classical)
    pub position_probs: Vec<f64>,

    /// Coin state (0 or 1)
    pub coin_state: u8,

    /// Time steps taken
    pub steps: usize,
}

impl CoinedWalkState {
    /// Initialize at vertex with coin
    pub fn new(vertices: usize) -> Self {
        let mut probs = vec![0.0; vertices];
        probs[0] = 1.0; // Start at vertex 0

        CoinedWalkState {
            position_probs: probs,
            coin_state: 0,
            steps: 0,
        }
    }

    /// Get probability at position after mixing
    pub fn prob_at(&self, v: usize) -> Option<f64> {
        if v < self.position_probs.len() {
            Some(self.position_probs[v])
        } else {
            None
        }
    }

    /// Mixing time (steps to near-uniform distribution)
    pub fn mixing_time(&self, target_uniformity: f64) -> Option<usize> {
        let n = self.position_probs.len() as f64;
        let uniform = 1.0 / n;

        // Check if within target_uniformity of uniform
        let all_close = self
            .position_probs
            .iter()
            .all(|&p| (p - uniform).abs() < target_uniformity);

        if all_close {
            Some(self.steps)
        } else {
            None
        }
    }
}

/// 1D line quantum walk
#[derive(Debug, Clone)]
pub struct LineQuantumWalk {
    /// Position range [-n, n]
    pub n: usize,

    /// Probability distribution
    pub probs: Vec<f64>,

    /// Current position (mapped to 0..2n+1)
    pub position: usize,

    /// Steps taken
    pub steps: usize,
}

impl LineQuantumWalk {
    /// Create new 1D walk on line [-n, n]
    pub fn new(n: usize) -> Self {
        let size = 2 * n + 1;
        let mut probs = vec![0.0; size];
        probs[n] = 1.0; // Start at 0

        LineQuantumWalk {
            n,
            probs,
            position: n,
            steps: 0,
        }
    }

    /// Take one quantum step
    pub fn step(&mut self) -> AlgorithmResult<()> {
        // Simplified: move to neighbors with 50% each
        let mut new_probs = vec![0.0; self.probs.len()];

        for (pos, prob) in self.probs.iter().enumerate() {
            if *prob > 0.0 {
                // Move left
                if pos > 0 {
                    new_probs[pos - 1] += prob * 0.5;
                }
                // Move right
                if pos < self.probs.len() - 1 {
                    new_probs[pos + 1] += prob * 0.5;
                }
            }
        }

        self.probs = new_probs;
        self.steps += 1;
        Ok(())
    }

    /// Run for t steps
    pub fn run(&mut self, t: usize) -> AlgorithmResult<()> {
        for _ in 0..t {
            self.step()?;
        }
        Ok(())
    }

    /// Get probability distribution
    pub fn distribution(&self) -> Vec<f64> {
        self.probs.clone()
    }

    /// Check if reached uniform distribution
    pub fn is_uniform(&self, tolerance: f64) -> bool {
        let target = 1.0 / self.probs.len() as f64;
        self.probs
            .iter()
            .all(|&p| (p - target).abs() < tolerance)
    }
}

/// Cycle graph quantum walk
#[derive(Debug, Clone)]
pub struct CycleQuantumWalk {
    /// Cycle size
    pub n: usize,

    /// Probability distribution
    pub probs: Vec<f64>,

    /// Steps taken
    pub steps: usize,
}

impl CycleQuantumWalk {
    /// Create walk on cycle of size n
    pub fn new(n: usize) -> AlgorithmResult<Self> {
        if n == 0 {
            return Err(AlgorithmError::InvalidGraph("Cycle size must be positive".to_string()));
        }

        let mut probs = vec![0.0; n];
        probs[0] = 1.0; // Start at vertex 0

        Ok(CycleQuantumWalk {
            n,
            probs,
            steps: 0,
        })
    }

    /// Take one step
    pub fn step(&mut self) -> AlgorithmResult<()> {
        let mut new_probs = vec![0.0; self.n];

        for (pos, prob) in self.probs.iter().enumerate() {
            if *prob > 0.0 {
                // Move clockwise
                let next = (pos + 1) % self.n;
                new_probs[next] += prob * 0.5;

                // Move counter-clockwise
                let prev = (pos + self.n - 1) % self.n;
                new_probs[prev] += prob * 0.5;
            }
        }

        self.probs = new_probs;
        self.steps += 1;
        Ok(())
    }

    /// Get mixing time to uniform distribution
    pub fn mixing_time(&mut self, tolerance: f64) -> AlgorithmResult<usize> {
        let target = 1.0 / self.n as f64;

        loop {
            self.step()?;

            let all_uniform = self
                .probs
                .iter()
                .all(|&p| (p - target).abs() < tolerance);

            if all_uniform || self.steps > 1000 {
                break;
            }
        }

        Ok(self.steps)
    }

    /// Get spectral gap (for analysis)
    pub fn spectral_gap(&self) -> f64 {
        // For cycle: λ₂ = 2 - 2cos(2π/n)
        2.0 - 2.0 * (2.0 * std::f64::consts::PI / self.n as f64).cos()
    }
}

/// Adjacency matrix representation (for mixing analysis)
#[derive(Debug, Clone)]
pub struct AdjacencyMatrixWalk {
    /// Adjacency matrix (n×n)
    pub matrix: Vec<Vec<f64>>,

    /// Probability distribution
    pub probs: Vec<f64>,

    /// Steps
    pub steps: usize,
}

impl AdjacencyMatrixWalk {
    /// Create from graph
    pub fn from_graph(graph: &Graph) -> AlgorithmResult<Self> {
        let n = graph.vertices;
        let mut matrix = vec![vec![0.0; n]; n];

        for u in 0..n {
            let deg = graph.degree(u)?;
            for &v in &graph.edges[u] {
                matrix[u][v] = 1.0 / deg as f64;
            }
        }

        let mut probs = vec![0.0; n];
        probs[0] = 1.0;

        Ok(AdjacencyMatrixWalk {
            matrix,
            probs,
            steps: 0,
        })
    }

    /// Apply transition matrix
    pub fn step(&mut self) {
        let n = self.probs.len();
        let mut new_probs = vec![0.0; n];

        for i in 0..n {
            for j in 0..n {
                new_probs[i] += self.matrix[j][i] * self.probs[j];
            }
        }

        self.probs = new_probs;
        self.steps += 1;
    }

    /// Run t steps
    pub fn run(&mut self, t: usize) {
        for _ in 0..t {
            self.step();
        }
    }

    /// Stationary distribution
    pub fn stationary_distribution(&self) -> Vec<f64> {
        // For undirected graphs: uniform over all vertices
        vec![1.0 / self.probs.len() as f64; self.probs.len()]
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_graph_creation() {
        let graph = Graph::new(3);
        assert_eq!(graph.vertices, 3);
    }

    #[test]
    fn test_graph_add_edge() {
        let mut graph = Graph::new(3);
        assert!(graph.add_edge(0, 1).is_ok());
        assert!(graph.add_edge(0, 0).is_err()); // No self-loops
    }

    #[test]
    fn test_graph_neighbors() {
        let mut graph = Graph::new(3);
        graph.add_edge(0, 1).unwrap();
        graph.add_edge(0, 2).unwrap();

        let neighbors = graph.neighbors(0).unwrap();
        assert_eq!(neighbors.len(), 2);
    }

    #[test]
    fn test_coined_walk_state() {
        let state = CoinedWalkState::new(5);
        assert_eq!(state.prob_at(0), Some(1.0));
        assert_eq!(state.prob_at(1), Some(0.0));
    }

    #[test]
    fn test_line_quantum_walk() {
        let walk = LineQuantumWalk::new(10);
        assert_eq!(walk.probs.len(), 21);
    }

    #[test]
    fn test_line_walk_step() {
        let mut walk = LineQuantumWalk::new(5);
        assert!(walk.step().is_ok());
        assert_eq!(walk.steps, 1);
    }

    #[test]
    fn test_cycle_quantum_walk() {
        let walk = CycleQuantumWalk::new(5);
        assert!(walk.is_ok());
    }

    #[test]
    fn test_cycle_walk_step() {
        let mut walk = CycleQuantumWalk::new(4).unwrap();
        assert!(walk.step().is_ok());
        assert_eq!(walk.steps, 1);
    }

    #[test]
    fn test_spectral_gap() {
        let walk = CycleQuantumWalk::new(4).unwrap();
        let gap = walk.spectral_gap();
        assert!(gap > 0.0);
    }

    #[test]
    fn test_adjacency_matrix_walk() {
        let mut graph = Graph::new(3);
        graph.add_edge(0, 1).unwrap();
        graph.add_edge(1, 2).unwrap();
        graph.add_edge(2, 0).unwrap();

        let walk = AdjacencyMatrixWalk::from_graph(&graph);
        assert!(walk.is_ok());
    }
}

// Made with Bob