Add the qdk.openqasm parser and semantic analysis Python API - #3580
Open
Ian Davis (idavis) wants to merge 5 commits into
Open
Add the qdk.openqasm parser and semantic analysis Python API#3580Ian Davis (idavis) wants to merge 5 commits into
qdk.openqasm parser and semantic analysis Python API#3580Ian Davis (idavis) wants to merge 5 commits into
Conversation
…of OpenQASM programs, including the AST binding layer, type stubs, tests, and Python API documentation
Ian Davis (idavis)
force-pushed
the
iadavis/qdk-openqasm-python
branch
from
August 13, 2026 23:23
4b137b6 to
5f950b1
Compare
Ian Davis (idavis)
marked this pull request as ready for review
August 14, 2026 18:00
Ian Davis (idavis)
requested review from
Andrew Casey (amcasey),
Bill Ticehurst (billti) and
Stefan J. Wernli (swernli)
as code owners
August 14, 2026 18:00
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Exposed the QDK's OpenQASM front end to Python. The
qdk.openqasmpackage gained two new public modules,parserfor the syntactic AST andsemanticfor the resolved, typed AST, plus a shared source-document layer, a read-only visitor, and canonical serialization. Both trees are eagerly materialized, read-only Python objects that hold no reference back into the Rust parser, so they can be retained, inspected across threads, and traversed after the call returns. The API is marked preview in its docstrings and will change soon as the OpenQASM spec is updated.Future work
To minimize the size of this PR, there is a set of work that is out of scope:
\in OpenQASM stringsChanges
Python public API
Added three modules under qdk/openqasm/ and widened the package's
__all__to re-export them along with the classes both layers share.parser.parse()returned aParseResultcarrying aProgram, an immutableSourceDocument, and structured diagnostics. Errors were collected on the result rather than raised. parser.py exported 107 names, includingparse_program,dumps/dump,QASMVisitor,QASM3ParsingError,QASMUnparseError, and the full syntactic node family.semantic.analyze()returned anAnalysisResultadding aSymbolTableand resolvedType/const_valueinformation. semantic.py exported 101 names covering the ~22-variant resolved type hierarchy,Angle,Duration,Symbol, and the semantic node family.source.pyexported the position and span machinery:Position,PositionEncoding,SourceRange,SourceDocument,SourceFile,SourceMap, andResolutionStatus. Spans are global, half-open UTF-8 byte ranges mapped throughdocument.source_map.range_from_span(span)._layers.pyadded theSyntaxNode/SemanticNodeabstract markers. Most class names appear in both layers, so a value namedProgramorIntTypeis otherwise ambiguous; the four shared base classes answerFalseto both._visitor.pyadded aQASMVisitorbase that dispatches on concrete node type and recurses through each node'schildren(), with optional context-passing detected per method.stdgates.inc,qelib1.inc, and the QDK extensionqdk.incare built in; nothing else falls back to the filesystem or network, and unresolved keys become diagnostics. A fresh resolver bridge is used per call and the result retains neither the mapping nor the callback.Native binding layer
Added the src/openqasm/ module tree to the
qdkcrate (~7,000 lines) and took a path dependency onqdk_openqasm.node_macro.rsgenerates the per-variant#[pyclass]families declaratively; syntax.rs and semantic.rs declare the node sets, and nodes.rs, span.rs, source.rs, eq.rs, repr.rs, resolver.rs, and diagnostics.rs supply the supporting projections.register_openqasm_submodule, called from_native. The pre-existing interop functions (run_qasm_program,compile_qasm_program_to_qir,compile_qasm_to_qsharp,compile_stim_to_qir, and friends) stayed on the flat_nativemodule, only their registration site moved, so no import path changed for existing callers._semanticnative submodule so their un-prefixed names do not collide with the syntax layer'sopenqasm3-parity names in the flat module. Each class advertisesqdk.openqasm.semanticas its__module__.verify_classes_are_sendable()was extended throughverify_interop_struct_traits()to cover the new classes.Telemetry
Added six events to telemetry_events.py:
on_parse_qasm/on_parse_qasm_end,on_analyze_qasm/on_analyze_qasm_end, andon_dumps_qasm/on_dumps_qasm_end. Properties are booleans and power-of-ten bucketed byte counts; no source text is emitted.Tests
repr, source/position mapping, layer markers, node and stub docstrings, visitor dispatch, projection sharing, node reachability, unparsing, and the documentation examples.run,compile, andcircuit(across all threeCircuitGenerationMethodvalues) preserve operation multiplicity forqubit[3] q; x q;.Documentation and benchmarks
qdk.openqasmagainst the reference OpenQASM 3 parser. It builds its own virtual environment from the newest wheel in target/wheels, runs each matrix cell in a fresh worker, and samples RSS from a separate coordinator so memory polling does not perturb latency.OpenQASM parser comparison
1.0.1(antlr44.13.2)Median latency
Median throughput (MiB/s)
Median throughput (statements/s)
Incremental peak resident set size (RSS)
RSS is the amount of a process's memory currently held in physical RAM, as reported by the OS.
Peak RSS as a multiple of source size