Skip to content

feature: Add a QIR to OpenQASM translator#83

Open
keyiyin233 wants to merge 1 commit intomainfrom
feature/qir_translator
Open

feature: Add a QIR to OpenQASM translator#83
keyiyin233 wants to merge 1 commit intomainfrom
feature/qir_translator

Conversation

@keyiyin233
Copy link

Description of changes:

  • Add the initial QIR → OpenQASM 3 translator implementation.
  • New/updated modules:
    • builder.py – Builder hierarchy (FunctionBuilder, InstructionBuilder, DeclBuilder, etc.) plus SymbolTable for type/variable tracking and QIR→QASM lowering.
    • cfg_pattern.py / control_flow.py – Control-flow graph utilities and pattern-matching helpers for translating LLVM br into structured if/while OpenQASM constructs.
    • qir_profile.pyProfile and BaseProfile definitions that register the standard builder for QIR structures (Qubit, Result), classical instructions, and standard QIS functions in QIR.
    • translator.py – High-level Exporter and QASM3Builder that parse an LLVM ModuleRef, build the OpenQASM 3 AST, and emit a complete program with includes, variable declarations, gate definitions, and translated main body.
  • Supports:
    • Direct QIS gate mapping (e.g., __quantum__qis__h__bodyh).
    • Classical arithmetic ops (add, sub, mul) via BinaryExpressionBuilder.
    • Measurement, reset, defcal placeholders for opaque QIR functions.
    • Symbol-table–driven type conversion (type_qir2qasm) and temporary/IO variable allocation.
    • Support the translation of CFG with Simple single-exit loop (one back-edge, one exit) and Nested single-exit loops.

Testing done:

  • Unit tests for individual builders (gate, measurement, arithmetic) verifying correct OpenQASM 3 AST output.
  • End-to-end test: translated a sample CUDA-Q generated QIR module containing allocations, measurements, and classical branches.
  • Verified correct generation of OpenQASM 3 while/ if blocks with expected conditions and body statements.

Merge Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your pull request.

General

Tests

  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have checked that my tests are not configured for a specific region or account (if appropriate)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

* Initial QIR-Trans

* feature: Add qir2qasm_trans source code directly

* set up import and unit tests

* fix: partial linter fixes

* remove py3.9 from github checks

* WIP: local changes before pulling

* fix: add test case

* fix: add mresetz test case

* fix: example folder

* fix: restore the .converagerc

* fix: replace NamedTemporaryFile with TemporaryDirectory

* fix: linting

* fix: add some docstring

* fix: add Printer class as a argument for Exporter and remove stdgate.inc

* add comment to the code

* fix: fix test case bug

* fix: linting

* Update setup.py

* remove usage of NamedTemporaryFile

* replace default θ character with _theta

* add tests for includes

* fix: add new control flow translation

* fix the code format

* add new feature: support for the translation of  no-break while loop

* fix the code format

* fix: linting

* change includes -> include_files

* Add comments

* remove commented code

* add comments

---------

Co-authored-by: Tim <yitchen@amazon.com>
@keyiyin233 keyiyin233 requested a review from a team as a code owner September 17, 2025 23:26
@codecov
Copy link

codecov bot commented Sep 17, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a94c25b) to head (2e2ae01).

Additional details and impacted files
@@            Coverage Diff             @@
##              main       #83    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           48        54     +6     
  Lines         1997      2841   +844     
  Branches       228       337   +109     
==========================================
+ Hits          1997      2841   +844     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@yitchen-tim yitchen-tim changed the title New feature: Add a QIR to OpenQASM translator (#74) feature: Add a QIR to OpenQASM translator (#74) Sep 18, 2025
@keyiyin233 keyiyin233 changed the title feature: Add a QIR to OpenQASM translator (#74) feature: Add a QIR to OpenQASM translator Sep 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant