onq 0.5.0

Operations for Next-generation Quantum computing
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// src/vm/interpreter.rs

//! Defines the ONQ Virtual Machine (ONQ-VM) interpreter.

use super::program::{Instruction, Program}; // Use super to access sibling module
use crate::core::{OnqError, QduId};
use crate::simulation::SimulationResult; // Needed temporarily for stabilize call
use crate::simulation::engine::SimulationEngine; // Use pub(crate) engine
use std::collections::{HashMap, HashSet};

/// The ONQ Virtual Machine (ONQ-VM).
///
/// Interprets and executes [`Program`](super::program::Program) instructions,
/// managing both quantum state evolution (via an internal [`SimulationEngine`])
/// and classical state (registers stored in a `HashMap`). It enables mixed
/// classical/quantum algorithms with control flow based on intermediate
/// stabilization results.
///
/// # Examples
///
/// ```
/// # use onq::{ProgramBuilder, Instruction, Operation, QduId, vm::OnqVm, OnqError};
/// # fn qid(id: u64) -> QduId { QduId(id) }
/// // Simple program: H(0), Stabilize(0), Record(0, "m") -> Halt
/// let program = ProgramBuilder::new()
///     .pb_add(Instruction::QuantumOp(Operation::InteractionPattern {
///         target: qid(0),
///         pattern_id: "Superposition".to_string()
///     }))
///     .pb_add(Instruction::Stabilize { targets: vec![qid(0)] })
///     .pb_add(Instruction::Record { qdu: qid(0), register: "m".to_string() })
///     .pb_add(Instruction::Halt)
///     .build()
///     .expect("Failed to build program");
///
/// let mut vm = OnqVm::new();
/// match vm.run(&program) {
///     Ok(()) => {
///         let measurement_result = vm.get_classical_register("m");
///         println!("Stabilization outcome for QDU(0): {}", measurement_result);
///         // Outcome (0 or 1) is deterministic for this VM run.
///         assert!(measurement_result == 0 || measurement_result == 1);
///     }
///     Err(e) => eprintln!("VM run failed: {}", e),
/// }
/// ```
#[derive(Debug)]
pub struct OnqVm {
    /// The underlying simulation engine. Initialized during `run`.
    engine: Option<SimulationEngine>,
    /// Named classical registers holding u64 values.
    classical_memory: HashMap<String, u64>,
    /// Stores the outcomes from the most recently executed `Stabilize` instruction.
    /// Keyed by QduId, maps to the resolved StableState value (0 or 1).
    last_stabilization_outcomes: HashMap<QduId, u64>,
    /// Program Counter: index of the next instruction to execute.
    program_counter: usize,
    /// Flag indicating if the VM has halted.
    is_halted: bool,
    // Potential future fields: cycle count, error state details, configuration
}

impl OnqVm {
    /// Creates a new, uninitialized ONQ-VM.
    pub fn new() -> Self {
        Self {
            engine: None,
            classical_memory: HashMap::new(),
            last_stabilization_outcomes: HashMap::new(),
            program_counter: 0,
            is_halted: false,
        }
    }

    /// Resets the VM state (PC, halted flag, memory, engine) for a new run.
    fn reset(&mut self) {
        self.engine = None; // Engine needs re-initialization based on program QDUs
        self.classical_memory.clear();
        self.last_stabilization_outcomes.clear();
        self.program_counter = 0;
        self.is_halted = false;
    }

