-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrade_script.py
More file actions
70 lines (56 loc) · 2.49 KB
/
Copy pathtrade_script.py
File metadata and controls
70 lines (56 loc) · 2.49 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
import subprocess
import os
import datetime
# === Config ===
case_name = "simon_our"
depth_start = 10
depth_end = 25
mode = "all"
# Conda envs
depth_env = "Qubi"
# check_env = "check"
# Paths
input_path = f"./Benchmarks/trade/{case_name}.qasm"
output_dir = f"./Outputs/trade_off_new/{case_name}"
log_dir = output_dir
summary_log_file = f"./log/summarize/trade/trade_{case_name}.log"
check_script_path = "check.py"
# Setup
os.makedirs(output_dir, exist_ok=True)
os.makedirs(log_dir, exist_ok=True)
with open(summary_log_file, "w") as log_file:
log_file.write(f"Depth Sweep Summary for {case_name}\n")
log_file.write("=" * 60 + "\n\n")
# Helper: run command in conda env
def run_in_conda_env(env_name, command, description, log_path):
conda_command = f"conda run -n {env_name} {command}"
try:
print(f"[{description}] Running: {conda_command}")
start_time = datetime.datetime.now()
result = subprocess.run(conda_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
end_time = datetime.datetime.now()
with open(log_path, "a") as log_file:
log_file.write(f"--- {description} (depth sweep) ---\n")
log_file.write(f"Time: {start_time} -> {end_time}\n")
log_file.write(f"Command: {conda_command}\n")
log_file.write(f"Output:\n{result.stdout}\n")
if result.stderr:
log_file.write(f"Errors:\n{result.stderr}\n")
log_file.write("\n")
if result.stderr:
print(f"[{description}] Error:\n{result.stderr}")
except Exception as e:
with open(log_path, "a") as log_file:
log_file.write(f"[{description}] Failed with Exception: {e}\n\n")
# === Run each depth ===
for depth in range(depth_end, depth_start - 1, -1):
print(f"\nRunning for depth = {depth}")
output_qasm = os.path.join(output_dir, f"{case_name}_depth{depth}.qasm")
log_path = os.path.join(log_dir, f"{case_name}_depth{depth}.log")
# 1. Run depth.py
depth_command = f"python main.py --input {input_path} --output {output_qasm} --log {log_path} --mode {mode} --depth {depth}"
run_in_conda_env(depth_env, depth_command, f"main.py (depth={depth})", summary_log_file)
# # 2. Run check.py
# check_command = f"python {check_script_path} {input_path} {output_qasm}"
# run_in_conda_env(check_env, check_command, f"check.py (depth={depth})", summary_log_file)
print(f"\nDepth sweep with checking finished! Summary: {summary_log_file}")