Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor builder #364

Merged
merged 95 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
238ff5b
refactor builder
Roger-luo Aug 2, 2023
13f6722
keep refactoring
Roger-luo Aug 4, 2023
1e6d54d
flatten works
Roger-luo Aug 4, 2023
3f9de26
cleanup
Roger-luo Aug 4, 2023
59cccc4
add a sketch of compile pipeline
Roger-luo Aug 4, 2023
752ef83
support device fn
Roger-luo Aug 5, 2023
f71fe0f
setup a basic lexer like codegen for builder
Roger-luo Aug 5, 2023
6f0bae1
Merge branch 'main' into roger/refactor-builder
weinbe58 Aug 6, 2023
102d00a
waveform and address is working
Roger-luo Aug 7, 2023
fd8a2bc
reexporting objects to `bloqade.ir` and `bloqade`
weinbe58 Aug 7, 2023
5dd92ee
adding `BoundedBravais` and `ParallelRegister` to imports.
weinbe58 Aug 7, 2023
24081bc
adding `create`/`copy` interface to `BuilderStream`
weinbe58 Aug 7, 2023
5dd4bc1
wrap up whatever I have
Roger-luo Aug 7, 2023
cbfbe77
docstring.
weinbe58 Aug 8, 2023
2a10ff8
Merge branch 'roger/refactor-builder' of https://github.com/Happy-Dio…
weinbe58 Aug 8, 2023
72fa7bf
renambing fields of pulse and sequence
weinbe58 Aug 9, 2023
7ffac9e
implmenting compiler for pulse sequence.
weinbe58 Aug 9, 2023
bdc5e51
fix routing.
weinbe58 Aug 9, 2023
967e0db
adding better error messages for not implemented backends.
weinbe58 Aug 9, 2023
c558a48
stashing changes.
weinbe58 Aug 9, 2023
0acd268
adding `static_assign` functionality to scalars
weinbe58 Aug 9, 2023
9034624
moving from just kargs to storing parameters as Scalar variables.
weinbe58 Aug 9, 2023
fae4ec0
stash
weinbe58 Aug 9, 2023
2f4cfb0
Merge branch 'main' into roger/refactor-builder
weinbe58 Aug 9, 2023
e438102
rename
weinbe58 Aug 9, 2023
1b3f618
fixing typo
weinbe58 Aug 9, 2023
4dfb09c
adding unit tests for
weinbe58 Aug 9, 2023
8a2cd50
rename class.
weinbe58 Aug 9, 2023
efbc7d1
moving visitor methods for primatives.
weinbe58 Aug 9, 2023
776c2ac
update dev
weinbe58 Aug 9, 2023
0fec268
Merge branch 'main' into roger/refactor-builder
weinbe58 Aug 9, 2023
b803134
Merge branch 'main' into roger/refactor-builder
weinbe58 Aug 10, 2023
021d1f6
new compiler for sequence.
weinbe58 Aug 10, 2023
0a5e86a
Merge branch 'roger/refactor-builder' of https://github.com/Happy-Dio…
weinbe58 Aug 10, 2023
93ee5c1
removing main2.py
weinbe58 Aug 10, 2023
1e0ab61
rename modules.
weinbe58 Aug 10, 2023
c3fd6c4
creating visitor for scalar AST, removing `static_assign` implement a…
weinbe58 Aug 10, 2023
2343d48
adding canonicalization
weinbe58 Aug 10, 2023
a925392
reorder imports.
weinbe58 Aug 10, 2023
3b7b556
adding match arguments and serializers
weinbe58 Aug 11, 2023
137a838
adding JSON serialization for Scalar IR.
weinbe58 Aug 11, 2023
14a652e
moving ir compiler into compiler subpackage
weinbe58 Aug 11, 2023
88e1786
creating stream submodule and json submodule.
weinbe58 Aug 11, 2023
8d76448
adding Sample node for builder.
weinbe58 Aug 11, 2023
625f18f
rename `checkpoint` to `coeff`
weinbe58 Aug 11, 2023
5322915
adding json compiler for waveforms.
weinbe58 Aug 11, 2023
38a8990
adding json encoder for builder.
weinbe58 Aug 11, 2023
674a149
task sketch.
weinbe58 Aug 11, 2023
09485f3
adding `apply` builder method for sequence objects.
weinbe58 Aug 14, 2023
bc152c5
adding explicit check for `Waveform` type.
weinbe58 Aug 14, 2023
2495852
adding serialization for builder + BloqadeIR
weinbe58 Aug 14, 2023
7338e18
fixing parallelize option for submit/flatten routes.
weinbe58 Aug 14, 2023
d8ff7bd
commenting out old compiler. Adding register compiler
weinbe58 Aug 14, 2023
c72d431
setting up compiler trait.
weinbe58 Aug 14, 2023
512d3ac
fixing compiler after refactoring circular imports
weinbe58 Aug 14, 2023
5381c92
fixing imports
weinbe58 Aug 15, 2023
b9a6da5
adding JSON compiler to builder.
weinbe58 Aug 15, 2023
6d8b88a
adding compiler traits.
weinbe58 Aug 15, 2023
ed33618
compiler pipeline
weinbe58 Aug 15, 2023
12d2eb3
adding ir caching option.
weinbe58 Aug 15, 2023
d720348
updating caching to happen at all levels of bloqade-IR
weinbe58 Aug 15, 2023
3af6c87
adding deserialization for bloqade-IR
weinbe58 Aug 16, 2023
c6a7ec9
refactor base backend.
weinbe58 Aug 16, 2023
0560d6a
finished draft implementation of serializer/deserializer
weinbe58 Aug 17, 2023
488e857
Khwu/refactor task (#391)
kaihsin Aug 17, 2023
9b84a0a
Merge branch 'main' into roger/refactor-builder
weinbe58 Aug 17, 2023
c631174
removing shots from options for backends.
weinbe58 Aug 17, 2023
294ad09
keyword arg for `parent`
weinbe58 Aug 17, 2023
72a6f7d
missing impl for static assignment on register IR objects.
weinbe58 Aug 17, 2023
ec07fb3
sketching bug fix.
weinbe58 Aug 17, 2023
618a3c8
fix static assign, and visit_register issue
Aug 17, 2023
9fe128f
fixing builder compiler to blqoade IR
weinbe58 Aug 17, 2023
930967c
tm
Aug 17, 2023
1bef659
merge
Aug 17, 2023
db113b5
fixing another bug in builder compiler.
weinbe58 Aug 17, 2023
e2602e6
Merge branch 'roger/refactor-builder' of https://github.com/QuEraComp…
Aug 17, 2023
5ec1ca1
remove redundant print
Aug 17, 2023
109a119
fix bugs for batches
Aug 17, 2023
86954dc
update, fix bugs for report()
Aug 17, 2023
1084fb9
fix bugs in waveform builder
Aug 17, 2023
76dcee4
fix bugs with PythonFn waveform default kwargs. fix most of the testing
Aug 17, 2023
73f382c
Khwu/rename (#399)
weinbe58 Aug 21, 2023
aff408b
adding serialization for batch. (#400)
weinbe58 Aug 21, 2023
6ba5f45
update, fix bugs in naming _compile_taskdata, recover more tests
Aug 22, 2023
bc7dfeb
add load_batch + doc for tree_depth()
Aug 22, 2023
eb2428c
fix bugs in DefaultVariable should be Scalar not real, and member sho…
Aug 22, 2023
9f473a3
fix backend api_config need to pass in with dereferences
Aug 22, 2023
6467274
Phil/save load batch (#404)
weinbe58 Aug 22, 2023
22addb4
fix bug in static_assign visit Negative waveform feed in wrong ast
Aug 22, 2023
759ea62
Merge branch 'roger/refactor-builder' into khwu/testing_v3
Aug 22, 2023
a7d690f
fixing tests
Aug 22, 2023
1ca24dc
fixing issue with pattern matching with `Poly`
weinbe58 Aug 22, 2023
50fe7ab
Khwu/testing v3 (#408)
weinbe58 Aug 22, 2023
d35c8b0
Merge branch 'main' into roger/refactor-builder
weinbe58 Aug 22, 2023
d80b934
Khwu/testing v4 (#409)
kaihsin Aug 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixing compiler after refactoring circular imports
  • Loading branch information
weinbe58 committed Aug 14, 2023
commit 512d3ac4804fcc1da16687f391657d4abc7a3852
3 changes: 1 addition & 2 deletions src/bloqade/builder2/base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from typing import Optional
from .compile.trait import CompileJSON


class Builder(CompileJSON):
class Builder:
__match_args__ = ("__parent__",)

def __init__(
Expand Down
68 changes: 41 additions & 27 deletions src/bloqade/builder2/compile/ir.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
from typing import Tuple, Union
from .stream import BuilderNode, BuilderStream
from ... import ir
from typing import Tuple, Union, TYPE_CHECKING

from ..base import Builder

from ..coupling import LevelCoupling, Rydberg, Hyperfine
from ..field import Field, Detuning, RabiAmplitude, RabiPhase
from ..spatial import SpatialModulation, Location, Uniform, Var, Scale
from ..waveform import (
WaveformPrimitive,
# Linear,
# Constant,
# Poly,
# PiecewiseConstant,
# PiecewiseLinear,
# Fn,
Slice,
Record,
)
from ..parallelize import Parallelize, ParallelizeFlatten

if TYPE_CHECKING:
from bloqade.ir import Waveform as ir_Waveform
from bloqade.ir import SpatialModulation as ir_SpatialModulation
from bloqade.ir import Field as ir_Field
from bloqade.ir import Sequence as ir_Sequence
from bloqade.ir import ParallelRegister as ir_ParallelRegister
from bloqade.ir import AtomArrangement as ir_AtomArrangement
from .stream import BuilderNode


class BuilderCompiler:
def __init__(self, ast: Builder) -> None:
from .stream import BuilderStream

self.stream = BuilderStream.create(ast)


class SequenceCompiler(BuilderCompiler):
def read_address(self) -> Tuple[LevelCoupling, Field, BuilderNode]:
def read_address(self) -> Tuple[LevelCoupling, Field, "BuilderNode"]:
spatial = self.stream.eat([Location, Uniform, Var], [Scale])
curr = spatial

Expand All @@ -52,9 +56,9 @@ def read_address(self) -> Tuple[LevelCoupling, Field, BuilderNode]:
else: # only spatial is updated
return (None, None, spatial)

def read_waveform(self, head: BuilderNode) -> Tuple[ir.Waveform, BuilderNode]:
def read_waveform(self, head: "BuilderNode") -> Tuple["ir_Waveform", "BuilderNode"]:
curr = head
waveform: ir.Waveform = head.node.__bloqade_ir__()
waveform = head.node.__bloqade_ir__()
curr = curr.next
while curr is not None:
match curr.node:
Expand All @@ -70,8 +74,10 @@ def read_waveform(self, head: BuilderNode) -> Tuple[ir.Waveform, BuilderNode]:
return waveform, curr

def read_spatial_modulation(
self, head: BuilderNode
) -> Tuple[ir.SpatialModulation, BuilderNode]:
self, head: "BuilderNode"
) -> Tuple["ir_SpatialModulation", "BuilderNode"]:
import bloqade.ir as ir

curr = head
spatial_modulation = None
scaled_locations = ir.ScaledLocations({})
Expand All @@ -95,22 +101,26 @@ def read_spatial_modulation(
else:
return spatial_modulation, curr

def read_field(self, head) -> Tuple[ir.Field, BuilderNode]:
def read_field(self, head) -> "ir_Field":
from bloqade.ir import Field

sm, curr = self.read_spatial_modulation(head)
wf, curr = self.read_waveform(curr)
return ir.Field({sm: wf}), curr
wf, _ = self.read_waveform(curr)
return Field({sm: wf})

def compile(self) -> "ir_Sequence":
from bloqade.ir import Sequence, Pulse, Field

def compile(self) -> ir.Sequence:
coupling_builder, field_builder, spatial_head = self.read_address()

sequence = ir.Sequence({})
sequence = Sequence({})
coupling_name = coupling_builder.__bloqade_ir__()
field_name = field_builder.__bloqade_ir__()

pulse = sequence.pulses.get(coupling_name, ir.Pulse({}))
field = pulse.fields.get(field_name, ir.Field({}))
pulse = sequence.pulses.get(coupling_name, Pulse({}))
field = pulse.fields.get(field_name, Field({}))

new_field, _ = self.read_field(spatial_head)
new_field = self.read_field(spatial_head)
field = field.add(new_field)

while True:
Expand All @@ -121,24 +131,28 @@ def compile(self) -> ir.Sequence:
sequence.pulses[coupling_name] = pulse
# create/access new pulse
coupling_name = coupling_builder.__bloqade_ir__()
pulse = sequence.pulses.get(coupling_name, ir.Pulse({}))
pulse = sequence.pulses.get(coupling_name, Pulse({}))

if field_builder is not None:
# update old field
pulse.fields[field_name] = field
# create/access new field
field_name = field_builder.__bloqade_ir__()
field = pulse.fields.get(field_name, ir.Field({}))
field = pulse.fields.get(field_name, Field({}))

if spatial_head is None:
break

new_field, _ = self.read_field(spatial_head)
new_field = self.read_field(spatial_head)
field = field.add(new_field)

return sequence


class RegisterCompiler(BuilderCompiler):
def compile(self) -> Union[ir.AtomArrangement, ir.ParallelRegister]:
def compile(self) -> Union["ir_AtomArrangement", "ir_ParallelRegister"]:
from bloqade.ir import ParallelRegister

# register is always head of the stream
register_block = self.stream.read()

Expand All @@ -148,6 +162,6 @@ def compile(self) -> Union[ir.AtomArrangement, ir.ParallelRegister]:

if parallel_options is not None:
parallel_options = parallel_options.node
return ir.ParallelRegister(register, parallel_options._cluster_spacing)
return ParallelRegister(register, parallel_options._cluster_spacing)

return register
33 changes: 17 additions & 16 deletions src/bloqade/builder2/compile/json.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
from ...codegen.common.json import BloqadeIRSerializer
from ..start import ProgramStart
from ..sequence_builder import SequenceBuilder
from ..base import Builder
from ..spatial import Location, Scale, Var
from ..waveform import (
Linear,
Constant,
Poly,
Fn,
Apply,
Slice,
Record,
Sample,
PiecewiseLinear,
PiecewiseConstant,
)


class BuilderSerializer(BloqadeIRSerializer):
def default(self, obj):
from ..start import ProgramStart
from ..sequence_builder import SequenceBuilder
from ..base import Builder
from ..spatial import Location, Scale, Var
from ..waveform import (
Linear,
Constant,
Poly,
Fn,
Apply,
Slice,
Record,
Sample,
PiecewiseLinear,
PiecewiseConstant,
)

match obj:
case Constant(value, duration, parent):
return {
Expand Down
11 changes: 8 additions & 3 deletions src/bloqade/builder2/compile/trait.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from ... import ir
from .ir import SequenceCompiler, RegisterCompiler
from .json import BuilderSerializer
from ...codegen.common.static_assign import StaticAssignProgram
from typing import Union
import json


class CompileJSON:
def json(self, **json_options) -> str:
from .json import BuilderSerializer

return json.dumps(self, cls=BuilderSerializer, **json_options)

def __repr__(self):
Expand All @@ -16,11 +15,15 @@ def __repr__(self):

class CompileRegister:
def compile_register(self) -> Union[ir.AtomArrangement, ir.ParallelRegister]:
from .ir import RegisterCompiler

return RegisterCompiler(self).compile()


class CompileSequence:
def compile_sequence(self):
from .ir import SequenceCompiler

return SequenceCompiler(self).compile()


Expand All @@ -31,4 +34,6 @@ def compile_program(self) -> ir.Program:

class Compile(CompileProgram):
def compile_bloqade_ir(self, **mapping):
from ...codegen.common.static_assign import StaticAssignProgram

return StaticAssignProgram(mapping).emit(self.compile_program())
27 changes: 9 additions & 18 deletions src/bloqade/builder2/spatial.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from typing import Union, Optional
from typing import Union, Optional, TYPE_CHECKING
from .waveform import WaveformAttachable
from .base import Builder, DefaultBuilderEncoder
from .base import Builder
from ..ir import Scalar

if TYPE_CHECKING:
from bloqade.ir.control.field import UniformModulation

ScalarType = Union[float, str, Scalar]


Expand All @@ -11,7 +14,10 @@ class SpatialModulation(WaveformAttachable):


class Uniform(SpatialModulation):
pass
def __bloqade_ir__(self) -> "UniformModulation":
from ..ir import Uniform

return Uniform


class Location(SpatialModulation):
Expand Down Expand Up @@ -47,18 +53,3 @@ def __init__(self, name: str, parent: Optional[Builder] = None) -> None:
assert isinstance(name, str)
super().__init__(parent)
self._name = name


class SpatialSerializer(DefaultBuilderEncoder):
def default(self, obj):
match obj:
case Uniform(parent):
return dict(type="Uniform", parent=self.default(parent))
case Var(name, parent):
return dict(type="Var", name=name, parent=self.default(parent))
case Scale(factor, parent):
return dict(type="Scale", factor=factor, parent=self.default(parent))
case Location(label, parent):
return dict(type="Location", label=label, parent=self.default(parent))
case _:
return super().default(obj)
Loading