Skip to content

feat(qasm3): export parameterized circuits in QASM3#147

Open
Roll249 wants to merge 1 commit into
Qiskit:mainfrom
Roll249:fix/qasm3-parameterized-export-146
Open

feat(qasm3): export parameterized circuits in QASM3#147
Roll249 wants to merge 1 commit into
Qiskit:mainfrom
Roll249:fix/qasm3-parameterized-export-146

Conversation

@Roll249
Copy link
Copy Markdown

@Roll249 Roll249 commented Jun 3, 2026

Summary

Refactors QuantumCircuit::to_qasm3 into a standalone Qasm3Exporter class and adds full support for exporting parameterized quantum circuits to OpenQASM 3.0.

Changes

  1. New Qasm3Exporter class that encapsulates all QASM3 export logic
  2. Uses qk_param_str() to preserve symbolic expressions like rx(theta)
  3. All existing tests pass
  4. New tests cover parameterized circuit export

Files changed

  • src/circuit/qasm3_exporter.hpp (new)
  • src/circuit/qasm3_exporter.cpp (new)
  • src/circuit/qasm3_exporter_impl.hpp (new)
  • src/circuit/quantumcircuit_def.hpp (refactored)
  • src/circuit/classicalregister.hpp (added missing include)
  • src/circuit/quantumregister.hpp (added missing include)
  • test/test_circuit.cpp (new tests)
  • test/CMakeLists.txt (updated)
  • releasenotes/notes/qasm3-parameterized-export-146.yaml (new)

Fixes #146

Made with Cursor

…upport

Extract the `to_qasm3` implementation into a new `Qasm3Exporter` class
(src/circuit/qasm3_exporter.hpp, src/circuit/qasm3_exporter.cpp) so that:

1. The `QuantumCircuit` class no longer needs to know about the textual
   QASM representation.
2. Parameterized circuits can now be serialized: `qk_param_str` is
   used to convert each gate parameter, preserving symbolic expressions
   (e.g. `rx(theta) q[0];`) and numeric values alike.

New tests cover:
- Symbolic parameter export (`rx(theta)`)
- Compound parameter expressions (`rx(1.5 + 2*theta)`)
- Numeric parameter export (unchanged behavior)
- The `U` gate special-case remains intact

Fixes: Qiskit#146
Co-authored-by: Cursor <cursoragent@cursor.com>
Copilot AI review requested due to automatic review settings June 3, 2026 18:38
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Jun 3, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Copy Markdown

Copilot AI left a comment

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.

Refactors OpenQASM 3.0 export into a dedicated Qasm3Exporter and adds regression tests to ensure symbolic Parameter expressions are serialized correctly (issue #146).

Changes:

  • Extracted QuantumCircuit::to_qasm3 implementation into a new Qasm3Exporter class.
  • Added regression tests for symbolic and numeric parameter serialization in QASM3 output.
  • Added release notes documenting parameterized QASM3 export support.

Reviewed changes

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

Show a summary per file
File Description
test/test_circuit.cpp Adds regression tests for symbolic/numeric parameter serialization in to_qasm3.
test/CMakeLists.txt Updates test build to compile the new exporter and modifies include paths.
src/circuit/quantumregister.hpp Adds <memory> include (likely needed after refactor/build changes).
src/circuit/classicalregister.hpp Adds <memory> include (likely needed after refactor/build changes).
src/circuit/quantumcircuit_def.hpp Replaces large inline to_qasm3 body with a declaration and refactor note.
src/circuit/qasm3_exporter.hpp Introduces new exporter API for QASM3 serialization.
src/circuit/qasm3_exporter.cpp Implements Qasm3Exporter and QuantumCircuit::to_qasm3.
src/circuit/qasm3_exporter_impl.hpp Adds inline exporter implementation (currently duplicates .cpp).
releasenotes/notes/qasm3-parameterized-export-146.yaml Documents the new QASM3 parameter export support and refactor.

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

Comment thread test/CMakeLists.txt
Comment on lines +35 to +38
add_executable (test_driver ${source_files} ../src/circuit/qasm3_exporter.cpp)
# ...include the location of the header file...
target_include_directories (test_driver PRIVATE ${CMAKE_SOURCE_DIR} nlohmann_json::nlohmann_json ../src deps/qiskit/dist/c/include)
target_include_directories (test_driver PRIVATE /home/khang/pro-lab/coding-space/skill-quantum/qiskit-cpp-work/qiskit-cpp/src /home/khang/pro-lab/coding-space/skill-quantum/qiskit-cpp-work/qiskit-source/dist/c/include)
Comment thread test/CMakeLists.txt
add_library(common STATIC common.cpp)
# Actually define the test driver program executable to be built...
add_executable (test_driver ${source_files})
add_executable (test_driver ${source_files} ../src/circuit/qasm3_exporter.cpp)
return out.str();
}

inline rust_circuit *Qasm3Exporter::rust_circuit_ptr(void) {
out << "include \"stdgates.inc\";" << std::endl;
}

inline void Qasm3Exporter::write_gate_definitions(std::stringstream &out) const {
}
}

inline void Qasm3Exporter::write_instructions(std::stringstream &out) const {
Comment on lines +43 to +45
inline Qasm3Exporter::Qasm3Exporter(QuantumCircuit *circ) : circ_(circ) {}

inline std::string Qasm3Exporter::run(void) {
Comment on lines +369 to +371
for (uint_t i = 0; i < nops; i++) {
QkCircuitInstruction *op = new QkCircuitInstruction;
qk_circuit_get_instruction(rust_circuit_ptr(), i, op);
Comment on lines +407 to +408
qk_circuit_instruction_clear(op);
}
Comment thread test/test_circuit.cpp
Comment on lines +673 to +679
// `2*theta + 1.5` may be canonicalised slightly differently by
// `qk_param_str`; just check that the symbolic name `theta` is preserved
// and that we do not fall back to a numeric-only representation.
if (actual.find("2*theta") == std::string::npos) {
std::cerr << " to_qasm3_parameter_expression test : missing symbolic '2*theta' in: " << actual << std::endl;
return EqualityError;
}
@1ucian0
Copy link
Copy Markdown
Member

1ucian0 commented Jun 4, 2026

This looks an unsupervised LLM submission...

@Roll249 did you verify and refine the LLM output as asked in https://unitaryhack.dev/ai-guide/ ?

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.

QASM3 exporter with parameterized circuit support

4 participants