CUDA-Q emitter implementation.
This module provides a unified emitter for the CUDA-Q backend:
CudaqKernelEmitter: Generates@cudaq.kerneldecorated Python source code for all circuits. The emitter supports two execution modes:STATIC: Measurement-free kernels compatible with
cudaq.sample(),cudaq.get_state(), andcudaq.observe().RUNNABLE: Kernels with explicit mid-circuit measurements and a final
return mz(q), compatible withcudaq.run().
The execution mode is determined by the materializer based on whether the circuit contains runtime measurement-dependent control flow.
Overview¶
| Class | Description |
|---|---|
CudaqExpr | Arithmetic-capable wrapper for CUDA-Q source expressions. |
CudaqKernelArtifact | Compiled CUDA-Q decorator-kernel artifact. |
CudaqKernelEmitter | Unified GateEmitter that generates @cudaq.kernel Python source code. |
EmitError | Error during backend code emission. |
ExecutionMode | Execution mode for a CUDA-Q kernel artifact. |
MeasurementMode | How a backend handles measurement operations. |
Classes¶
CudaqExpr [source]¶
class CudaqExprArithmetic-capable wrapper for CUDA-Q source expressions.
Enables operator-overload-based expression building while the CUDA-Q
materializer renders target-neutral scalar expressions. Each arithmetic
operation returns a new CudaqExpr with a properly parenthesized string
representation. str(expr) yields the raw source expression suitable
for embedding directly in generated @cudaq.kernel code.
Parameters:
| Name | Type | Description |
|---|---|---|
expr | str | Source expression string (e.g. "thetas[0]"). |
Constructor¶
def __init__(self, expr: str) -> NoneInitialize a source expression wrapper.
Parameters:
| Name | Type | Description |
|---|---|---|
expr | str | CUDA-Q-compatible source expression. |
CudaqKernelArtifact [source]¶
class CudaqKernelArtifactCompiled CUDA-Q decorator-kernel artifact.
Wraps a @cudaq.kernel decorated function produced by
CudaqKernelEmitter. The execution_mode field determines
which CUDA-Q runtime APIs are valid for this artifact.
Parameters:
| Name | Type | Description |
|---|---|---|
kernel_func | Any | The @cudaq.kernel decorated function, or None before finalize() is called. |
num_qubits | int | Number of qubits in the circuit. |
num_clbits | int | Number of classical bits in the circuit. |
source | str | Generated Python source code (for debugging). |
entry_source | str | Generated Python source for the entry-point kernel only. This is identical to source in the single-entrypoint materialization path. |
execution_mode | ExecutionMode | Whether this is a STATIC or RUNNABLE kernel. |
param_count | int | Number of variational parameters. |
Constructor¶
def __init__(
self,
kernel_func: Any,
num_qubits: int,
num_clbits: int,
source: str = '',
entry_source: str = '',
execution_mode: ExecutionMode = ExecutionMode.STATIC,
param_count: int = 0,
) -> NoneAttributes¶
entry_source: strexecution_mode: ExecutionModekernel_func: Anynum_clbits: intnum_qubits: intparam_count: intsource: str
CudaqKernelEmitter [source]¶
class CudaqKernelEmitterUnified GateEmitter that generates @cudaq.kernel Python source code.
Used for all CUDA-Q circuits. Gate calls are accumulated as Python
source lines forming a @cudaq.kernel decorated function. The
finalize() method compiles the source via exec() and populates
the artifact’s kernel_func.
The measurement_mode property controls measurement behaviour:
MeasurementMode.STATIC:emit_measureis a no-op;cudaq.sample()auto-measures all qubits.MeasurementMode.RUNNABLE:emit_measureemits explicitmz()calls for mid-circuit measurement variables.
Parameters:
| Name | Type | Description |
|---|---|---|
parametric | bool | Initial hint for parametricity. The materializer overrides this after emission based on _param_count. |
Constructor¶
def __init__(self, parametric: bool = False) -> NoneAttributes¶
measurement_mode: MeasurementMode Return the current measurement mode.
Methods¶
begin_source_block¶
def begin_source_block(self, header: str) -> intOpen an indented source block and return its initial line count.
Parameters:
| Name | Type | Description |
|---|---|---|
header | str | Block header without indentation, such as if x:. |
Returns:
int — Line count used to detect an empty block at close time.
clbit_ref¶
def clbit_ref(self, index: int) -> strReturn the generated source reference for a classical bit slot.
Parameters:
| Name | Type | Description |
|---|---|---|
index | int | Classical bit slot. |
Returns:
str — CUDA-Q source expression for the bit.
configure_runtime_clbits¶
def configure_runtime_clbits(self, num_clbits: int) -> NoneRepresent every runtime classical bit as a mutable one-item list.
Parameters:
| Name | Type | Description |
|---|---|---|
num_clbits | int | Number of classical bits assigned by lowering. |
create_circuit¶
def create_circuit(self, num_qubits: int, num_clbits: int) -> CudaqKernelArtifactStart building a @cudaq.kernel function.
Resets internal state and emits the q = cudaq.qvector(N)
allocation line.
Parameters:
| Name | Type | Description |
|---|---|---|
num_qubits | int | Number of qubits to allocate. |
num_clbits | int | Number of classical bits. |
Returns:
CudaqKernelArtifact — A new CudaqKernelArtifact with kernel_func=None
CudaqKernelArtifact — (populated by finalize()).
create_parameter¶
def create_parameter(self, name: str) -> CudaqExprReturn a CudaqExpr referencing thetas[i].
Returns an arithmetic-capable expression object so the materializer
can compose gate-angle expressions such as gamma * Jij without
operating directly on raw strings.
Parameters:
| Name | Type | Description |
|---|---|---|
name | str | Symbolic parameter name. |
Returns:
CudaqExpr — CudaqExpr wrapping a source expression like
CudaqExpr — "thetas[0]".
define_adjoint_helper¶
def define_adjoint_helper(self, helper_name: str, num_qubits: int) -> strDefine a reusable adjoint wrapper around another CUDA-Q kernel.
Parameters:
| Name | Type | Description |
|---|---|---|
helper_name | str | Base helper kernel name. |
num_qubits | int | Number of target-qubit arguments. |
Returns:
str — Generated adjoint-wrapper name.
define_helper¶
def define_helper(
self,
key: Hashable,
display_name: str,
num_qubits: int,
emit_body: Callable[[], None],
) -> strDefine one reusable CUDA-Q kernel without flattening its call site.
Parameters:
| Name | Type | Description |
|---|---|---|
key | Hashable | Materializer-owned identity used to deduplicate repeated references to the same reusable body. |
display_name | str | Human-readable source-name component. |
num_qubits | int | Number of scalar qubit arguments. |
emit_body | Callable[[], None] | Callback that emits the helper body through this emitter. |
Returns:
str — Unique generated CUDA-Q kernel name.
Raises:
ValueError— If recursive helper generation is detected.
emit_barrier¶
def emit_barrier(self, circuit: CudaqKernelArtifact, qubits: list[int]) -> NoneNo-op: CUDA-Q does not support barrier instructions.
emit_ch¶
def emit_ch(self, circuit: CudaqKernelArtifact, control: int, target: int) -> NoneEmit controlled-Hadamard via decomposition.
Mirrors the CH_DECOMPOSITION recipe from
qamomile.circuit.transpiler.decompositions. Inlined here (rather
than calling :func:emit_decomposition) so that the string-based
codegen produces the exact source contract expected by the tracing
test emitter, without double-recording primitive gate calls.
emit_cp¶
def emit_cp(
self,
circuit: CudaqKernelArtifact,
control: int,
target: int,
angle: float | Any,
) -> NoneEmit controlled phase through CUDA-Q’s exact controlled R1.
emit_crx¶
def emit_crx(
self,
circuit: CudaqKernelArtifact,
control: int,
target: int,
angle: float | Any,
) -> NoneEmit controlled-RX gate.
emit_cry¶
def emit_cry(
self,
circuit: CudaqKernelArtifact,
control: int,
target: int,
angle: float | Any,
) -> NoneEmit controlled-RY gate.
emit_crz¶
def emit_crz(
self,
circuit: CudaqKernelArtifact,
control: int,
target: int,
angle: float | Any,
) -> NoneEmit controlled-RZ gate.
emit_cx¶
def emit_cx(self, circuit: CudaqKernelArtifact, control: int, target: int) -> NoneEmit CNOT (controlled-X) gate.
emit_cy¶
def emit_cy(self, circuit: CudaqKernelArtifact, control: int, target: int) -> NoneEmit controlled-Y via decomposition.
Mirrors the CY_DECOMPOSITION recipe from
qamomile.circuit.transpiler.decompositions. See
:meth:emit_ch for why this is inlined rather than delegating to
:func:emit_decomposition.
emit_cz¶
def emit_cz(self, circuit: CudaqKernelArtifact, control: int, target: int) -> NoneEmit controlled-Z gate.
emit_exp_pauli¶
def emit_exp_pauli(
self,
circuit: CudaqKernelArtifact,
qubit_indices: list[int],
pauli_str: str,
angle: float | Any,
) -> NoneEmit a native exp_pauli Pauli evolution term.
Implements exp(i * angle * P) for a single Pauli term P,
where pauli_str[k] acts on qubit_indices[k]. CUDA-Q applies
the j-th character of the word to the j-th qubit in the
passed list, so the word and qubit list share the same order and no
reversal is needed (verified against the Qiskit PauliEvolutionGate
reference for asymmetric multi-qubit terms such as X0 Z1).
Parameters:
| Name | Type | Description |
|---|---|---|
circuit | CudaqKernelArtifact | Artifact currently being built. |
qubit_indices | list[int] | Physical qubit slots pauli_str acts on, in natural (pauli_str[k] -> qubit_indices[k]) order. |
pauli_str | str | Pauli letters (X / Y / Z) for the term, one per entry in qubit_indices. |
angle | float | Any | Rotation angle or CUDA-Q source expression. |
emit_global_phase¶
def emit_global_phase(
self,
circuit: CudaqKernelArtifact,
angle: float | Any,
carrier: int | None = None,
) -> NoneSynthesize exp(i * angle) I on an existing CUDA-Q qubit.
The exact identity is R1(2a) RZ(-2a) = exp(ia) I; it preserves an
arbitrary state of the selected carrier qubit.
Parameters:
| Name | Type | Description |
|---|---|---|
circuit | CudaqKernelArtifact | Artifact currently being built. |
angle | float | Any | Concrete or source-level phase in radians. |
carrier | int | None | Existing qubit used by the identity sequence. Defaults to qubit zero when the artifact is nonempty. |
Raises:
EmitError— If the artifact has no qubit orcarrieris outside its physical width.
emit_global_phase_on_clean_carrier¶
def emit_global_phase_on_clean_carrier(self, circuit: CudaqKernelArtifact, angle: float | Any, carrier: int) -> NoneSynthesize exp(i * angle) on a dedicated clean carrier.
Because the carrier is known to remain in |0>, the single gate
RZ(-2a) produces exp(ia)|0> exactly. This specialized form is
not valid for an arbitrary-state logical qubit.
Parameters:
| Name | Type | Description |
|---|---|---|
circuit | CudaqKernelArtifact | Artifact currently being built. |
angle | float | Any | Concrete or source-level phase in radians. |
carrier | int | Internal qubit known to be initialized in |0>. |
Raises:
EmitError— Ifcarrieris outside the artifact’s physical width.
emit_h¶
def emit_h(self, circuit: CudaqKernelArtifact, qubit: int) -> NoneEmit Hadamard gate.
emit_measure¶
def emit_measure(self, circuit: CudaqKernelArtifact, qubit: int, clbit: int) -> NoneEmit measurement, respecting the current mode.
In STATIC mode (measurement_mode == STATIC), this is a
no-op: cudaq.sample() auto-measures all qubits.
In RUNNABLE mode (measurement_mode == RUNNABLE), emits
explicit mz() to capture mid-circuit measurement results as
local variables for if / while conditions.
emit_multi_controlled_p¶
def emit_multi_controlled_p(
self,
circuit: CudaqKernelArtifact,
control_indices: list[int],
target_idx: int,
angle: float | Any,
) -> NoneEmit a multi-controlled phase rotation.
Parameters:
| Name | Type | Description |
|---|---|---|
circuit | CudaqKernelArtifact | Artifact currently being built. |
control_indices | list[int] | Physical control qubit indices. If empty, this method emits an ordinary phase gate. |
target_idx | int | Physical target qubit index. |
angle | float | Any | Rotation angle or CUDA-Q source expression. |
emit_multi_controlled_rx¶
def emit_multi_controlled_rx(
self,
circuit: CudaqKernelArtifact,
control_indices: list[int],
target_idx: int,
angle: float | Any,
) -> NoneEmit a multi-controlled RX rotation.
Parameters:
| Name | Type | Description |
|---|---|---|
circuit | CudaqKernelArtifact | Artifact currently being built. |
control_indices | list[int] | Physical control qubit indices. If empty, this method emits an ordinary RX gate. |
target_idx | int | Physical target qubit index. |
angle | float | Any | Rotation angle or CUDA-Q source expression. |
emit_multi_controlled_ry¶
def emit_multi_controlled_ry(
self,
circuit: CudaqKernelArtifact,
control_indices: list[int],
target_idx: int,
angle: float | Any,
) -> NoneEmit a multi-controlled RY rotation.
Parameters:
| Name | Type | Description |
|---|---|---|
circuit | CudaqKernelArtifact | Artifact currently being built. |
control_indices | list[int] | Physical control qubit indices. If empty, this method emits an ordinary RY gate. |
target_idx | int | Physical target qubit index. |
angle | float | Any | Rotation angle or CUDA-Q source expression. |
emit_multi_controlled_rz¶
def emit_multi_controlled_rz(
self,
circuit: CudaqKernelArtifact,
control_indices: list[int],
target_idx: int,
angle: float | Any,
) -> NoneEmit a multi-controlled RZ rotation.
Parameters:
| Name | Type | Description |
|---|---|---|
circuit | CudaqKernelArtifact | Artifact currently being built. |
control_indices | list[int] | Physical control qubit indices. If empty, this method emits an ordinary RZ gate. |
target_idx | int | Physical target qubit index. |
angle | float | Any | Rotation angle or CUDA-Q source expression. |
emit_multi_controlled_x¶
def emit_multi_controlled_x(
self,
circuit: CudaqKernelArtifact,
control_indices: list[int],
target_idx: int,
) -> NoneEmit multi-controlled X using x.ctrl(c0, c1, ..., target).
emit_p¶
def emit_p(self, circuit: CudaqKernelArtifact, qubit: int, angle: float | Any) -> NoneEmit phase gate (R1).
emit_reset¶
def emit_reset(self, circuit: CudaqKernelArtifact, qubit: int) -> NoneEmit reset to the |0> state.
emit_reusable_call¶
def emit_reusable_call(
self,
helper_name: str,
targets: tuple[int, ...],
controls: tuple[int, ...] = (),
) -> NoneEmit one direct or controlled named CUDA-Q kernel invocation.
Parameters:
| Name | Type | Description |
|---|---|---|
helper_name | str | Generated helper-kernel name. |
targets | tuple[int, ...] | Target qubit slots. |
controls | tuple[int, ...] | Control qubit slots. Defaults to empty. |
emit_rx¶
def emit_rx(self, circuit: CudaqKernelArtifact, qubit: int, angle: float | Any) -> NoneEmit RX rotation gate.
emit_ry¶
def emit_ry(self, circuit: CudaqKernelArtifact, qubit: int, angle: float | Any) -> NoneEmit RY rotation gate.
emit_rz¶
def emit_rz(self, circuit: CudaqKernelArtifact, qubit: int, angle: float | Any) -> NoneEmit RZ rotation gate.
emit_rzz¶
def emit_rzz(
self,
circuit: CudaqKernelArtifact,
qubit1: int,
qubit2: int,
angle: float | Any,
) -> NoneEmit RZZ via CNOT + RZ decomposition.
RZZ(q1, q2, theta) = CNOT(q1, q2) RZ(q2, theta) CNOT(q1, q2)
emit_s¶
def emit_s(self, circuit: CudaqKernelArtifact, qubit: int) -> NoneEmit S (phase) gate.
emit_sdg¶
def emit_sdg(self, circuit: CudaqKernelArtifact, qubit: int) -> NoneEmit S-dagger gate.
emit_source_line¶
def emit_source_line(self, line: str) -> NoneAppend one source line through the materializer-facing API.
Parameters:
| Name | Type | Description |
|---|---|---|
line | str | Unindented Python/CUDA-Q source line. |
emit_swap¶
def emit_swap(self, circuit: CudaqKernelArtifact, qubit1: int, qubit2: int) -> NoneEmit SWAP gate.
emit_t¶
def emit_t(self, circuit: CudaqKernelArtifact, qubit: int) -> NoneEmit T gate.
emit_tdg¶
def emit_tdg(self, circuit: CudaqKernelArtifact, qubit: int) -> NoneEmit T-dagger gate.
emit_toffoli¶
def emit_toffoli(
self,
circuit: CudaqKernelArtifact,
control1: int,
control2: int,
target: int,
) -> NoneEmit Toffoli (CCX) gate.
Delegates to :meth:emit_multi_controlled_x so that all
multi-controlled X emission flows through one implementation.
emit_x¶
def emit_x(self, circuit: CudaqKernelArtifact, qubit: int) -> NoneEmit Pauli-X gate.
emit_y¶
def emit_y(self, circuit: CudaqKernelArtifact, qubit: int) -> NoneEmit Pauli-Y gate.
emit_z¶
def emit_z(self, circuit: CudaqKernelArtifact, qubit: int) -> NoneEmit Pauli-Z gate.
end_source_block¶
def end_source_block(self, initial_line_count: int) -> NoneClose a source block, inserting pass when it is empty.
Parameters:
| Name | Type | Description |
|---|---|---|
initial_line_count | int | Value returned by :meth:begin_source_block for this block. |
finalize¶
def finalize(self, circuit: CudaqKernelArtifact, mode: ExecutionMode) -> CudaqKernelArtifactCompile accumulated source into a @cudaq.kernel function.
For RUNNABLE mode, appends return [__b0, __b1, ...] to
return per-shot logical clbit values. For STATIC mode,
generates a void function with no explicit terminal measurement.
Parameters:
| Name | Type | Description |
|---|---|---|
circuit | CudaqKernelArtifact | The artifact to finalize. |
mode | ExecutionMode | Execution mode determining the function signature. |
Returns:
CudaqKernelArtifact — The same artifact with kernel_func and source populated.
qubit_ref¶
def qubit_ref(self, index: int) -> strReturn the generated source reference for a qubit slot.
Parameters:
| Name | Type | Description |
|---|---|---|
index | int | Physical qubit slot. |
Returns:
str — CUDA-Q source expression for the qubit.
set_parametric¶
def set_parametric(self, parametric: bool) -> NoneSet whether the generated entrypoint accepts a parameter vector.
Parameters:
| Name | Type | Description |
|---|---|---|
parametric | bool | Whether at least one runtime parameter is used. |
EmitError [source]¶
class EmitError(QamomileCompileError)Error during backend code emission.
Constructor¶
def __init__(self, message: str, operation: str | None = None)Initialize a backend emission diagnosis.
Parameters:
| Name | Type | Description |
|---|---|---|
message | str | Human-readable emission failure. |
operation | str | None | Related operation description. Defaults to None. |
Attributes¶
operation
ExecutionMode [source]¶
class ExecutionMode(enum.Enum)Execution mode for a CUDA-Q kernel artifact.
Determines which CUDA-Q runtime API is used for execution:
STATIC: Compatible withcudaq.sample(),cudaq.get_state(), andcudaq.observe(). The kernel has no explicit terminal measurements and no return value.RUNNABLE: Compatible withcudaq.run(). The kernel has explicit mid-circuit measurements and returnsmz(q)(list[bool]).
Attributes¶
RUNNABLESTATIC
MeasurementMode [source]¶
class MeasurementMode(Enum)How a backend handles measurement operations.
Attributes¶
NATIVERUNNABLESTATIC