|
| 1 | +from itertools import repeat |
| 2 | +from typing import Any, TypeVar |
| 3 | + |
| 4 | +from bloqade.geometry.dialects import grid |
| 5 | +from kirin.dialects import ilist |
| 6 | + |
| 7 | +from bloqade.shuttle import action, gate, schedule, spec |
| 8 | +from bloqade.shuttle.prelude import move, tweezer |
| 9 | + |
| 10 | +from .asserts import assert_sorted |
| 11 | + |
| 12 | + |
| 13 | +def get_spec(num_x: int, num_y: int, spacing: float = 10.0) -> spec.ArchSpec: |
| 14 | + """Create a static trap spec with a single zone. compatible with the stdlib |
| 15 | +
|
| 16 | + Args: |
| 17 | + num_x (int): Number of traps in the x direction. |
| 18 | + num_y (int): Number of traps in the y direction. |
| 19 | + spacing (float): Spacing between traps in both directions. Default is 10.0. |
| 20 | +
|
| 21 | + Returns: |
| 22 | + spec.Spec: A specification object containing the layout with a single zone. |
| 23 | +
|
| 24 | + """ |
| 25 | + x_spacing = tuple(repeat(spacing, num_x - 1)) |
| 26 | + y_spacing = tuple(repeat(spacing, num_y - 1)) |
| 27 | + |
| 28 | + return spec.ArchSpec( |
| 29 | + layout=spec.Layout( |
| 30 | + static_traps={"traps": grid.Grid(x_spacing, y_spacing, 0.0, 0.0)}, |
| 31 | + fillable=set(["traps"]), |
| 32 | + ) |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +NumX = TypeVar("NumX") |
| 37 | +NumY = TypeVar("NumY") |
| 38 | + |
| 39 | + |
| 40 | +@tweezer |
| 41 | +def single_zone_move_cz( |
| 42 | + zone: grid.Grid[Any, Any], |
| 43 | + ctrl_x_ids: ilist.IList[int, NumX], |
| 44 | + ctrl_y_ids: ilist.IList[int, NumY], |
| 45 | + qarg_x_ids: ilist.IList[int, NumX], |
| 46 | + qarg_y_ids: ilist.IList[int, NumY], |
| 47 | + shift_x: float, |
| 48 | + shift_y: float, |
| 49 | +): |
| 50 | + assert len(ctrl_x_ids) == len( |
| 51 | + qarg_x_ids |
| 52 | + ), "Control and target x indices must have the same length." |
| 53 | + |
| 54 | + assert len(ctrl_y_ids) == len( |
| 55 | + qarg_y_ids |
| 56 | + ), "Control and target y indices must have the same length." |
| 57 | + |
| 58 | + assert_sorted(ctrl_x_ids) |
| 59 | + assert_sorted(ctrl_y_ids) |
| 60 | + assert_sorted(qarg_x_ids) |
| 61 | + assert_sorted(qarg_y_ids) |
| 62 | + |
| 63 | + start = grid.sub_grid(zone, ctrl_x_ids, ctrl_y_ids) |
| 64 | + target_atoms = grid.sub_grid(zone, qarg_x_ids, qarg_y_ids) |
| 65 | + |
| 66 | + end = grid.shift(target_atoms, shift_x, shift_y) |
| 67 | + first_pos = grid.shift(start, shift_x, shift_y) |
| 68 | + second_pos = grid.from_positions(grid.get_xpos(end), grid.get_ypos(first_pos)) |
| 69 | + |
| 70 | + action.set_loc(start) |
| 71 | + action.turn_on(action.ALL, action.ALL) |
| 72 | + action.move(first_pos) |
| 73 | + action.move(second_pos) |
| 74 | + action.move(end) |
| 75 | + |
| 76 | + |
| 77 | +@move |
| 78 | +def default_move_cz_impl( |
| 79 | + zone: grid.Grid[Any, Any], |
| 80 | + x_shift: float, |
| 81 | + y_shift: float, |
| 82 | + ctrl_x_ids: ilist.IList[int, NumX], |
| 83 | + ctrl_y_ids: ilist.IList[int, NumY], |
| 84 | + qarg_x_ids: ilist.IList[int, NumX], |
| 85 | + qarg_y_ids: ilist.IList[int, NumY], |
| 86 | +): |
| 87 | + """Move atoms from the start ids and run cz gate with the atoms at the end ids. |
| 88 | +
|
| 89 | + Args: |
| 90 | + zone (grid.Grid[Any, Any]): The grid representing the trap layout (zone) in which atoms are moved. |
| 91 | + x_shift (float): The amount to shift atoms in the x direction during the move. |
| 92 | + y_shift (float): The amount to shift atoms in the y direction during the move. |
| 93 | + ctrl_x_ids (ilist.IList[int, NumX]): The x-indices of the starting positions. |
| 94 | + ctrl_y_ids (ilist.IList[int, NumY]): The y-indices of the starting positions. |
| 95 | + qarg_x_ids (ilist.IList[int, NumX]): The x-indices of the ending positions. |
| 96 | + qarg_y_ids (ilist.IList[int, NumY]): The y-indices of the ending positions. |
| 97 | + """ |
| 98 | + if len(ctrl_x_ids) < 1 or len(qarg_x_ids) < 1: |
| 99 | + return |
| 100 | + |
| 101 | + fwd_kernel = schedule.device_fn( |
| 102 | + single_zone_move_cz, |
| 103 | + ilist.range(len(ctrl_x_ids)), |
| 104 | + ilist.range(len(ctrl_y_ids)), |
| 105 | + ) |
| 106 | + bwd_kernel = schedule.reverse(fwd_kernel) |
| 107 | + |
| 108 | + fwd_kernel(zone, ctrl_x_ids, ctrl_y_ids, qarg_x_ids, qarg_y_ids, x_shift, y_shift) |
| 109 | + gate.top_hat_cz(zone) |
| 110 | + bwd_kernel(zone, qarg_x_ids, qarg_y_ids, ctrl_x_ids, ctrl_y_ids, x_shift, y_shift) |
| 111 | + |
| 112 | + |
| 113 | +DEFAULT_X_SHIFT = 2.0 |
| 114 | +DEFAULT_Y_SHIFT = 2.0 |
| 115 | + |
| 116 | + |
| 117 | +@move |
| 118 | +def cz_move( |
| 119 | + ctrl_x_ids: ilist.IList[int, NumX], |
| 120 | + ctrl_y_ids: ilist.IList[int, NumY], |
| 121 | + qarg_x_ids: ilist.IList[int, NumX], |
| 122 | + qarg_y_ids: ilist.IList[int, NumY], |
| 123 | +): |
| 124 | + zone = spec.get_static_trap(zone_id="traps") |
| 125 | + default_move_cz_impl( |
| 126 | + zone, |
| 127 | + DEFAULT_X_SHIFT, |
| 128 | + DEFAULT_Y_SHIFT, |
| 129 | + ctrl_x_ids, |
| 130 | + ctrl_y_ids, |
| 131 | + qarg_x_ids, |
| 132 | + qarg_y_ids, |
| 133 | + ) |
0 commit comments