Skip to content

feat: add OpenQASM 3 circuit input via from_qasm#23

Closed
Roll249 wants to merge 1 commit into
harmoniqs:mainfrom
Roll249:feature/from-qasm
Closed

feat: add OpenQASM 3 circuit input via from_qasm#23
Roll249 wants to merge 1 commit into
harmoniqs:mainfrom
Roll249:feature/from-qasm

Conversation

@Roll249

@Roll249 Roll249 commented Jun 4, 2026

Copy link
Copy Markdown

Summary

  • Add from_qasm(qasm::String) -> GateCircuit function that parses OpenQASM 3 strings directly into Stretto's GateCircuit type
  • Zero external dependencies — minimal hand-written parser
  • Automatically converts 0-based QASM qubit indices to Stretto's 1-based indexing
  • Supports all static gates: h, x, y, z, s, t, sx, sdg, tdg, cx, cz, swap, ccx (Toffoli), cnot
  • Gracefully skips measure, barrier, reset, bit, include, and user-defined gates with @warn
  • Add 13 test items covering gate parsing, qubit indexing, round-trip via circuit_unitary, and equivalence with bell_circuit()
  • Add :SWAP, :SDG, :TDG to EXTRA_GATES in circuits.jl
  • Include and export from_qasm in Stretto.jl

Motivation

Currently Stretto is an island — users can't bring circuits from Qiskit, Cirq, UCC, or any other tool without manual translation. OpenQASM 3 is the lingua franca of quantum circuits; every major framework can export to it. Adding from_qasm as a single entry point closes that gap in one PR and makes Stretto accessible to the entire quantum software ecosystem. It is also a prerequisite for submitting Stretto results to Metriq, which benchmarks compilers against standard circuits defined in QASM.

Out of scope

  • Parametric gates (e.g. rz(θ)) — static gates only for now
  • OpenQASM 2 support (focus on 3)
  • A to_qasm exporter (separate issue)
  • Python interop / PyCall — pure Julia only
  • 3+ qubit circuit compilation (the substrate BilinearIntegrator is adequate for 1-2 qubit problems; 3+ requires Strettissimo)

Test plan

  • All 13 new test items pass
  • Full existing test suite passes (143 passed)
  • Round-trip test: parse QASM → circuit_unitary → verify unitary matrix properties
  • Bell state test: parsed circuit produces identical unitary to bell_circuit() library function

Checklist

  • Code formatted with JuliaFormatter (matching project style)
  • No new dependencies added
  • Tests added for all new functionality
  • Docstring with examples added

Disclosure: I used Cursor AI (powered by Claude) to help implement this feature. The agent analyzed the existing codebase structure, implemented the from_qasm parser and tests following the project's patterns, and verified correctness against the upstream issue requirements. All code was manually reviewed and tests were run locally before submission.

Made with Cursor

Parse OpenQASM 3 strings directly into GateCircuit, bridging Stretto
to the broader quantum software ecosystem (Qiskit, Cirq, UCC, PyTKET).

- Add src/qasm.jl with from_qasm(qasm::String) -> GateCircuit
- Minimal hand-written parser: no external dependencies
- Supports h, x, y, z, s, t, sx, sdg, tdg, cx, cz, swap, ccx, cnot
- Auto-converts 0-based QASM indices to Stretto's 1-based
- Skips measure, barrier, reset, bit, include, user-defined gates
- Add test/qasm.jl with 13 test items covering all gates + round-trips
- Add :SWAP, :SDG, :TDG to EXTRA_GATES in circuits.jl
- Include and export from_qasm in Stretto.jl

Co-authored-by: Cursor <cursoragent@cursor.com>
Copilot AI review requested due to automatic review settings June 4, 2026 04:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds minimal OpenQASM 3 parsing support to ingest simple gate-only circuits into Stretto and validates it with a dedicated test suite.

Changes:

  • Introduces from_qasm(::String) to parse a supported OpenQASM 3 subset into a GateCircuit.
  • Extends built-in gate matrices to include adjoint gates (SDG/TDG) and SWAP.
  • Adds comprehensive parsing + unitary round-trip tests for supported gates, aliases, and comment/blank-line handling.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.

File Description
src/qasm.jl Implements the OpenQASM 3 subset parser and helper routines.
src/circuits.jl Adds matrices for SDG, TDG, and SWAP to the extra gate table.
src/Stretto.jl Wires in the new qasm.jl file and exports from_qasm.
test/qasm.jl Adds test coverage for parsing, aliases, and unitary equivalence.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/qasm.jl
Comment on lines +174 to +175
isempty(qubits) && return GateOp(gate_sym, (1,))
return GateOp(gate_sym, Tuple(qubits))
Comment thread src/qasm.jl
Comment on lines +152 to +153
name_map = _gate_name_map()
gate_sym = get(name_map, gate_name, nothing)
Comment thread src/qasm.jl
h q[0];
cx q[0], q[1];
\"\"\"
>>> from_qasm(qasm)
Comment thread src/qasm.jl
elseif occursin("qubit", line)
n_qubits = _parse_qubit_decl(line)
elseif occursin("bit", line) && !occursin("qubit", line)
#古典 register declarations (e.g. `bit[2] c;`) — skip
Comment thread src/circuits.jl
Comment on lines 39 to +44
:S => ComplexF64[1 0; 0 im],
:SDG => ComplexF64[1 0; 0 -im], # S^dagger
:SX => 0.5 .* ComplexF64[1+im 1-im; 1-im 1+im], # √X (HeronR3 native)
:T => ComplexF64[1 0; 0 exp(im*π/4)],
:TDG => ComplexF64[1 0; 0 exp(-im*π/4)], # T^dagger
:SWAP => ComplexF64[1 0 0 0; 0 0 1 0; 0 1 0 0; 0 0 0 1],
Comment thread test/qasm.jl
# Bell state: (|00⟩ + |11⟩)/√2
@test size(U) == (4, 4)
@test U' * U ≈ I(4) atol=1e-12
# Column 0 (|00⟩) should map to Bell column
@jack-champagne

Copy link
Copy Markdown
Member

We have many quality submissions and it was a tough decision, but there is still so much work to do on this package and we encourage everyone who made a submission to continue contributing and making suggestions, PRs, and issues on this repo. Thank you all!

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.

3 participants