{"id":38615955,"url":"https://github.com/lorenzomaiuri-dev/quantum-gpt","last_synced_at":"2026-01-17T08:49:38.596Z","repository":{"id":329790378,"uuid":"1120656039","full_name":"lorenzomaiuri-dev/quantum-gpt","owner":"lorenzomaiuri-dev","description":"A hybrid Quantum-Classical Transformer implementation based on nanoGPT, using PyTorch and PennyLane to replace attention heads with Variational Quantum Circuits (VQC).","archived":false,"fork":false,"pushed_at":"2026-01-06T23:12:11.000Z","size":3122,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-01-09T03:46:19.749Z","etag":null,"topics":["gpt","language-model","nanogpt","natural-language-processing","pennylane","pytorch","qnlp","quantum","quantum-computing","quantum-machine-learning","research","transformers","variational-quantum-circuit"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lorenzomaiuri-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-12-21T17:11:24.000Z","updated_at":"2026-01-03T15:37:27.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/lorenzomaiuri-dev/quantum-gpt","commit_stats":null,"previous_names":["lorenzomaiuri-dev/quantum-gpt"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/lorenzomaiuri-dev/quantum-gpt","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorenzomaiuri-dev%2Fquantum-gpt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorenzomaiuri-dev%2Fquantum-gpt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorenzomaiuri-dev%2Fquantum-gpt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorenzomaiuri-dev%2Fquantum-gpt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lorenzomaiuri-dev","download_url":"https://codeload.github.com/lorenzomaiuri-dev/quantum-gpt/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorenzomaiuri-dev%2Fquantum-gpt/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28504539,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-17T06:57:29.758Z","status":"ssl_error","status_checked_at":"2026-01-17T06:56:03.931Z","response_time":85,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gpt","language-model","nanogpt","natural-language-processing","pennylane","pytorch","qnlp","quantum","quantum-computing","quantum-machine-learning","research","transformers","variational-quantum-circuit"],"created_at":"2026-01-17T08:49:38.533Z","updated_at":"2026-01-17T08:49:38.588Z","avatar_url":"https://github.com/lorenzomaiuri-dev.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Quantum GPT (Hybrid QNN-NanoGPT)\n\n![Python](https://img.shields.io/badge/python-3.9%2B-blue)\n![PyTorch](https://img.shields.io/badge/PyTorch-2.0%2B-red)\n![PennyLane](https://img.shields.io/badge/PennyLane-0.30%2B-yellow)\n![MIT](https://img.shields.io/badge/license-MIT-green)\n\nA hybrid Quantum-Classical implementation of a Generative Pre-trained Transformer (GPT).\nThis project adapts Andrej Karpathy's `nanoGPT` architecture by replacing classical linear layers in the Self-Attention mechanism with **Variational Quantum Circuits (VQC)** using PennyLane.\n\n## 🚀 Scientific Concept\n\nIn a standard Transformer, the Attention Head projects input tokens into Query, Key, and Value spaces using linear matrices ($W_Q, W_K, W_V$).\n\nIn this **Quantum-Hybrid architecture**, we replace these dense layers with a parameterized quantum evolution:\n\n$$\nx \\xrightarrow{\\text{Adapter}} z \\in \\mathbb{R}^n \\xrightarrow{R(\\phi)} |\\psi(z)\\rangle \\xrightarrow{U(\\theta)_{\\text{entangle}}} \\langle Z \\rangle \\to y\n$$\n\n**Where:**\n\n*   **Adapter**: A classical bottleneck layer compressing high-dimensional embeddings to $n$ qubits.\n*   $R(\\phi)$: **Angle embedding** encoding classical data into quantum states.\n*   $U(\\theta)$: A sequence of trainable entangling layers (**Strongly Entangling Layers**).\n*   $\\langle Z \\rangle$: **Expectation value measurement** returning the projected vector.\n\n### Why?\nThis architecture allows us to study if the high-dimensional **Hilbert space** and **quantum interference** can capture semantic relationships more efficiently (parameter-wise) than classical linear algebra, despite the constraints of current NISQ simulation.\n\nThis allows exploring the expressivity of quantum circuits within a sequence modeling task.\n\n    Note: We employ a Quantum Bottleneck architecture. High-dimensional classical embeddings are projected down to a lower-dimensional quantum latent space via a trainable adapter, processed by the VQC, and projected back. This maintains computational feasibility while exploiting quantum interference.\n\n\n## 📂 Project Structure\n\n```text\nquantum-transformer/\n├── checkpoints/                # Saved models\n├── data/                       # Input text data\n├── src/                        # Source code\n│   ├── config.py               # Hyperparameters \u0026 flags\n│   ├── dataset.py              # Tokenizer \u0026 Dataloader\n│   ├── model.py                # Transformer Architecture\n│   └── quantum_layers.py       # PennyLane Circuits \u0026 Hybrid Layers\n├── main.py                     # Entry point (Train/Generate)\n└── requirements.txt            # Dependencies\n```\n\n## 🛠️ Installation\nClone the repository:\n```bash\ngit clone https://github.com/lorenzomaiuri-dev/quantum-gpt.git\ncd quantum-transformer\n```\nInstall dependencies:\n```bash\npip install -r requirements.txt\n```\n\n## ⚡ Usage\n### Training\n\nTo train the model on the Shakespeare dataset (included in data/):\n```bash\npython main.py --mode train\n```\nNote: Quantum simulation is CPU-intensive. The default configuration uses a \"Quantum Bottleneck\" (4-8 qubits) to keep training times feasible on consumer hardware.\n\n### Generation\n\nTo generate text using the trained checkpoint:\n```bash\npython main.py --mode generate\n```\n\n## ⚙️ Configuration\n\nYou can modify hyperparameters in src/config.py:\n```Python\n# Quantum Settings\nUSE_QUANTUM = True      # Set False to use standard Linear Layers\nN_QUBITS = 4            # Number of qubits per head\nN_QLAYERS = 2           # Depth of the quantum circuit\n```\n\n## 🧠 Architecture Details\n\nEmbedding Dimension: 8 (scaled down for simulation speed)\\\nHeads: 2\\\nQubits per Head: 4\n\n## 📊 Preliminary Results (Coming Soon)\nComparison between Classical (64 params) vs Hybrid Quantum (4 qubits) attention heads:\n- [ ] Loss Convergence: Comparing training stability.\n- [ ] Parameter Efficiency: Can quantum circuits learn with fewer parameters?\n- [ ] Runtime Analysis: Quantifying the overhead of quantum simulation.\n\n## 🙏 Acknowledgements\nAndrej Karpathy for the original nanoGPT and Video Lecture.\\\nXanadu for the PennyLane library used for quantum machine learning.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Florenzomaiuri-dev%2Fquantum-gpt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Florenzomaiuri-dev%2Fquantum-gpt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Florenzomaiuri-dev%2Fquantum-gpt/lists"}