Qiskit C++ Issue #146: Parameter-Aware OpenQASM 3 Export#155
Open
GoiBasia wants to merge 5 commits into
Open
Conversation
|
|
Open
2 tasks
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
The final implementation follows the full long-term plan of C API symbol-enumeration: instead of relying on fragile wrapper-side parsing to recover parameter names, it adds a C API symbol-enumeration hook in Qiskit and uses that from qiskit-cpp. Please take into consideration that pure qiskit-cpp-only fix without Qiskit changes is possible, but only by using C++ wrapper-side parameter tracking, and it is less robust for circuits reconstructed from C API state.
On the qiskit-cpp side,
src/qasm3/qasm3_exporter.hppnow owns the exporter. The defaultqasm3::dumps(circuit)path asks the C API for the number of free symbols, enumerates their names withqk_circuit_param_symbol_name(...), and emits declarations such as:Those declarations are written after:
and before qubit, bit, and operation lines. Gate parameters themselves are still serialized with
qk_param_str(), so numeric parameters, symbolic parameters, and expressions keep the C API's existing formatting behavior.The explicit API:
is also supported. It validates that the caller-provided names match the circuit's free-parameter count and rejects empty or duplicate names.
On the Qiskit C API side, the fix adds:
This complements the existing
qk_circuit_num_param_symbols(...). The new function returns an owned C string that must be freed withqk_str_free, and returnsNULLwhen the index is out of range. Internally it usesCircuitData::parameters(), giving qiskit-cpp the same canonical deterministic parameter order that Qiskit already tracks in the Rust circuit data.Details and comments
The qiskit-cpp tests cover the expected exporter behavior:
theta + phiinputdeclarationsU(...)emissionqasm3::dumps(circuit, names)explicit-name exportQuantumCircuit::to_qasm3()compose(...)append(src[0])after indexing a parameterized source circuitThe Qiskit C API tests cover the new symbol-name accessor directly:
NULLA parameterized circuit can now export self-contained OpenQASM 3:
Verification recorded for the final fix:
qiskit-cppverification:cmake -S test -B test/build cmake --build test/build DYLD_LIBRARY_PATH=/usr/local/anaconda3/lib:/opt/anaconda3/lib \ ctest -V -C Debug --test-dir test/build git diff --checkQiskit C API verification, from
qiskit:The qiskit-cpp suite passed
3/3and the C API suite passed27/27.