-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
242 lines (219 loc) · 8.45 KB
/
config.py
File metadata and controls
242 lines (219 loc) · 8.45 KB
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
# Copyright 2025 Fraunhofer Institute for Open Communication Systems FOKUS
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import os
import torch
from dataclasses import dataclass, field
from typing import Dict, Optional
@dataclass
class DeviceConfig:
"""
Configuration for selecting the computing device.
"""
device: torch.device = field(init=False)
def __post_init__(self):
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@dataclass
class QCConfig:
"""
Quantum Circuit Configuration.
Attributes:
max_param_gate (int): Maximum number of parameters for a gate.
gate_set_ghz_a (list): Allowed gates for configuration 1, e.g., ['cx', 'h', 'rx', 'ry', 'rz', 'id'].
features_ghz_a (int): Number of features for gate set 1.
gate_set_ghz_b (list): Allowed gates for configuration 2, e.g., ['cz', 'id', 'rx', 'rz', 'rzz', 'sx', 'x'].
features_ghz_b (int): Number of features for gate set 2.
gate_set_ghz_a (list): Allowed gates for machine learning models.
features_ls_a (int): Number of features for the ML gate set.
"""
max_param_gate: int = 1
gate_set_ghz_a: list = field(default_factory=lambda: ['cx', 'h', 'rx', 'ry', 'rz', 'id'])
features_ghz_a: int = 7
gate_set_ghz_b: list = field(default_factory=lambda: ['cz', 'id', 'rx', 'rz', 'rzz', 'sx', 'x'])
features_ghz_b: int = 8
gate_set_ls_a: list = field(default_factory=lambda: ['cx', 'h', 'rx', 'ry', 'swap', 'crx', 'cry'])
features_ls_a: int = 8
@dataclass
class ModelConfig:
"""
Configuration for model hyperparameters.
"""
device: torch.device
seed: int
runseed: int
batch_size: int
num_workers: int
epochs: Optional[int] = None
emb_dim: Optional[int] = None
layer_num: Optional[int] = None
qubit_num: Optional[int] = None
num_node_features: Optional[int] = None
drop_ratio: Optional[float] = None
lr: Optional[float] = None
decay: Optional[float] = None
JK: Optional[str] = None
patience: Optional[int] = None
metric: Optional[str] = None
graph_pooling: Optional[str] = None
n_estimators: Optional[int] = None
max_depth: Optional[int] = None
random_state: Optional[int] = None
optuna_trials: Optional[int] = None
min_samples_split: Optional[int] = None
min_samples_leaf: Optional[int] = None
max_features: Optional[str] = None
n_jobs: Optional[int] = None
def get_model_config_from_path(model_config_path, device):
try:
with open(model_config_path, 'r') as f:
config = json.load(f)
config["device"] = device
if "PATHS" in config:
del config["PATHS"]
if "timestamp" in config:
del config["timestamp"]
return ModelConfig(**config)
except (FileNotFoundError, json.JSONDecodeError) as e:
raise RuntimeError(f"Failed to load model config from '{model_config_path}': {e}")
def get_default_model_config_by_search_space(model_type, search_space, features=None, device='cpu'):
"""
Retrieve the model configuration for a specific model name.
Args:
model_config_path (str): Path to the saved JSON config file.
model_name (str): Name of the model (used as a key in the config).
search_space (str): Name of the benchmark search space.
features (int): Number of input node features.
device (str): 'cpu' or 'cuda'.
Returns:
ModelConfig: A ModelConfig instance with updated parameters.
"""
try:
config = _get_default_model_config(search_space, model_type, features, device)
config['device'] = device
config['num_node_features'] = features
config.setdefault('seed', 42)
config.setdefault('runseed', 42)
config.setdefault('num_workers', 0)
config.setdefault('patience', 7)
return ModelConfig(**config)
except Exception as e:
raise RuntimeError(f"Error processing model config: {e}")
def _get_default_model_config(search_space, model_type, features=None, device="cpu"):
"""
Fallback config based on model_name and search_space.
"""
default_configs = {
"gcn_ghz_a": {
'emb_dim': 1200,
'layer_num': 8,
'qubit_num': 3,
'num_node_features': features,
'drop_ratio': 0.012714767230404513,
'batch_size': 32,
'epochs': 3,
'lr': 0.00042048670814195114,
'decay': 1.2239395743425164e-06,
'JK': 'mean',
'graph_pooling': 'max',
'metric': 'spearman',
},
"gcn_ghz_b": {
'emb_dim': 1050,
'layer_num': 8,
'qubit_num': 3,
'num_node_features': features,
'drop_ratio': 0.0644893118913786,
'batch_size': 128,
'epochs': 100,
'lr': 4.540520885756229e-05,
'decay': 1.917208797826118e-06,
'JK': 'mean',
'graph_pooling': 'attention',
'metric': 'spearman',
},
"gcn_ls_a": {
'emb_dim': 1050,
'layer_num': 8,
'qubit_num': 4,
'num_node_features': features,
'drop_ratio': 0.0644893118913786,
'batch_size': 32,
'epochs': 100,
'lr': 4.540520885756229e-05,
'decay': 1.917208797826118e-06,
'JK': 'mean',
'graph_pooling': 'attention',
'metric': 'spearman',
},
"random_forest_ghz_a": {
'n_estimators': 350,
'max_depth': 30,
'min_samples_split': 2,
'min_samples_leaf': 1,
'max_features': 'sqrt',
'n_jobs': -1,
'batch_size': 64,
'metric': 'mse',
},
"random_forest_ghz_b": {
'n_estimators': 325,
'max_depth': 30,
'min_samples_split': 3,
'min_samples_leaf': 3,
'max_features': 'sqrt',
'n_jobs': -1,
'batch_size': 64,
'metric': 'spearman',
},
"random_forest_ls_a": {
'n_estimators': 325,
'max_depth': 30,
'min_samples_split': 3,
'min_samples_leaf': 3,
'max_features': 'sqrt',
'n_jobs': -1,
'batch_size': 64,
'metric': 'spearman',
}
}
if model_type == "gcn":
fallback_key = f"gcn_{search_space}"
elif model_type == "random_forest":
fallback_key = f"random_forest_{search_space}"
else:
raise KeyError(f"Unknown model type for fallback: '{model_type}'")
config = default_configs[fallback_key].copy()
config["device"] = device
if fallback_key not in default_configs:
raise KeyError(f"No default config found for '{fallback_key}'")
return default_configs[fallback_key].copy()
@dataclass
class PathConfig:
"""
Configuration for project file paths.
"""
base_path: str = field(default_factory=lambda: os.path.dirname(os.path.abspath(__file__)))
paths: Dict[str, str] = field(init=False)
def __post_init__(self):
# Define subdirectories for tuning studies, datasets, models, benchmarks, and plots.
self.paths = {
'optuna_studies': os.path.join(self.base_path, 'surrogate_models/tuning', 'studies'),
'raw_data': os.path.join(self.base_path, 'data/raw_data/'),
'gcn_data': os.path.join(self.base_path, 'data/processed_data/', 'gcn_processed_data'),
'rf_data': os.path.join(self.base_path, 'data/processed_data/', 'rf_processed_data'),
# 'test_data': os.path.join(self.base_path, 'data', 'test_data'),
'trained_models': os.path.join(self.base_path, 'surrogate_models', 'trained_models'),
'benchmark_search_spaces': os.path.join(self.base_path, 'benchmark', 'search_spaces'),
# 'plots': os.path.join(self.base_path, 'benchmark/results', 'plots'),
}