Enhancing LLM-based Quantum Code Generation with Multi-Agent Optimization and Quantum Error Correction

Charlie Campbell, Hao Mark Chen, Wayne Luk, Hongxiang Fan · DOI

Abstract

As Large Language Models (LLMs) continue to revolutionize classical software engineering, their application to quantum computing remains a significant frontier. Quantum programming, particularly using frameworks like IBM’s Qiskit, presents unique hurdles: a scarcity of high-quality training data, rapidly evolving library syntax, and the inherent noise of near-term quantum hardware. This paper, authored by researchers at Imperial College London, introduces a multi-agent framework designed to bridge the gap between LLM capabilities and the rigorous requirements of quantum circuit design. By integrating a fine-tuned code generator with a semantic analyzer and a Quantum Error Correction (QEC) agent, the researchers demonstrate that Structured Chain-of-Thought (SCoT) reasoning and multi-pass inference significantly outperform standard retrieval-augmented generation, achieving up to a 50% improvement in code accuracy.

Introduction

While LLMs are increasingly adept at writing Python or C++, quantum programming remains a "frontier" domain. The quantum software ecosystem moves so fast that models trained just a few months ago are often out of date. Furthermore, it is easy for an LLM to write code that looks like Qiskit but is logically nonsensical, violating the laws of quantum mechanics. Finally, unlike classical bits, qubits are prone to "depolarizing noise," meaning even perfect code may fail on real hardware without error correction.

The Multi-Agent Framework

The researchers propose a specialized approach that treats quantum code generation as an iterative, collaborative process involving three distinct roles:

  • Code Generation Agent: Based on a fine-tuned StarCoder model, this agent produces the initial draft of the Qiskit code.
  • Semantic Analysis Agent: This agent uses multi-pass inference (inspired by OpenAI’s GPT-o1). Rather than generating code once, it iteratively reviews the output, identifies semantic errors, and attempts to fix them in subsequent passes.
  • QEC Decoder Agent: This novel addition analyzes the target quantum machine's topology and generates a surface code decoder. This "wraps" the generated program in a layer of error correction to mitigate physical hardware noise.

Experimental Methodology

To ensure the model learned the most recent syntax, the authors scraped GitHub for Qiskit-related repositories updated after February 2024. This resulted in a 3M token dataset, upsampled to 9M for training. Evaluation was conducted using a custom test suite categorized into Basic (syntactic requirements), Intermediate (algorithms like Shor’s and Grover’s), and Advanced (quantum teleportation and walks) tasks. The study also compared different prompt engineering techniques, specifically Chain-of-Thought (CoT) and Structured Chain-of-Thought (SCoT), against Retrieval-Augmented Generation (RAG).

Key Results and Insights

Reasoning vs. Retrieval

In a surprising finding, Retrieval-Augmented Generation (RAG) provided only a marginal 4% improvement. The authors suggest that because quantum libraries change so rapidly, simply "retrieving" documentation is insufficient. Instead, Structured Chain-of-Thought (SCoT)—which forces the model to reason through the algorithm's logic—boosted accuracy by 40-50%.

The Power of Multi-Pass Inference

Multi-pass inference proved essential for reducing semantic errors. Allowing the model to "debug" its own logic in a second or third pass significantly reduced hallucinations regarding library imports and deprecated functions, improving accuracy to 34% in the triple-pass configuration.

Hardware Noise Mitigation

The researchers demonstrated that an AI can successfully predict and correct quantum errors by generating a decoder tailored to the specific "syndromes" (error patterns) of a device. In simulations of the IBM Brisbane backend, the QEC agent successfully increased the probability of the expected result while decreasing the probability of error.

Limitations and Future Directions

Despite these gains, several challenges remain:

  • Dataset Size: 9M tokens is relatively small for a specialized domain; future work may require synthetic data or federated learning.
  • Topology Dependence: The current QEC agent is specific to the hardware layout it was trained on. A major goal is creating a topology-agnostic QEC decoder.
  • Computational Cost: Multi-pass inference and multi-agent coordination increase inference-time compute costs, which may eventually require hardware acceleration like FPGAs.

Conclusion

This research presents a path toward generating fault-tolerant quantum programs using LLMs. By combining fine-tuning on contemporary data with structured reasoning and post-generation error correction, the multi-agent framework significantly enhances the reliability of quantum code generation, moving closer to practical utility in the NISQ (Noisy Intermediate-Scale Quantum) era.

Summary updated

BibTeX
@inproceedings{campbell2025enhancing,
  author = {Charlie Campbell and Hao Mark Chen and Wayne Luk and Hongxiang Fan},
  title = {{Enhancing LLM-based Quantum Code Generation with Multi-Agent Optimization and Quantum Error Correction}},
  booktitle = {DAC},
  year = {2025},
  doi = {10.1109/dac63849.2025.11133316},
}
Figure