def test_linspace(): sweep = Linspace('a', 0.34, 9.16, 7) assert len(sweep) == 7 params = list(sweep.param_tuples()) assert len(params) == 7 assert params[0] == (('a', 0.34), ) assert params[-1] == (('a', 9.16), )
def test_run_circuit_sweeps(): circuit = Circuit.from_ops( ExpWGate(half_turns=Symbol('a')).on(Q1), XmonMeasurementGate('m').on(Q1), ) sweep = Linspace('a', 0, 5, 6) sweep2 = Linspace('a', 6, 10, 5) simulator = xmon_simulator.XmonSimulator() for i, result in enumerate( simulator.run_sweep(circuit, [sweep, sweep2], repetitions=1)): assert result.params['a'] == i assert result.measurements['m'] == [i % 2 != 0]
def test_gen_sweep_linspace(): sweep = SingleSweep() sweep.parameter_key = 'bar' sweep.linspace.first_point = 0 sweep.linspace.last_point = 10 sweep.linspace.num_points = 11 out = params._sweep_from_single_param_sweep(sweep) assert out == Linspace('bar', 0, 10, 11)
def test_gen_sweep_linspace(): sweep = { 'parameter_key': 'foo', 'linspace': { 'first_point': 0, 'last_point': 10, 'num_points': 11 } } out = params._sweep_from_single_param_sweep_proto_dict(sweep) assert out == Linspace('foo', 0, 10, 11)
def _sweep_from_single_param_sweep_proto_dict( single_param_sweep: Dict) -> Sweep: key = single_param_sweep['parameter_key'] if 'points' in single_param_sweep: points = single_param_sweep['points'] return Points(key, list(points['points'])) elif 'linspace' in single_param_sweep: sl = single_param_sweep['linspace'] return Linspace(key, sl['first_point'], sl['last_point'], sl['num_points']) else: raise ValueError('Single param sweep type undefined')
def _sweep_from_single_param_sweep( single_param_sweep: params_pb2.SingleSweep) -> Sweep: key = single_param_sweep.parameter_key which = single_param_sweep.WhichOneof('sweep') if which == 'points': sp = single_param_sweep.points return Points(key, list(sp.points)) elif which == 'linspace': sl = single_param_sweep.linspace return Linspace(key, sl.first_point, sl.last_point, sl.num_points) else: raise ValueError('unknown single param sweep type: {}'.format(which))
def test_equality(): et = EqualsTester() et.add_equality_group(Unit, Unit) # Simple sweeps with the same key are equal to themselves, but different # from each other even if they happen to contain the same points. et.make_equality_group(lambda: Linspace('a', 0, 10, 11)) et.make_equality_group(lambda: Linspace('b', 0, 10, 11)) et.make_equality_group(lambda: Points('a', list(range(11)))) et.make_equality_group(lambda: Points('b', list(range(11)))) # Product and Zip sweeps can also be equated. et.make_equality_group( lambda: Linspace('a', 0, 5, 6) * Linspace('b', 10, 15, 6)) et.make_equality_group( lambda: Linspace('a', 0, 5, 6) + Linspace('b', 10, 15, 6)) et.make_equality_group(lambda: Points('a', [1, 2]) * (Linspace('b', 0, 5, 6) + Linspace('c', 10, 15, 6)))
def test_linspace_one_point(): sweep = Linspace('a', 0.34, 9.16, 1) assert len(sweep) == 1 params = list(sweep.param_tuples()) assert len(params) == 1 assert params[0] == (('a', 0.34), )
def test_zip_duplicate_keys(): with pytest.raises(ValueError): _ = Linspace('a', 0, 9, 10) * Linspace('a', 0, 10, 11)
@pytest.mark.parametrize('param_sweep', example_sweeps()) def test_param_sweep_size_versus_gen(param_sweep): sweep = params.sweep_from_proto(param_sweep) predicted_size = len(sweep) out = list(sweep) assert len(out) == predicted_size @pytest.mark.parametrize('sweep,expected', [ ( Unit, Unit ), ( Linspace('a', 0, 10, 25), Product(Zip(Linspace('a', 0, 10, 25))) ), ( Points('a', [1, 2, 3]), Product(Zip(Points('a', [1, 2, 3]))) ), ( Zip(Linspace('a', 0, 1, 5), Points('b', [1, 2, 3])), Product(Zip(Linspace('a', 0, 1, 5), Points('b', [1, 2, 3]))), ), ( Product(Linspace('a', 0, 1, 5), Points('b', [1, 2, 3])), Product(Zip(Linspace('a', 0, 1, 5)), Zip(Points('b', [1, 2, 3]))), ), (
def test_product_duplicate_keys(): with pytest.raises(ValueError): Linspace('a', 0, 9, 10) * Linspace('a', 0, 10, 11)