@@ -69,56 +69,125 @@ Understanding `onq` requires recognizing its fundamental departures from standar
6969## Usage Example (Quantum Teleportation Analog)
7070
7171``` rust
72- // From examples/vm_teleportation.rs (showing structure)
72+ // From examples/vm_teleportation.rs
7373use onq :: {
74- core :: {OnqError , QduId },
75- operations :: Operation ,
76- vm :: {Instruction , LockType , OnqVm , ProgramBuilder }, // Use LockType if needed elsewhere
74+ CircuitBuilder , Operation , QduId , Simulator , StableState
7775};
7876
77+ // Helper for QduId creation for brevity in examples
7978fn qid (id : u64 ) -> QduId { QduId (id ) }
8079
81- fn main () -> Result <(), Box <dyn std :: error :: Error >> {
82- let msg_q = qid (0 );
83- let alice_q = qid (1 );
84- let bob_q = qid (2 );
85-
86- let program = ProgramBuilder :: new ()
87- // 1. Prep Message |+>
88- . add (Instruction :: QuantumOp (Operation :: InteractionPattern { target : msg_q , pattern_id : " Superposition" . to_string () }))
89- // 2. Create Bell Pair |Φ+> (Alice, Bob)
90- . add (Instruction :: QuantumOp (Operation :: InteractionPattern { target : alice_q , pattern_id : " Superposition" . to_string () }))
91- . add (Instruction :: QuantumOp (Operation :: ControlledInteraction { control : alice_q , target : bob_q , pattern_id : " QualityFlip" . to_string () }))
92- // 3. Bell Basis Change (Msg, Alice)
93- . add (Instruction :: QuantumOp (Operation :: ControlledInteraction { control : msg_q , target : alice_q , pattern_id : " QualityFlip" . to_string () }))
94- . add (Instruction :: QuantumOp (Operation :: InteractionPattern { target : msg_q , pattern_id : " Superposition" . to_string () }))
95- // 4. Stabilize & Record Alice's results
96- . add (Instruction :: Stabilize { targets : vec! [msg_q , alice_q ] })
97- . add (Instruction :: Record { qdu : msg_q , register : " m_msg" . to_string () })
98- . add (Instruction :: Record { qdu : alice_q , register : " m_alice" . to_string () })
99- // 5. Bob's Conditional Corrections
100- . add (Instruction :: Label (" X_Correction" . to_string ()))
101- . add (Instruction :: BranchIfZero { register : " m_alice" . to_string (), label : " skip_x_corr" . to_string () })
102- . add (Instruction :: QuantumOp (Operation :: InteractionPattern { target : bob_q , pattern_id : " QualityFlip" . to_string () }))
103- . add (Instruction :: Label (" skip_x_corr" . to_string ()))
104- . add (Instruction :: Label (" Z_Correction" . to_string ()))
105- . add (Instruction :: BranchIfZero { register : " m_msg" . to_string (), label : " skip_z_corr" . to_string () })
106- . add (Instruction :: QuantumOp (Operation :: InteractionPattern { target : bob_q , pattern_id : " PhaseIntroduce" . to_string () }))
107- . add (Instruction :: Label (" skip_z_corr" . to_string ()))
108- // 6. Stabilize Bob & Record
109- . add (Instruction :: Stabilize { targets : vec! [bob_q ] })
110- . add (Instruction :: Record { qdu : bob_q , register : " m_bob" . to_string () })
111- // 7. Halt
112- . add (Instruction :: Halt )
113- . build ()? ;
114-
115- println! (" Program:\ n {}" , program ); // Uses Program Display impl
116-
117- let mut vm = OnqVm :: new ();
118- vm . run (& program )? ;
119-
120- println! (" \ n Final Classical Memory: {:?}" , vm . get_classical_memory ());
121- Ok (())
80+ fn main () {
81+ // Define the three QDUs involved
82+ let msg_q = qid (0 ); // Message QDU (will be prepared in |+>)
83+ let alice_q = qid (1 ); // Alice's QDU (part of the Bell pair)
84+ let bob_q = qid (2 ); // Bob's QDU (part of the Bell pair, receives the state)
85+
86+ println! (" QDUs defined: msg={}, alice={}, bob={}" , msg_q , alice_q , bob_q );
87+
88+ // --- Build Circuit using Quantum Recovery Logic ---
89+
90+ let mut builder = CircuitBuilder :: new ();
91+ println! (" Building Teleportation Circuit..." );
92+
93+ // 1. Prepare Message State: Put msg_q in |+> state
94+ builder = builder . add_op (Operation :: InteractionPattern {
95+ target : msg_q ,
96+ pattern_id : " Superposition" . to_string (), // H analog
97+ });
98+ println! (" Step 1: Prepared Message QDU in |+> state (using Superposition)." );
99+
100+ // 2. Create Bell Pair between Alice and Bob: |Φ+> = (1/sqrt(2))(|00> + |11>)
101+ builder = builder . add_op (Operation :: InteractionPattern { // H on Alice
102+ target : alice_q ,
103+ pattern_id : " Superposition" . to_string (),
104+ });
105+ builder = builder . add_op (Operation :: ControlledInteraction { // CNOT(Alice, Bob)
106+ control : alice_q ,
107+ target : bob_q ,
108+ pattern_id : " QualityFlip" . to_string (), // X analog
109+ });
110+ println! (" Step 2: Created Bell Pair between Alice and Bob." );
111+
112+ // 3. Alice performs Bell Measurement operations (basis change)
113+ builder = builder . add_op (Operation :: ControlledInteraction { // CNOT(Message, Alice)
114+ control : msg_q ,
115+ target : alice_q ,
116+ pattern_id : " QualityFlip" . to_string (),
117+ });
118+ builder = builder . add_op (Operation :: InteractionPattern { // H on Message
119+ target : msg_q ,
120+ pattern_id : " Superposition" . to_string (),
121+ });
122+ println! (" Step 3: Applied Bell Measurement basis change gates (CNOT, H)." );
123+
124+ // 4. Quantum Recovery Operations (before stabilization)
125+ // These apply corrections based on the state *before* stabilization.
126+ builder = builder . add_op (Operation :: ControlledInteraction { // CNOT(Alice, Bob)
127+ control : alice_q ,
128+ target : bob_q ,
129+ pattern_id : " QualityFlip" . to_string (),
130+ });
131+ builder = builder . add_op (Operation :: ControlledInteraction { // CZ(Message, Bob) analog
132+ control : msg_q ,
133+ target : bob_q ,
134+ pattern_id : " PhaseIntroduce" . to_string (), // Use derived Z analog pattern
135+ });
136+ println! (" Step 4: Applied Quantum Recovery gates (CNOT, CZ analog)." );
137+
138+ // 5. Stabilize Bob's QDU to observe the teleported state
139+ // Optionally stabilize Alice and Message to see their final states too.
140+ builder = builder . add_op (Operation :: Stabilize {
141+ targets : vec! [msg_q , alice_q , bob_q ],
142+ });
143+ println! (" Step 5: Added final stabilization for all QDUs." );
144+
145+ let circuit = builder . build ();
146+
147+ // Print the constructed circuit diagram
148+ println! (" \ n Quantum Teleportation Circuit (Quantum Recovery):\ n {}" , circuit );
149+
150+ // --- Run Simulation ---
151+ let simulator = Simulator :: new ();
152+ println! (" \ n Running simulation..." );
153+
154+ match simulator . run (& circuit ) {
155+ Ok (result ) => {
156+ println! (" Simulation finished successfully." );
157+ println! (" \ n Simulation Result Details:" );
158+ println! (" {}" , result );
159+
160+ // --- Basic Result Analysis ---
161+ // Ideally, Bob's QDU (bob_q) now holds the original state of msg_q (which was |+>).
162+ // Stabilizing the |+> state = (1/sqrt(2))[|0> + |1>] depends on the rules.
163+ // Based on our current stabilization (S(0)=0.25, S(1)=0.25), the outcome for Bob
164+ // will be deterministically 0 or 1 based on the final state hash and PRNG.
165+ // A full verification would require state vector tomography (not implemented)
166+ // or running statistical tests if stabilization were probabilistic.
167+
168+ println! (" \ n Analysis:" );
169+ if let Some (StableState :: ResolvedQuality (bob_outcome )) = result . get_stable_state (& bob_q ) {
170+ println! (" - Bob's QDU ({}) stabilized to state: {}" , bob_q , bob_outcome );
171+ println! (" (Note: Expected pre-stabilization state was |+>, outcome {} depends on deterministic stabilization)" , bob_outcome );
172+ } else {
173+ println! (" - Bob's QDU ({}) was not found in stabilization results." , bob_q );
174+ }
175+ // Print Alice and Message outcomes too
176+ if let Some (StableState :: ResolvedQuality (alice_outcome )) = result . get_stable_state (& alice_q ) {
177+ println! (" - Alice's QDU ({}) stabilized to state: {}" , alice_q , alice_outcome );
178+ }
179+ if let Some (StableState :: ResolvedQuality (msg_outcome )) = result . get_stable_state (& msg_q ) {
180+ println! (" - Message's QDU ({}) stabilized to state: {}" , msg_q , msg_outcome );
181+ println! (" (These represent the classical bits Alice would send in standard protocol)" );
182+ }
183+ println! (" \ n Verification of perfect state teleportation would require state vector analysis." );
184+
185+ }
186+ Err (e ) => {
187+ eprintln! (" \ n --- Simulation Failed ---" );
188+ eprintln! (" Error: {}" , e );
189+ }
190+ }
122191}
123192```
124193
0 commit comments