def test_nest_3d(indep_params, dep_params): px, x, tablex = indep_params["x"] py, y, tabley = indep_params["y"] pz, z, tablez = indep_params["z"] def f(vx, vy, vz): return vx**2 + vy**2 + vz**2 pi, i, tablei = dep_params["i"] pi.get = lambda: f(px(), py(), pz()) sweep_values_x = [0, 1, 2] sweep_values_y = [5, 6, 7] sweep_values_z = [8, 9, 10] nest = Nest(Sweep(x, tablex, lambda: sweep_values_x), Sweep(y, tabley, lambda: sweep_values_y), Sweep(z, tablez, lambda: sweep_values_z), Measure(i, tablei)) meas = SweepMeasurement() meas.register_sweep(nest) interdeps = meas._interdeps assert interdeps.dependencies == { ParamSpecBase('i', 'numeric', '', ''): (ParamSpecBase('x', 'numeric', '', ''), ParamSpecBase('y', 'numeric', '', ''), ParamSpecBase('z', 'numeric', '', '')) } assert interdeps.inferences == {} assert interdeps.standalones == set()
def test_nest_3d(indep_params, dep_params): px, x, tablex = indep_params["x"] py, y, tabley = indep_params["y"] pz, z, tablez = indep_params["z"] def f(vx, vy, vz): return vx**2 + vy**2 + vz**2 pi, i, tablei = dep_params["i"] pi.get = lambda: f(px(), py(), pz()) sweep_values_x = [0, 1, 2] sweep_values_y = [5, 6, 7] sweep_values_z = [8, 9, 10] nest = Nest( Sweep(x, tablex, lambda: sweep_values_x), Sweep(y, tabley, lambda: sweep_values_y), Sweep(z, tablez, lambda: sweep_values_z), Measure(i, tablei) ) assert list(nest) == [ {"x": xval, "y": yval, "z": zval, "i": f(xval, yval, zval)} for xval, yval, zval in itertools.product( sweep_values_x, sweep_values_y, sweep_values_z) ]
def test_nest_chain(indep_params, dep_params): px, x, tablex = indep_params["x"] py, y, tabley = indep_params["y"] pi, i, tablei = dep_params["i"] pj, j, tablej = dep_params["j"] def f(vx, vy): return vx**2 + vy**3 pi.get = lambda: f(px(), py()) def g(vx, vy): return vx**2 + vy**2 pj.get = lambda: g(px(), py()) sweep_values_x = [0, 1, 2] sweep_values_y = [4, 5, 6] sweep_object = Nest( Sweep(x, tablex, lambda: sweep_values_x), Sweep(y, tabley, lambda: sweep_values_y), Chain( Measure(i, tablei), Measure(j, tablej) ) ) for xvalue in sweep_values_x: for yvalue in sweep_values_y: assert next(sweep_object) == { "x": xvalue, "y": yvalue, "i": f(xvalue, yvalue)} assert next(sweep_object) == { "x": xvalue, "y": yvalue, "j": g(xvalue, yvalue)}
def sweep(fun_or_param, set_points): if isinstance(fun_or_param, Parameter): fun = parameter_setter(fun_or_param) else: fun = fun_or_param if not callable(set_points): sweep_object = Sweep(*fun(), lambda: set_points) else: sweep_object = Sweep(*fun(), set_points) return sweep_object
def sweep(fun_or_param, set_points, paramtype: str = None): if isinstance(fun_or_param, Parameter): fun = parameter_setter(fun_or_param, paramtype=paramtype) elif isinstance(fun_or_param, SweepFunction): fun = fun_or_param else: raise ValueError("Can only sweep a QCoDeS parameter or a function " "decorated with pytopo.setter") if not callable(set_points): sweep_object = Sweep(fun, fun.parameter_table, lambda: set_points) else: sweep_object = Sweep(fun, fun.parameter_table, set_points) return sweep_object
def test_chain_simple(indep_params): px, x, tablex = indep_params["x"] py, y, tabley = indep_params["y"] sweep_values_x = [0, 1, 2] sweep_values_y = [4, 5, 6] parameter_sweep = Chain( Sweep(x, tablex, lambda: sweep_values_x), Sweep(y, tabley, lambda: sweep_values_y) ) expected_result = [{"x": value} for value in sweep_values_x] expected_result.extend([{"y": value} for value in sweep_values_y]) assert list(parameter_sweep) == expected_result
def test_sweep_parameter(indep_params): px, x, table = indep_params["x"] sweep_values = [0, 1, 2] parameter_sweep = Sweep(x, table, lambda: sweep_values) assert list(parameter_sweep) == [{"x": value} for value in sweep_values]
def test_error_no_nest_in_chain(indep_params, dep_params): px, x, tablex = indep_params["x"] py, y, tabley = indep_params["y"] pi, i, tablei = dep_params["i"] sweep_values_x = [0, 1, 2] sweep_values_y = [4, 5, 6] with pytest.raises(TypeError): Nest( Chain( Sweep(x, tablex, lambda: sweep_values_x), Sweep(y, tabley, lambda: sweep_values_y) ), Measure(i, tablei) )
def test_error_no_nest_in_measurable(indep_params, dep_params): px, x, tablex = indep_params["x"] pi, i, tablei = dep_params["i"] with pytest.raises(TypeError): Nest( Measure(i, tablei), Sweep(x, tablex, lambda: []) )
def test_chain_simple(indep_params): px, x, tablex = indep_params["x"] py, y, tabley = indep_params["y"] sweep_values_x = [0, 1, 2] sweep_values_y = [4, 5, 6] parameter_sweep = Chain(Sweep(x, tablex, lambda: sweep_values_x), Sweep(y, tabley, lambda: sweep_values_y)) meas = SweepMeasurement() meas.register_sweep(parameter_sweep) interdeps = meas._interdeps assert interdeps.dependencies == {} assert interdeps.inferences == {} assert interdeps.standalones == { ParamSpecBase('y', 'numeric', '', ''), ParamSpecBase('x', 'numeric', '', '') }
def test_interleave_1d_2d(indep_params, dep_params): px, x, tablex = indep_params["x"] py, y, tabley = indep_params["y"] pi, i, tablei = dep_params["i"] pj, j, tablej = dep_params["j"] def f(vx): return vx ** 2 pi.get = lambda: f(px()) def g(vx, vy): return vx ** 2 + vy ** 2 pj.get = lambda: g(px(), py()) sweep_values_x = [0, 1, 2] sweep_values_y = [4, 5, 6] sweep_object = Nest( Sweep(x, tablex, lambda: sweep_values_x), Chain( Measure(i, tablei), Nest( Sweep(y, tabley, lambda: sweep_values_y), Measure(j, tablej) ) ) ) meas = SweepMeasurement() meas.register_sweep(sweep_object) interdeps = meas._interdeps assert interdeps.dependencies == { ParamSpecBase('i', 'numeric', '', ''): (ParamSpecBase('x', 'numeric', '', ''),), ParamSpecBase('j', 'numeric', '', ''): (ParamSpecBase('x', 'numeric', '', ''), ParamSpecBase('y', 'numeric', '', ''))} assert interdeps.inferences == {} assert interdeps.standalones == set()
def test_sweep_parameter(indep_params): px, x, table = indep_params["x"] sweep_values = [0, 1, 2] parameter_sweep = Sweep(x, table, lambda: sweep_values) meas = SweepMeasurement() meas.register_sweep(parameter_sweep) interdeps = meas._interdeps assert interdeps.dependencies == {} assert interdeps.inferences == {} assert interdeps.standalones == {ParamSpecBase('x', 'numeric', '', '')}
def test_nest(indep_params, dep_params): px, x, tablex = indep_params["x"] pi, i, tablei = dep_params["i"] def f(value): return value**2 pi.get = lambda: f(px()) sweep_values = [0, 1, 2] nest = Nest( Sweep(x, tablex, lambda: sweep_values), Measure(i, tablei) ) assert list(nest) == [{"x": xval, "i": f(xval)} for xval in sweep_values]
def test_error_no_nest_in_chain_2(indep_params, dep_params): px, x, tablex = indep_params["x"] pi, i, tablei = dep_params["i"] pj, j, tablej = dep_params["j"] sweep_values = [0, 1, 2] sweep_object = Nest( Sweep(x, tablex, lambda: sweep_values), Chain( Measure(i, tablei) ) ) with pytest.raises(TypeError): Nest( sweep_object, Measure(j, tablej) )
def test_nest_in_chain_2_whatever(indep_params, dep_params): px, x, tablex = indep_params["x"] pi, i, tablei = dep_params["i"] pj, j, tablej = dep_params["j"] sweep_values = [0, 1, 2] sweep_object = Nest(Sweep(x, tablex, lambda: sweep_values), Chain(Measure(i, tablei))) meas = SweepMeasurement() meas.register_sweep(sweep_object) interdeps = meas._interdeps assert interdeps.dependencies == { ParamSpecBase('i', 'numeric', '', ''): (ParamSpecBase('x', 'numeric', '', ''), ) } assert interdeps.inferences == {} assert interdeps.standalones == set()
def test_nest(indep_params, dep_params): px, x, tablex = indep_params["x"] pi, i, tablei = dep_params["i"] def f(value): return value**2 pi.get = lambda: f(px()) sweep_values = [0, 1, 2] nest = Nest(Sweep(x, tablex, lambda: sweep_values), Measure(i, tablei)) meas = SweepMeasurement() meas.register_sweep(nest) interdeps = meas._interdeps assert interdeps.dependencies == { ParamSpecBase('i', 'numeric', '', ''): (ParamSpecBase('x', 'numeric', '', ''), ) } assert interdeps.inferences == {} assert interdeps.standalones == set()