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
// src/lib.rs
// Enforce documentation warnings during build
//! `onq`: Operations for Next Generation Quantum Computating Simulation Library
//!
//! This library provides Rust structures and functions for simulating computation
//! based *only* on abstract principles.
//!
//! ## Core Idea
//!
//! Unlike standard quantum simulators modeling quantum mechanics, `onq` explores
//! computation emerging necessarily from a self-containing distinction. It models
//! phenomena analogous to quantum computation (superposition,
//! entanglement analogs, interference, stabilization) without assuming physics,
//! relying solely on the structural and logical consequences defined by the framework
//!
//! ## Key Components
//!
//! * **Core Types (`onq::core`):** Defines fundamental concepts like `QduId`,
//! `PotentialityState` (complex state vectors), and `StableState`.
//! * **Operations (`onq::operations`):** Defines quantum operations (`Operation`, `LockType`)
//! (analogs of H, X, Z, S, T, CNOT, CZ, CPhase, etc.).
//! * **Circuits (`onq::circuits`):** Provides `Circuit` to represent ordered sequences
//! of quantum operations and `CircuitBuilder` for easy construction.
//! * **Validation (`onq::validation`):** Offers functions to check state validity
//! (normalization, phase coherence interpretation).
//! * **ONQ Virtual Machine (`onq::vm`):** An interpreter (`OnqVm`) that executes
//! `Program`s containing mixed sequences of `Instruction`s (quantum ops, classical ops,
//! control flow based on stabilization results).
//! * **Simulation Engine (`onq::simulation::engine` - internal):** Handles the underlying
//! state vector evolution and stabilization logic.
//!
//! ## Interpretation & Differences from QM
//!
//! Users should be aware that `onq` simulation relies heavily on **interpretations**
//! of the abstract and sometimes mathematically ambiguous framework.
//! Key differences from standard Quantum Mechanics include:
//!
//! * **Stabilization vs Measurement:** `Stabilize` is deterministic (seeded by state hash),
//! uses scoring (interpreting Phase Coherence and Pattern Resonance),
//! and resolves potentiality based on framework rules, not probabilistic collapse.
//! * **Locking:** `RelationalLock` uses non-unitary projection to model state integration.
//! * **Operations:** Gate availability and behavior are strictly based on derivations.
//!
//! **See the project README for detailed explanations of concepts, interpretations, and limitations.**
// Re-export the most common types for easier top-level use
pub use ;
pub use ; // Removed Qdu, ReferenceFrame unless needed publicly
pub use Operation;
pub use ;
pub use ;
pub use ;
// Example 1: Single QDU Superposition and Stabilization
// Demonstrates creating a superposition using a derived gate and observing
// the outcome based on the stabilization logic.
/// ```
/// use onq::{QduId, CircuitBuilder, Operation, Simulator, StableState, OnqError};
/// use std::f64::consts::PI; /// Used for potential phase shifts if added later
///
/// // Helper for creating QduId
/// fn qid(id: u64) -> QduId { QduId(id) }
///
/// let q0 = qid(0);
///
/// // Create circuit: Apply Superposition, then Stabilize
/// let circuit = CircuitBuilder::new()
/// .add_op(Operation::InteractionPattern {
/// target: q0,
/// // Uses tentative derived matrix creating equal potentiality |0> + |1>
/// pattern_id: "Superposition".to_string(),
/// })
/// .add_op(Operation::Stabilize { targets: vec![q0] })
/// .build();
///
/// // Run simulation
/// let simulator = Simulator::new();
/// match simulator.run(&circuit) {
/// Ok(result) => {
/// println!("\n--- Example 1: Single QDU Superposition ---");
/// println!("Circuit:\n{}", circuit); // Display requires Circuit Display impl
/// println!("Result:\n{}", result);
///
/// // Analysis based on current interpretation:
/// // Input state |0> -> Superposition -> (1/sqrt(2))(|0> + |1>)
/// // Scores: S(0) = C_A(0)*C_B(0)*amp(0)^2 = 1.0 * 1.0 * 0.5 = 0.5
/// // S(1) = C_A(1)*C_B(1)*amp(1)^2 = 1.0 * 0.5 * 0.5 = 0.25
/// // Outcome |0> is favored due to higher C_B score (lower Hamming weight).
/// // Since selection is deterministic (seeded PRNG), it should consistently pick |0>.
/// let outcome = result.get_stable_state(&q0);
/// println!("Expected outcome for {}: 0 (based on C_A/C_B scoring)", q0);
/// assert_eq!(outcome, Some(&StableState::ResolvedQuality(0)));
/// }
/// Err(e) => {
/// eprintln!("Example 1 failed: {}", e);
/// assert!(false, "Example 1 failed"); // Force test failure
/// }
/// }
/// ```
const _: = ; // Attaches the preceding doc comment block to a hidden item
// Example 2: Two QDU Controlled Interaction
// Demonstrates a CNOT-like sequence using derived gating logic for
// ControlledInteraction and derived flip/stabilization logic.
/// ```
/// use onq::{QduId, CircuitBuilder, Operation, Simulator, StableState, OnqError};
///
/// // Helper for creating QduId
/// fn qid(id: u64) -> QduId { QduId(id) }
///
/// let q0 = qid(0); // Control QDU
/// let q1 = qid(1); // Target QDU
///
/// // Create circuit: Flip q0, then C-Flip q1 controlled by q0, then Stabilize
/// let circuit = CircuitBuilder::new()
/// // 1. Prepare control state |1>: Apply QualityFlip to q0 (State |10>)
/// .add_op(Operation::InteractionPattern {
/// target: q0,
/// pattern_id: "QualityFlip".to_string(),
/// })
/// // 2. Apply controlled interaction: If q0 is |1>, flip q1 (State |11>)
/// .add_op(Operation::ControlledInteraction {
/// control: q0,
/// target: q1,
/// // Use derived flip pattern
/// pattern_id: "QualityFlip".to_string(),
/// })
/// // 3. Stabilize both QDUs
/// .add_op(Operation::Stabilize { targets: vec![q0, q1] })
/// .build();
///
/// // Run simulation
/// let simulator = Simulator::new();
/// match simulator.run(&circuit) {
/// Ok(result) => {
/// println!("\n--- Example 2: Two QDU Controlled Interaction ---");
/// println!("Circuit:\n{}", circuit);
/// println!("Result:\n{}", result);
///
/// // Analysis:
/// // Initial: |00>
/// // After Flip q0: |10>
/// // After C-Flip: |11> (Control q0 is |1>, so target q1 flips from |0> to |1>)
/// // Stabilize input: |11> (State vector [0, 0, 0, 1])
/// // Only basis state |11> (k=3) has non-zero score -> deterministic outcome |11>.
/// let outcome0 = result.get_stable_state(&q0);
/// let outcome1 = result.get_stable_state(&q1);
/// println!("Expected outcome for {}: 1", q0);
/// println!("Expected outcome for {}: 1", q1);
/// assert_eq!(outcome0, Some(&StableState::ResolvedQuality(1)));
/// assert_eq!(outcome1, Some(&StableState::ResolvedQuality(1)));
/// }
/// Err(e) => {
/// eprintln!("Example 2 failed: {}", e);
/// assert!(false, "Example 2 failed"); // Force test failure
/// }
/// }
/// ```
const _: = ; // Attaches the preceding doc comment block to a hidden item