Skip to content

Commit 4174167

Browse files
committed
Added parser for gateset.
1 parent 1fbffe2 commit 4174167

File tree

7 files changed

+73
-36
lines changed

7 files changed

+73
-36
lines changed

c3/c3objs.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ def __init__(self, name, desc="", comment="", params={}):
2525
self.name = name
2626
self.desc = desc
2727
self.comment = comment
28-
self.params = params
28+
self.params = {}
2929
for name, par in params.items():
30-
self.params[name] = Quantity(**par)
30+
if isinstance(par, Quantity):
31+
self.params[name] = par
32+
else:
33+
self.params[name] = Quantity(**par)
3134

3235

3336
class Quantity:
@@ -56,8 +59,11 @@ class Quantity:
5659
def __init__(self, value, min_val, max_val, unit="undefined", symbol=r"\alpha"):
5760
if unit[-3:] == "2pi":
5861
pref = 2 * np.pi
62+
elif unit[-2:] == "pi":
63+
pref = np.pi
5964
else:
6065
pref = 1
66+
self.pref = pref
6167
self.offset = np.array(min_val * pref)
6268
self.scale = np.abs(np.array(max_val * pref) - np.array(min_val * pref))
6369
try:
@@ -76,6 +82,16 @@ def __init__(self, value, min_val, max_val, unit="undefined", symbol=r"\alpha"):
7682
self.shape = ()
7783
self.length = 1
7884

85+
def asdict(self):
86+
pref = self.pref
87+
return {
88+
"value": self.numpy(),
89+
"min_val": self.offset / pref,
90+
"max_val": (self.scale + self.offset) / pref,
91+
"unit": self.unit,
92+
"symbol": self.symbol
93+
}
94+
7995
def __add__(self, other):
8096
return self.numpy() + other
8197

@@ -147,6 +163,7 @@ def set_value(self, val):
147163
if np.any(tmp < -1) or np.any(tmp > 1):
148164
# TODO choose which error to raise
149165
# raise Exception(f"Value {val} out of bounds for quantity.")
166+
print(f"Value {val} out of bounds for quantity.")
150167
raise ValueError
151168
# TODO if we want we can extend bounds when force flag is given
152169
else:

c3/signal/gates.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def __init__(
240240
channels: list = [],
241241
t_start: np.float64 = 0.0,
242242
t_end: np.float64 = 0.0,
243-
):
243+
):
244244
self.name = name
245245
self.t_start = t_start
246246
self.t_end = t_end

c3/signal/pulse.py