    /// Runs a given `Program` until it halts or encounters an error.
    ///
    /// # Arguments
    /// * `program` - The `Program` to execute.
    ///
    /// # Returns
    /// * `Ok(())` if the program halts successfully.
    /// * `Err(OnqError)` if a simulation error or runtime error occurs (e.g., label not found, invalid op).
    pub fn run(&mut self, program: &Program) -> Result<(), OnqError> {
        self.reset();
        println!("[VM RUN START]"); // DEBUG

        // 1. Determine all QDUs involved...
        let all_qdus = Self::collect_qdus(program)?;
        if !all_qdus.is_empty() {
            self.engine = Some(SimulationEngine::init(&all_qdus)?);
            println!("[VM Engine Initialized for {:?}]", all_qdus); // DEBUG
        } else {
            self.engine = None;
            println!("[VM Engine Not Needed (No QDUs)]"); // DEBUG
        }

        // 2. Execution Loop
        let mut executed_instruction_count = 0; // DEBUG loop counter
        const MAX_INSTRUCTIONS: u64 = 1000; // DEBUG limit

        while !self.is_halted {
            // --- DEBUG: Safety break for infinite loops ---
            if executed_instruction_count > MAX_INSTRUCTIONS {
                return Err(OnqError::SimulationError {
                    message: format!(
                        "Execution exceeded maximum instruction limit ({}) - potential infinite loop?",
                        MAX_INSTRUCTIONS
                    ),
                });
            }
            executed_instruction_count += 1;
            // --- End DEBUG ---

            let pc = self.program_counter;

            // Fetch instruction
            println!("[VM] PC={:04} Fetching...", pc); // DEBUG
            let instruction =
                program
                    .get_instruction(pc)
                    .ok_or_else(|| OnqError::SimulationError {
                        message: format!(
                            "Program Counter ({}) out of bounds (0..{}).",
                            pc,
                            program.instruction_count()
                        ),
                    })?;
            println!("[VM] PC={:04} Executing: {:?}", pc, instruction); // DEBUG

            // Advance PC before execution (simplifies branching)
            self.program_counter += 1;

            // Execute instruction
            match instruction {
                Instruction::QuantumOp(op) => {
                    if let Some(engine) = self.engine.as_mut() {
                        engine.apply_operation(op)?;
                    } else {
                        return Err(OnqError::InvalidOperation { message: "Cannot execute QuantumOp: SimulationEngine not initialized (no QDUs defined in program?).".to_string() });
                    }
                }
                Instruction::Stabilize { targets } => {
                    if targets.is_empty() {
                        println!("[VM] PC={:04} Stabilize: No targets.", pc); // DEBUG
                        continue;
                    }
                    if let Some(engine) = self.engine.as_mut() {
                        let mut temp_result = SimulationResult::new();
                        println!(
                            "[VM] PC={:04} Calling engine.stabilize for {:?}",
                            pc, targets
                        ); // DEBUG
                        engine.stabilize(targets, &mut temp_result)?; // This might return Err
                        println!(
                            "[VM] PC={:04} engine.stabilize finished. Temp result: {:?}",
                            pc, temp_result
                        ); // DEBUG

                        // Store the u64 outcomes for Record instruction
                        self.last_stabilization_outcomes = temp_result.all_stable_outcomes().iter()
                             .filter_map(|(qid, state)| {
                                 // DEBUG: See what get_resolved_value returns
                                 let resolved = state.get_resolved_value();
                                 println!("[VM] PC={:04} Stabilize: QDU {}, State {:?}, Resolved Value: {:?}", pc, qid, state, resolved); // DEBUG
                                 resolved.map(|val| (*qid, val))
                             })
                             .collect();
                        println!(
                            "[VM] PC={:04} Stored last_stabilization_outcomes: {:?}",
                            pc, self.last_stabilization_outcomes
                        ); // DEBUG
                    } else {
                        return Err(OnqError::InvalidOperation {
                            message: "Cannot execute Stabilize: SimulationEngine not initialized."
                                .to_string(),
                        });
                    }
                }
                Instruction::Record { qdu, register } => {
                    println!("[VM] PC={:04} Attempting to record for QDU {}", pc, qdu); // DEBUG
                    println!(
                        "[VM] PC={:04} Current last_stabilization_outcomes: {:?}",
                        pc, self.last_stabilization_outcomes
                    ); // DEBUG
                    // Attempt to get the value
                    let value_option = self.last_stabilization_outcomes.get(qdu);
                    println!(
                        "[VM] PC={:04} Value Option for QDU {}: {:?}",
                        pc, qdu, value_option
                    ); // DEBUG

                    let value = value_option.ok_or_else(|| {
                        OnqError::InvalidOperation { message: format!("Cannot Record: QDU {} was not found in the last stabilization results ({:?}). Was Stabilize called immediately prior with this QDU?", qdu, self.last_stabilization_outcomes) }
                    })?;
                    println!(
                        "[VM] PC={:04} Recording value {} to register '{}'",
                        pc, value, register
                    ); // DEBUG
                    self.classical_memory.insert(register.clone(), *value);
                    println!(
                        "[VM] PC={:04} Classical memory now: {:?}",
                        pc, self.classical_memory
                    ); // DEBUG
                }
                Instruction::Label(_) => {
                    println!("[VM] PC={:04} Encountered Label (No-Op)", pc); // DEBUG
                    // No operation, labels handled during build/jump resolution
                }
                Instruction::Jump(label) => {
                    let target_pc =
                        program
                            .get_label_pc(label)
                            .ok_or_else(|| OnqError::SimulationError {
                                message: format!(
                                    "Runtime Error: Jump target label '{}' not found.",
                                    label
                                ),
                            })?;
                    println!(
                        "[VM] PC={:04} Jumping to label '{}' (PC={})",
                        pc, label, target_pc
                    ); // DEBUG
                    self.program_counter = target_pc; // Set PC to target instruction index
                }
                Instruction::BranchIfZero { register, label } => {
                    let reg_value = self.classical_memory.get(register).copied().unwrap_or(0); // Default to 0
                    println!(
                        "[VM] PC={:04} BranchIfZero: Reg '{}' = {}",
                        pc, register, reg_value
                    ); // DEBUG
                    if reg_value == 0 {
                        let target_pc = program.get_label_pc(label).ok_or_else(|| {
                            OnqError::SimulationError {
                                message: format!(
                                    "Runtime Error: Branch target label '{}' not found.",
                                    label
                                ),
                            }
                        })?;
                        println!(
                            "[VM] PC={:04} Branch taken to label '{}' (PC={})",
                            pc, label, target_pc
                        ); // DEBUG
                        self.program_counter = target_pc;
                    } else {
                        println!("[VM] PC={:04} Branch not taken.", pc); // DEBUG
                    }
                    // If branch not taken, PC remains incremented from before match
                }
                Instruction::LoadImmediate { register, value } => {
                    println!("[VM] PC={:04} LoadImm: Reg '{}' = {}", pc, register, value); // DEBUG
                    self.classical_memory.insert(register.clone(), *value);
                }
                Instruction::Copy {
                    source_reg,
                    dest_reg,
                } => {
                    let value = self.classical_memory.get(source_reg).copied().unwrap_or(0);
                    println!(
                        "[VM] PC={:04} Copy: Reg '{}' = {} from Reg '{}'",
                        pc, dest_reg, value, source_reg
                    ); // DEBUG
                    self.classical_memory.insert(dest_reg.clone(), value);
                }
                Instruction::OnqAdd {
                    r_dest,
                    r_src1,
                    r_src2,
                } => {
                    let val1 = self.classical_memory.get(r_src1).copied().unwrap_or(0);
                    let val2 = self.classical_memory.get(r_src2).copied().unwrap_or(0);
                    self.classical_memory
                        .insert(r_dest.clone(), val1.wrapping_add(val2));
                }
                Instruction::Addi {
                    r_dest,
                    r_src,
                    value,
                } => {
                    let val_src = self.classical_memory.get(r_src).copied().unwrap_or(0);
                    let result = val_src.wrapping_add(*value);
                    println!(
                        "[VM] PC={:04} Addi: Reg '{}' = {} + {} = {}",
                        pc, r_dest, val_src, value, result
                    ); // DEBUG
                    self.classical_memory.insert(r_dest.clone(), result);
                }
                Instruction::Sub {
                    r_dest,
                    r_src1,
                    r_src2,
                } => {
                    let val1 = self.classical_memory.get(r_src1).copied().unwrap_or(0);
                    let val2 = self.classical_memory.get(r_src2).copied().unwrap_or(0);
                    self.classical_memory
                        .insert(r_dest.clone(), val1.wrapping_sub(val2));
                }
                Instruction::Mul {
                    r_dest,
                    r_src1,
                    r_src2,
                } => {
                    let val1 = self.classical_memory.get(r_src1).copied().unwrap_or(0);
                    let val2 = self.classical_memory.get(r_src2).copied().unwrap_or(0);
                    self.classical_memory
                        .insert(r_dest.clone(), val1.wrapping_mul(val2));
                }
                Instruction::OnqNot { r_dest, r_src } => {
                    let val_src = self.classical_memory.get(r_src).copied().unwrap_or(0);
                    self.classical_memory.insert(r_dest.clone(), !val_src); // Bitwise NOT
                }
                Instruction::And {
                    r_dest,
                    r_src1,
                    r_src2,
                } => {
                    let val1 = self.classical_memory.get(r_src1).copied().unwrap_or(0);
                    let val2 = self.classical_memory.get(r_src2).copied().unwrap_or(0);
                    self.classical_memory.insert(r_dest.clone(), val1 & val2); // Bitwise AND
                }
                Instruction::Or {
                    r_dest,
                    r_src1,
                    r_src2,
                } => {
                    let val1 = self.classical_memory.get(r_src1).copied().unwrap_or(0);
                    let val2 = self.classical_memory.get(r_src2).copied().unwrap_or(0);
                    self.classical_memory.insert(r_dest.clone(), val1 | val2); // Bitwise OR
                }
                Instruction::Xor {
                    r_dest,
                    r_src1,
                    r_src2,
                } => {
                    let val1 = self.classical_memory.get(r_src1).copied().unwrap_or(0);
                    let val2 = self.classical_memory.get(r_src2).copied().unwrap_or(0);
                    self.classical_memory.insert(r_dest.clone(), val1 ^ val2); // Bitwise XOR
                }
                Instruction::CmpEq {
                    r_dest,
                    r_src1,
                    r_src2,
                } => {
                    let val1 = self.classical_memory.get(r_src1).copied().unwrap_or(0);
                    let val2 = self.classical_memory.get(r_src2).copied().unwrap_or(0);
                    let result = if val1 == val2 { 1 } else { 0 };
                    println!(
                        "[VM] PC={:04} CmpEq: Reg '{}' = ({} == {}) = {}",
                        pc, r_dest, val1, val2, result
                    ); // DEBUG
                    self.classical_memory.insert(r_dest.clone(), result);
                }
                Instruction::CmpLt {
                    r_dest,
                    r_src1,
                    r_src2,
                } => {
                    let val1 = self.classical_memory.get(r_src1).copied().unwrap_or(0);
                    let val2 = self.classical_memory.get(r_src2).copied().unwrap_or(0);
                    self.classical_memory
                        .insert(r_dest.clone(), if val1 < val2 { 1 } else { 0 });
                }
                // Add similar println! for other classical ops if needed
                Instruction::Halt => {
                    println!("[VM] PC={:04} Halting.", pc); // DEBUG
                    self.is_halted = true;
                }
                Instruction::NoOp => {
                    println!("[VM] PC={:04} NoOp.", pc); // DEBUG
                    // Do nothing
                }
                Instruction::CmpGt {
                    r_dest,
                    r_src1,
                    r_src2,
                } => {
                    let val1 = self.classical_memory.get(r_src1).copied().unwrap_or(0);
                    let val2 = self.classical_memory.get(r_src2).copied().unwrap_or(0);
                    self.classical_memory
                        .insert(r_dest.clone(), if val1 > val2 { 1 } else { 0 });
                }
            } // End match instruction

            // Check if PC ran off the end without halting
            if !self.is_halted && self.program_counter >= program.instruction_count() {
                println!(
                    "[VM] PC={} Reached end of program. Halting.",
                    self.program_counter
                ); // DEBUG
                self.is_halted = true;
            }
        } // End while !self.is_halted

        println!("[VM RUN END]"); // DEBUG
        Ok(())
    }

