Skip to content

Target

QASM2

QASM2(
    qelib1: bool = True,
    allow_parallel: bool = False,
    allow_global: bool = False,
    custom_gate: bool = True,
    unroll_ifs: bool = True,
    allow_noise: bool = True,
)

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
allow_parallel bool

Allow parallel gate in the resulting QASM2 AST. Defaults to False. In the case its False, and the input kernel uses parallel gates, they will get rewrite into uop gates.

False
allow_global bool

Allow global gate in the resulting QASM2 AST. Defaults to False. In the case its False, and the input kernel uses global gates, they will get rewrite into parallel gates. If both allow_parallel and allow_global are False, the input kernel will be rewritten to use uop gates.

False
qelib1 bool

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

True
custom_gate bool

Include the custom gate definitions in the resulting QASM2 AST. Defaults to True. If False, all the qasm2.gate will be inlined.

True
unroll_ifs bool

Unrolls if statements with multiple qasm2 statements in the body in order to produce valid qasm2 output, which only allows a single operation in an if body. Defaults to True.

True
Source code in .venv/lib/python3.12/site-packages/bloqade/qasm2/emit/target.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
def __init__(
    self,
    qelib1: bool = True,
    allow_parallel: bool = False,
    allow_global: bool = False,
    custom_gate: bool = True,
    unroll_ifs: bool = True,
    allow_noise: bool = True,
) -> None:
    """Initialize the QASM2 target.

    Args:
        allow_parallel (bool):
            Allow parallel gate in the resulting QASM2 AST. Defaults to `False`.
            In the case its False, and the input kernel uses parallel gates, they will get rewrite into uop gates.

        allow_global (bool):
            Allow global gate in the resulting QASM2 AST. Defaults to `False`.
            In the case its False, and the input kernel uses global gates, they will get rewrite into parallel gates.
            If both `allow_parallel` and `allow_global` are False, the input kernel will be rewritten to use uop gates.

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

        custom_gate (bool):
            Include the custom gate definitions in the resulting QASM2 AST. Defaults to `True`. If `False`, all the qasm2.gate will be inlined.

        unroll_ifs (bool):
            Unrolls if statements with multiple qasm2 statements in the body in order to produce valid qasm2 output, which only allows a single
            operation in an if body. Defaults to `True`.



    """
    from bloqade import qasm2

    self.main_target = qasm2.main
    self.gate_target = qasm2.gate

    self.qelib1 = qelib1
    self.custom_gate = custom_gate
    self.allow_parallel = allow_parallel
    self.allow_global = allow_global
    self.unroll_ifs = unroll_ifs

    if allow_parallel:
        self.main_target = self.main_target.add(qasm2.dialects.parallel)
        self.gate_target = self.gate_target.add(qasm2.dialects.parallel)

    if allow_global:
        self.main_target = self.main_target.add(qasm2.dialects.glob)
        self.gate_target = self.gate_target.add(qasm2.dialects.glob)

    if allow_noise:
        self.main_target = self.main_target.add(qasm2.dialects.noise)
        self.gate_target = self.gate_target.add(qasm2.dialects.noise)

    if allow_global or allow_parallel or allow_noise:
        self.main_target = self.main_target.add(ilist)
        self.gate_target = self.gate_target.add(ilist)

emit

emit(entry: 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 .venv/lib/python3.12/site-packages/bloqade/qasm2/emit/target.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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"

    # make a cloned instance of kernel
    entry = entry.similar()
    QASM2Fold(
        entry.dialects,
        inline_gate_subroutine=not self.custom_gate,
        unroll_ifs=self.unroll_ifs,
    ).fixpoint(entry)

    if not self.allow_global:
        # rewrite global to parallel
        GlobalToParallel(dialects=entry.dialects)(entry)

    if not self.allow_parallel:
        # rewrite parallel to uop
        ParallelToUOp(dialects=entry.dialects)(entry)

    Py2QASM(entry.dialects)(entry)
    target_main = EmitQASM2Main(self.main_target)
    target_main.run(entry, ())

    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 _, fns in cg.defs.items():
            if len(fns) != 1:
                raise ValueError("Incorrect callgraph")

            (fn,) = fns
            if fn is entry:
                continue

            fn = fn.similar()
            QASM2Fold(fn.dialects).fixpoint(fn)

            if not self.allow_global:
                # rewrite global to parallel
                GlobalToParallel(dialects=fn.dialects)(fn)

            if not self.allow_parallel:
                # rewrite parallel to uop
                ParallelToUOp(dialects=fn.dialects)(fn)

            Py2QASM(fn.dialects)(fn)

            target_gate.run(fn, tuple(ast.Name(name) for name in fn.arg_names[1:]))
            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: 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 .venv/lib/python3.12/site-packages/bloqade/qasm2/emit/target.py
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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()