Skip to content

Target

QASM2

QASM2(
    main_target: ir.DialectGroup | None = None,
    gate_target: ir.DialectGroup | None = None,
    qelib1: bool = False,
)

QASM2 target for Bloqade kernels.

QASM2 target that accepts a Bloqade kernel and produces an AST that you can then obtain a string for printing or saving as a file.

Parameters:

Name Type Description Default
main_target DialectGroup | None

The dialects that were used in the definition of the kernel. This is used to generate the correct header for the resulting QASM2 AST. Argument defaults to None. Internally set to the qasm2.main group of dialects.

None
gate_target DialectGroup | None

The dialects involved in defining any custom gates in the kernel. Argument defaults to None. Internally set to the qasm2.gate group of dialects.

None
qelib1 bool

Include the include "qelib1.inc" line in the resulting QASM2 AST that's submitted to qBraid. Defaults to True.

False
Source code in src/bloqade/qasm2/emit/target.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def __init__(
    self,
    main_target: ir.DialectGroup | None = None,
    gate_target: ir.DialectGroup | None = None,
    qelib1: bool = False,
) -> None:
    """Initialize the QASM2 target.

    Args:
        main_target (ir.DialectGroup | None):
            The dialects that were used in the definition of the kernel. This is used to
            generate the correct header for the resulting QASM2 AST. Argument defaults to `None`.
            Internally set to the `qasm2.main` group of dialects.
        gate_target (ir.DialectGroup | None):
            The dialects involved in defining any custom gates in the kernel. Argument defaults to `None`.
            Internally set to the `qasm2.gate` group of dialects.
        qelib1 (bool):
            Include the `include "qelib1.inc"` line in the resulting QASM2 AST that's
            submitted to qBraid. Defaults to `True`.
    """
    from bloqade import qasm2

    self.main_target = main_target or qasm2.main
    self.gate_target = gate_target or qasm2.gate
    self.qelib1 = qelib1
    self.custom_gate = False

emit

emit(entry: ir.Method) -> ast.MainProgram

Emit a QASM2 AST from the Bloqade kernel.

Parameters:

Name Type Description Default
entry Method

The Bloqade kernel to convert to the QASM2 AST

required

Returns:

Type Description
MainProgram

ast.MainProgram: A QASM2 AST object

Source code in src/bloqade/qasm2/emit/target.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def emit(self, entry: ir.Method) -> ast.MainProgram:
    """Emit a QASM2 AST from the Bloqade kernel.

    Args:
        entry (ir.Method):
            The Bloqade kernel to convert to the QASM2 AST

    Returns:
        ast.MainProgram:
            A QASM2 AST object

    """
    assert len(entry.args) == 0, "entry method should not have arguments"
    entry = entry.similar()
    QASM2Fold(entry.dialects).fixpoint(entry)
    Py2QASM(entry.dialects)(entry)
    target_main = EmitQASM2Main(self.main_target)
    target_main.run(
        entry, tuple(ast.Name(name) for name in entry.arg_names[1:])
    ).expect()
    main_program = target_main.output
    assert main_program is not None, f"failed to emit {entry.sym_name}"

    extra = []
    if self.qelib1:
        extra.append(ast.Include("qelib1.inc"))
    if self.custom_gate:
        cg = CallGraph(entry)
        target_gate = EmitQASM2Gate(self.gate_target)

        for _, fn in cg.defs.items():
            if fn is entry:
                continue

            fn = fn.similar(self.gate_target)
            QASM2Fold(fn.dialects).fixpoint(fn)
            Py2QASM(fn.dialects)(fn)
            target_gate.run(
                fn, tuple(ast.Name(name) for name in fn.arg_names[1:])
            ).expect()
            assert target_gate.output is not None, f"failed to emit {fn.sym_name}"
            extra.append(target_gate.output)

    main_program.statements = extra + main_program.statements
    return main_program

emit_str

emit_str(entry: ir.Method) -> str

Emit a QASM2 AST from the Bloqade kernel.

Parameters:

Name Type Description Default
entry Method

The Bloqade kernel to convert to the QASM2 AST

required

Returns:

Name Type Description
str str

A string with the QASM2 representation of the kernel

Source code in src/bloqade/qasm2/emit/target.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def emit_str(self, entry: ir.Method) -> str:
    """Emit a QASM2 AST from the Bloqade kernel.

    Args:
        entry (ir.Method):
            The Bloqade kernel to convert to the QASM2 AST

    Returns:
        str:
            A string with the QASM2 representation of the kernel

    """
    console = Console(
        file=io.StringIO(),
        force_terminal=False,
        force_interactive=False,
        force_jupyter=False,
        record=True,
    )
    pprint(self.emit(entry), console=console)
    return console.export_text()