Skip to content

Commit 6c4f71c

Browse files
authored
Updating GHZ Demo. (#93)
* using more spec api in demo * fill gate locations from middle index
1 parent 51d5e1d commit 6c4f71c

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

demo/log_depth_ghz.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
11
import math
2-
from typing import Any, Literal, TypeVar
2+
from typing import Any, TypeVar
33

44
from bloqade.geometry.dialects import grid
55
from kirin.dialects import ilist
66

7-
from bloqade.shuttle import action, gate, init, measure, schedule, spec
7+
from bloqade.shuttle import action, gate, init, schedule, spec
88
from bloqade.shuttle.prelude import move, tweezer
9-
from bloqade.shuttle.visualizer import MatplotlibRenderer, PathVisualizer
9+
from bloqade.shuttle.visualizer import PathVisualizer
1010

1111
NMove = TypeVar("NMove")
1212

1313

1414
@tweezer
1515
def entangle_move(
16-
mem_zone: grid.Grid[Any, Literal[1]],
17-
gate_zone: grid.Grid[NMove, Literal[2]],
1816
ctrl_ids: ilist.IList[int, NMove],
1917
qarg_ids: ilist.IList[int, NMove],
2018
gate_ids: ilist.IList[int, NMove],
2119
):
2220

21+
mem_zone = spec.get_static_trap(zone_id="mem")
22+
gate_zone = spec.get_special_grid(grid_id="gate")
23+
2324
mem_y = grid.get_ypos(mem_zone)[0]
24-
ctrl_start = grid.get_xpos(grid.sub_grid(mem_zone, ctrl_ids, [0]))
25-
qarg_start = grid.get_xpos(grid.sub_grid(mem_zone, qarg_ids, [0]))
25+
ctrl_start = grid.get_xpos(mem_zone[ctrl_ids, 0])
26+
qarg_start = grid.get_xpos(mem_zone[qarg_ids, 0])
2627

27-
pos_1 = grid.from_positions(ctrl_start, [mem_y, mem_y])
28-
pos_2 = grid.shift(grid.from_positions(ctrl_start, [mem_y - 4.0, mem_y]), 2.0, 0.0)
29-
pos_3 = grid.from_positions(qarg_start, [mem_y - 4.0, mem_y])
30-
gate_pos = grid.sub_grid(gate_zone, gate_ids, [0, 1])
28+
pos_1 = grid.from_positions(ctrl_start, [mem_y, mem_y + 4.0])
29+
pos_2 = grid.shift(pos_1, 0.0, -4.0)
30+
pos_3 = grid.from_positions(qarg_start, grid.get_ypos(pos_2))
31+
pos_4 = grid.shift(pos_3, 0.0, -4.0)
32+
gate_pos = gate_zone[gate_ids, :]
3133

3234
action.set_loc(pos_1)
3335
action.turn_on(action.ALL, [0])
3436
action.move(pos_2)
3537
action.move(pos_3)
3638
action.turn_on([], [1])
39+
action.move(pos_4)
3740
action.move(gate_pos)
3841

3942

@@ -45,33 +48,28 @@ def apply_h(zone: grid.Grid[Any, Any]):
4548
gate.global_r(0.0, -math.pi / 2.0)
4649

4750

48-
@move
49-
def apply_cx(gate_zone: grid.Grid[Any, Any]):
50-
# TODO: This is wrong and needs to be fixed.
51-
gate.global_r(0.0, math.pi)
52-
gate.top_hat_cz(gate_zone, upper_buffer=3.0, lower_buffer=3.0)
53-
gate.global_r(0.0, -math.pi)
54-
55-
5651
@move
5752
def run_entangle_move(
58-
mem_zone: grid.Grid[Any, Literal[1]],
59-
gate_zone: grid.Grid[Any, Literal[2]],
6053
ctrl_ids: ilist.IList[int, NMove],
6154
qarg_ids: ilist.IList[int, NMove],
6255
gate_ids: ilist.IList[int, NMove],
6356
):
6457

58+
gate_zone = spec.get_special_grid(grid_id="gate")
59+
mem_zone = spec.get_static_trap(zone_id="mem")
60+
6561
num = len(ctrl_ids)
6662
xtones = ilist.range(num)
6763
ytones = [0, 1]
6864

6965
dtask = schedule.device_fn(entangle_move, xtones, ytones)
7066
rev_dtask = schedule.reverse(dtask)
7167

72-
dtask(mem_zone, gate_zone, ctrl_ids, qarg_ids, gate_ids)
73-
apply_cx(gate_zone)
74-
rev_dtask(mem_zone, gate_zone, ctrl_ids, qarg_ids, gate_ids)
68+
gate.local_r(0.0, math.pi, mem_zone[qarg_ids, 0])
69+
dtask(ctrl_ids, qarg_ids, gate_ids)
70+
gate.top_hat_cz(gate_zone)
71+
rev_dtask(ctrl_ids, qarg_ids, gate_ids)
72+
gate.local_r(0.0, -math.pi, mem_zone[qarg_ids, 0])
7573

7674

7775
N = TypeVar("N")
@@ -106,7 +104,7 @@ def ghz_prep_steps(
106104
):
107105
jobs = []
108106
layers = get_layers(qubit_ids)
109-
107+
mid = gate_width // 2
110108
for i in range(len(layers)):
111109
ctrl_ids = layers[i][0]
112110
qarg_ids = layers[i][1]
@@ -119,8 +117,12 @@ def ghz_prep_steps(
119117
if end > n_gates:
120118
end = n_gates
121119

120+
layer_size = end - start
121+
gate_start = mid - (layer_size // 2)
122+
gate_end = gate_start + layer_size
123+
122124
jobs = jobs + [
123-
(ctrl_ids[start:end], qarg_ids[start:end], range(end - start))
125+
(ctrl_ids[start:end], qarg_ids[start:end], range(gate_start, gate_end))
124126
]
125127

126128
return jobs
@@ -129,20 +131,20 @@ def ghz_prep_steps(
129131
@move
130132
def run_prep_steps(
131133
qubit_ids: ilist.IList[int, N],
132-
gate: grid.Grid[Any, Literal[2]],
133-
mem: grid.Grid[Any, Literal[1]],
134134
):
135135

136-
gate_shape = grid.shape(gate)
136+
gate_zone = spec.get_special_grid(grid_id="gate")
137+
mem_zone = spec.get_static_trap(zone_id="mem")
138+
gate_shape = grid.shape(gate_zone)
137139
gate_width = gate_shape[0]
138140

139141
jobs = ghz_prep_steps(qubit_ids, gate_width)
140-
apply_h(grid.sub_grid(mem, [qubit_ids[0]], [0]))
142+
apply_h(mem_zone[qubit_ids[0], 0])
141143
for i in range(len(jobs)):
142144
ctrl_ids = jobs[i][0]
143145
qarg_ids = jobs[i][1]
144146
gate_ids = jobs[i][2]
145-
run_entangle_move(mem, gate, ctrl_ids, qarg_ids, gate_ids)
147+
run_entangle_move(ctrl_ids, qarg_ids, gate_ids)
146148

147149

148150
def run_ghz():
@@ -167,7 +169,6 @@ def run_ghz():
167169
gate_shift = mid_mem - mid_gate
168170

169171
gate = gate.shift(gate_shift, 0.0)
170-
171172
spec_value = spec.ArchSpec(
172173
layout=spec.Layout(
173174
static_traps={
@@ -185,17 +186,16 @@ def run_ghz():
185186
@move
186187
def main():
187188
init.fill([spec.get_static_trap(zone_id="mem")])
188-
run_prep_steps([1, 2, 4, 5, 7, 9, 14, 28, 29, 32], gate, mem) # type: ignore
189-
return measure.measure((mem,))
189+
run_prep_steps([1, 2, 4, 5, 7, 9, 14, 28, 29, 32]) # type: ignore
190+
# return measure.measure((mem,))
190191

191192
return main, spec_value
192193

193194

194195
def run_plotter():
195196
main, spec_value = run_ghz()
196197

197-
renderer = MatplotlibRenderer()
198-
PathVisualizer(main.dialects, renderer=renderer, arch_spec=spec_value).run(main, ())
198+
PathVisualizer(main.dialects, arch_spec=spec_value).run(main, args=())
199199

200200

201201
if __name__ == "__main__":

0 commit comments

Comments
 (0)