How miles0428/qrl works
Purpose and execution
QRL is a configuration-driven reinforcement-learning experiment framework combining classical PyTorch networks and variational quantum circuits (VQCs). Its executable entry point is scripts/run_experiment.py; YAML experiments under configs/ select environment, encoder/model, trainer, and hyperparameters. Representative presets include configs/dino_cnn_dqn.yaml, configs/qdqn_quantum.yaml, and configs/mlp_a2c.yaml.
Configuration and assembly boundary
Configuration is parsed, schema-defined, and validated before runtime objects are built in qrl/config.py, qrl/config_schema.py, and qrl/config_validator.py. Factories separate declarative experiment selection from concrete implementations: environment construction is in qrl/env_wrapper/factory.py and trainer selection is in qrl/trainers/factory.py.
Environment layer
The framework trains against custom game environments and Gymnasium-compatible tasks. Dino Run implementation and environment adaptation reside in games/dino/dino_game.py and games/dino/env.py. QuantumSpinCartPole is a physics/simulation environment organized around games/spin_cartpole/env.py, with Hamiltonian and processor logic in games/spin_cartpole/hamiltonian.py and games/spin_cartpole/processor.py.
Observation-to-model pipeline
Environment observations cross an encoding/model boundary. Classical models are implemented by qrl/models/mlp.py and qrl/models/cnn.py; shared model abstractions are in qrl/models/base.py. Quantum and hybrid paths use qrl/models/vqc.py and qrl/models/vqc_policy.py / qrl/models/vqc_value.py, allowing outputs to serve either value/Q estimation or action-policy roles.
Quantum execution backend
Quantum models delegate circuit evaluation to backend implementations under qrl/backends/. qrl/backends/torch_statevector.py provides the Torch statevector path, while qrl/backends/vqc.py and qrl/backends/common.py define VQC/backend behavior. The material external quantum stack is Qiskit plus QuTiP/qutip-qip; this simulation is CPU-oriented even when classical CNN training uses CUDA-capable PyTorch.
RL training control flow
Trainers repeatedly reset/step an environment, infer Q-values or policy/value outputs, select actions, accumulate transitions/rollouts, and optimize model parameters. The algorithm boundary is explicit: qrl/trainers/dqn_trainer.py handles DQN (including Double-DQN support), qrl/trainers/a2c_trainer.py handles actor-critic updates, and qrl/trainers/pg_trainer.py implements REINFORCE-style policy gradient. qrl/trainers/trainer_base.py centralizes shared trainer lifecycle behavior; DQN replay storage is isolated in qrl/trainers/replay.py.
Outputs and runtime dependencies
Experiments write training results to a runtime-created result/ directory. Primary runtime dependencies are Python, PyTorch, Gymnasium compatibility, YAML configuration, and the Qiskit/QuTiP quantum simulation stack, declared through requirements.txt.
Main entry points: factory.py Environment factory · base.py Model abstractions · dqn_trainer.py DQN trainer · trainer_base.py Trainer lifecycle
Components 23
Configuration
- YAML experiment presets
- Config parsing
qrl/config.py - Config schema
qrl/config_schema.py - Config validation
qrl/config_validator.py
Environments
- Environment factory
qrl/env_wrapper/factory.py - Dino Run environment
games/dino/env.py - Dino game simulation
games/dino/dino_game.py - QuantumSpinCartPole
games/spin_cartpole/env.py - Spin Hamiltonian
games/spin_cartpole/hamiltonian.py - Spin processor
games/spin_cartpole/processor.py
Models & Quantum Backend
- Model abstractions
qrl/models/base.py - MLP and CNN models
qrl/models/mlp.py - VQC policy/value models
qrl/models/vqc.py - VQC backend interface
qrl/backends/vqc.py - Torch statevector backend · CPU-oriented circuit evaluation path.
qrl/backends/torch_statevector.py
Training
- Trainer factory
qrl/trainers/factory.py - Trainer lifecycle
qrl/trainers/trainer_base.py - DQN trainer · Supports Double-DQN.
qrl/trainers/dqn_trainer.py - A2C trainer
qrl/trainers/a2c_trainer.py - Policy-gradient trainer
qrl/trainers/pg_trainer.py - Replay storage
qrl/trainers/replay.py
Other components
- Experiment runner
scripts/run_experiment.py - Experiment results
Connections 30
- Experiment runner → YAML experiment presets: loads
- YAML experiment presets → Config parsing: parsed by
- Config parsing → Config schema: uses
- Config parsing → Config validation: validates with
- Config validation → Environment factory: selects environment
- Config validation → Trainer factory: selects trainer
- Config validation → Model abstractions: selects model
- Environment factory → Dino Run environment: constructs
- Environment factory → QuantumSpinCartPole: constructs
- Dino Run environment → Dino game simulation: adapts
- QuantumSpinCartPole → Spin Hamiltonian: uses
- QuantumSpinCartPole → Spin processor: uses
- Model abstractions → MLP and CNN models: implemented by
- Model abstractions → VQC policy/value models: implemented by
- VQC policy/value models → VQC backend interface: evaluates circuits through
- VQC backend interface → Torch statevector backend: implemented by
- Trainer factory → DQN trainer: constructs
- Trainer factory → A2C trainer: constructs
- Trainer factory → Policy-gradient trainer: constructs
- Trainer lifecycle → DQN trainer: shared lifecycle
- Trainer lifecycle → A2C trainer: shared lifecycle
- Trainer lifecycle → Policy-gradient trainer: shared lifecycle
- DQN trainer → Replay storage: stores transitions
- DQN trainer → Environment factory: reset and step
- A2C trainer → Environment factory: collects rollouts
- Policy-gradient trainer → Environment factory: collects episodes
- DQN trainer → Model abstractions: optimizes Q model
- A2C trainer → Model abstractions: optimizes policy/value
- Policy-gradient trainer → Model abstractions: optimizes policy
- Trainer lifecycle → Experiment results: writes metrics