File size: 1,836 Bytes
9f8cf99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Protocol contracts for quantum execution workflows."""

from __future__ import annotations

from dataclasses import dataclass
from typing import Any, Protocol

from surface_code_in_stem.rl_control.envs.base import EnvBuildContext


@dataclass(frozen=True)
class ProtocolContract:
    """Execution protocol contract metadata."""

    name: str
    family: str
    description: str
    capabilities: list[str]

    def normalize_context(self, context: EnvBuildContext) -> EnvBuildContext:
        return context

    def validate_context(self, context: EnvBuildContext) -> None:
        if context.distance <= 0:
            raise ValueError("protocol requires distance > 0")
        if context.rounds <= 0:
            raise ValueError("protocol requires rounds > 0")


class QuantumProtocol(Protocol):
    """Extension point for protocol-specific execution behavior."""

    contract: ProtocolContract

    def supports(self, context: EnvBuildContext) -> bool:
        ...

    def normalize_context(self, context: EnvBuildContext) -> EnvBuildContext:
        ...

    def validate_context(self, context: EnvBuildContext) -> None:
        ...


class ProtocolRegistry:
    """Registry of quantum protocols available at runtime."""

    def __init__(self) -> None:
        self._protocols: dict[str, QuantumProtocol] = {}

    def register(self, protocol: QuantumProtocol) -> None:
        name = protocol.contract.name
        if name in self._protocols:
            raise ValueError(f"Protocol '{name}' already registered.")
        self._protocols[name] = protocol

    def get(self, name: str) -> QuantumProtocol:
        if name not in self._protocols:
            raise KeyError(f"Unknown protocol '{name}'.")
        return self._protocols[name]

    def list(self) -> list[str]:
        return sorted(self._protocols.keys())