feat: add OpenQASM 3 circuit input via from_qasm#23
Closed
Roll249 wants to merge 1 commit into
Closed
Conversation
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>
There was a problem hiding this comment.
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 aGateCircuit. - 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 on lines
+174
to
+175
| isempty(qubits) && return GateOp(gate_sym, (1,)) | ||
| return GateOp(gate_sym, Tuple(qubits)) |
Comment on lines
+152
to
+153
| name_map = _gate_name_map() | ||
| gate_sym = get(name_map, gate_name, nothing) |
| h q[0]; | ||
| cx q[0], q[1]; | ||
| \"\"\" | ||
| >>> from_qasm(qasm) |
| 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 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], |
| # 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 |
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! |
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
from_qasm(qasm::String) -> GateCircuitfunction that parses OpenQASM 3 strings directly into Stretto'sGateCircuittypeh,x,y,z,s,t,sx,sdg,tdg,cx,cz,swap,ccx(Toffoli),cnotmeasure,barrier,reset,bit,include, and user-defined gates with@warncircuit_unitary, and equivalence withbell_circuit():SWAP,:SDG,:TDGtoEXTRA_GATESincircuits.jlfrom_qasminStretto.jlMotivation
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_qasmas 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
rz(θ)) — static gates only for nowto_qasmexporter (separate issue)BilinearIntegratoris adequate for 1-2 qubit problems; 3+ requires Strettissimo)Test plan
circuit_unitary→ verify unitary matrix propertiesbell_circuit()library functionChecklist
Disclosure: I used Cursor AI (powered by Claude) to help implement this feature. The agent analyzed the existing codebase structure, implemented the
from_qasmparser 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