def test_measure_grouped_settings_read_checkpoint(tmpdir): qubits = cirq.LineQubit.range(1) (q,) = qubits setting = cw.InitObsSetting( init_state=cirq.KET_ZERO(q), observable=cirq.Z(q), ) grouped_settings = {setting: [setting]} circuit = cirq.Circuit(cirq.I.on_each(*qubits)) with pytest.raises(ValueError, match=r'same filename.*'): _ = cw.measure_grouped_settings( circuit=circuit, grouped_settings=grouped_settings, sampler=cirq.Simulator(), stopping_criteria=cw.RepetitionsStoppingCriteria(1_000, repetitions_per_chunk=500), checkpoint=True, checkpoint_fn=f'{tmpdir}/obs.json', checkpoint_other_fn=f'{tmpdir}/obs.json', # Same filename ) _ = cw.measure_grouped_settings( circuit=circuit, grouped_settings=grouped_settings, sampler=cirq.Simulator(), stopping_criteria=cw.RepetitionsStoppingCriteria(1_000, repetitions_per_chunk=500), checkpoint=True, checkpoint_fn=f'{tmpdir}/obs.json', checkpoint_other_fn=f'{tmpdir}/obs.prev.json', ) results = cirq.read_json(f'{tmpdir}/obs.json') (result,) = results # one group assert result.n_repetitions == 1_000 assert result.means() == [1.0]
def test_measure_grouped_settings_calibration_validation(): dummy_ro_calib = _MockBitstringAccumulator() grouped_settings, qubits = _get_some_grouped_settings() with pytest.raises( ValueError, match=r'Readout calibration only works if `readout_symmetrization` is enabled' ): cw.measure_grouped_settings( circuit=cirq.Circuit(cirq.I.on_each(*qubits)), grouped_settings=grouped_settings, sampler=cirq.Simulator(), stopping_criteria=cw.RepetitionsStoppingCriteria(10_000), readout_calibrations=dummy_ro_calib, readout_symmetrization=False, # no-no! )
def test_measure_grouped_settings(with_circuit_sweep, checkpoint, tmpdir): qubits = cirq.LineQubit.range(1) (q, ) = qubits tests = [ (cirq.KET_ZERO, cirq.Z, 1), (cirq.KET_ONE, cirq.Z, -1), (cirq.KET_PLUS, cirq.X, 1), (cirq.KET_MINUS, cirq.X, -1), (cirq.KET_IMAG, cirq.Y, 1), (cirq.KET_MINUS_IMAG, cirq.Y, -1), ] if with_circuit_sweep: ss = cirq.Linspace('a', 0, 1, 12) else: ss = None if checkpoint: checkpoint_fn = f'{tmpdir}/obs.json' else: checkpoint_fn = None for init, obs, coef in tests: setting = cw.InitObsSetting( init_state=init(q), observable=obs(q), ) grouped_settings = {setting: [setting]} circuit = cirq.Circuit(cirq.I.on_each(*qubits)) results = cw.measure_grouped_settings( circuit=circuit, grouped_settings=grouped_settings, sampler=cirq.Simulator(), stopping_criteria=cw.RepetitionsStoppingCriteria( 1_000, repetitions_per_chunk=500), circuit_sweep=ss, checkpoint=CheckpointFileOptions(checkpoint=checkpoint, checkpoint_fn=checkpoint_fn), ) if with_circuit_sweep: for result in results: assert result.means() == [coef] else: (result, ) = results # one group assert result.means() == [coef]
def test_measure_grouped_settings(with_circuit_sweep): qubits = cirq.LineQubit.range(1) (q, ) = qubits tests = [ (cirq.KET_ZERO, cirq.Z, 1), (cirq.KET_ONE, cirq.Z, -1), (cirq.KET_PLUS, cirq.X, 1), (cirq.KET_MINUS, cirq.X, -1), (cirq.KET_IMAG, cirq.Y, 1), (cirq.KET_MINUS_IMAG, cirq.Y, -1), ] if with_circuit_sweep: ss = cirq.Linspace('a', 0, 1, 12) else: ss = None for init, obs, coef in tests: setting = cw.InitObsSetting( init_state=init(q), observable=obs(q), ) grouped_settings = {setting: [setting]} circuit = cirq.Circuit(cirq.I.on_each(*qubits)) results = cw.measure_grouped_settings( circuit=circuit, grouped_settings=grouped_settings, sampler=cirq.Simulator(), desired_repetitions=1_000, circuit_sweep=ss, ) if with_circuit_sweep: for result in results: assert result.means() == [coef] else: (result, ) = results # one group assert result.means() == [coef]