File size: 5,903 Bytes
80d7559
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// CARRY Sovereign Agent Quantum Simulator & Research Platform
// Module exports

pub mod core;
pub mod fsm;
pub mod agents;
pub mod dag;
pub mod icp;
pub mod asp;
pub mod ftb;
pub mod topological;
pub mod emulator_6052;
pub mod gitc;
pub mod benchmarks;

#[cfg(test)]
mod tests {
    use super::core::*;
    use super::fsm::*;
    use super::agents::*;
    use super::dag::*;
    use super::icp::*;
    use super::asp::*;
    use super::ftb::*;
    use super::topological::*;
    use super::emulator_6052::*;
    use super::gitc::*;
    use super::benchmarks::*;

    #[test]
    fn test_core_simulator_statevector_and_purity() {
        let mut sim = QuantumSimulator::new(2, SimulationMode::StateVector, ErrorModel::None);
        assert!(sim.state.is_valid_state());
        assert_eq!(sim.state.purity(), 1.0);

        let bell = BenchmarkSuite::build_bell_circuit();
        for g in &bell.gates {
            sim.apply_gate(g).unwrap();
        }
        assert!(sim.state.is_valid_state());
        assert_eq!(sim.entanglement_graph.are_entangled(0, 1), true);
    }

    #[test]
    fn test_density_matrix_simulator() {
        let mut sim = QuantumSimulator::new(2, SimulationMode::DensityMatrix, ErrorModel::None);
        assert!(sim.state.is_valid_state());
        assert_eq!(sim.state.purity(), 1.0);

        sim.apply_gate(&Gate::h(0)).unwrap();
        assert!(sim.state.is_valid_state());
    }

    #[test]
    fn test_quantum_fsm_and_max_cycle() {
        let mut fsm = QuantumFSM::new(3);
        assert_eq!(fsm.state, QuantumFSMState::Init);

        fsm.transition(QuantumFSMState::Prepare, "agent1", vec![0], "PREP", "None", "StateReady").unwrap();
        fsm.transition(QuantumFSMState::Entangle, "agent1", vec![0, 1], "ENT", "StateReady", "Entangled").unwrap();
        fsm.transition(QuantumFSMState::Compute, "agent1", vec![0, 1], "COMP", "Entangled", "Computed").unwrap();

        // 4th transition exceeds max_cycle = 3
        let res = fsm.transition(QuantumFSMState::Measure, "agent1", vec![0], "MEAS", "Computed", "Measured");
        assert!(res.is_err());
        assert_eq!(fsm.state, QuantumFSMState::CycleLimit);
    }

    #[test]
    fn test_agent_pair_cross_verification() {
        let primary = Agent::new_primary("a1_compute", vec![0, 1], 100);
        let partner = Agent::new_partner("a2_verify", "a1_compute", AgentRole::PartnerVerify, vec![0, 1]);

        let pair = AgentPair::new(primary, partner, "group_0").unwrap();
        let sim = QuantumSimulator::new(2, SimulationMode::StateVector, ErrorModel::None);

        let ver = pair.cross_verify_state(&sim).unwrap();
        assert!(ver.contains("PARTNER_VERIFIED"));
    }

    #[test]
    fn test_quantum_dag_provenance() {
        let mut dag = QuantumDAG::new();
        let n1 = dag.add_node(QuantumDAGNodeType::Input, "LoadInput", "a1", vec![0], vec![]).unwrap();
        let n2 = dag.add_node(QuantumDAGNodeType::StatePreparation, "Prep", "a1", vec![0], vec![n1]).unwrap();
        let n3 = dag.add_node(QuantumDAGNodeType::ICPGate, "ICP", "icp_gate", vec![0], vec![n2]).unwrap();
        let _n4 = dag.add_node(QuantumDAGNodeType::Commit, "Commit", "a1", vec![0], vec![n3]).unwrap();

        assert!(dag.validate_path().is_ok());
    }

    #[test]
    fn test_icp_governance_gate_and_halt() {
        let mut icp = ICPGate::new();
        let mut sim = QuantumSimulator::new(2, SimulationMode::StateVector, ErrorModel::None);
        let mut fsm = QuantumFSM::new(10);

        // Attempt operation on out-of-bounds qubit 5 -> must HALT
        let res = icp.execute_pipeline("agent1", 5, "H", &mut sim, &mut fsm, |s| {
            s.apply_gate(&Gate::h(5)).map(|_| "OK".to_string())
        });

        assert!(res.is_err());
        assert_eq!(fsm.state, QuantumFSMState::Halted);
    }

    #[test]
    fn test_asp_constraint_engine() {
        let mut asp = ASPEngine::new();
        asp.add_fact(ASPFact::Qubit("q0".to_string()));
        asp.add_fact(ASPFact::Agent("a1".to_string()));
        asp.add_fact(ASPFact::Controls("a1".to_string(), "q0".to_string()));

        match asp.solve() {
            ASPSolverResult::SAT { .. } => {}
            _ => panic!("Expected SAT"),
        }

        // Add invalid control reference
        asp.add_fact(ASPFact::Controls("a1".to_string(), "q99".to_string())); // q99 doesn't exist
        match asp.solve() {
            ASPSolverResult::UNSAT { .. } => {}
            _ => panic!("Expected UNSAT"),
        }
    }

    #[test]
    fn test_6052_emulator_hard_cycle_stop() {
        let mut emu = Emulator6052::new(2);
        for _ in 0..5 {
            emu.push_instruction(Insn6052::QGATE { gate: Gate::h(0) });
        }

        let mut sim = QuantumSimulator::new(1, SimulationMode::StateVector, ErrorModel::None);
        let mut fsm = QuantumFSM::new(10);

        let receipt = emu.execute(&mut sim, &mut fsm);
        assert_eq!(receipt.status, "CYCLE_LIMIT");
        assert_eq!(receipt.cycles_executed, 2);
    }

    #[test]
    fn test_gitc_experiment() {
        let mut gitc = GITCExperiment::new(5);
        let summary = gitc.run_experiment().unwrap();
        assert!(summary.contains("GITC_EXPERIMENT_COMPLETE"));
        assert_eq!(gitc.results.len(), 5);
    }

    #[test]
    fn test_reference_circuits_benchmark() {
        let bell = BenchmarkSuite::build_bell_circuit();
        let report = BenchmarkSuite::run_benchmark("Bell", &bell, 100).unwrap();
        assert_eq!(report.num_qubits, 2);
        assert_eq!(report.status, "SUCCESS");

        let grover = BenchmarkSuite::build_grover_circuit();
        let report2 = BenchmarkSuite::run_benchmark("Grover", &grover, 100).unwrap();
        assert_eq!(report2.num_qubits, 2);
    }
}