poseidon666 commited on
Commit
73da82e
·
verified ·
1 Parent(s): 86039ce

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. gradio_app.py +3 -122
  2. server/app.py +4 -15
gradio_app.py CHANGED
@@ -133,15 +133,7 @@ def build_score_bars(obs):
133
  def do_reset(task_name):
134
  """Reset environment with selected task."""
135
  global current_obs, step_history
136
- task_map = {
137
- "Bell State (Easy)": "easy",
138
- "GHZ State (Medium)": "medium",
139
- "Unitary Approx (Hard)": "hard",
140
- "Imperfect but Efficient": "efficient",
141
- "Noise-Dominant": "noisy",
142
- "Budgeted Optimization": "budget",
143
- "Approximate Target": "approx"
144
- }
145
  task_id = task_map.get(task_name, "easy")
146
  current_obs = env.reset(task_id=task_id)
147
  step_history = []
@@ -258,98 +250,6 @@ def do_step(action_type, gate_type, qubit_0, qubit_1, parameter):
258
  "\n".join(step_history),
259
  )
260
 
261
- def do_premade_scenario(scenario_name):
262
- """Fast-forward through a preconfigured set of gates for visual demonstration."""
263
- global current_obs, step_history
264
-
265
- scenarios = {
266
- "Optimal Bell State": {
267
- "task": "easy",
268
- "actions": [
269
- QuantumAction(action_type=ActionType.ADD, gate=GateType.H, qubits=[0]),
270
- QuantumAction(action_type=ActionType.ADD, gate=GateType.CNOT, qubits=[0, 1]),
271
- QuantumAction(action_type=ActionType.STOP)
272
- ]
273
- },
274
- "Optimal GHZ State": {
275
- "task": "medium",
276
- "actions": [
277
- QuantumAction(action_type=ActionType.ADD, gate=GateType.H, qubits=[0]),
278
- QuantumAction(action_type=ActionType.ADD, gate=GateType.CNOT, qubits=[0, 1]),
279
- QuantumAction(action_type=ActionType.ADD, gate=GateType.CNOT, qubits=[1, 2]),
280
- QuantumAction(action_type=ActionType.STOP)
281
- ]
282
- },
283
- "Budget Overflow (Failure Simulation)": {
284
- "task": "budget",
285
- "actions": [
286
- QuantumAction(action_type=ActionType.ADD, gate=GateType.H, qubits=[0]),
287
- QuantumAction(action_type=ActionType.ADD, gate=GateType.CNOT, qubits=[0, 1]),
288
- QuantumAction(action_type=ActionType.ADD, gate=GateType.X, qubits=[1]),
289
- QuantumAction(action_type=ActionType.ADD, gate=GateType.H, qubits=[2]),
290
- QuantumAction(action_type=ActionType.STOP)
291
- ]
292
- },
293
- "Noise Deep Circuit (Failure Simulation)": {
294
- "task": "noisy",
295
- "actions": [
296
- QuantumAction(action_type=ActionType.ADD, gate=GateType.H, qubits=[0]),
297
- QuantumAction(action_type=ActionType.ADD, gate=GateType.CNOT, qubits=[0, 1]),
298
- QuantumAction(action_type=ActionType.ADD, gate=GateType.X, qubits=[1]),
299
- QuantumAction(action_type=ActionType.ADD, gate=GateType.X, qubits=[1]),
300
- QuantumAction(action_type=ActionType.ADD, gate=GateType.X, qubits=[1]),
301
- QuantumAction(action_type=ActionType.ADD, gate=GateType.X, qubits=[1]),
302
- QuantumAction(action_type=ActionType.STOP)
303
- ]
304
- }
305
- }
306
-
307
- if scenario_name not in scenarios:
308
- return "Invalid Scenario", "(none)", "", "{}", ""
309
-
310
- conf = scenarios[scenario_name]
311
-
312
- # Reset
313
- current_obs = env.reset(task_id=conf["task"])
314
- step_history = [f"--- Running Scenario: {scenario_name} ---"]
315
-
316
- for act in conf["actions"]:
317
- current_obs = env.step(act)
318
-
319
- gate_str = ""
320
- if act.gate:
321
- gate_str = f" {act.gate.value}({','.join(str(q) for q in act.qubits)})"
322
-
323
- step_entry = (
324
- f"Step {current_obs.steps_taken}: {act.action_type.value}"
325
- + gate_str
326
- + f" -> fid={current_obs.fidelity:.4f} score={current_obs.score:.4f}"
327
- + f" reward={current_obs.reward:+.4f}"
328
- + (" [DONE]" if current_obs.done else "")
329
- )
330
- step_history.append(step_entry)
331
-
332
- obs_json = format_obs_json(current_obs)
333
- circuit_text = format_circuit_display(current_obs)
334
- score_html = build_score_bars(current_obs)
335
-
336
- status = (
337
- f"Scenario Complete: {scenario_name}\n"
338
- + f"Step {current_obs.steps_taken}/{current_obs.max_steps}"
339
- + f" | Fidelity: {current_obs.fidelity:.4f}"
340
- + f" | Score: {current_obs.score:.4f}"
341
- + f"\n\nEPISODE COMPLETE! Final score: {current_obs.score:.4f}"
342
- )
343
-
344
- return (
345
- status,
346
- circuit_text,
347
- score_html,
348
- json.dumps(obs_json, indent=2),
349
- "\n".join(step_history),
350
- )
351
-
352
-
353
 