    /// Collects all unique QDU IDs mentioned in a program.
    fn collect_qdus(program: &Program) -> Result<HashSet<QduId>, OnqError> {
        let mut qdus = HashSet::new();
        for instruction in &program.instructions {
            match instruction {
                Instruction::QuantumOp(op) => {
                    qdus.extend(op.involved_qdus());
                }
                Instruction::Stabilize { targets } => {
                    qdus.extend(targets);
                }
                Instruction::Record { qdu, .. } => {
                    qdus.insert(*qdu);
                }
                // Classical/Control flow ops don't directly involve QDUs
                _ => {}
            }
        }
        Ok(qdus)
    }

    /// Reads the value of a classical register after a run.
    /// Returns 0 if the register does not exist.
    pub fn get_classical_register(&self, name: &str) -> u64 {
        self.classical_memory.get(name).copied().unwrap_or(0)
    }

    /// Returns a clone of the entire classical memory map.
    pub fn get_classical_memory(&self) -> HashMap<String, u64> {
        self.classical_memory.clone()
    }

    /// Returns a clone of the current quantum PotentialityState, if the
    /// simulation engine has been initialized (i.e., if the program contained quantum ops).
    /// Returns `None` if no quantum state exists (e.g., purely classical program or before run).
    /// Cloning can be expensive for large state vectors.
    pub fn get_final_state(&self) -> Option<crate::PotentialityState> {
        // Access the engine's state via the pub(crate) get_state method and clone it
        self.engine.as_ref().map(|e| e.get_state().clone())
        // Note: PotentialityState derives Clone, which uses Vec::clone, performing a deep copy.
    }
    // Potential future methods:
    // - step(): Execute one instruction
    // - get_potentiality_state(): Get a clone of the engine's state (if engine exists)
    // - set_initial_state(...): Allow starting from non-|0...0> state
    // - inject_error(...): For noise simulation
}

// Default implementation
impl Default for OnqVm {
    fn default() -> Self {
        Self::new()
    }
}