Skip to content

Target

QASM3 target emitter.

Uses the EmitABC abstract-interpretation pattern (like the QASM2 emitter) with dialect-registered method tables for dispatch.

QASM3Emitter dataclass

QASM3Emitter(
    include_files: list[str] = (lambda: ["stdgates.inc"])(),
)

Emit OpenQASM 3.0 strings from QASM3 dialect IR.

Uses abstract interpretation via EmitABC with method tables, following the same pattern as the QASM2 emitter.

emit

emit(entry: Method) -> str

Convert an ir.Method in QASM3 dialect to an OpenQASM 3.0 string.

Parameters:

Name Type Description Default
entry Method

The IR method to emit. Must be in the QASM3 dialect.

required

Returns:

Type Description
str

A syntactically valid OpenQASM 3.0 string.

Source code in .venv/lib/python3.12/site-packages/bloqade/qasm3/emit/target.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def emit(self, entry: ir.Method) -> str:
    """Convert an ir.Method in QASM3 dialect to an OpenQASM 3.0 string.

    Args:
        entry: The IR method to emit. Must be in the QASM3 dialect.

    Returns:
        A syntactically valid OpenQASM 3.0 string.
    """
    target_main = EmitQASM3Main(dialects=entry.dialects).initialize()

    target_main.run(entry)

    body = target_main.output
    assert body is not None, f"failed to emit {entry.sym_name}"

    # Build header
    header_lines: list[str] = ["OPENQASM 3.0;"]
    for inc in self.include_files:
        header_lines.append(f'include "{inc}";')
    header_lines.append("")

    return "\n".join(header_lines) + "\n" + body + "\n"