You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In light of more tutorials coming in (see #260 ) I figured
re-structuring that section of the docs makes sense.
This also adds a very short example showing how you can manually include
noise in a squin kernel.
That example requires
QuEraComputing/bloqade-circuit#441 to be merged
and released though.
Copy file name to clipboardExpand all lines: docs/digital/dialects_and_kernels.md
+53-7Lines changed: 53 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,39 @@
1
1
# Dialects and kernels
2
2
3
+
Bloqade provides a set of pre-defined dialects, with which you can write your programs and circuits.
4
+
5
+
Once you have your kernel, you can inspect their intermediate representation (IR), apply different optimizations using [compiler passes](../quick_start/circuits/compiler_passes/index.md), or run them on a [(simulator) device](./simulator_device/simulator_device.md).
6
+
3
7
!!! info
4
8
A **kernel** function is a piece of code that runs on specialized hardware such as a quantum computer.
5
9
6
10
A **dialect** is a domain-specific language (DSL) with which you can write such a kernel.
7
11
Each dialect comes with a specific set of statements and instructions you can use in order to write your program.
8
12
9
-
Bloqade provides a set of pre-defined dialects, with which you can write your programs and circuits.
10
13
11
-
Once you have your kernel, you can inspect their intermediate representation (IR), apply different optimizations using [compiler passes](../quick_start/circuits/compiler_passes/index.md), or run them on a [(simulator) device](./simulator_device/simulator_device.md).
14
+
When running code that targets a specialized execution environment, there are typically several layers involved.
15
+
At the surface, the programmer writes functions in a syntax that may resemble a host language (e.g., Python), but is actually expressed in a dialect— a domain-specific variant with its own semantics.
16
+
A decorator marks these functions so they can be intercepted before normal host-language execution.
17
+
All dialects can be used by decorating a function.
18
+
19
+
!!! info
20
+
Here's a short primer on decorators: a decorator in Python is simply a function (or any callable really) that takes in another function as argument and returns yet another function (callable).
21
+
Usually, the returned function will be a modified version of the input.
22
+
Decorators are used with the `@` syntax.
23
+
24
+
25
+
Instead of running directly, the kernel function body is parsed and translated (lowered) into an intermediate representation (IR).
26
+
This IR can be manipulated (e.g. to perform optimizations) and can later be executed by an interpreter that understands the dialect's semantics.
27
+
The interpreter uses an internal instruction set to execute the code on the intended backend, which may be a simulator, virtual machine, or physical device.
28
+
This separation lets developers write high-level, expressive code while the interpreter ensures it runs correctly in the target environment.
29
+
[QuEra's Kirin](https://queracomputing.github.io/kirin/latest/) infrastructure uses this concept by defining custom dialects that are tailored towards the needs to program neutral atom quantum computers.
30
+
While the dialects are not python syntax, Kirin still uses the python interpreter to execute the code.
31
+
32
+
33
+
!!! note
34
+
It is important to understand that when you are writing a kernel function in a dialect you are generally **not writing Python** code, even though it looks a lot like it.
35
+
Therefore, kernel functions can usually not be called directly.
36
+
Think of this as trying to execute another programming language with the Python interpreter: of course, that will error.
12
37
13
38
14
39
# Available dialects
@@ -128,18 +153,39 @@ main.print()
128
153
129
154
## squin
130
155
131
-
This dialect is, in a sense, more expressive than the qasm2 dialects: it allows you to specify operators rather than just gate applications.
132
-
That can be useful if you're trying to e.g. simulate a Hamiltonian time evolution.
133
-
134
156
!!! warning
135
157
The squin dialect is in an early stage of development.
136
158
Expect substantial changes to it in the near future.
137
159
160
+
161
+
This dialect is, in a sense, more expressive than the qasm2 dialects: it allows you to specify operators rather than just gate applications.
162
+
That can be useful if you're trying to e.g. simulate a Hamiltonian time evolution.
163
+
164
+
For simple circuits, however, gate applications also have short-hand standard library definitions defined in the `squin.gate` submodule.
138
165
Here's a short example:
139
166
140
167
```python
141
168
from bloqade import squin
142
169
170
+
@squin.kernel
171
+
defmain():
172
+
q = squin.qubit.new(2)
173
+
squin.gate.h(q[0])
174
+
squin.gate.cx(q[0], q[1])
175
+
return squin.qubit.measure(q)
176
+
177
+
# have a look at the IR
178
+
main.print()
179
+
```
180
+
181
+
182
+
As mentioned above, you can also build up more complex "operators" that are then applied to any number of qubits.
183
+
To show how you can do that, here's an example on how to write the above kernel defining the gates as separate operators.
184
+
This isn't exactly a practical use-case, but serves as an example.
0 commit comments