feat: add OpenQASM 3 export support to qiskit-cpp (#146)#157
Conversation
|
I think having a list of Parameter in |
Thank you very much for your kind review and suggestions. I agree. I would like to remove the QuantumCircuit-side parameter metadata from this PR. For the qiskit-cpp-only path #157, I' d like to keep the exporter but require explicit parameter names via |
|
Thank you for review. I have implemented the # 146 requested solution in this PR.
This currently returns the parameter symbol names tracked on the C++ wrapper side. The implementation can later be replaced by the C API once symbol-name enumeration is available from the Rust-side circuit data.
Verification: This keeps the current implementation independent of the new Qiskit C API, while preserving a clean replacement point for the future C API-based implementation. |
| std::map<std::string, std::shared_ptr<int>> parameter_symbol_refs; | ||
| std::map<uint_t, std::vector<parameter_symbol_refs_t>> instruction_parameter_symbols; | ||
| }; | ||
| std::shared_ptr<ParameterSymbolData> parameter_symbol_data_ = std::make_shared<ParameterSymbolData>(); |
There was a problem hiding this comment.
As I mentioned (#157 (comment)) a list of parameter symbols should not be stored on C++ side.
A list of parameter symbols can be parsed by traversing instructions in the circuit
There was a problem hiding this comment.
Thanks, I updated the fix accordingly.
The implementation no longer stores a circuit-level parameter symbol list on the C++ side. I removed ParameterSymbolData, parameter_symbol_data_, instruction_parameter_symbols, and the symbol metadata from Parameter.
QuantumCircuit::parameter_symbols() now derives the symbol list by traversing the circuit instructions and parsing each instruction parameter expression from the actual QkParam values. This keeps the temporary implementation local to the stub and avoids maintaining a duplicate circuit parameter list in C++.
Verified with the local test suite: 100% tests passed, 0 tests failed out of 4
|
@doichanj Thanks for the review. I pushed a new commit ( Main changes:
|
e66980a to
cec0dcb
Compare
Summary
This PR version implements OpenQASM 3 export support entirely within
qiskit-cpp. It adds a dedicatedQiskit::qasm3::dumps(...)exporter, keepsQuantumCircuit::to_qasm3()as a compatibility wrapper, and reuses the existing parameter serialization path to support parameter-aware QASM3 output.No changes to the upstream Qiskit library or its C API are required for this implementation.
Details and comments
Fix
src/qasm3/qasm3_exporter.hppAdds the dedicated QASM 3 exporter and public
Qiskit::qasm3::dumps(...)API. It adds RAII wrappers for C API resources such asQkOpCounts,QkCircuitInstruction, andqk_param_str()strings, and emits parameter declarations before registers and operations.src/circuit/quantumcircuit_def.hppRemoves inline QASM text generation from
QuantumCircuit::to_qasm3()and delegates to the new exporter. It also records parameter-symbol metadata when parameterized gates are added, appended, composed, copied, or recovered throughoperator[].src/circuit/quantumcircuit.hppIncludes the new QASM 3 exporter header so the compatibility
to_qasm3()path can delegate cleanly.src/circuit/parameter.hppTracks local parameter-symbol metadata on C++
Parameterobjects. Symbol names are preserved across copies and parameter operations, including arithmetic and common unary functions, so expressions retain the names needed for QASM declarations.test/test_circuit.cppAdds coverage for parameter-aware QASM export, including direct symbolic parameters, expressions, numeric-only parameters, uppercase
U(...), explicit parameter names, invalid explicit names, parameterizedcompose(...),append(src[0]), and the fail-closed path when default names cannot be recovered.Example Result
A parameterized circuit now exports self-contained OpenQASM 3:
Numeric-only parameters do not emit unnecessary
inputdeclarations:Coverage
The qiskit-cpp tests cover:
U(...)outputQiskit::qasm3::dumps(circuit, names)happy pathQuantumCircuit::to_qasm3()compose(...)append(src[0])Verification
The qiskit-cpp fix was verified with:
The qiskit-cpp test suite passed
3/3.Checklist
test/