File size: 2,692 Bytes
ca961b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
EM Module - Session Integration

Provides session save/load hooks specifically for EM module state.
"""

from typing import Optional, Dict, Any
from session_integration import SessionIntegration


class EMSessionIntegration:
    """
    Extends SessionIntegration with EM-specific state handling.
    """
    
    def __init__(self, session_integration: SessionIntegration):
        self.session_integration = session_integration
    
    def capture_em_state(self, trame_state: Any) -> Dict[str, Any]:
        """Capture EM-specific state variables."""
        em_keys = [
            # Geometry
            "grid_size",
            "geometry_type",
            "geometry_rotation_x",
            "geometry_rotation_y",
            "geometry_rotation_z",
            
            # Excitation
            "excitation_type",
            "excitation_frequency",
            "excitation_location_x",
            "excitation_location_y",
            "excitation_location_z",
            
            # Simulation
            "frequency",
            "num_qubits",
            "t_final",
            "dt",
            "outer_boundary_condition",
            
            # Backend
            "backend_type",
            "selected_simulator",
            "selected_qpu",
            "aqc_enabled",
            "measurement_type",
        ]
        
        state_dict = {}
        for key in em_keys:
            if hasattr(trame_state, key):
                try:
                    value = getattr(trame_state, key)
                    # Try to serialize to ensure it's JSON-compatible
                    import json
                    json.dumps(value)
                    state_dict[key] = value
                except (TypeError, ValueError):
                    pass
        
        return state_dict
    
    def restore_em_state(self, trame_state: Any) -> None:
        """Restore EM-specific state from session."""
        if not self.session_integration.current_state:
            return
        
        for key, value in self.session_integration.current_state.state_data.items():
            if hasattr(trame_state, key):
                try:
                    setattr(trame_state, key, value)
                except Exception as e:
                    print(f"Warning: Could not restore {key}: {e}")
    
    def save_em_session(self, trame_state: Any) -> bool:
        """Save EM session with state capture."""
        state_dict = self.capture_em_state(trame_state)
        if self.session_integration.current_state:
            self.session_integration.current_state.state_data.update(state_dict)
        return self.session_integration.save_current_session(trame_state)