354
  # ---------------------------------------------------------------------------
355
  # Build Gradio UI
@@ -388,30 +288,13 @@ def create_gradio_app():
388
  with gr.Column(scale=1):
389
  gr.Markdown("### 1. Select Task & Reset")
390
  task_dropdown = gr.Dropdown(
391
- choices=[
392
- "Bell State (Easy)", "GHZ State (Medium)", "Unitary Approx (Hard)",
393
- "Imperfect but Efficient", "Noise-Dominant",
394
- "Budgeted Optimization", "Approximate Target"
395
- ],
396
  value="Bell State (Easy)",
397
  label="Task",
398
  )
399
  reset_btn = gr.Button("Reset Environment", variant="primary", size="lg")
400
 
401
- gr.Markdown("### 2. Pre-made Scenarios")
402
- scenario_dropdown = gr.Dropdown(
403
- choices=[
404
- "Optimal Bell State",
405
- "Optimal GHZ State",
406
- "Budget Overflow (Failure Simulation)",
407
- "Noise Deep Circuit (Failure Simulation)"
408
- ],
409
- value="Optimal Bell State",
410
- label="Select Scenario"
411
- )
412
- scenario_btn = gr.Button("Run Scenario Fast-Forward", variant="secondary")
413
-
414
- gr.Markdown("### 3. Build Circuit Manually")
415
  action_dropdown = gr.Dropdown(
416
  choices=["ADD", "REMOVE", "SWAP", "PARAM", "STOP"],
417
  value="ADD",
@@ -457,8 +340,6 @@ def create_gradio_app():
457
  reset_outputs = [status_box, circuit_display, score_bars, json_display, history_display]
458
  reset_btn.click(fn=do_reset, inputs=[task_dropdown], outputs=reset_outputs)
459
 
460
- scenario_btn.click(fn=do_premade_scenario, inputs=[scenario_dropdown], outputs=reset_outputs)
461
-
462
  step_outputs = [status_box, circuit_display, score_bars, json_display, history_display]
463
  step_btn.click(
464
  fn=do_step,
 
133
  def do_reset(task_name):
134
  """Reset environment with selected task."""
135
  global current_obs, step_history
136
+ task_map = {"Bell State (Easy)": "easy", "GHZ State (Medium)": "medium", "Unitary Approx (Hard)": "hard"}
 
 
 
 
 
 
 
 
137
  task_id = task_map.get(task_name, "easy")
138
  current_obs = env.reset(task_id=task_id)
139
  step_history = []
 
250
  "\n".join(step_history),
251
  )
252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
 
254
  # ---------------------------------------------------------------------------
255
  # Build Gradio UI
 
288
  with gr.Column(scale=1):
289
  gr.Markdown("### 1. Select Task & Reset")
290
  task_dropdown = gr.Dropdown(
291
+ choices=["Bell State (Easy)", "GHZ State (Medium)", "Unitary Approx (Hard)"],
 
 
 
 
292
  value="Bell State (Easy)",
293
  label="Task",
294
  )
295
  reset_btn = gr.Button("Reset Environment", variant="primary", size="lg")
296
 
297
+ gr.Markdown("### 2. Build Circuit")
 
 
 
 
 
 
 
 
 
 
 
 
 
298
  action_dropdown = gr.Dropdown(
299
  choices=["ADD", "REMOVE", "SWAP", "PARAM", "STOP"],
300
  value="ADD",
 
340
  reset_outputs = [status_box, circuit_display, score_bars, json_display, history_display]
341
  reset_btn.click(fn=do_reset, inputs=[task_dropdown], outputs=reset_outputs)
342
 
 
 
343
  step_outputs = [status_box, circuit_display, score_bars, json_display, history_display]
344
  step_btn.click(
345
  fn=do_step,
server/app.py CHANGED
@@ -470,20 +470,9 @@ def create_gradio_app():
470
 
471
 
472
  # ---------------------------------------------------------------------------
473
- # ASGI Setup for Uvicorn Deployment on Hugging Face
474
  # ---------------------------------------------------------------------------
475
- from fastapi import FastAPI
476
-
477
- fastapi_app = FastAPI()
478
-
479
- @fastapi_app.get("/health")
480
- def health_check():
481
- return {"status": "ok"}
482
-
483
- gradio_demo = create_gradio_app()
484
- app = gr.mount_gradio_app(fastapi_app, gradio_demo, path="/")
485
-
486
-
487
  if __name__ == "__main__":
488
- import uvicorn
489
- uvicorn.run(app, host="0.0.0.0", port=8000)
 
 
470
 
471
 
472
  # ---------------------------------------------------------------------------
473
+ # Standalone entry point
474
  # ---------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
475
  if __name__ == "__main__":
476
+ demo = create_gradio_app()
477
+ demo.launch(server_name="0.0.0.0", server_port=7860)
478
+