def test_meas_specs_still_todo(): bsa, meas_spec = _set_up_meas_specs_for_testing() stop = cw.RepetitionsStoppingCriteria(1_000) # 1. before taking any data still_todo, reps = _check_meas_specs_still_todo( meas_specs=[meas_spec], accumulators={meas_spec: bsa}, stopping_criteria=stop) assert still_todo == [meas_spec] assert reps == 1_000 # 2. After taking a mocked-out 997 shots. bsa.consume_results(np.zeros((997, 3), dtype=np.uint8)) still_todo, reps = _check_meas_specs_still_todo( meas_specs=[meas_spec], accumulators={meas_spec: bsa}, stopping_criteria=stop) assert still_todo == [meas_spec] assert reps == 3 # 3. After taking the final 3 shots bsa.consume_results(np.zeros((reps, 3), dtype=np.uint8)) still_todo, reps = _check_meas_specs_still_todo( meas_specs=[meas_spec], accumulators={meas_spec: bsa}, stopping_criteria=stop) assert still_todo == [] assert reps == 0
def test_meas_spec_still_todo_too_many_params(monkeypatch): monkeypatch.setattr(cw.observable_measurement, 'MAX_REPETITIONS_PER_JOB', 30_000) bsa, meas_spec = _set_up_meas_specs_for_testing() lots_of_meas_spec = [meas_spec] * 3_001 stop = cw.RepetitionsStoppingCriteria(10_000) with pytest.raises(ValueError, match='too many parameter settings'): _, _ = _check_meas_specs_still_todo(meas_specs=lots_of_meas_spec, accumulators={meas_spec: bsa}, stopping_criteria=stop)
def test_meas_spec_still_todo_lots_of_params(monkeypatch): monkeypatch.setattr(cw.observable_measurement, 'MAX_REPETITIONS_PER_JOB', 30_000) bsa, meas_spec = _set_up_meas_specs_for_testing() lots_of_meas_spec = [meas_spec] * 4 stop = cw.RepetitionsStoppingCriteria(10_000) with pytest.warns(UserWarning, match='will be throttled from 10000 to 7500'): _, _ = _check_meas_specs_still_todo(meas_specs=lots_of_meas_spec, accumulators={meas_spec: bsa}, stopping_criteria=stop)
def test_meas_specs_still_todo(): q0, q1 = cirq.LineQubit.range(2) setting = cw.InitObsSetting(init_state=cirq.KET_ZERO(q0) * cirq.KET_ZERO(q1), observable=cirq.X(q0) * cirq.Y(q1)) meas_spec = _MeasurementSpec( max_setting=setting, circuit_params={ 'beta': 0.123, 'gamma': 0.456, }, ) bsa = cw.BitstringAccumulator( meas_spec, [], {q: i for i, q in enumerate(cirq.LineQubit.range(3))}) # 1. before taking any data still_todo, reps = _check_meas_specs_still_todo( meas_specs=[meas_spec], accumulators={meas_spec: bsa}, desired_repetitions=1_000) assert still_todo == [meas_spec] assert reps == 1_000 # 2. After taking a mocked-out 997 shots. bsa.consume_results(np.zeros((997, 3), dtype=np.uint8)) still_todo, reps = _check_meas_specs_still_todo( meas_specs=[meas_spec], accumulators={meas_spec: bsa}, desired_repetitions=1_000) assert still_todo == [meas_spec] assert reps == 3 # 3. After taking the final 3 shots bsa.consume_results(np.zeros((reps, 3), dtype=np.uint8)) still_todo, reps = _check_meas_specs_still_todo( meas_specs=[meas_spec], accumulators={meas_spec: bsa}, desired_repetitions=1_000) assert still_todo == [] assert reps == 0
def test_meas_spec_still_todo_bad_spec(): bsa, meas_spec = _set_up_meas_specs_for_testing() class BadStopping(StoppingCriteria): def more_repetitions(self, accumulator: BitstringAccumulator) -> int: return -23 bad_stop = BadStopping() with pytest.raises(ValueError, match='positive'): _, _ = _check_meas_specs_still_todo(meas_specs=[meas_spec], accumulators={meas_spec: bsa}, stopping_criteria=bad_stop)
def test_variance_stopping_criteria_aggregate_n_repetitions(): stop = _WildVarianceStoppingCriteria() acc1 = _MockBitstringAccumulator() acc2 = _MockBitstringAccumulator() accumulators = {'FakeMeasSpec1': acc1, 'FakeMeasSpec2': acc2} with pytest.warns(UserWarning, match='the largest value will be used: 6.'): still_todo, reps = _check_meas_specs_still_todo( meas_specs=sorted(accumulators.keys()), accumulators=accumulators, stopping_criteria=stop, ) assert still_todo == ['FakeMeasSpec1', 'FakeMeasSpec2'] assert reps == 6