# laamiriouail/qrlx architecture

> Architecture diagram of the GitHub repository laamiriouail/qrlx, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/laamiriouail/qrlx
- Repository: https://github.com/laamiriouail/qrlx

## Overview

#### Purpose and entry points
QRLX is a Python framework for composing hybrid quantum-classical reinforcement-learning experiments as backend-agnostic DAGs. Primary programmatic flow is YAML resolution → graph compilation → graph execution → trainer-driven learning. Runnable entry points include examples such as `examples/01_cartpole_hybrid.py`, configuration-driven scripts `scripts/execute_manifest.py` and `scripts/expand_sweeps.py`, and YAML experiment definitions under `configs/`.

#### Core contracts and tensor-neutral IR
The zero-dependency core defines the stable abstractions shared by all integrations: components/stages, environments, backends, trainers, gradients, tracking, registry, and errors. The IR carries opaque tensor values in `Payload` plus metadata in `TensorSpec`; core never depends on a tensor library. Key paths: `qrl/core/contracts.py`, `qrl/core/ir.py`, `qrl/core/registry.py`. Trainer common behavior and persistence/metrics conventions reside in `qrl/core/_trainer_base.py` and `qrl/core/trainer.py`.

#### DAG compilation and execution
The pipeline engine models components and edges, compiles a validated executable graph, then runs it without implicit side effects. Compilation validates graph ordering and quantum-edge gradient compatibility; execution moves payloads through stages. Graph export supports inspection/tooling. Key paths: `qrl/dag/graph.py`, `qrl/dag/compiler.py`, `qrl/dag/runner.py`, `qrl/dag/export.py`. This layer is standard-library-only and must not import configuration or plugin code.

#### Configuration, manifests, and experiment orchestration
Configuration is an inversion-of-control boundary: versioned YAML manifests are parsed, validated, resolved into experiment objects, and optionally expanded into sweeps before runtime assembly. It does not import concrete plugins; callers provide/select registered implementations. Key paths: `qrl/config/manifest.py`, `qrl/config/resolver.py`, `qrl/config/executor.py`, `qrl/config/sweeps.py`. Representative manifests include `configs/cartpole_sb3_ppo_quantum.yaml` and `configs/pendulum_numpy_sac.yaml`.

#### Plugin boundary and backend exchange
Concrete ecosystem support lives exclusively in plugins, which implement core contracts. Quantum implementations include PennyLane Torch/JAX, Qiskit, Cirq, Braket, and IBM-related packages; RL adapters span SB3, TorchRL, CleanRL, RLlib, and a dependency-free NumPy reference backend. Paths: `qrl/plugins/pennylane/torch_backend/backend.py`, `qrl/plugins/pennylane/jax_backend/backend.py`, `qrl/plugins/numpy_backend/backend.py`, `qrl/plugins/sb3/sb3_adapter.py`. Native tensor conversion occurs here, not in core/DAG. Cross-runtime edges use bridges, including DLPack Torch↔JAX and NumPy↔Torch: `qrl/plugins/bridges/torch_jax_bridge.py`, `qrl/plugins/bridges/numpy_torch_bridge.py`.

#### Training, environments, and observability
Trainer adapters attach a compiled graph/runner and an external environment, own the learning loop, and emit canonical metrics to tracker fan-out. Environment adapters isolate Gymnasium, PettingZoo, and Ray vectorization semantics. Key paths: `qrl/plugins/gymnasium/env_adapter.py`, `qrl/plugins/pettingzoo/__init__.py`, `qrl/plugins/ray/vecenv.py`, `qrl/plugins/tracking/_base.py`, `qrl/plugins/tracking/model_registry.py`. External services include Weights & Biases, TensorBoard, MLflow, Ray/RLlib, Amazon Braket, and IBM Quantum.

#### Placement, state, and multi-agent extensions
Device assignment and cross-device transfer are side orchestration concerns: `qrl/devices/device_map.py`. Stateful stages support episode-scoped recurrent state, while multi-agent orchestration manages agent reuse and topological execution separately from the single-agent DAG engine: `qrl/marl/multi_agent_pipeline.py`, `qrl/plugins/stateful_examples/rnn.py`. Built-in stage functionality includes JIT-oriented stages at `qrl/stages/jit.py`.

