File size: 9,305 Bytes
1bb4678
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""
Workflows Module: Predefined workflow definitions.
Workflows are sequences of steps that produce useful outputs.

EXPECTED REQUEST COUNTS PER WORKFLOW TYPE:
==========================================

NAKED (Baseline - Direct MCP):
  - LLM requests: 0 per problem
  - MCP requests: 1-2 per problem (direct circuit generation)
  - Total API calls: 1-2 per problem
  - Rate limit impact: NONE (no LLM calls)
  - Expected time: <1 second per problem

GUIDED (Rigid Agentic - Rule-Based State Machine):
  - LLM requests: 4 per problem (one per agent: Architect, Builder, Validator, Scorer)
  - MCP requests: 2-4 per problem (template selection, circuit generation)
  - Total API calls: 6-8 per problem
  - Rate limit impact: LOW (sequential agent calls with 5s rate limiting)
  - Expected time: ~20-30 seconds per problem with rate limiting

BLACKBOARD (Flexible Agentic - Event-Driven):
  - LLM requests: 8-12 per problem (multiple collaborative rounds)
  - MCP requests: 4-8 per problem (iterative refinement)
  - Total API calls: 12-20 per problem
  - Rate limit impact: MODERATE (many LLM calls, needs careful rate management)
  - Expected time: ~60-90 seconds per problem with rate limiting

For 9 test problems (3 easy, 3 medium, 3 hard):
  - Naked: ~9-18 API calls total (all MCP, no rate limiting) = ~9 seconds
  - Guided: ~54-72 API calls (36 LLM + 18-36 MCP) = ~3-6 minutes with rate limiting
  - Blackboard: ~108-180 API calls (72-108 LLM + 36-72 MCP) = ~6-15 minutes

