Export parameterized circuits to OpenQASM 3#161
Closed
alyabouzaid wants to merge 1 commit into
Closed
Conversation
to_qasm3() emitted gate arguments that referenced parameter symbols without ever declaring them, so the OpenQASM 3 for any parameterized circuit was invalid. Collect the free parameter symbols and declare them as `input float[64]` before the qubit declaration. The symbol names are gathered by a new QuantumCircuit::parameter_symbols(), which scans the instruction parameters since the Qiskit C API exposes only the count of symbols (qk_circuit_num_param_symbols), not the symbols themselves. The method is the single replacement point for when per-symbol accessors are added to the C API. Gate arguments that are compound expressions are rejected rather than emitted as invalid identifiers, and the recovered names are cross-checked against the tracked count. Closes Qiskit#146 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Aly Abouzaid seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
Member
|
Closing in favor of #159 Thanks @alyabouzaid for the contribution anyway! If you would like to work on another issue, take a look at https://qisk.it/good-first-issues for a list of newcomer-friendly issues. |
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.
Closes #146.
to_qasm3()already prints parameter symbols inside gate arguments, but it never declared them, so the OpenQASM 3 for any parameterized circuit was invalid: it referenced symbols that don't exist. For example a circuit withrx(theta)andry(phi)produced output that started straight atqubit[2] q;and then usedtheta/phiwith no declaration.This follows the approach laid out in the issue discussion: add a small
QuantumCircuit::parameter_symbols()stab and feed its result to the exporter, so the C++ side does not keep a second copy of the parameter list that lives on the Rust side.Changes
QuantumCircuit::parameter_symbols()returns the free parameter symbol names used by the circuit, in first-encountered order. The C API currently exposes only the count of symbols (qk_circuit_num_param_symbols), not the symbols themselves, so the names are recovered by scanning the instruction parameters. This is the single place to swap in a C API call once per-symbol accessors exist.to_qasm3()now emits oneinput float[64] <name>;per symbol, after the gate definitions and before the register declarations, so every referenced symbol is declared.Limitations (from the C API, not this change)
2*thetacan't be reduced to a single input name, so it raisesstd::runtime_errorinstead of emitting an invalid identifier.Parameterobjects that share a name are merged into one symbol, because the C API tracks symbols by name and gives no UUID to tell them apart. The recovered names are cross-checked againstqk_circuit_num_param_symbolsas a guard.Both limitations are noted in the method docs and a release note.
Tests
Added
test_parameter_symbols,test_to_qasm3_parameterized, andtest_to_qasm3_shared_parametertotest_circuit.cpp. Full suite passes locally (Qiskit 2.4.1 C extension, macOS):🤖 Generated with Claude Code