quantum-qec / client.py
Deployment Bot
Deploy quantum-qec application
b68f7d0
Raw History Blame Contribute Delete
3.74 kB
"""OpenEnv client for Quantum-QEC environment."""
import json
import requests
from typing import Any, Dict, Optional, Tuple
from pydantic import BaseModel
class QuantamClientEnvironment:
"""Client-side wrapper for Quantum-QEC OpenEnv."""
def __init__(self, server_url: str = "http://localhost:8000"):
"""Initialize the client.
Args:
server_url: Base URL of the FastAPI server
"""
self.server_url = server_url.rstrip("/")
self.current_task = None
self.current_seed = None
def reset(self, task_id: str = "easy", seed: Optional[int] = None) -> Dict[str, Any]:
"""Reset episode and return initial observation.
Args:
task_id: Task identifier (easy, medium, hard)
seed: Random seed for reproducibility
Returns:
Dictionary with observation and additional info
"""
params = {"task_id": task_id}
if seed is not None:
params["seed"] = seed
response = requests.post(
f"{self.server_url}/reset",
params=params
)
response.raise_for_status()
result = response.json()
self.current_task = task_id
self.current_seed = seed
return result
def step(
self,
action: Dict[str, Any]
) -> Tuple[Dict[str, Any], float, bool, Dict[str, Any]]:
"""Execute one step of the environment.
Args:
action: Action dictionary with 'annealing' and 'noop' fields
Returns:
observation, reward, done, info
"""
response = requests.post(
f"{self.server_url}/step",
json=action
)
response.raise_for_status()
result = response.json()
return (
result.get("observation", {}),
float(result.get("reward", 0.0)),
bool(result.get("done", False)),
result.get("info", {})
)
def get_state(self) -> Dict[str, Any]:
"""Get current environment state.
Returns:
Full state dictionary
"""
response = requests.get(f"{self.server_url}/state")
response.raise_for_status()
return response.json()
def grade(self) -> Dict[str, Any]:
"""Get task grade at current state.
Returns:
Grade information with score and metrics
"""
response = requests.post(f"{self.server_url}/grade")
response.raise_for_status()
return response.json()
def list_tasks(self) -> Dict[str, Any]:
"""List available tasks with specifications.
Returns:
Task specifications
"""
response = requests.get(f"{self.server_url}/tasks")
response.raise_for_status()
return response.json()
def health(self) -> Dict[str, Any]:
"""Check server health.
Returns:
Health status
"""
response = requests.get(f"{self.server_url}/health")
response.raise_for_status()
return response.json()
# OpenEnv interface: env is a callable that returns an environment instance
def env(*args, **kwargs) -> QuantamClientEnvironment:
"""Factory function for OpenEnv framework.
Returns:
Client environment instance
"""
server_url = kwargs.get("server_url", "http://localhost:8000")
return QuantamClientEnvironment(server_url=server_url)
if __name__ == "__main__":
# Quick test
client = QuantamClientEnvironment("http://localhost:8000")
print("Testing health check...")
print(client.health())