A research compiler framework that translates a subset of Standard C into executable Quantum Circuits (QASM/Qiskit) using Draper's QFT-based arithmetic algorithms.
C2Q is a complete compilation infrastructure designed to bridge classical programming and quantum computation. The compiler transforms C source code through a multi-stage pipeline, ultimately producing optimized quantum circuits that can be simulated or executed on quantum hardware.
For a comprehensive understanding of the project's scope, results, and the academic contributions it enabled, please refer to the detailed project paper, see paper.pdf.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β C2Q COMPILATION PIPELINE β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β C Source βββββββββββββ βββββββββββββ βββββββββββββββββββββββββ β
β Code ββββΊ β Frontend βββββΊβ IR Gen βββββΊβ Quantum MLIR Dialect β β
β (.c) β (Lexer/ β β (Draper β β (xDSL Framework) β β
β β Parser) β β QFT). β β β β
β βββββββββββββ βββββββββββββ βββββββββββββ¬ββββββββββββ β
β β β
β βΌ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β OPTIMIZATION PIPELINE β β
β β β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β Phase Precision Filtering (Primary) β β β
β β β β’ Eliminates negligible phase rotations < threshold β β β
β β β β’ Iterative convergence (typically 2 iterations) β β β
β β β β’ Average: 20-56% phase gate reduction β β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β β
β β Supporting Passes (Circuit-Dependent): β β
β β β’ Dead Code Elimination β’ CCNOT Decomposition β β
β β β’ Adjacent Phase Consolidation β’ QFT Depth Analysis β β
β β β’ Hadamard Cancellation β’ Redundant SWAP Elimination β β
β β β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββββββββ ββββββββββββββββββββββββββββββββββ β
β Circuit βββββ Qiskit Backend ββββββ SSA-Aware Circuit Generation β β
β β (Simulation/ β β (Handles Aggressive Renaming) β β
β β Visualization) β β β β
β βββββββββββββββββββββ ββββββββββββββββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
The compiler leverages the xDSL framework for MLIR-based intermediate representation, enabling powerful pattern-based optimizations and a clean separation of concerns between frontend parsing, IR generation, optimization passes, and backend circuit synthesis.
New here? If you want a zero-configuration setup, start with Docker in the next section and skip native dependency installation.
This repository includes a zero-configuration Docker environment based on python:3.12.
The container image installs all runtime dependencies used by the compiler and benchmark pipeline:
xdsl, qiskit, qiskit-aer, matplotlib, and typing_extensions.
# Clone repository
git clone https://github.com/pitesse/C2Q.git
cd C2Qdocker compose buildCompile a test input with the default command (--rm ensures the container is removed after computation):
docker compose run --rm c2qRun custom compiler commands (copy-pasteable examples):
# Emit IR and print it
docker compose run --rm c2q python -m C2Q tests/inputs/test_add.c --emit ir --ir
# Compile with optimization disabled
docker compose run --rm c2q python -m C2Q tests/inputs/test_add.c --no-optimize
# Validate expected output
docker compose run --rm c2q python -m C2Q tests/inputs/test_add.c --force-optimize --validate 8docker compose run --rm benchmarkBenchmark artifacts are written to benchmarks_data/ on the host (for example benchmarks_data/results.csv and the generated *_base.mlir / *_opt.mlir files).
- LaTeX sources under
paper/are excluded from the Docker build context. - The compose setup mounts only
C2Q/,tests/, andbenchmarks_data/into containers for a clean runtime environment.
Python Version Requirement: Python 3.11 or 3.12 is required. Python 3.13+ is not yet supported by qiskit-aer.
# Clone repository
git clone https://github.com/pitesse/C2Q.git
cd C2QUse this path only if you want to run the project directly on your machine.
# Create virtual environment with Python 3.12
python3.12 -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
pip install xdsl qiskit qiskit-aer matplotlibThe compiler is invoked as a Python module with various options:
Note: All commands below assume the virtual environment is activated. Use
python(notpython3.12) when running inside the venv.
# Basic compilation: generates AST, MLIR, and circuit visualization
python -m C2Q tests/inputs/test_add.c
# Compile with optimizations and validate result
python -m C2Q tests/inputs/test_add.c --force-optimize --validate 8
# Print intermediate representation to console
python -m C2Q tests/inputs/test_add.c --ir
# Generate AST only (no circuit generation)
python -m C2Q tests/inputs/test_add.c --emit ast
# Disable optimizations for debugging
python -m C2Q tests/inputs/test_add.c --no-optimize| Option | Description |
|---|---|
--emit {ast,ir} |
Output format (default: ir) |
--ir |
Print generated MLIR to console |
--force-optimize |
Apply optimizations during validation |
--no-optimize |
Disable optimization passes |
--validate N |
Validate circuit output equals N |
--signed |
Interpret result as signed two's complement |
--result-width N |
Result register width in bits (default: 8, use 16 for multiplication) |
--precision F |
Phase precision threshold (default: 1e-4) |
# Run full benchmark suite (8 test cases)
python -m C2Q.benchmark.run_benchmarks
# Results are saved to benchmarks_data/results.csv
# Individual MLIR files saved to benchmarks_data/*.mlir
- Adjacent CNOT cancellation debug:
C2Q_DEBUG_CNOT_CANCEL=1(legacy:C2Q_DEBUG_INPLACING=1)
# 1. Compile and view the circuit
python -m C2Q tests/inputs/test_mult_2x3.c --ir
# 2. Validate correctness (2 Γ 3 = 6)
python -m C2Q tests/inputs/test_mult_2x3.c --force-optimize --validate 6 --result-width 16
# 3. Compare optimized vs unoptimized
python -m C2Q tests/inputs/test_mult_2x3.c --no-optimize # Baseline
python -m C2Q tests/inputs/test_mult_2x3.c # OptimizedC_to_Quantum/
βββ C2Q/ # Main compiler package
β βββ __main__.py # Entry point and CLI
β β
β βββ frontend/ # Parsing and IR generation
β β βββ lexer.py # Tokenization of C source
β β βββ parser.py # Recursive-descent parser
β β βββ c_ast.py # AST node definitions
β β βββ ir_gen.py # C AST β Quantum MLIR
β β βββ quantum_arithmetic.py # Draper QFT algorithms
β β βββ location.py # Source location tracking
β β
β βββ dialects/ # MLIR dialect definitions
β β βββ quantum_dialect.py # Quantum operations (xDSL/IRDL)
β β
β βββ middle_end/ # Optimization passes
β β βββ optimizations/
β β βββ integrated_optimizer.py # Main optimization pipeline
β β βββ draper_optimizer.py # QFT-specific optimizations
β β βββ remove_unused_op.py # Dead code elimination
β β βββ ccnot_decomposition.py # Toffoli decomposition
β β βββ cnot_cancellation.py # Adjacent CNOT cancellation (quantum-safe)
β β
β βββ backend/ # Circuit generation
β β βββ run_qasm.py # MLIR β Qiskit conversion
β β βββ validate.py # MPS simulation framework
β β
β βββ benchmark/ # Benchmarking infrastructure
β βββ run_benchmarks.py # Automated test suite
β
βββ tests/ # Test suite
β βββ inputs/ # C test programs
β β βββ test_add.c # Addition: 3 + 5 = 8
β β βββ test_sub.c # Subtraction: 8 - 3 = 5
β β βββ test_mult_2x3.c # Multiplication: 2 Γ 3 = 6
β β βββ ... # Additional test cases
β βββ outputs/ # Generated AST/MLIR/circuits
β
βββ benchmarks_data/ # Benchmark results and artifacts
β βββ results.csv # Metrics comparison table
β βββ *.mlir # Base and optimized IR files
β
βββ demos/ # Analysis and demo scripts
βββ paper/ # Academic paper (LaTeX)
βββ README.md # This file
| Module | Purpose |
|---|---|
frontend/ |
Lexical analysis, parsing, and AST construction. Transforms C source into an abstract syntax tree with semantic validation. |
frontend/ir_gen.py |
Core IR generation. Maps C operations to quantum gate sequences using Draper QFT arithmetic. Manages quantum register allocation. |
dialects/ |
Defines the quantum MLIR dialect using xDSL's IRDL framework. Includes all quantum operations (gates, measurements, control flow). |
middle_end/ |
Optimization passes operating on quantum MLIR. Pattern-based rewrites using xDSL's RewritePattern infrastructure. |
backend/ |
Converts optimized MLIR to Qiskit circuits. Handles SSA value tracking, gate mapping, and circuit metrics calculation. |
- Variable declarations:
int a = 5; - Assignment statements:
a = b + c; - Arithmetic operations:
+,-,*(addition, subtraction, multiplication) - Function definitions:
int main() { ... } - Return statements:
return expression; - 8-bit integer representation (configurable width)
// tests/inputs/test_add.c
int main() {
int a = 3;
int b = 5;
int c = a + b;
return c;
}The compiler produces:
- AST: Abstract syntax tree representation
- Quantum MLIR: Intermediate representation with quantum operations
- Qiskit Circuit: Executable quantum circuit with visualization
- Metrics: Gate count, depth, qubit usage statistics
If you use C2Q in academic research, please cite:
@article{pizzoccheri2025c2q,
title = {C2Q: A Compiler Framework for Translating C Programs to Quantum Circuits},
author = {Pizzoccheri, Pietro},
journal = {Politecnico di Milano},
year = {2026}
}MIT License
Copyright (c) 2025 Pietro Pizzoccheri
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.