Free tier limits (Gemini 2.5 Flash-Lite): 15 RPM, 1000 RPD
With 80% buffer (12 RPM = 5s intervals): Can process ~2-3 Guided problems/min or ~1 Blackboard problem/min
"""

from dataclasses import dataclass, field
from typing import List, Dict, Any, Optional, Callable
from enum import Enum


class WorkflowStatus(Enum):
    """Status of workflow execution."""
    NOT_STARTED = "not_started"
    IN_PROGRESS = "in_progress"
    COMPLETED = "completed"
    FAILED = "failed"
    PAUSED = "paused"


@dataclass
class WorkflowStep:
    """A single step in a workflow."""
    name: str
    agent_type: str
    description: str
    required: bool = True
    timeout_seconds: float = 60.0
    retry_count: int = 1
    inputs: List[str] = field(default_factory=list)  # Keys from context
    outputs: List[str] = field(default_factory=list)  # Keys to store in context


@dataclass
class WorkflowDefinition:
    """Definition of a complete workflow."""
    name: str
    description: str
    steps: List[WorkflowStep]
    entry_point: str = ""  # First step name
    final_output: str = ""  # Key for final result
    
    def __post_init__(self):
        if not self.entry_point and self.steps:
            self.entry_point = self.steps[0].name


@dataclass
class WorkflowExecution:
    """Runtime state of workflow execution."""
    workflow: WorkflowDefinition
    status: WorkflowStatus = WorkflowStatus.NOT_STARTED
    current_step_index: int = 0
    context: Dict[str, Any] = field(default_factory=dict)
    results: Dict[str, Any] = field(default_factory=dict)
    errors: List[str] = field(default_factory=list)
    
    @property
    def current_step(self) -> Optional[WorkflowStep]:
        if 0 <= self.current_step_index < len(self.workflow.steps):
            return self.workflow.steps[self.current_step_index]
        return None
        
    def advance(self):
        """Move to next step."""
        self.current_step_index += 1
        if self.current_step_index >= len(self.workflow.steps):
            self.status = WorkflowStatus.COMPLETED
            
    def fail(self, error: str):
        """Mark workflow as failed."""
        self.errors.append(error)
        self.status = WorkflowStatus.FAILED


# ============================================================
# PREDEFINED WORKFLOWS
# ============================================================

BUILD_WORKFLOW = WorkflowDefinition(
    name="build",
    description="Create a new quantum circuit from a description or template",
    steps=[
        WorkflowStep(
            name="plan",
            agent_type="architect",
            description="Plan the circuit structure",
            inputs=["goal"],
            outputs=["plan", "circuit_qasm"]
        ),
        WorkflowStep(
            name="build",
            agent_type="builder",
            description="Build the circuit based on plan",
            inputs=["plan"],
            outputs=["circuit_qasm"]
        ),
        WorkflowStep(
            name="validate",
            agent_type="validator",
            description="Validate the built circuit",
            inputs=["circuit_qasm"],
            outputs=["validation_result"]
        ),
        WorkflowStep(
            name="score",
            agent_type="scorer",
            description="Score the final circuit",
            inputs=["circuit_qasm"],
            outputs=["scores"],
            required=False
        )
    ],
    final_output="circuit_qasm"
)


OPTIMIZE_WORKFLOW = WorkflowDefinition(
    name="optimize",
    description="Optimize an existing quantum circuit",
    steps=[
        WorkflowStep(
            name="analyze",
            agent_type="analyzer",
            description="Analyze the current circuit",
            inputs=["circuit_qasm"],
            outputs=["analysis"]
        ),
        WorkflowStep(
            name="optimize",
            agent_type="optimizer",
            description="Apply optimizations",
            inputs=["circuit_qasm", "analysis"],
            outputs=["optimized_qasm"]
        ),
        WorkflowStep(
            name="validate",
            agent_type="validator",
            description="Validate optimized circuit",
            inputs=["optimized_qasm"],
            outputs=["validation_result"]
        ),
        WorkflowStep(
            name="compare",
            agent_type="scorer",
            description="Compare before/after scores",
            inputs=["circuit_qasm", "optimized_qasm"],
            outputs=["comparison"]
        )
    ],
    final_output="optimized_qasm"
)


EVALUATE_WORKFLOW = WorkflowDefinition(
    name="evaluate",
    description="Evaluate a quantum circuit comprehensively",
    steps=[
        WorkflowStep(
            name="validate",
            agent_type="validator",
            description="Validate circuit correctness",
            inputs=["circuit_qasm"],
            outputs=["validation_result"]
        ),
        WorkflowStep(
            name="analyze",
            agent_type="analyzer",
            description="Analyze circuit properties",
            inputs=["circuit_qasm"],
            outputs=["analysis"]
        ),
        WorkflowStep(
            name="score",
            agent_type="scorer",
            description="Score the circuit",
            inputs=["circuit_qasm"],
            outputs=["scores"]
        ),
        WorkflowStep(
            name="simulate",
            agent_type="simulator",
            description="Simulate and get results",
            inputs=["circuit_qasm"],
            outputs=["simulation_results"]
        )
    ],
    final_output="scores"
)


FULL_PIPELINE_WORKFLOW = WorkflowDefinition(
    name="full_pipeline",
    description="Complete circuit creation, optimization, and evaluation",
    steps=[
        WorkflowStep(
            name="plan",
            agent_type="architect",
            description="Plan circuit architecture",
            inputs=["goal"],
            outputs=["plan"]
        ),
        WorkflowStep(
            name="build",
            agent_type="builder",
            description="Build initial circuit",
            inputs=["plan"],
            outputs=["circuit_qasm"]
        ),
        WorkflowStep(
            name="validate_initial",
            agent_type="validator",
            description="Validate initial build",
            inputs=["circuit_qasm"],
            outputs=["initial_validation"]
        ),
        WorkflowStep(
            name="analyze",
            agent_type="analyzer",
            description="Analyze for optimization",
            inputs=["circuit_qasm"],
            outputs=["analysis"]
        ),
        WorkflowStep(
            name="optimize",
            agent_type="optimizer",
            description="Optimize circuit",
            inputs=["circuit_qasm", "analysis"],
            outputs=["optimized_qasm"],
            required=False
        ),
        WorkflowStep(
            name="validate_final",
            agent_type="validator",
            description="Validate final circuit",
            inputs=["optimized_qasm"],
            outputs=["final_validation"]
        ),
        WorkflowStep(
            name="score",
            agent_type="scorer",
            description="Final scoring",
            inputs=["optimized_qasm"],
            outputs=["scores"]
        )
    ],
    final_output="optimized_qasm"
)


# Registry of available workflows
WORKFLOWS = {
    "build": BUILD_WORKFLOW,
    "optimize": OPTIMIZE_WORKFLOW,
    "evaluate": EVALUATE_WORKFLOW,
    "full_pipeline": FULL_PIPELINE_WORKFLOW
}


def get_workflow(name: str) -> Optional[WorkflowDefinition]:
    """Get a workflow by name."""
    return WORKFLOWS.get(name)


def list_workflows() -> List[str]:
    """List all available workflow names."""
    return list(WORKFLOWS.keys())