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_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_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 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_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_get_hamiltonian(): simple_reg = Register.from_coordinates([[10, 0], [0, 0]], prefix='atom') detun = 1. rise = Pulse.ConstantDetuning(RampWaveform(1500, 0., 2.), detun, 0.) simple_seq = Sequence(simple_reg, Chadoq2) simple_seq.declare_channel('ising', 'rydberg_global') simple_seq.add(rise, 'ising') simple_sim = Simulation(simple_seq, sampling_rate=0.01) with pytest.raises(ValueError, match='larger than'): simple_sim.get_hamiltonian(1650) with pytest.raises(ValueError, match='negative'): simple_sim.get_hamiltonian(-10) # Constant detuning, so |rr><rr| term is C_6/r^6 - 2*detuning for any time simple_ham = simple_sim.get_hamiltonian(143) assert (simple_ham[0, 0] == Chadoq2.interaction_coeff / 10**6 - 2 * detun)
def test_get_hamiltonian(): simple_reg = Register.from_coordinates([[10, 0], [0, 0]], prefix="atom") detun = 1.0 rise = Pulse.ConstantDetuning(RampWaveform(1500, 0.0, 2.0), detun, 0.0) simple_seq = Sequence(simple_reg, Chadoq2) simple_seq.declare_channel("ising", "rydberg_global") simple_seq.add(rise, "ising") simple_sim = Simulation(simple_seq, sampling_rate=0.01) with pytest.raises(ValueError, match="less than or equal to"): simple_sim.get_hamiltonian(1650) with pytest.raises(ValueError, match="greater than or equal to"): simple_sim.get_hamiltonian(-10) # Constant detuning, so |rr><rr| term is C_6/r^6 - 2*detuning for any time simple_ham = simple_sim.get_hamiltonian(143) assert np.isclose(simple_ham[0, 0], Chadoq2.interaction_coeff / 10**6 - 2 * detun) np.random.seed(123) simple_sim_noise = Simulation(simple_seq, config=SimConfig(noise="doppler", temperature=20000)) simple_ham_noise = simple_sim_noise.get_hamiltonian(144) assert np.isclose( simple_ham_noise.full(), np.array([ [ 4.47984523 + 0.0j, 0.09606404 + 0.0j, 0.09606404 + 0.0j, 0.0 + 0.0j, ], [ 0.09606404 + 0.0j, 12.03082372 + 0.0j, 0.0 + 0.0j, 0.09606404 + 0.0j, ], [ 0.09606404 + 0.0j, 0.0 + 0.0j, -12.97113702 + 0.0j, 0.09606404 + 0.0j, ], [0.0 + 0.0j, 0.09606404 + 0.0j, 0.09606404 + 0.0j, 0.0 + 0.0j], ]), ).all()
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
# 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)
from pulser.json.coders import PulserDecoder, PulserEncoder from pulser.parametrized import ParamObj, Variable from pulser.waveforms import ( 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"):
import json from unittest.mock import patch import numpy as np import pytest from pulser._json_coders import PulserEncoder, PulserDecoder from pulser.parametrized import Variable, ParamObj from pulser.waveforms import (ConstantWaveform, RampWaveform, BlackmanWaveform, CustomWaveform, CompositeWaveform) np.random.seed(20201105) constant = ConstantWaveform(100, -3) ramp = RampWaveform(2e3, 5, 19) arb_samples = np.random.random(52) custom = CustomWaveform(arb_samples) blackman = BlackmanWaveform(40, np.pi) composite = CompositeWaveform(blackman, constant, custom) 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)
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_)