Skip to content

[Python] Add native OpenQASM 2.0/3.0 parser and expand Qiskit interoperability#4613

Open
QuantumRaul wants to merge 4 commits into
NVIDIA:mainfrom
QuantumRaul:fix/openqasm-qiskit
Open

[Python] Add native OpenQASM 2.0/3.0 parser and expand Qiskit interoperability#4613
QuantumRaul wants to merge 4 commits into
NVIDIA:mainfrom
QuantumRaul:fix/openqasm-qiskit

Conversation

@QuantumRaul
Copy link
Copy Markdown
Contributor

Summary

Expands cudaq.contrib interoperability with two related improvements:

  1. Native OpenQASM 2.0 / 3.0 translator (from_qasm_str, from_qasm) — pure-Python parser that converts OpenQASM source directly into a CUDA-Q kernel, no Qiskit dependency. Dispatches on the OPENQASM <version>; header; 2.x is handled by _QASM2Translator, 3.x by the _QASM3Translator subclass.
  2. Expanded Qiskit gate coverage in from_qiskit — a complete dispatch table for the qelib1.inc / stdgates.inc gate sets, with a recursive fallback through Instruction.definition for custom and composite gates.

Both helpers share a common gate handler table (_GATE_HANDLERS in qiskit_convert.py) so the two translators stay in lockstep on gate semantics.

Motivation

The new native translator removes the dependency for qiskit over OpenQASM translation needs, adds first-class 3.0 support, and keeps from_qiskit robust enough to round-trip circuits built with the full Qiskit standard library.

Changes

python/cudaq/contrib/qasm_convert.py

  • New _QASM2Translator covering the full qelib1.inc gate set (Paulis, 1-qubit Clifford, universal u1/u2/u3/u/p/u0, rotations, extended single-qubit sx/sxdg/r, controlled one- and two-qubit gates, swap family, two-qubit parametrics rxx/ryy/rzz/rzx, and multi-qubit ccx/ccz/rccx/c3x/c4x/mcx/mcp), plus measure, reset, barrier, opaque, and user-defined gate blocks with recursive expansion.
  • _QASM3Translator (subclass) adds the 3.0 surface: qubit[N] / bit[N] declarations, c = measure q; assignment, built-in U(θ,φ,λ) and 4-parameter U(θ,φ,λ,γ), gphase(γ), legacy uppercase CX and I, and include "stdgates.inc";. Gate modifiers (ctrl @, negctrl @, inv @, pow(n) @) and classical control / typed-variable declarations will be lately implemented, at the moment raise NotImplementedError with a clear message
  • Safe expression evaluator _eval_expr built on ast.parse with an allowlist (constants pi/e/tau; standard math functions; + - * / ** ^ and unary + -), used for gate-parameter expressions and nested custom-gate environments.
  • QASM 2.0 register broadcasting (h q; applies element-wise, mismatched sizes rejected).

python/cudaq/contrib/qiskit_convert.py

  • Replaces the previous ad-hoc if/elif dispatch with a single _GATE_HANDLERS dict keyed by operation.name, covering the full Qiskit standard library.
  • Adds faithful decompositions for the two-qubit parametric family (RXX, RYY, RZZ, RZX, ECR, iSWAP, XXPlusYY, XXMinusYY) and multi-qubit gates (CCZ, RCCX/Margolus, MCX variants, MCP/MCPhase, CSX, CU).
  • _apply_instruction gracefully handles unbound ParameterExpressions (returns False so the caller raises a clear ValueError) and recursively expands any gate not in the dispatch table through Instruction.definition.

python/cudaq/contrib/__init__.py

  • Exports the new from_qasm / from_qasm_str entry points alongside from_qiskit.

Tests

  • python/tests/contrib/test_from_qasm.py — unit coverage for the expression evaluator, both QASM 2.0 and 3.0 gate surfaces, register broadcasting, and user-defined gates.
  • python/tests/contrib/test_from_qiskit.py — per-gate tests for the expanded dispatch table.
  • python/tests/contrib/test_roundtrip.py (new) — end-to-end round-trip tests across the three conversion paths (direct QASM, QASM→Qiskit→CUDA-Q, Qiskit→QASM→CUDA-Q), including a consistency cross-check that direct and indirect paths produce the same measurement distributions.

Signed-off-by: Raul Martinez <raul.marpa01@gmail.com>
@copy-pr-bot
Copy link
Copy Markdown

copy-pr-bot Bot commented May 27, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

Signed-off-by: QuantumRaul <raul.marpa01@gmail.com>
…perability in cudaq.contrib

Signed-off-by: QuantumRaul <raul.marpa01@gmail.com>
…aul Martinez <raul.marpa01@gmail.com>, hereby add my Signed-off-by to this commit: b3c6dd13cdad451494b71cc55acd27b81dbb9157Signed-off-by: Raul Martinez <raul.marpa01@gmail.com>

Signed-off-by: QuantumRaul <raul.marpa01@gmail.com>
@QuantumRaul QuantumRaul force-pushed the fix/openqasm-qiskit branch from 0f1a492 to f5ef498 Compare May 27, 2026 08:05
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