How laamiriouail/qrlx works
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: runner.py Graph Runner · sb3_adapter.py RL Trainer Adapter · resolver.py Manifest Resolution · ir.py Opaque Tensor IR
Components 20
Pure Python Core
Tensor-neutral contracts and DAG runtime with no plugin or config dependencies.
- Core Contracts · Components, backends, environments, trainers, gradients, tracking, and errors.
qrl/core/contracts.py - Opaque Tensor IR · Carries tensor values without a tensor-library dependency.
qrl/core/ir.py - Implementation Registry
qrl/core/registry.py - Trainer Base · Defines common persistence and canonical metrics behavior.
qrl/core/_trainer_base.py - Pipeline Graph
qrl/dag/graph.py - Graph Compiler · Checks ordering and per-edge quantum gradient compatibility.
qrl/dag/compiler.py - Graph Runner · Moves opaque payloads through compiled stages without implicit side effects.
qrl/dag/runner.py
Experiment Orchestration
- Manifest Scripts
scripts/execute_manifest.py - YAML Manifests
configs/cartpole_sb3_ppo_quantum.yaml - Sweep Expansion
qrl/config/sweeps.py - Manifest Resolution · Validates and resolves manifests using registered implementations.
qrl/config/resolver.py - Experiment Executor
qrl/config/executor.py
Plugin Integrations
Optional runtime-specific implementations and adapters.
- Backend Plugins · Implement core contracts and perform native tensor conversion.
qrl/plugins/pennylane/torch_backend/backend.py - RL Trainer Adapter · Owns the learning loop around an environment and graph runner.
qrl/plugins/sb3/sb3_adapter.py - Environment Adapter
qrl/plugins/gymnasium/env_adapter.py - Runtime Bridges · Provides declared conversions such as Torch↔JAX and NumPy↔Torch.
qrl/plugins/bridges/torch_jax_bridge.py - Tracking Fan-out · Receives canonical trainer metrics and can register models.
qrl/plugins/tracking/_base.py
Other components
- Examples
examples/01_cartpole_hybrid.py - Device Placement
qrl/devices/device_map.py - Multi-agent Pipeline · Manages agent reuse and topological execution separately from the single-agent DAG.
qrl/marl/multi_agent_pipeline.py
Connections 22
- 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