# apayne185/qatabasis architecture

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

- Interactive diagram: https://gitdiagram.com/apayne185/qatabasis
- Repository: https://github.com/apayne185/qatabasis

## Overview

#### Purpose and Entry Points
Qatabasis is hybrid quantum-classical VQE middleware for molecular ground-state estimation across local CPU simulation, NVIDIA GPU simulation, MPI ranks, and IBM Quantum QPUs. Main programmatic entry is `QatabasisStack`; user-facing runs begin through `Makefile`, benchmark runners, or the walkthrough.
- `src/api/interface.py`
- `benchmarks/local_test_run.py`
- `benchmarks/ibm_test_run.py`

#### Problem Construction
`ChemistryProblem` converts registered or resolved molecular input into an electronic-structure problem using PySCF, then applies Jordan–Wigner mapping to produce a Pauli Hamiltonian and selects a hardware-efficient ansatz. Molecule resolution accepts registry names, geometries, SMILES, and optionally PubChem lookup.
- `src/api/problems.py`
- `src/api/molecule_resolver.py`
- `MOLECULES.md`

#### Hardware Policy and Backend Routing
Startup hardware discovery determines CPU/GPU availability, GPU class and memory fit, precision policy, MPI rank count, output hardware slug, and preferred simulator backend. Environment variables can force backend, GPU use, precision, molecules, iterations, seed, and IBM credentials. Production routing selects GPU-capable Qiskit Aer/cuStateVec when available, otherwise CPU statevector; IBM mode selects the Qiskit Runtime path.
- `src/api/hardware.py`
- `src/api/interface.py`
- `.env.example`

#### Primary VQE Control Flow
Rank 0 owns SPSA state. For each iteration it creates two perturbed parameter vectors, evaluates their energies, estimates a gradient, updates parameters, detects convergence, and periodically checkpoints optimizer state. In simulator mode, ranks independently build the full statevector and evaluate partitions of Pauli terms; collective reduction produces total energies. This boundary is important: only observable evaluation is distributed, while per-rank statevector construction remains exponential and redundant.
- `src/api/interface.py`
- `src/api/problems.py`
- `src/api/results.py`

#### IBM QPU Boundary
For `BACKEND=ibm_cloud`, the Python path submits both SPSA perturbation evaluations together as EstimatorV2 PUBs, reducing cloud round trips. It depends on IBM Quantum Runtime credentials and backend selection. The native C++ client is a separate REST-oriented implementation, not the documented production route.
- `src/api/interface.py`
- `src/dispatcher/qpu_client.cpp`
- `.env.example`

#### Native MPI/CUDA Dispatch Path
A separately built `hpc_core` module exposes C++ execution to Python through pybind11. The dispatcher uses nonblocking MPI broadcast and all-reduce around CPU/CUDA local work; shared workload/result/Pauli contracts are defined in C++ headers. Its CUDA kernel uses a mixed-precision mean-field Pauli evaluation and is diagnostic-only, explicitly not validated for production chemical accuracy. CPU-only builds use a stub fallback.
- `src/dispatcher/bridge.cpp`
- `src/dispatcher/dispatcher.cpp`
- `src/classical/cuda/kernel.cu`
- `src/classical/cpu/kernel_stub.cpp`
- `include/stack_types.h`

#### Execution, Deployment, and Persistence
Docker provides CUDA, OpenMPI, and Python; native deployment uses Conda/CMake and Slurm scripts for HPC scheduling. Results are timestamped structured JSON organized by detected hardware and run category, then consumed by aggregation/plotting scripts. Material external dependencies are Qiskit/Qiskit Nature, PySCF, mpi4py/OpenMPI, NVIDIA CUDA/cuQuantum/Qiskit Aer, and IBM Quantum Runtime.
- `Dockerfile`
- `CMakeLists.txt`
- `scripts/install_native.sh`
- `scripts/slurm_scaling.sh`
- `benchmarks/aggregate_scaling.py`

