Example #1
0
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)
Example #2
0
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)
Example #3
0
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)
Example #4
0
def test_slm_mask():
    reg = Register({"q0": (0, 0), "q1": (10, 10), "q2": (-10, -10)})
    targets = ["q0", "q2"]
    pulse1 = Pulse.ConstantPulse(100, 10, 0, 0)
    pulse2 = Pulse.ConstantPulse(200, 10, 0, 0)

    # Set mask when an XY pulse is already in the schedule
    seq_xy1 = Sequence(reg, MockDevice)
    seq_xy1.declare_channel("ch_xy", "mw_global")
    seq_xy1.add(pulse1, "ch_xy")
    seq_xy1.config_slm_mask(targets)
    assert seq_xy1._slm_mask_time == [0, 100]

    # Set mask and then add an XY pulse to the schedule
    seq_xy2 = Sequence(reg, MockDevice)
    seq_xy2.config_slm_mask(targets)
    seq_xy2.declare_channel("ch_xy", "mw_global")
    seq_xy2.add(pulse1, "ch_xy")
    assert seq_xy2._slm_mask_time == [0, 100]

    # Check that adding extra pulses does not change SLM mask time
    seq_xy2.add(pulse2, "ch_xy")
    assert seq_xy2._slm_mask_time == [0, 100]

    # Check that SLM mask time is updated accordingly if a new pulse with
    # earlier start is added
    seq_xy3 = Sequence(reg, MockDevice)
    seq_xy3.declare_channel("ch_xy1", "mw_global")
    seq_xy3.config_slm_mask(targets)
    seq_xy3.delay(duration=100, channel="ch_xy1")
    seq_xy3.add(pulse1, "ch_xy1")
    assert seq_xy3._slm_mask_time == [100, 200]
    seq_xy3.declare_channel("ch_xy2", "mw_global")
    seq_xy3.add(pulse1, "ch_xy2", "no-delay")
    assert seq_xy3._slm_mask_time == [0, 100]

    # Same as previous check, but mask is added afterwards
    seq_xy4 = Sequence(reg, MockDevice)
    seq_xy4.declare_channel("ch_xy1", "mw_global")
    seq_xy4.delay(duration=100, channel="ch_xy1")
    seq_xy4.add(pulse1, "ch_xy1")
    seq_xy4.declare_channel("ch_xy2", "mw_global")
    seq_xy4.add(pulse1, "ch_xy2", "no-delay")
    seq_xy4.config_slm_mask(targets)
    assert seq_xy4._slm_mask_time == [0, 100]

    # Check that paramatrize works with SLM mask
    seq_xy5 = Sequence(reg, MockDevice)
    seq_xy5.declare_channel("ch", "mw_global")
    var = seq_xy5.declare_variable("var")
    seq_xy5.add(Pulse.ConstantPulse(200, var, 0, 0), "ch")
    assert seq_xy5.is_parametrized()
    seq_xy5.config_slm_mask(targets)
    seq_xy5_str = seq_xy5.serialize()
    seq_xy5_ = Sequence.deserialize(seq_xy5_str)
    assert str(seq_xy5) == str(seq_xy5_)

    # Check drawing method
    with patch("matplotlib.pyplot.show"):
        seq_xy2.draw()
Example #5
0
def test_mappable_register():
    layout = RegisterLayout([[0, 0], [1, 1], [1, 0], [0, 1]])
    mapp_reg = layout.make_mappable_register(2)
    new_mapp_reg = encode_decode(mapp_reg)
    assert new_mapp_reg.layout == layout
    assert new_mapp_reg.qubit_ids == ("q0", "q1")

    seq = Sequence(mapp_reg, MockDevice)
    assert seq.is_register_mappable()
    mapped_seq = seq.build(qubits={"q0": 2, "q1": 1})
    assert not mapped_seq.is_register_mappable()
    new_mapped_seq = Sequence.deserialize(mapped_seq.serialize())
    assert not new_mapped_seq.is_register_mappable()
Example #6
0
def test_magnetic_field():
    seq = Sequence(reg, MockDevice)
    with pytest.raises(
            AttributeError,
            match="only defined when the sequence "
            "is in 'XY Mode'.",
    ):
        seq.magnetic_field
    seq.declare_channel("ch0", "mw_global")  # seq in XY mode
    # mag field is the default
    assert np.all(seq.magnetic_field == np.array((0.0, 0.0, 30.0)))
    seq.set_magnetic_field(bx=1.0, by=-1.0, bz=0.5)
    assert np.all(seq.magnetic_field == np.array((1.0, -1.0, 0.5)))
    with pytest.raises(ValueError, match="magnitude greater than 0"):
        seq.set_magnetic_field(bz=0.0)
    assert seq._empty_sequence
    seq.add(Pulse.ConstantPulse(100, 1, 1, 0), "ch0")
    assert not seq._empty_sequence
    with pytest.raises(ValueError, match="can only be set on an empty seq"):
        seq.set_magnetic_field(1.0, 0.0, 0.0)

    seq2 = Sequence(reg, MockDevice)
    seq2.declare_channel("ch0", "rydberg_global")  # not in XY mode
    with pytest.raises(ValueError, match="can only be set in 'XY Mode'."):
        seq2.set_magnetic_field(1.0, 0.0, 0.0)

    seq3 = Sequence(reg, MockDevice)
    seq3.set_magnetic_field(1.0, 0.0, 0.0)  # sets seq to XY mode
    assert set(seq3.available_channels) == {"mw_global"}
    seq3.declare_channel("ch0", "mw_global")
    # Does not change to default
    assert np.all(seq3.magnetic_field == np.array((1.0, 0.0, 0.0)))
    var = seq3.declare_variable("var")
    # Sequence is marked as non-empty when parametrized too
    seq3.add(Pulse.ConstantPulse(100, var, 1, 0), "ch0")
    assert seq3.is_parametrized()
    with pytest.raises(ValueError, match="can only be set on an empty seq"):
        seq3.set_magnetic_field()

    seq3_str = seq3.serialize()
    seq3_ = Sequence.deserialize(seq3_str)
    assert seq3_._in_xy
    assert str(seq3) == str(seq3_)
    assert np.all(seq3_.magnetic_field == np.array((1.0, 0.0, 0.0)))
Example #7
0
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_)
Example #8
0
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')

    with patch('matplotlib.pyplot.show'):
        seq.draw()

    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()

    s = seq.serialize()
    assert json.loads(s)["__version__"] == pulser.__version__
    seq_ = Sequence.deserialize(s)
    assert str(seq) == str(seq_)