# cytnx-dev/cytnx architecture

> Architecture diagram of the GitHub repository cytnx-dev/cytnx, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cytnx-dev/cytnx
- Repository: https://github.com/cytnx-dev/cytnx

## Overview

Based on our analysis, Cytnx is a high-performance tensor network library geared toward quantum physics simulations. It is engineered as a dual–language (C++ and Python) project, with nearly identical APIs across both languages (facilitated by the pybind interface). Its design supports both CPU and GPU operations and provides a comprehensive collection of tensor operations, linear algebra routines, and higher-level abstractions (such as UniTensor, Network, and Symmetry) that are critical for tensor network algorithms.

##### Python Binding Integration
- A specific “Pybind Module” block that links the C++ core with Python. This “pybind” directory contains the wrapper code (e.g., bond_py.cpp, tensor_py.cpp, etc.) and effectively provides the bridge between Python code (as seen in the “cytnx” directory containing Python files) and the underlying C++ implementations.

##### Supporting and Auxiliary Layers
- Build & Packaging Infrastructure:
▪ Represent the build system (CMake, conda_build, GitHub workflows, and other CI/CD configuration files such as .github/workflows/ and CMakeLists.txt files).
▪ Include an “Installation/Deployment” block that shows integration with external libraries (e.g., CUTENSOR, CUQUANTUM – which are discovered using cmake modules) and packaging (conda, setup.py).
- Examples and Testing:
▪ Show an “Example Applications” block that refers to the example folders (e.g., DMRG, TDVP, iTEBD, etc.) where high-level tensor network algorithms are illustrated.
- Documentation & Developer Tools:
▪ Optionally add a section for documentation (Doxygen, docs.doxygen, doxygen_conf) and developer tools (the “developer_tools” directory).

##### Data Flow & Interaction
- That operations initiated at the high-level (such as tensor contraction or moving tensors between CPU and GPU) are serviced by backend-specific implementations.
- Separation between the “front-end logic” (API calls, Python wrappers) and the “computation engine” (tensors, storage, and the optimized linear algebra routines).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Interfaces
    subgraph "Frontend Interfaces"
        PythonAPI["Python API"]
        CppAPI["C++ API"]
    end

    %% Pybind Module for Python-C++ integration
    PybindModule["Pybind Module"]

    %% Core Library Layer with subdivisions
    subgraph "Core Library"
        subgraph "Tensor & Storage Management"
            TSCpp["C++: Tensor & Storage"]
            TSPy["Python: Tensor & Storage"]
        end
        subgraph "Backend Implementations"
            BackendCPU["CPU Backend"]
            BackendGPU["GPU Backend"]
        end
        subgraph "Linear Algebra & Tensor Network Algorithms"
            LinAlg["Linear Algebra"]
            TNAlgo["Tensor Network Algorithms"]
        end
        subgraph "Utility Functions"
            UtilCpp["C++ Utilities"]
            UtilPy["Python Utilities"]
        end
    end

    %% Supporting Infrastructure Layer
    subgraph "Supporting Infrastructure"
        BuildPkg["Build & Packaging"]
        ExampleApps["Example Applications"]
        TestValid["Testing/Validation"]
        DocDev["Documentation & Developer Tools"]
    end

    %% Data Flow Connections
    PythonAPI -->|"calls"| PybindModule
    PybindModule -->|"bridges"| CoreLibrary
    CppAPI -->|"calls"| CoreLibrary

    %% Internal Core Library interactions (illustrative)
    TSCpp --- TSPy
    LinAlg --- TNAlgo
    BackendCPU --- BackendGPU
    UtilCpp --- UtilPy

    %% Core Library to Supporting Infrastructure
    CoreLibrary -->|"integrates"| BuildPkg
    CoreLibrary -->|"integrates"| ExampleApps
    CoreLibrary -->|"integrates"| TestValid
    CoreLibrary -->|"integrates"| DocDev

    %% Additional interactions between core subcomponents (indicative)
    TSCpp -->|"manipulates"| BackendCPU
    TSCpp -->|"manipulates"| BackendGPU
    LinAlg -->|"utilizes"| BackendCPU
    LinAlg -->|"utilizes"| BackendGPU

    %% Styling Definitions
    classDef frontend fill:#FFEEAD,stroke:#333,stroke-width:2px;
    classDef binding fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef core fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef backend fill:#F9C0C0,stroke:#333,stroke-width:2px;
    classDef auxiliary fill:#FFD1DC,stroke:#333,stroke-width:2px;

    class PythonAPI,CppAPI frontend;
    class PybindModule binding;
    class TSCpp,TSPy,LinAlg,TNAlgo,UtilCpp,UtilPy core;
    class BackendCPU,BackendGPU backend;
    class BuildPkg,ExampleApps,TestValid,DocDev auxiliary;

    %% Click Events for Component Mapping
    click PythonAPI "https://github.com/cytnx-dev/cytnx/tree/master/cytnx/"
    click CppAPI "https://github.com/cytnx-dev/cytnx/tree/master/include/"
    click TSCpp "https://github.com/cytnx-dev/cytnx/blob/master/include/Tensor.hpp"
    click TSPy "https://github.com/cytnx-dev/cytnx/blob/master/cytnx/Tensor_conti.py"
    click BackendCPU "https://github.com/cytnx-dev/cytnx/tree/master/src/backend/algo_internal_cpu/"
    click BackendGPU "https://github.com/cytnx-dev/cytnx/tree/master/src/backend/algo_internal_gpu/"
    click LinAlg "https://github.com/cytnx-dev/cytnx/tree/master/src/linalg/"
    click TNAlgo "https://github.com/cytnx-dev/cytnx/tree/master/src/tn_algo/"
    click UtilCpp "https://github.com/cytnx-dev/cytnx/tree/master/include/utils/"
    click UtilPy "https://github.com/cytnx-dev/cytnx/blob/master/cytnx/utils.py"
    click PybindModule "https://github.com/cytnx-dev/cytnx/tree/master/pybind/"
    click BuildPkg "https://github.com/cytnx-dev/cytnx/blob/master/CMakeLists.txt"
    click ExampleApps "https://github.com/cytnx-dev/cytnx/tree/master/example/"
    click TestValid "https://github.com/cytnx-dev/cytnx/tree/master/tests/"
    click DocDev "https://github.com/cytnx-dev/cytnx/tree/master/developer_tools/"
```