Main entry points: [`src/api/interface.py`](https://github.com/apayne185/qatabasis/blob/main/src/api/interface.py) (SPSA optimizer), [`src/dispatcher/dispatcher.cpp`](https://github.com/apayne185/qatabasis/blob/main/src/dispatcher/dispatcher.cpp) (MPI dispatcher), [`src/api/hardware.py`](https://github.com/apayne185/qatabasis/blob/main/src/api/hardware.py) (Hardware policy), [`src/dispatcher/bridge.cpp`](https://github.com/apayne185/qatabasis/blob/main/src/dispatcher/bridge.cpp) (hpc_core bridge)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_python["Python VQE stack"]
  node_stack["QatabasisStack<br/>VQE orchestrator<br/>[interface.py]"]
  node_hardware{{"Hardware policy<br/>discovery and routing<br/>[hardware.py]"}}
  node_molecule["Molecule resolver<br/>input resolution"]
  node_problem["ChemistryProblem<br/>electronic structure<br/>[problems.py]"]
  node_spsa["SPSA optimizer<br/>rank-0 control loop<br/>[interface.py]"]
  node_sim_eval["Distributed observable evaluation<br/>simulator execution<br/>[interface.py]"]
  node_results[("Result persistence<br/>structured JSON<br/>[results.py]")]
  node_ibm{{"IBM Runtime EstimatorV2<br/>QPU execution<br/>[interface.py]"}}
end

subgraph group_native["Native MPI/CUDA path"]
  node_bridge["hpc_core bridge<br/>pybind11 module<br/>[bridge.cpp]"]
  node_dispatcher["MPI dispatcher<br/>native orchestration<br/>[dispatcher.cpp]"]
  node_contracts["Shared execution contracts<br/>C++ headers<br/>[stack_types.h]"]
  node_cuda["CUDA Pauli kernel<br/>diagnostic GPU evaluator<br/>[kernel.cu]"]
  node_cpu_stub["CPU stub kernel<br/>native fallback<br/>[kernel_stub.cpp]"]
end

subgraph group_ops["Execution and results"]
  node_entry["Run entry points<br/>Makefile and benchmarks<br/>[local_test_run.py]"]
  node_build["Native build and image<br/>CMake, Conda, Docker<br/>[CMakeLists.txt]"]
  node_slurm["Slurm scaling runs<br/>HPC scheduler<br/>[slurm_scaling.sh]"]
  node_aggregate["Scaling aggregation<br/>reporting script"]
end

node_entry -->|"starts"| node_stack
node_stack -->|"discovers policy"| node_hardware
node_stack -->|"resolves input"| node_molecule
node_molecule -->|"molecular input"| node_problem
node_problem -->|"Hamiltonian and ansatz"| node_spsa
node_hardware -->|"CPU/GPU simulator route"| node_sim_eval
node_hardware -->|"ibm_cloud route"| node_ibm
node_spsa -->|"perturbed energies"| node_sim_eval
node_spsa -->|"paired PUB evaluations"| node_ibm
node_sim_eval -->|"reduced energies"| node_spsa
node_ibm -->|"Estimator results"| node_spsa
node_spsa -->|"checkpoints and final run"| node_results
node_sim_eval -.->|"optional native execution"| node_bridge
node_bridge -->|"calls"| node_dispatcher
node_dispatcher -->|"uses contracts"| node_contracts
node_dispatcher -->|"CUDA local work"| node_cuda
node_dispatcher -->|"CPU-only fallback"| node_cpu_stub
node_build -->|"builds module"| node_bridge
node_slurm -->|"schedules runs"| node_entry
node_results -->|"consumed for plots"| node_aggregate

click node_entry "https://github.com/apayne185/qatabasis/blob/main/benchmarks/local_test_run.py"
click node_stack "https://github.com/apayne185/qatabasis/blob/main/src/api/interface.py"
click node_hardware "https://github.com/apayne185/qatabasis/blob/main/src/api/hardware.py"
click node_molecule "https://github.com/apayne185/qatabasis/blob/main/src/api/molecule_resolver.py"
click node_problem "https://github.com/apayne185/qatabasis/blob/main/src/api/problems.py"
click node_spsa "https://github.com/apayne185/qatabasis/blob/main/src/api/interface.py"
click node_sim_eval "https://github.com/apayne185/qatabasis/blob/main/src/api/interface.py"
click node_results "https://github.com/apayne185/qatabasis/blob/main/src/api/results.py"
click node_ibm "https://github.com/apayne185/qatabasis/blob/main/src/api/interface.py"
click node_bridge "https://github.com/apayne185/qatabasis/blob/main/src/dispatcher/bridge.cpp"
click node_dispatcher "https://github.com/apayne185/qatabasis/blob/main/src/dispatcher/dispatcher.cpp"
click node_contracts "https://github.com/apayne185/qatabasis/blob/main/include/stack_types.h"
click node_cuda "https://github.com/apayne185/qatabasis/blob/main/src/classical/cuda/kernel.cu"
click node_cpu_stub "https://github.com/apayne185/qatabasis/blob/main/src/classical/cpu/kernel_stub.cpp"
click node_build "https://github.com/apayne185/qatabasis/blob/main/CMakeLists.txt"
click node_slurm "https://github.com/apayne185/qatabasis/blob/main/scripts/slurm_scaling.sh"
click node_aggregate "https://github.com/apayne185/qatabasis/blob/main/benchmarks/aggregate_scaling.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_stack,node_hardware,node_molecule,node_problem,node_spsa,node_sim_eval,node_results,node_ibm toneBlue
class node_bridge,node_dispatcher,node_contracts,node_cuda,node_cpu_stub toneAmber
class node_entry,node_build,node_slurm,node_aggregate toneMint
```

## Components

### Python VQE stack

- [QatabasisStack](https://github.com/apayne185/qatabasis/blob/main/src/api/interface.py): VQE orchestrator · Owns routing, SPSA control, and checkpointing. · `src/api/interface.py`
- [Hardware policy](https://github.com/apayne185/qatabasis/blob/main/src/api/hardware.py): discovery and routing · Selects CPU, GPU, MPI, precision, and simulator policy. · `src/api/hardware.py`
- [Molecule resolver](https://github.com/apayne185/qatabasis/blob/main/src/api/molecule_resolver.py): input resolution · Resolves registry names, geometry, SMILES, and optional PubChem input. · `src/api/molecule_resolver.py`
- [ChemistryProblem](https://github.com/apayne185/qatabasis/blob/main/src/api/problems.py): electronic structure · Builds PySCF problems, Jordan–Wigner Pauli Hamiltonians, and ansatzes. · `src/api/problems.py`
- [SPSA optimizer](https://github.com/apayne185/qatabasis/blob/main/src/api/interface.py): rank-0 control loop · Creates perturbations, estimates gradients, updates parameters, and converges. · `src/api/interface.py`
- [Distributed observable evaluation](https://github.com/apayne185/qatabasis/blob/main/src/api/interface.py): simulator execution · Each rank builds a full statevector; ranks partition Pauli terms then reduce energies. · `src/api/interface.py`
- [Result persistence](https://github.com/apayne185/qatabasis/blob/main/src/api/results.py): structured JSON · Stores timestamped runs by detected hardware and category. · `src/api/results.py`
- [IBM Runtime EstimatorV2](https://github.com/apayne185/qatabasis/blob/main/src/api/interface.py): QPU execution · Batches both SPSA perturbation evaluations as PUBs. · `src/api/interface.py`

### Native MPI/CUDA path

- [hpc_core bridge](https://github.com/apayne185/qatabasis/blob/main/src/dispatcher/bridge.cpp): pybind11 module · Exposes native dispatch to Python. · `src/dispatcher/bridge.cpp`
- [MPI dispatcher](https://github.com/apayne185/qatabasis/blob/main/src/dispatcher/dispatcher.cpp): native orchestration · Uses nonblocking broadcast and all-reduce around local work. · `src/dispatcher/dispatcher.cpp`
- [Shared execution contracts](https://github.com/apayne185/qatabasis/blob/main/include/stack_types.h): C++ headers · Defines workload, result, and Pauli data contracts. · `include/stack_types.h`
- [CUDA Pauli kernel](https://github.com/apayne185/qatabasis/blob/main/src/classical/cuda/kernel.cu): diagnostic GPU evaluator · Mixed-precision mean-field evaluation; not production-accuracy validated. · `src/classical/cuda/kernel.cu`
- [CPU stub kernel](https://github.com/apayne185/qatabasis/blob/main/src/classical/cpu/kernel_stub.cpp): native fallback · Supports CPU-only native builds. · `src/classical/cpu/kernel_stub.cpp`

### Execution and results

- [Run entry points](https://github.com/apayne185/qatabasis/blob/main/benchmarks/local_test_run.py): Makefile and benchmarks · `benchmarks/local_test_run.py`
- [Native build and image](https://github.com/apayne185/qatabasis/blob/main/CMakeLists.txt): CMake, Conda, Docker · Packages CUDA, OpenMPI, Python, and the native module. · `CMakeLists.txt`
- [Slurm scaling runs](https://github.com/apayne185/qatabasis/blob/main/scripts/slurm_scaling.sh): HPC scheduler · `scripts/slurm_scaling.sh`
- [Scaling aggregation](https://github.com/apayne185/qatabasis/blob/main/benchmarks/aggregate_scaling.py): reporting script · `benchmarks/aggregate_scaling.py`

## Connections

- Run entry points → QatabasisStack: starts
- QatabasisStack → Hardware policy: discovers policy
- QatabasisStack → Molecule resolver: resolves input
- Molecule resolver → ChemistryProblem: molecular input
- ChemistryProblem → SPSA optimizer: Hamiltonian and ansatz
- Hardware policy → Distributed observable evaluation: CPU/GPU simulator route
- Hardware policy → IBM Runtime EstimatorV2: ibm_cloud route
- SPSA optimizer → Distributed observable evaluation: perturbed energies
- SPSA optimizer → IBM Runtime EstimatorV2: paired PUB evaluations
- Distributed observable evaluation → SPSA optimizer: reduced energies
- IBM Runtime EstimatorV2 → SPSA optimizer: Estimator results
- SPSA optimizer → Result persistence: checkpoints and final run
- Distributed observable evaluation → hpc_core bridge: optional native execution
- hpc_core bridge → MPI dispatcher: calls
- MPI dispatcher → Shared execution contracts: uses contracts
- MPI dispatcher → CUDA Pauli kernel: CUDA local work
- MPI dispatcher → CPU stub kernel: CPU-only fallback
- Native build and image → hpc_core bridge: builds module
- Slurm scaling runs → Run entry points: schedules runs
- Result persistence → Scaling aggregation: consumed for plots
