Skip to content

Commit 127cc9b

Browse files
authored
simplify loads (#240)
address #238 one question tho, was there a particular reason why the `loads` and `loadfile` was living inside the lowering target? @weinbe58 I think if not would be nice to move them out. This is not an object we expect users to use directly cuz it would be too confusing without knowing about lowering process.
1 parent 99360b1 commit 127cc9b

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

src/bloqade/qasm2/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
)
1818
from .groups import gate as gate, main as main, extended as extended
1919
from ._wrappers import * # noqa: F403
20+
from ._qasm_loading import loads as loads, loadfile as loadfile

src/bloqade/qasm2/_qasm_loading.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from typing import Any
2+
3+
from kirin import ir
4+
5+
from .groups import main
6+
from .parse.lowering import QASM2
7+
8+
9+
def loads(
10+
qasm: str,
11+
*,
12+
kernel_name: str = "main",
13+
dialects: ir.DialectGroup | None = None,
14+
globals: dict[str, Any] | None = None,
15+
file: str | None = None,
16+
lineno_offset: int = 0,
17+
col_offset: int = 0,
18+
compactify: bool = True,
19+
) -> ir.Method[[], None]:
20+
"""Loads a QASM2 string and returns the corresponding kernel object.
21+
22+
Args:
23+
qasm (str): The QASM2 string to load.
24+
25+
Keyword Args:
26+
kernel_name (str): The name of the kernel to load. Defaults to "main".
27+
dialects (ir.DialectGroup | None): The dialects to use. Defaults to `qasm2.main`.
28+
returns (str | None): The return type of the kernel. Defaults to None.
29+
globals (dict[str, Any] | None): The global variables to use. Defaults to None.
30+
file (str | None): The file name for error reporting. Defaults to None.
31+
lineno_offset (int): The line number offset for error reporting. Defaults to 0.
32+
col_offset (int): The column number offset for error reporting. Defaults to 0.
33+
compactify (bool): Whether to compactify the output. Defaults to True.
34+
35+
Example:
36+
37+
```python
38+
from bloqade import qasm2
39+
method = qasm2.loads('''
40+
OPENQASM 2.0;
41+
qreg q[2];
42+
creg c[2];
43+
h q[0];
44+
cx q[0], q[1];
45+
measure q[0] -> c[0];
46+
''')
47+
```
48+
"""
49+
return QASM2(dialects or main).loads(
50+
qasm,
51+
kernel_name=kernel_name,
52+
globals=globals,
53+
file=file,
54+
lineno_offset=lineno_offset,
55+
col_offset=col_offset,
56+
compactify=compactify,
57+
)
58+
59+
60+
def loadfile(
61+
qasm_file: str,
62+
*,
63+
kernel_name: str = "main",
64+
dialects: ir.DialectGroup | None = None,
65+
globals: dict[str, Any] | None = None,
66+
file: str | None = None,
67+
lineno_offset: int = 0,
68+
col_offset: int = 0,
69+
compactify: bool = True,
70+
) -> ir.Method[[], None]:
71+
"""Loads a QASM2 file and returns the corresponding kernel object. See also `loads`.
72+
73+
Args:
74+
qasm_file (str): The QASM2 file to load.
75+
76+
Keyword Args:
77+
kernel_name (str): The name of the kernel to load. Defaults to "main".
78+
dialects (ir.DialectGroup | None): The dialects to use. Defaults to `qasm2.main`.
79+
returns (str | None): The return type of the kernel. Defaults to None.
80+
globals (dict[str, Any] | None): The global variables to use. Defaults to None.
81+
file (str | None): The file name for error reporting. Defaults to None.
82+
lineno_offset (int): The line number offset for error reporting. Defaults to 0.
83+
col_offset (int): The column number offset for error reporting. Defaults to 0.
84+
compactify (bool): Whether to compactify the output. Defaults to True.
85+
"""
86+
with open(qasm_file, "r") as f:
87+
qasm = f.read()
88+
return loads(
89+
qasm,
90+
kernel_name=kernel_name,
91+
dialects=dialects,
92+
globals=globals,
93+
file=file,
94+
lineno_offset=lineno_offset,
95+
col_offset=col_offset,
96+
compactify=compactify,
97+
)

0 commit comments

Comments
 (0)