+22-24
Original file line numberDiff line numberDiff line change
@@ -33,49 +33,47 @@ def __init__(
3333
comment: str = " ",
3434
params: dict = {},
3535
shape: types.FunctionType = None,
36-
):
37-
super().__init__(
38-
name=name,
39-
desc=desc,
40-
comment=comment,
41-
params=params,
42-
)
36+
):
4337
self.shape = shape
44-
if 'amp' not in params:
45-
params['amp'] = Qty(
38+
params_default = {
39+
'amp': Qty(
4640
value=0.0,
4741
min_val=-1.0,
4842
max_val=+1.0,
4943
unit="V"
50-
)
51-
if 'delta' not in params:
52-
params['delta'] = Qty(
44+
),
45+
'delta': Qty(
5346
value=0.0,
5447
min_val=-1.0,
5548
max_val=+1.0,
5649
unit="V"
57-
)
58-
if 'freq_offset' not in params:
59-
params['freq_offset'] = Qty(
50+
),
51+
'freq_offset': Qty(
6052
value=0.0,
6153
min_val=-1.0,
6254
max_val=+1.0,
6355
unit='Hz 2pi'
64-
)
65-
if 'xy_angle' not in params:
66-
params['xy_angle'] = Qty(
56+
),
57+
'xy_angle': Qty(
6758
value=0.0,
6859
min_val=-1.0,
6960
max_val=+1.0,
7061
unit='rad'
71-
)
72-
if 't_final' not in params:
73-
params['t_final'] = Qty(
62+
),
63+
't_final': Qty(
7464
value=0.0,
7565
min_val=-1.0,
7666
max_val=+1.0,
7767
unit="s"
7868
)
69+
}
70+
params_default.update(params)
71+
super().__init__(
72+
name=name,
73+
desc=desc,
74+
comment=comment,
75+
params=params_default,
76+
)
7977

8078
def get_shape_values(self, ts, t_before=None):
8179
"""Return the value of the shape function at the specified times.
@@ -96,7 +94,7 @@ def get_shape_values(self, ts, t_before=None):
9694
vals = self.shape(ts, self.params)
9795
mask = tf.cast(ts < t_final.numpy(), tf.float64)
9896
# With the offset, we make sure the signal starts with amplitude 0.
99-
return vals*mask
97+
return vals * mask
10098

10199

102100
@comp_reg_deco
@@ -109,10 +107,10 @@ def __init__(
109107
desc: str = " ",
110108
comment: str = " ",
111109
params: dict = {},
112-
):
110+
):
113111
super().__init__(
114112
name=name,
115113
desc=desc,
116114
comment=comment,
117115
params=params,
118-
)
116+
)

c3/system/chip.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,6 @@ def __init__(self, **props):
315315
self.hamiltonian_func = h_func
316316
else:
317317
self.hamiltonian_func = hamiltonians[h_func]
318-
params = props.pop("params", {})
319-
self.params = params
320-
for name, par in params.items():
321-
self.params[name] = Quantity(**par)
322318
super().__init__(**props)
323319
self.Hs = {}
324320

@@ -335,6 +331,7 @@ class Coupling(LineComponent):
335331
all physical components coupled via this specific coupling
336332
337333
"""
334+
338335
def init_Hs(self, opers_list):
339336
self.Hs['strength'] = tf.constant(
340337
self.hamiltonian_func(opers_list), dtype=tf.complex128

c3/utils/parsers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ def create_model(filepath: str) -> Model:
6363
line_components.append(
6464
chip.Coupling(**props)
6565
)
66-
print(f"Creating coupling with\n {props}")
6766
for name, props in cfg["Drives"].items():
6867
props.update({"name": name})
6968
line_components.append(
@@ -91,8 +90,9 @@ def create_gateset(filepath: str) -> GateSet:
9190
for key, gate in cfg.items():
9291
if "mapto" in gate.keys():
9392
instr = copy.deepcopy(gateset.instructions[gate["mapto"]])
93+
instr.name = key
9494
for drive_chan, comps in gate["drive_channels"].items():
95-
for comp, props in comps:
95+
for comp, props in comps.items():
9696
for par, val in props["params"].items():
9797
instr.comps[drive_chan][comp].params[par].set_value(val)
9898
else:

test/instructions.cfg

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"d1":{
5454
"gaussian": {
5555
"params":{
56-
"xy_angle": 1.5617
56+
"xy_angle": 0.5
5757
}
5858
}
5959
}
@@ -65,7 +65,7 @@
6565
"d1":{
6666
"gaussian": {
6767
"params":{
68-
"xy_angle": 3.1415
68+
"xy_angle": 1
6969
}
7070
}
7171
}
@@ -77,7 +77,7 @@
7777
"d1":{
7878
"gaussian": {
7979
"params":{
80-
"xy_angle": 4.7
80+
"xy_angle": -0.5
8181
}
8282
}
8383
}

test/test_quantity.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Unit tests for Quantity class"""
2+
3+
import hjson
4+
from c3.c3objs import Quantity
5+
6+
amp = Quantity(value=0.0, min_val=-1.0, max_val=+1.0, unit="V")
7+
amp_dict = {
8+
'value': 0.0,
9+
'min_val': -1.0,
10+
'max_val': 1.0,
11+
'unit': 'V',
12+
'symbol': '\\alpha'
13+
}
14+
15+
16+
def test_qty_asdict() -> None:
17+
assert amp.asdict() == amp_dict
18+
19+
20+
def test_qty_write_cfg() -> None:
21+
print(hjson.dumps(amp.asdict()))
22+
23+
24+
def test_qty_read_cfg() -> None:
25+
assert Quantity(**amp_dict).asdict() == amp.asdict()

0 commit comments

Comments
 (0)