def test_rare_cases(): reg = Register.square(4) seq = Sequence(reg, Chadoq2) var = seq.declare_variable("var") wf = BlackmanWaveform(100, var) with pytest.warns(UserWarning, match="Calls to methods of parametrized " "objects"): s = encode(wf.draw()) with pytest.warns(UserWarning, match="not encode a Sequence"): wf_ = Sequence.deserialize(s) var._assign(-10) with pytest.raises(ValueError, match="No value assigned"): wf_.build() var_ = wf_._variables["var"] var_._assign(-10) with patch('matplotlib.pyplot.show'): wf_.build() rotated_reg = parametrize(Register.rotate)(reg, var) with pytest.raises(NotImplementedError): encode(rotated_reg)
def seq_rydberg() -> pulser.Sequence: reg = pulser.Register.from_coordinates(np.array([[0.0, 0.0], [2.0, 0.0]]), prefix="q") seq = pulser.Sequence(reg, MockDevice) seq.declare_channel("ch0", "rydberg_global") seq.declare_channel("ch1", "rydberg_local", initial_target="q0") seq.add( Pulse.ConstantDetuning(BlackmanWaveform(100, np.pi / 8), 0.0, 0.0), "ch0", ) seq.delay(20, "ch0") seq.add( Pulse.ConstantAmplitude(0.0, BlackmanWaveform(100, np.pi / 8), 0.0), "ch0", ) seq.add( Pulse.ConstantDetuning(BlackmanWaveform(100, np.pi / 8), 0.0, 0.0), "ch1", ) seq.target("q1", "ch1") seq.add( Pulse.ConstantAmplitude(1.0, BlackmanWaveform(100, np.pi / 8), 0.0), "ch1", ) seq.target(["q0", "q1"], "ch1") seq.add( Pulse.ConstantDetuning(BlackmanWaveform(100, np.pi / 8), 0.0, 0.0), "ch1", ) seq.measure() return seq
def test_rare_cases(): reg = Register.square(4) seq = Sequence(reg, Chadoq2) var = seq.declare_variable("var") wf = BlackmanWaveform(var * 100 // 10, var) with pytest.raises( ValueError, match="Serialization of calls to parametrized objects" ): s = encode(wf.draw()) s = encode(wf) with pytest.raises(ValueError, match="not encode a Sequence"): wf_ = Sequence.deserialize(s) wf_ = decode(s) seq._variables["var"]._assign(-10) with pytest.raises(ValueError, match="No value assigned"): wf_.build() var_ = wf_._variables["var"] var_._assign(10) assert wf_.build() == BlackmanWaveform(100, 10) with pytest.warns(UserWarning, match="Serialization of 'getattr'"): draw_func = wf_.draw with patch("matplotlib.pyplot.show"): with pytest.warns( UserWarning, match="Calls to methods of parametrized objects" ): draw_func().build() rotated_reg = parametrize(Register.rotate)(reg, var) with pytest.raises(NotImplementedError): encode(rotated_reg)
def test_support(): seq = Sequence(Register.square(2), Chadoq2) var = seq.declare_variable("var") obj_dict = BlackmanWaveform.from_max_val(1, var)._to_dict() del obj_dict["__module__"] with pytest.raises(TypeError, match="Invalid 'obj_dict'."): validate_serialization(obj_dict) obj_dict["__module__"] = "pulser.fake" with pytest.raises( SerializationError, match="No serialization support for module 'pulser.fake'.", ): validate_serialization(obj_dict) wf_obj_dict = obj_dict["__args__"][0] wf_obj_dict["__submodule__"] = "RampWaveform" with pytest.raises( SerializationError, match="No serialization support for attributes of " "'pulser.waveforms.RampWaveform'", ): validate_serialization(wf_obj_dict) del wf_obj_dict["__submodule__"] with pytest.raises( SerializationError, match="No serialization support for 'pulser.waveforms.from_max_val'", ): validate_serialization(wf_obj_dict)
def test_build(): reg_ = Register.rectangle(2, 1, prefix="q") sb = Sequence(reg_, device) var = sb.declare_variable("var") targ_var = sb.declare_variable("targ_var", size=2, dtype=int) sb.declare_channel("ch1", "rydberg_local") sb.declare_channel("ch2", "raman_local") sb.target_index(targ_var[0], "ch2") sb.target_index(targ_var[1], "ch1") wf = BlackmanWaveform(var * 100, np.pi) pls = Pulse.ConstantDetuning(wf, var, var) sb.add(pls, "ch1") sb.delay(var * 50, "ch1") sb.align("ch2", "ch1") sb.phase_shift_index(var, targ_var[0]) pls2 = Pulse.ConstantPulse(var * 100, var, var, 0) sb.add(pls2, "ch2") sb.measure() with pytest.warns(UserWarning, match="No declared variables"): sb.build(t=100, var=2, targ_var=reg_.find_indices(["q1", "q0"])) with pytest.raises(TypeError, match="Did not receive values for"): sb.build(var=2) seq = sb.build(var=2, targ_var=reg_.find_indices(["q1", "q0"])) assert seq._schedule["ch2"][-1].tf == 500 assert seq.current_phase_ref("q1") == 2.0 assert seq.current_phase_ref("q0") == 0.0 assert seq._measurement == "ground-rydberg" s = sb.serialize() sb_ = Sequence.deserialize(s) assert str(sb) == str(sb_) s2 = sb_.serialize() sb_2 = Sequence.deserialize(s2) assert str(sb) == str(sb_2)
def test_modulation(): rydberg_global = Rydberg.Global(2 * np.pi * 20, 2 * np.pi * 2.5) raman_local = Raman.Local( 2 * np.pi * 20, 2 * np.pi * 10, mod_bandwidth=4, # MHz ) wf = ConstantWaveform(100, 1) assert rydberg_global.mod_bandwidth is None with pytest.warns(UserWarning, match="No modulation bandwidth defined"): out_samples = rydberg_global.modulate(wf.samples) assert np.all(out_samples == wf.samples) with pytest.raises(TypeError, match="doesn't have a modulation bandwidth"): rydberg_global.calc_modulation_buffer(wf.samples, out_samples) out_ = raman_local.modulate(wf.samples) tr = raman_local.rise_time assert len(out_) == wf.duration + 2 * tr assert raman_local.calc_modulation_buffer(wf.samples, out_) == (tr, tr) wf2 = BlackmanWaveform(800, np.pi) side_buffer_len = 45 out_ = raman_local.modulate(wf2.samples) assert len(out_) == wf2.duration + 2 * tr # modulate() does not truncate assert raman_local.calc_modulation_buffer(wf2.samples, out_) == ( side_buffer_len, side_buffer_len, )
def test_SLM_samples(seq_with_SLM): pulse = Pulse.ConstantDetuning(BlackmanWaveform(200, np.pi / 2), 0.0, 0.0) a_samples = pulse.amplitude.samples def z() -> np.ndarray: return np.zeros(seq_with_SLM.get_duration()) want: dict = { "Global": {}, "Local": { "ground-rydberg": { "batman": { "amp": z(), "det": z(), "phase": z() }, "superman": { "amp": z(), "det": z(), "phase": z() }, } }, } want["Local"]["ground-rydberg"]["batman"]["amp"][200:400] = a_samples want["Local"]["ground-rydberg"]["superman"]["amp"][0:200] = a_samples want["Local"]["ground-rydberg"]["superman"]["amp"][200:400] = a_samples got = sample(seq_with_SLM) assert_nested_dict_equality(got, want)
def test_ops(): assert -constant == ConstantWaveform(100, 3) assert ramp * 2 == RampWaveform(2e3, 10, 38) assert --custom == custom assert blackman / 2 == BlackmanWaveform(40, np.pi / 2) assert composite * 1 == composite with pytest.raises(ZeroDivisionError): constant / 0
def test_add_max_step_and_delays(): reg = Register.from_coordinates([(0, 0)]) seq = Sequence(reg, Chadoq2) seq.declare_channel("ch", "rydberg_global") seq.delay(1500, "ch") seq.add(Pulse.ConstantDetuning(BlackmanWaveform(600, np.pi), 0, 0), "ch") seq.delay(2000, "ch") seq.add(Pulse.ConstantDetuning(BlackmanWaveform(600, np.pi / 2), 0, 0), "ch") sim = Simulation(seq) res_large_max_step = sim.run(max_step=1) res_auto_max_step = sim.run() r = qutip.basis(2, 0) occ_large = res_large_max_step.expect([r.proj()])[0] occ_auto = res_auto_max_step.expect([r.proj()])[0] assert np.isclose(occ_large[-1], 0, 1e-4) assert np.isclose(occ_auto[-1], 0.5, 1e-4)
def mod_seq(mod_device: Device) -> pulser.Sequence: reg = pulser.Register.from_coordinates(np.array([[0.0, 0.0]]), prefix="q") seq = pulser.Sequence(reg, mod_device) seq.declare_channel("ch0", "rydberg_global") seq.add( Pulse.ConstantDetuning(BlackmanWaveform(1000, np.pi / 2), 0.0, 0.0), "ch0", ) seq.measure() return seq
def test_duration(): with pytest.raises(TypeError, match='needs to be castable to an int'): ConstantWaveform("s", -1) RampWaveform([0, 1, 3], 1, 0) with pytest.raises(ValueError, match='at least 16 ns'): ConstantWaveform(15, -10) RampWaveform(-20, 3, 4) with pytest.raises(ValueError, match='at most 4194304 ns'): BlackmanWaveform(2**23, np.pi/2) with pytest.raises(ValueError, match='waveform of invalid duration'): CustomWaveform(np.random.random(50)) with pytest.warns(UserWarning): wf = BlackmanWaveform(np.pi*10, 1) assert wf.duration == 28 assert custom.duration == 52 assert composite.duration == 192
def seq_with_SLM() -> pulser.Sequence: q_dict = { "batman": np.array([-4.0, 0.0]), # sometimes masked "superman": np.array([4.0, 0.0]), # always unmasked } reg = pulser.Register(q_dict) seq = pulser.Sequence(reg, MockDevice) seq.declare_channel("ch0", "rydberg_global") seq.config_slm_mask(["batman"]) seq.add( Pulse.ConstantDetuning(BlackmanWaveform(200, np.pi / 2), 0.0, 0.0), "ch0", ) seq.add( Pulse.ConstantDetuning(BlackmanWaveform(200, np.pi / 2), 0.0, 0.0), "ch0", ) seq.measure() return seq
def test_inXY() -> None: """Test sequence in XY mode.""" pulse = Pulse( BlackmanWaveform(200, np.pi / 2), RampWaveform(200, -np.pi / 2, np.pi / 2), 0.0, ) reg = pulser.Register.from_coordinates(np.array([[0.0, 0.0]]), prefix="q") seq = pulser.Sequence(reg, MockDevice) seq.declare_channel("ch0", "mw_global") seq.add(pulse, "ch0") seq.measure(basis="XY") assert_same_samples_as_sim(seq)
def test_duration(): with pytest.raises(TypeError, match="needs to be castable to an int"): ConstantWaveform("s", -1) RampWaveform([0, 1, 3], 1, 0) with pytest.raises(ValueError, match="positive duration"): ConstantWaveform(15, -10) RampWaveform(-20, 3, 4) with pytest.warns(UserWarning): wf = BlackmanWaveform(np.pi * 10, 1) assert wf.duration == 31 assert custom.duration == 52 assert composite.duration == 192
def test_one_pulse_sampling(): """Test the sample function on a one-pulse sequence.""" reg = pulser.Register.square(1, prefix="q") seq = pulser.Sequence(reg, MockDevice) seq.declare_channel("ch0", "rydberg_global") N = 1000 amp_wf = BlackmanWaveform(N, np.pi) det_wf = RampWaveform(N, -np.pi / 2, np.pi / 2) phase = 1.234 seq.add(Pulse(amp_wf, det_wf, phase), "ch0") seq.measure() got = sample(seq)["Global"]["ground-rydberg"] want = (amp_wf.samples, det_wf.samples, np.ones(N) * phase) for i, key in enumerate(["amp", "det", "phase"]): np.testing.assert_array_equal(got[key], want[i])
def test_results_xy(): q_dict = { "A": np.array([0.0, 0.0]), "B": np.array([0.0, 10.0]), } reg = Register(q_dict) duration = 1000 pi = Pulse.ConstantDetuning(BlackmanWaveform(duration, np.pi), 0.0, 0) seq = Sequence(reg, MockDevice) # Declare Channels seq.declare_channel("ch0", "mw_global") seq.add(pi, "ch0") seq.measure("XY") sim = Simulation(seq) results = sim.run() ground = qutip.tensor([qutip.basis(2, 1), qutip.basis(2, 1)]) assert results._dim == 2 assert results._size == 2 assert results._basis_name == "XY" assert results._meas_basis == "XY" assert results.states[0] == ground with pytest.raises(TypeError, match="Can't reduce a system in"): results.get_final_state(reduce_to_basis="all") with pytest.raises(TypeError, match="Can't reduce a system in"): results.get_final_state(reduce_to_basis="ground-rydberg") with pytest.raises(TypeError, match="Can't reduce a system in"): results.get_final_state(reduce_to_basis="digital") state = results.get_final_state(reduce_to_basis="XY") assert np.all( np.isclose(np.abs(state.full()), np.abs(results.states[-1].full()), atol=1e-5)) # Check that measurement projectors are correct assert results._meas_projector(0) == qutip.basis(2, 1).proj() assert results._meas_projector(1) == qutip.basis(2, 0).proj()
def seqs(seq_rydberg) -> list[pulser.Sequence]: seqs: list[pulser.Sequence] = [] pulse = Pulse( BlackmanWaveform(200, np.pi / 2), RampWaveform(200, -np.pi / 2, np.pi / 2), 0.0, ) reg = pulser.Register.from_coordinates(np.array([[0.0, 0.0]]), prefix="q") seq = pulser.Sequence(reg, MockDevice) seq.declare_channel("ch0", "raman_global") seq.add(pulse, "ch0") seq.measure() seqs.append(deepcopy(seq)) seqs.append(seq_rydberg) return seqs
def test_blackman(): with pytest.raises(TypeError): BlackmanWaveform(100, np.array([1, 2])) wf = BlackmanWaveform(100, -2) assert np.isclose(wf.integral, -2) assert np.all(wf.samples <= 0) assert wf == BlackmanWaveform(100, np.array([-2])) with pytest.raises(ValueError, match="matching signs"): BlackmanWaveform.from_max_val(-10, np.pi) wf = BlackmanWaveform.from_max_val(10, 2 * np.pi) assert np.isclose(wf.integral, 2 * np.pi) assert np.max(wf.samples) < 10 wf = BlackmanWaveform.from_max_val(-10, -np.pi) assert np.isclose(wf.integral, -np.pi) assert np.min(wf.samples) > -10
def test_blackman(): with pytest.raises(TypeError): BlackmanWaveform(100, np.array([1, 2])) wf = BlackmanWaveform(100, -2) assert np.isclose(wf.integral, -2) assert np.all(wf.samples <= 0) assert wf == BlackmanWaveform(100, np.array([-2])) with pytest.raises(ValueError, match="matching signs"): BlackmanWaveform.from_max_val(-10, np.pi) wf = BlackmanWaveform.from_max_val(10, 2*np.pi) assert np.isclose(wf.integral, 2*np.pi) assert np.max(wf.samples) < 10 wf = BlackmanWaveform.from_max_val(-10, -np.pi) assert np.isclose(wf.integral, -np.pi) assert np.min(wf.samples) > -10 var = Variable("var", float) wf_var = BlackmanWaveform.from_max_val(-10, var) assert isinstance(wf_var, ParamObj) var._assign(-np.pi) assert wf_var.build() == wf
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from unittest.mock import patch import numpy as np import pytest from pulser import Pulse from pulser.waveforms import BlackmanWaveform, ConstantWaveform, RampWaveform cwf = ConstantWaveform(100, -10) bwf = BlackmanWaveform(200, 3) rwf = RampWaveform(200, 0, 1) pls = Pulse(bwf, bwf, 2 * np.pi) pls2 = Pulse.ConstantPulse(100, 1, -10, -np.pi) pls3 = Pulse.ConstantAmplitude(1, cwf, -np.pi) pls4 = Pulse.ConstantDetuning(bwf, -10, 0) def test_creation(): with pytest.raises(TypeError): Pulse(10, 0, 0, post_phase_shift=2) Pulse(cwf, 1, 0) Pulse(0, bwf, 1) Pulse(bwf, cwf, bwf) Pulse(bwf, cwf, 0, post_phase_shift=cwf)
def test_sequence(): seq = Sequence(reg, device) assert seq.get_duration() == 0 with pytest.raises(RuntimeError, match="empty sequence"): seq.draw() seq.declare_channel("ch0", "raman_local", initial_target="q0") seq.declare_channel("ch1", "rydberg_local", initial_target="q0") seq.declare_channel("ch2", "rydberg_global") assert seq.get_duration("ch0") == 0 assert seq.get_duration("ch2") == 0 seq.phase_shift(np.pi, "q0", basis="ground-rydberg") with patch("matplotlib.pyplot.show"): with patch("matplotlib.figure.Figure.savefig"): seq.draw(fig_name="my_sequence.pdf") seq.draw(draw_register=True, fig_name="both.pdf") pulse1 = Pulse( InterpolatedWaveform(500, [0, 1, 0]), InterpolatedWaveform(500, [-1, 1, 0]), phase=0, post_phase_shift=np.pi, ) pulse2 = Pulse.ConstantDetuning(BlackmanWaveform(1e3, np.pi / 4), 25, np.pi, post_phase_shift=1) with pytest.raises(TypeError): seq.add([1, 5, 3], "ch0") with pytest.raises(ValueError, match="amplitude goes over the maximum"): seq.add(Pulse.ConstantPulse(20, 2 * np.pi * 10, -2 * np.pi * 100, 0), "ch2") with pytest.raises(ValueError, match="detuning values go out of the range"): seq.add(Pulse.ConstantPulse(500, 2 * np.pi, -2 * np.pi * 100, 0), "ch0") with pytest.raises(ValueError, match="qubits with different phase ref"): seq.add(pulse2, "ch2") with pytest.raises(ValueError, match="Invalid protocol"): seq.add(pulse1, "ch0", protocol="now") wf_ = CompositeWaveform(BlackmanWaveform(30, 1), RampWaveform(15, 0, 2)) with pytest.raises(TypeError, match="Failed to automatically adjust"): with pytest.warns(UserWarning, match="rounded up to 48 ns"): seq.add(Pulse.ConstantAmplitude(1, wf_, 0), "ch0") pulse1_ = Pulse.ConstantPulse(499, 2, -10, 0, post_phase_shift=np.pi) with pytest.warns(UserWarning, match="rounded up to 500 ns"): seq.add(pulse1_, "ch0") seq.add(pulse1, "ch1") seq.add(pulse2, "ch2") assert seq._last("ch0").ti == 0 assert seq._last("ch0").tf == seq._last("ch1").ti assert seq._last("ch2").tf == seq._last("ch2").ti + 1000 assert seq.current_phase_ref("q0", "digital") == np.pi seq.add(pulse1, "ch2") assert seq.get_duration("ch2") == 2500 seq.add(pulse2, "ch1", protocol="no-delay") assert seq.get_duration("ch1") == 3500 seq.add(pulse1, "ch0", protocol="no-delay") assert seq._last("ch0").ti == 500 assert seq.get_duration("ch0") == 1000 assert seq.current_phase_ref("q0", "digital") == 0 seq.phase_shift(np.pi / 2, "q1") seq.target("q1", "ch0") assert seq._last_used["digital"]["q1"] == 0 assert seq._last_target["ch0"] == 1000 assert seq._last("ch0").ti == 1000 assert seq.get_duration("ch0") == 1000 seq.add(pulse1, "ch0") assert seq._last("ch0").ti == 2500 assert seq.get_duration("ch0") == 3000 seq.add(pulse1, "ch0", protocol="wait-for-all") assert seq._last("ch0").ti == 3500 assert seq.get_duration("ch2") != seq.get_duration("ch0") seq.align("ch0", "ch2") assert seq.get_duration("ch2") == seq.get_duration("ch0") with patch("matplotlib.pyplot.show"): seq.draw(draw_phase_shifts=True) assert seq.get_duration() == 4000 seq.measure(basis="digital") with patch("matplotlib.pyplot.show"): seq.draw(draw_phase_area=True) s = seq.serialize() assert json.loads(s)["__version__"] == pulser.__version__ seq_ = Sequence.deserialize(s) assert str(seq) == str(seq_)
def test_blackman(): with pytest.raises(TypeError): BlackmanWaveform(100, np.array([1, 2])) wf = BlackmanWaveform(100, -2) assert np.isclose(wf.integral, -2) assert np.all(wf.samples <= 0) assert wf == BlackmanWaveform(100, np.array([-2])) with pytest.raises(ValueError, match="matching signs"): BlackmanWaveform.from_max_val(-10, np.pi) wf = BlackmanWaveform.from_max_val(10, 2 * np.pi) assert np.isclose(wf.integral, 2 * np.pi) assert np.max(wf.samples) < 10 wf = BlackmanWaveform.from_max_val(-10, -np.pi) assert np.isclose(wf.integral, -np.pi) assert np.min(wf.samples) > -10 var = Variable("var", float) wf_var = BlackmanWaveform.from_max_val(-10, var) assert isinstance(wf_var, ParamObj) var._assign(-np.pi) assert wf_var.build() == wf # Check moving back to previous even duration area: float = np.pi / 6 max_val: float = 46 wf: BlackmanWaveform = BlackmanWaveform.from_max_val(max_val, area) duration = wf.duration assert duration % 2 == 0 wf2 = BlackmanWaveform(duration + 1, area) assert np.max(wf2.samples) < np.max(wf.samples) <= max_val # Same but with a negative max value wf: BlackmanWaveform = BlackmanWaveform.from_max_val(-max_val, -area) duration = wf.duration assert duration % 2 == 0 wf2 = BlackmanWaveform(duration + 1, -area) assert np.min(wf2.samples) > np.min(wf.samples) >= -max_val
BlackmanWaveform, CompositeWaveform, ConstantWaveform, CustomWaveform, InterpolatedWaveform, KaiserWaveform, RampWaveform, ) np.random.seed(20201105) constant = ConstantWaveform(100, -3) ramp = RampWaveform(2000, 5, 19) arb_samples = np.random.random(52) custom = CustomWaveform(arb_samples) blackman = BlackmanWaveform(40, np.pi) composite = CompositeWaveform(blackman, constant, custom) interp_values = [0, 1, 4.4, 2, 3, 1, 0] interp = InterpolatedWaveform(1000, interp_values) kaiser = KaiserWaveform(40, np.pi) def test_duration(): with pytest.raises(TypeError, match="needs to be castable to an int"): ConstantWaveform("s", -1) RampWaveform([0, 1, 3], 1, 0) with pytest.raises(ValueError, match="positive duration"): ConstantWaveform(15, -10) RampWaveform(-20, 3, 4)
import qutip from pulser import Sequence, Pulse, Register, Simulation from pulser.devices import Chadoq2 from pulser.waveforms import BlackmanWaveform q_dict = { "control1": np.array([-4., 0.]), "target": np.array([0., 4.]), "control2": np.array([4., 0.]) } reg = Register(q_dict) duration = 1000 pi = Pulse.ConstantDetuning(BlackmanWaveform(duration, np.pi), 0., 0) twopi = Pulse.ConstantDetuning(BlackmanWaveform(duration, 2 * np.pi), 0., 0) pi_Y = Pulse.ConstantDetuning(BlackmanWaveform(duration, np.pi), 0., -np.pi / 2) seq = Sequence(reg, Chadoq2) # Declare Channels seq.declare_channel('ryd', 'rydberg_local', 'control1') seq.declare_channel('raman', 'raman_local', 'control1') d = 0 # Pulse Duration # Prepare state 'hhh': seq.add(pi_Y, 'raman') seq.target('target', 'raman')
import numpy as np import pytest from pulser import Pulse from pulser.json.coders import PulserDecoder, PulserEncoder from pulser.parametrized import Variable from pulser.waveforms import BlackmanWaveform, CompositeWaveform a = Variable("a", float) b = Variable("b", int, size=2) b._assign([-1.5, 1.5]) d = Variable("d", float, size=1) d._assign([0.5]) t = Variable("t", int) bwf = BlackmanWaveform(t, a) pulse = Pulse.ConstantDetuning(bwf, b[0], b[1]) pulse2 = Pulse(bwf, bwf, 1) def test_var(): with pytest.raises(TypeError, match="'name' has to be of type 'str'"): Variable(1, dtype=int) with pytest.raises(TypeError, match="Invalid data type"): Variable("x", dtype=list, size=4) with pytest.raises(TypeError, match="'size' is not of type 'int'"): Variable("x", dtype=float, size=(2, 2)) with pytest.raises(ValueError, match="size 1 or larger"): Variable("x", dtype=int, size=0) x = Variable("x", dtype=float) assert x.value is None
def test_hardware_constraints(): rydberg_global = Rydberg.Global( 2 * np.pi * 20, 2 * np.pi * 2.5, phase_jump_time=120, # ns mod_bandwidth=4, # MHz ) raman_local = Raman.Local( 2 * np.pi * 20, 2 * np.pi * 10, phase_jump_time=120, # ns fixed_retarget_t=200, # ns mod_bandwidth=7, # MHz ) ConstrainedChadoq2 = Device( name="ConstrainedChadoq2", dimensions=2, rydberg_level=70, max_atom_num=100, max_radial_distance=50, min_atom_distance=4, _channels=( ("rydberg_global", rydberg_global), ("raman_local", raman_local), ), ) with pytest.warns(UserWarning, match="should be imported from 'pulser.devices'"): seq = Sequence(reg, ConstrainedChadoq2) seq.declare_channel("ch0", "rydberg_global") seq.declare_channel("ch1", "raman_local", initial_target="q1") const_pls = Pulse.ConstantPulse(100, 1, 0, np.pi) seq.add(const_pls, "ch0") black_wf = BlackmanWaveform(500, np.pi) black_pls = Pulse.ConstantDetuning(black_wf, 0, 0) seq.add(black_pls, "ch1") blackman_slot = seq._last("ch1") # The pulse accounts for the modulation buffer assert (blackman_slot.ti == const_pls.duration + rydberg_global.rise_time * 2) seq.target("q0", "ch1") target_slot = seq._last("ch1") fall_time = black_pls.fall_time(raman_local) assert (fall_time == raman_local.rise_time + black_wf.modulation_buffers(raman_local)[1]) fall_time += (raman_local.clock_period - fall_time % raman_local.clock_period) assert target_slot.ti == blackman_slot.tf + fall_time assert target_slot.tf == target_slot.ti + raman_local.fixed_retarget_t assert raman_local.min_retarget_interval > raman_local.fixed_retarget_t seq.target("q2", "ch1") assert (seq.get_duration("ch1") == target_slot.tf + raman_local.min_retarget_interval) # Check for phase jump buffer seq.add(black_pls, "ch0") # Phase = 0 tf_ = seq.get_duration("ch0") mid_delay = 40 seq.delay(mid_delay, "ch0") seq.add(const_pls, "ch0") # Phase = π assert seq._last("ch0").ti - tf_ == rydberg_global.phase_jump_time added_delay_slot = seq._schedule["ch0"][-2] assert added_delay_slot.type == "delay" assert (added_delay_slot.tf - added_delay_slot.ti == rydberg_global.phase_jump_time - mid_delay) tf_ = seq.get_duration("ch0") seq.align("ch0", "ch1") fall_time = const_pls.fall_time(rydberg_global) assert seq.get_duration() == tf_ + fall_time with pytest.raises(ValueError, match="'mode' must be one of"): seq.draw(mode="all") with patch("matplotlib.pyplot.show"): with pytest.warns( UserWarning, match="'draw_phase_area' doesn't work in 'output' mode", ): seq.draw(mode="output", draw_interp_pts=False, draw_phase_area=True) with pytest.warns( UserWarning, match="'draw_interp_pts' doesn't work in 'output' mode", ): seq.draw(mode="output") seq.draw(mode="input+output")
def test_sequence(): seq = Sequence(reg, device) with pytest.raises(SystemError, match='empty sequence'): seq.draw() seq.declare_channel('ch0', 'raman_local', initial_target='q0') seq.declare_channel('ch1', 'rydberg_local', initial_target='q0') seq.declare_channel('ch2', 'rydberg_global') seq.phase_shift(np.pi, 'q0', basis='ground-rydberg') pulse1 = Pulse.ConstantPulse(500, 2, -10, 0, post_phase_shift=np.pi) pulse2 = Pulse.ConstantDetuning(BlackmanWaveform(1e3, np.pi / 4), 25, np.pi, post_phase_shift=1) with pytest.raises(TypeError): seq.add([1, 5, 3], 'ch0') with pytest.raises(ValueError, match='amplitude goes over the maximum'): seq.add(Pulse.ConstantPulse(20, 2 * np.pi * 10, -2 * np.pi * 100, 0), 'ch2') with pytest.raises(ValueError, match='detuning values go out of the range'): seq.add(Pulse.ConstantPulse(500, 2 * np.pi, -2 * np.pi * 100, 0), 'ch0') with pytest.raises(ValueError, match='qubits with different phase ref'): seq.add(pulse2, 'ch2') with pytest.raises(ValueError, match='Invalid protocol'): seq.add(pulse1, 'ch0', protocol='now') seq.add(pulse1, 'ch0') seq.add(pulse1, 'ch1') seq.add(pulse2, 'ch2') assert seq._last('ch0').ti == 0 assert seq._last('ch0').tf == seq._last('ch1').ti assert seq._last('ch2').tf == seq._last('ch2').ti + 1000 assert seq.current_phase_ref('q0', 'digital') == np.pi seq.add(pulse1, 'ch2') assert seq._last('ch2').tf == 2500 seq.add(pulse2, 'ch1', protocol='no-delay') assert seq._last('ch1').tf == 3500 seq.add(pulse1, 'ch0', protocol='no-delay') assert seq._last('ch0').ti == 500 assert seq._last('ch0').tf == 1000 assert seq.current_phase_ref('q0', 'digital') == 0 seq.phase_shift(np.pi / 2, 'q1') seq.target('q1', 'ch0') assert seq._last_used['digital']['q1'] == 0 assert seq._last_target['ch0'] == 1000 assert seq._last('ch0').ti == 1000 assert seq._last('ch0').tf == 1000 seq.add(pulse1, 'ch0') assert seq._last('ch0').ti == 2500 assert seq._last('ch0').tf == 3000 seq.add(pulse1, 'ch0', protocol='wait-for-all') assert seq._last('ch0').ti == 3500 assert seq._last('ch2').tf != seq._last('ch0').tf seq.align('ch0', 'ch2') assert seq._last('ch2').tf == seq._last('ch0').tf with patch('matplotlib.pyplot.show'): seq.draw() assert seq._total_duration == 4000 with pytest.raises(ValueError, match='not supported'): seq.measure(basis='computational') seq.measure(basis='digital') with pytest.raises(SystemError, match='already been measured'): seq.measure(basis='digital') with pytest.raises(SystemError, match='Nothing more can be added.'): seq.add(pulse1, 'ch0') with patch('matplotlib.pyplot.show'): seq.draw()
from pulser import Pulse, Register, Sequence from pulser.devices import Chadoq2, MockDevice from pulser.waveforms import BlackmanWaveform from pulser_simulation import SimConfig, Simulation from pulser_simulation.simresults import CoherentResults, NoisyResults np.random.seed(123) q_dict = { "A": np.array([0.0, 0.0]), "B": np.array([0.0, 10.0]), } reg = Register(q_dict) duration = 1000 pi = Pulse.ConstantDetuning(BlackmanWaveform(duration, np.pi), 0.0, 0) seq = Sequence(reg, Chadoq2) # Declare Channels seq.declare_channel("ryd", "rydberg_global") seq.add(pi, "ryd") seq_no_meas = deepcopy(seq) seq_no_meas_noisy = deepcopy(seq) seq.measure("ground-rydberg") sim = Simulation(seq) cfg_noisy = SimConfig(noise=("SPAM", "doppler", "amplitude")) sim_noisy = Simulation(seq, config=cfg_noisy) results = sim.run() results_noisy = sim_noisy.run()