File size: 2,863 Bytes
8b66d81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc63321
 
 
 
 
8b66d81
 
fc63321
 
b3dfb35
8b66d81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a9d55d
 
 
 
8b66d81
 
 
 
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

"""
Data models for the Quantum Circuit Optimization Environment.

The quantum_openenv_env environment is an RL environment where the agent
learns to optimize quantum circuits by applying mathematical identities and cancellations.

Supports multi-qubit quantum circuits with individual gate-qubit targeting.
"""

from openenv.core.env_server.types import Action, Observation
from pydantic import BaseModel, Field
from typing import Dict, Any


class QuantumGate(BaseModel):
    """
    Represents a single quantum gate in a circuit.
    
    Each gate has a name (e.g., 'H', 'X', 'Y', 'Z', 'CNOT') and target qubits.
    For single-qubit gates, target_qubits is a list with one element (e.g., [0]).
    For two-qubit gates like CNOT, target_qubits is [control, target] (e.g., [0, 1]).
    """

    name: str = Field(..., description="Gate name (e.g., 'H', 'X', 'Y', 'Z', 'CNOT', 'CX', 'CZ', 'SWAP')")
    target_qubits: list[int] = Field(..., description="List of target qubits. For single-qubit: [q], for two-qubit: [control, target]")


class QuantumAction(Action):
    target_index: int = Field(
        ..., 
        ge=0, # Prevents negative numbers in the UI
        description="Index in the circuit to attempt the action"
    )
    action_type: int = Field(
        default=1, 
        ge=1, # Minimum action type is 1
        le=4, # Maximum action type is 4
        description="1: Cancel identical gates, 2: Swap commuting gates, 3: Replace H-X-H with Z, 4: Replace 3-CNOT sequence with SWAP"
    )


class QuantumObservation(Observation):
    """
    Observation from the Quantum Circuit Optimization environment.
    
    Contains the current state of the multi-qubit quantum circuit and metadata.
    """

    circuit: list[QuantumGate] = Field(
        default_factory=list, 
        description="List of QuantumGate objects representing the current circuit"
    )
    gate_count: int = Field(
        default=0, 
        description="Total number of gates in the current circuit"
    )
    num_qubits: int = Field(
        default=0,
        description="Number of qubits in the system"
    )
    done: bool = Field(
        default=False,
        description="Whether the environment has reached a terminal state"
    )
    reward: float = Field(
        default=0.0,
        description="The reward achieved on the last step"
    )
    prompt: str = Field(
        default="",
        description="Human-readable game rules and current circuit state for the UI playground."
    )
    metadata: Dict[str, Any] = Field(
        default_factory=dict,
        description="Additional episode metadata (e.g., initial gate count, seed)"
    )