Skip to content

Export parameterized circuits to OpenQASM 3#161

Closed
alyabouzaid wants to merge 1 commit into
Qiskit:mainfrom
alyabouzaid:qasm3-parameterized-export-146
Closed

Export parameterized circuits to OpenQASM 3#161
alyabouzaid wants to merge 1 commit into
Qiskit:mainfrom
alyabouzaid:qasm3-parameterized-export-146

Conversation

@alyabouzaid

Copy link
Copy Markdown

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 with rx(theta) and ry(phi) produced output that started straight at qubit[2] q; and then used theta/phi with 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 one input float[64] <name>; per symbol, after the gate definitions and before the register declarations, so every referenced symbol is declared.
OPENQASM 3.0;
include "stdgates.inc";
input float[64] theta;
input float[64] phi;
qubit[2] q;
rx(theta) q[0];
ry(phi) q[1];

Limitations (from the C API, not this change)

  • Gate arguments must be bare symbols. A compound expression such as 2*theta can't be reduced to a single input name, so it raises std::runtime_error instead of emitting an invalid identifier.
  • Two distinct Parameter objects 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 against qk_circuit_num_param_symbols as a guard.

Both limitations are noted in the method docs and a release note.

Tests

Added test_parameter_symbols, test_to_qasm3_parameterized, and test_to_qasm3_shared_parameter to test_circuit.cpp. Full suite passes locally (Qiskit 2.4.1 C extension, macOS):

100% tests passed, 0 tests failed out of 4

🤖 Generated with Claude Code

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>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


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.

@1ucian0

1ucian0 commented Jun 16, 2026

Copy link
Copy Markdown
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.

@1ucian0 1ucian0 closed this Jun 16, 2026
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

3 participants