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_single_atom_simulation(): one_reg = Register.from_coordinates([(0, 0)], 'atom') one_seq = Sequence(one_reg, Chadoq2) one_seq.declare_channel('ch0', 'rydberg_global') one_seq.add(Pulse.ConstantDetuning(ConstantWaveform(16, 1.), 1., 0), 'ch0') one_sim = Simulation(seq) one_res = one_sim.run() assert (one_res._size == one_sim._size)
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 test_single_atom_simulation(): one_reg = Register.from_coordinates([(0, 0)], "atom") one_seq = Sequence(one_reg, Chadoq2) one_seq.declare_channel("ch0", "rydberg_global") one_seq.add(Pulse.ConstantDetuning(ConstantWaveform(16, 1.0), 1.0, 0), "ch0") one_sim = Simulation(one_seq) one_res = one_sim.run() assert one_res._size == one_sim._size one_sim = Simulation(one_seq, evaluation_times="Minimal") one_resb = one_sim.run() assert one_resb._size == one_sim._size
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 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 test_draw(): pls_ = Pulse.ConstantDetuning(bwf, -10, 1, post_phase_shift=-np.pi) with patch("matplotlib.pyplot.show"): pls_.draw()
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) with pytest.raises(ValueError, match="The duration of"): Pulse(bwf, cwf, 0) with pytest.raises(ValueError, match="All samples of an amplitude"): Pulse(cwf, cwf, 0)
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 assert x._count == 0
from dataclasses import FrozenInstanceError import numpy as np import pytest from pulser import Pulse 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]) c = Variable("c", str) t = Variable("t", int) bwf = BlackmanWaveform(t, a) pulse = Pulse.ConstantDetuning(bwf, *b) 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=str, size=0) x = Variable("x", dtype=float) assert x.value is None assert x._count == 0
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()
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()
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) 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_)