#### Architectural constraints
Dependency direction is inward: plugins depend on core contracts; `qrl/core` and `qrl/dag` remain pure Python and isolated from plugins, config, and third-party runtimes. Maintain the opaque-IR boundary, batch-first quantum backend behavior, and declared per-edge gradient strategy. Optional dependencies are selected through package extras in `pyproject.toml`; Python 3.11+ is required.

Main entry points: [`qrl/dag/runner.py`](https://github.com/laamiriouail/qrlx/blob/main/qrl/dag/runner.py) (Graph Runner), [`qrl/plugins/sb3/sb3_adapter.py`](https://github.com/laamiriouail/qrlx/blob/main/qrl/plugins/sb3/sb3_adapter.py) (RL Trainer Adapter), [`qrl/config/resolver.py`](https://github.com/laamiriouail/qrlx/blob/main/qrl/config/resolver.py) (Manifest Resolution), [`qrl/core/ir.py`](https://github.com/laamiriouail/qrlx/blob/main/qrl/core/ir.py) (Opaque Tensor IR)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Pure Python Core"]
  node_contracts["Core Contracts<br/>stable abstractions<br/>[contracts.py]"]
  node_ir["Opaque Tensor IR<br/>Payload and TensorSpec<br/>[ir.py]"]
  node_registry["Implementation Registry<br/>core registry<br/>[registry.py]"]
  node_trainer_base["Trainer Base<br/>trainer conventions<br/>[_trainer_base.py]"]
  node_graph["Pipeline Graph<br/>DAG model<br/>[graph.py]"]
  node_compiler["Graph Compiler<br/>DAG validator<br/>[compiler.py]"]
  node_runner["Graph Runner<br/>DAG executor<br/>[runner.py]"]
end

subgraph group_orchestration["Experiment Orchestration"]
  node_manifest_scripts["Manifest Scripts<br/>CLI scripts"]
  node_manifest["YAML Manifests<br/>experiment config"]
  node_sweeps["Sweep Expansion<br/>config service<br/>[sweeps.py]"]
  node_resolver["Manifest Resolution<br/>IoC resolver<br/>[resolver.py]"]
  node_executor["Experiment Executor<br/>runtime assembly<br/>[executor.py]"]
end

subgraph group_plugins["Plugin Integrations"]
  node_plugin_backends{{"Backend Plugins<br/>quantum and tensor runtimes<br/>[backend.py]"}}
  node_rl_adapter{{"RL Trainer Adapter<br/>SB3 integration<br/>[sb3_adapter.py]"}}
  node_environment{{"Environment Adapter<br/>Gymnasium integration<br/>[env_adapter.py]"}}
  node_bridges{{"Runtime Bridges<br/>cross-runtime conversion"}}
  node_tracking{{"Tracking Fan-out<br/>observability plugins<br/>[_base.py]"}}
end

node_examples["Examples<br/>entry points"]
node_device_map["Device Placement<br/>side orchestration<br/>[device_map.py]"]
node_marl["Multi-agent Pipeline<br/>multi-agent orchestration"]

node_examples -->|"builds"| node_graph
node_manifest_scripts -->|"loads"| node_manifest
node_manifest -.->|"optional expansion"| node_sweeps
node_manifest -->|"parses and validates"| node_resolver
node_sweeps -->|"resolved variants"| node_resolver
node_resolver -->|"selects implementations"| node_registry
node_resolver -->|"experiment objects"| node_executor
node_executor -->|"assembles"| node_graph
node_graph -->|"validates and compiles"| node_compiler
node_compiler -->|"executable graph"| node_runner
node_contracts -->|"defines payload boundary"| node_ir
node_contracts -->|"trainer contract"| node_trainer_base
node_ir -->|"payload flow"| node_runner
node_plugin_backends -->|"implements"| node_contracts
node_plugin_backends -->|"converts native tensors"| node_ir
node_bridges -->|"cross-runtime edges"| node_ir
node_rl_adapter -->|"implements"| node_trainer_base
node_rl_adapter -->|"drives learning with"| node_runner
node_environment -->|"external environment"| node_rl_adapter
node_rl_adapter -->|"canonical metrics"| node_tracking
node_device_map -.->|"placement and transfer"| node_runner
node_marl -.->|"separate orchestration"| node_runner

click node_examples "https://github.com/laamiriouail/qrlx/blob/main/examples/01_cartpole_hybrid.py"
click node_manifest_scripts "https://github.com/laamiriouail/qrlx/blob/main/scripts/execute_manifest.py"
click node_manifest "https://github.com/laamiriouail/qrlx/blob/main/configs/cartpole_sb3_ppo_quantum.yaml"
click node_sweeps "https://github.com/laamiriouail/qrlx/blob/main/qrl/config/sweeps.py"
click node_resolver "https://github.com/laamiriouail/qrlx/blob/main/qrl/config/resolver.py"
click node_executor "https://github.com/laamiriouail/qrlx/blob/main/qrl/config/executor.py"
click node_contracts "https://github.com/laamiriouail/qrlx/blob/main/qrl/core/contracts.py"
click node_ir "https://github.com/laamiriouail/qrlx/blob/main/qrl/core/ir.py"
click node_registry "https://github.com/laamiriouail/qrlx/blob/main/qrl/core/registry.py"
click node_trainer_base "https://github.com/laamiriouail/qrlx/blob/main/qrl/core/_trainer_base.py"
click node_graph "https://github.com/laamiriouail/qrlx/blob/main/qrl/dag/graph.py"
click node_compiler "https://github.com/laamiriouail/qrlx/blob/main/qrl/dag/compiler.py"
click node_runner "https://github.com/laamiriouail/qrlx/blob/main/qrl/dag/runner.py"
click node_plugin_backends "https://github.com/laamiriouail/qrlx/blob/main/qrl/plugins/pennylane/torch_backend/backend.py"
click node_rl_adapter "https://github.com/laamiriouail/qrlx/blob/main/qrl/plugins/sb3/sb3_adapter.py"
click node_environment "https://github.com/laamiriouail/qrlx/blob/main/qrl/plugins/gymnasium/env_adapter.py"
click node_bridges "https://github.com/laamiriouail/qrlx/blob/main/qrl/plugins/bridges/torch_jax_bridge.py"
click node_tracking "https://github.com/laamiriouail/qrlx/blob/main/qrl/plugins/tracking/_base.py"
click node_device_map "https://github.com/laamiriouail/qrlx/blob/main/qrl/devices/device_map.py"
click node_marl "https://github.com/laamiriouail/qrlx/blob/main/qrl/marl/multi_agent_pipeline.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_contracts,node_ir,node_registry,node_trainer_base,node_graph,node_compiler,node_runner toneBlue
class node_manifest_scripts,node_manifest,node_sweeps,node_resolver,node_executor toneAmber
class node_plugin_backends,node_rl_adapter,node_environment,node_bridges,node_tracking toneMint
class node_examples,node_device_map,node_marl toneNeutral
```

## Components

### Pure Python Core

Tensor-neutral contracts and DAG runtime with no plugin or config dependencies.

- [Core Contracts](https://github.com/laamiriouail/qrlx/blob/main/qrl/core/contracts.py): stable abstractions · Components, backends, environments, trainers, gradients, tracking, and errors. · `qrl/core/contracts.py`
- [Opaque Tensor IR](https://github.com/laamiriouail/qrlx/blob/main/qrl/core/ir.py): Payload and TensorSpec · Carries tensor values without a tensor-library dependency. · `qrl/core/ir.py`
- [Implementation Registry](https://github.com/laamiriouail/qrlx/blob/main/qrl/core/registry.py): core registry · `qrl/core/registry.py`
- [Trainer Base](https://github.com/laamiriouail/qrlx/blob/main/qrl/core/_trainer_base.py): trainer conventions · Defines common persistence and canonical metrics behavior. · `qrl/core/_trainer_base.py`
- [Pipeline Graph](https://github.com/laamiriouail/qrlx/blob/main/qrl/dag/graph.py): DAG model · `qrl/dag/graph.py`
- [Graph Compiler](https://github.com/laamiriouail/qrlx/blob/main/qrl/dag/compiler.py): DAG validator · Checks ordering and per-edge quantum gradient compatibility. · `qrl/dag/compiler.py`
- [Graph Runner](https://github.com/laamiriouail/qrlx/blob/main/qrl/dag/runner.py): DAG executor · Moves opaque payloads through compiled stages without implicit side effects. · `qrl/dag/runner.py`

### Experiment Orchestration

- [Manifest Scripts](https://github.com/laamiriouail/qrlx/blob/main/scripts/execute_manifest.py): CLI scripts · `scripts/execute_manifest.py`
- [YAML Manifests](https://github.com/laamiriouail/qrlx/blob/main/configs/cartpole_sb3_ppo_quantum.yaml): experiment config · `configs/cartpole_sb3_ppo_quantum.yaml`
- [Sweep Expansion](https://github.com/laamiriouail/qrlx/blob/main/qrl/config/sweeps.py): config service · `qrl/config/sweeps.py`
- [Manifest Resolution](https://github.com/laamiriouail/qrlx/blob/main/qrl/config/resolver.py): IoC resolver · Validates and resolves manifests using registered implementations. · `qrl/config/resolver.py`
- [Experiment Executor](https://github.com/laamiriouail/qrlx/blob/main/qrl/config/executor.py): runtime assembly · `qrl/config/executor.py`

### Plugin Integrations

Optional runtime-specific implementations and adapters.

- [Backend Plugins](https://github.com/laamiriouail/qrlx/blob/main/qrl/plugins/pennylane/torch_backend/backend.py): quantum and tensor runtimes · Implement core contracts and perform native tensor conversion. · `qrl/plugins/pennylane/torch_backend/backend.py`
- [RL Trainer Adapter](https://github.com/laamiriouail/qrlx/blob/main/qrl/plugins/sb3/sb3_adapter.py): SB3 integration · Owns the learning loop around an environment and graph runner. · `qrl/plugins/sb3/sb3_adapter.py`
- [Environment Adapter](https://github.com/laamiriouail/qrlx/blob/main/qrl/plugins/gymnasium/env_adapter.py): Gymnasium integration · `qrl/plugins/gymnasium/env_adapter.py`
- [Runtime Bridges](https://github.com/laamiriouail/qrlx/blob/main/qrl/plugins/bridges/torch_jax_bridge.py): cross-runtime conversion · Provides declared conversions such as Torch↔JAX and NumPy↔Torch. · `qrl/plugins/bridges/torch_jax_bridge.py`
- [Tracking Fan-out](https://github.com/laamiriouail/qrlx/blob/main/qrl/plugins/tracking/_base.py): observability plugins · Receives canonical trainer metrics and can register models. · `qrl/plugins/tracking/_base.py`

### Other components

- [Examples](https://github.com/laamiriouail/qrlx/blob/main/examples/01_cartpole_hybrid.py): entry points · `examples/01_cartpole_hybrid.py`
- [Device Placement](https://github.com/laamiriouail/qrlx/blob/main/qrl/devices/device_map.py): side orchestration · `qrl/devices/device_map.py`
- [Multi-agent Pipeline](https://github.com/laamiriouail/qrlx/blob/main/qrl/marl/multi_agent_pipeline.py): multi-agent orchestration · Manages agent reuse and topological execution separately from the single-agent DAG. · `qrl/marl/multi_agent_pipeline.py`

## Connections

- Examples → Pipeline Graph: builds
- Manifest Scripts → YAML Manifests: loads
- YAML Manifests → Sweep Expansion: optional expansion
- YAML Manifests → Manifest Resolution: parses and validates
- Sweep Expansion → Manifest Resolution: resolved variants
- Manifest Resolution → Implementation Registry: selects implementations
- Manifest Resolution → Experiment Executor: experiment objects
- Experiment Executor → Pipeline Graph: assembles
- Pipeline Graph → Graph Compiler: validates and compiles
- Graph Compiler → Graph Runner: executable graph
- Core Contracts → Opaque Tensor IR: defines payload boundary
- Core Contracts → Trainer Base: trainer contract
- Opaque Tensor IR → Graph Runner: payload flow
- Backend Plugins → Core Contracts: implements
- Backend Plugins → Opaque Tensor IR: converts native tensors
- Runtime Bridges → Opaque Tensor IR: cross-runtime edges
- RL Trainer Adapter → Trainer Base: implements
- RL Trainer Adapter → Graph Runner: drives learning with
- Environment Adapter → RL Trainer Adapter: external environment
- RL Trainer Adapter → Tracking Fan-out: canonical metrics
- Device Placement → Graph Runner: placement and transfer
- Multi-agent Pipeline → Graph Runner: separate orchestration
