def test_dephasing(): np.random.seed(123) reg = Register.from_coordinates([(0, 0)], prefix="q") seq = Sequence(reg, Chadoq2) seq.declare_channel("ch0", "rydberg_global") duration = 2500 pulse = Pulse.ConstantPulse(duration, np.pi, 0.0 * 2 * np.pi, 0) seq.add(pulse, "ch0") sim = Simulation(seq, sampling_rate=0.01, config=SimConfig(noise="dephasing")) assert sim.run().sample_final_state() == Counter({"0": 595, "1": 405}) assert len(sim._collapse_ops) != 0 with pytest.warns(UserWarning, match="first-order"): reg = Register.from_coordinates([(0, 0), (0, 10)], prefix="q") seq2 = Sequence(reg, Chadoq2) seq2.declare_channel("ch0", "rydberg_global") duration = 2500 pulse = Pulse.ConstantPulse(duration, np.pi, 0.0 * 2 * np.pi, 0) seq2.add(pulse, "ch0") sim = Simulation( seq2, sampling_rate=0.01, config=SimConfig(noise="dephasing", dephasing_prob=0.5), )
def test_drawing(): with pytest.raises(ValueError, match="Blockade radius"): reg = Register.from_coordinates([(1, 0), (0, 1)]) reg.draw(blockade_radius=0.0, draw_half_radius=True) reg = Register.from_coordinates([(1, 0), (0, 1)]) with patch("matplotlib.pyplot.show"): reg.draw(blockade_radius=0.1, draw_graph=True) reg = Register.triangular_lattice(3, 8) with patch("matplotlib.pyplot.show"): reg.draw() with patch("matplotlib.pyplot.show"): with patch("matplotlib.pyplot.savefig"): reg.draw(fig_name="my_register.pdf") reg = Register.rectangle(1, 8) with patch("matplotlib.pyplot.show"): reg.draw(blockade_radius=5, draw_half_radius=True, draw_graph=True) with pytest.raises(ValueError, match="'blockade_radius' to draw."): reg.draw(draw_half_radius=True) reg = Register.square(1) with pytest.raises(NotImplementedError, match="Needs more than one atom"): reg.draw(blockade_radius=5, draw_half_radius=True)
def test_square(): # Check side with pytest.raises(ValueError, match="The number of atoms per side"): Register.square(0) # Check spacing with pytest.raises(ValueError, match="Spacing"): Register.square(2, 0.0)
def test_rotation(): with pytest.raises(NotImplementedError): reg_ = Register.from_coordinates([(1, 0, 0), (0, 1, 4)]) reg_.rotate(20) reg = Register.square(2, spacing=np.sqrt(2)) reg.rotate(45) coords_ = np.array([(0, -1), (1, 0), (-1, 0), (0, 1)], dtype=float) assert np.all(np.isclose(reg._coords, coords_))
def test_config_slm_mask(): reg_s = Register({"q0": (0, 0), "q1": (10, 10), "q2": (-10, -10)}) seq_s = Sequence(reg_s, device) with pytest.raises(TypeError, match="must be castable to set"): seq_s.config_slm_mask(0) with pytest.raises(TypeError, match="must be castable to set"): seq_s.config_slm_mask((0)) with pytest.raises(ValueError, match="exist in the register"): seq_s.config_slm_mask("q0") with pytest.raises(ValueError, match="exist in the register"): seq_s.config_slm_mask(["q3"]) with pytest.raises(ValueError, match="exist in the register"): seq_s.config_slm_mask(("q3", )) with pytest.raises(ValueError, match="exist in the register"): seq_s.config_slm_mask({"q3"}) with pytest.raises(ValueError, match="exist in the register"): seq_s.config_slm_mask([0]) with pytest.raises(ValueError, match="exist in the register"): seq_s.config_slm_mask((0, )) with pytest.raises(ValueError, match="exist in the register"): seq_s.config_slm_mask({0}) targets_s = ["q0", "q2"] seq_s.config_slm_mask(targets_s) assert seq_s._slm_mask_targets == {"q0", "q2"} with pytest.raises(ValueError, match="configured only once"): seq_s.config_slm_mask(targets_s) reg_i = Register({0: (0, 0), 1: (10, 10), 2: (-10, -10)}) seq_i = Sequence(reg_i, device) with pytest.raises(TypeError, match="must be castable to set"): seq_i.config_slm_mask(0) with pytest.raises(TypeError, match="must be castable to set"): seq_i.config_slm_mask((0)) with pytest.raises(ValueError, match="exist in the register"): seq_i.config_slm_mask("q0") with pytest.raises(ValueError, match="exist in the register"): seq_i.config_slm_mask([3]) with pytest.raises(ValueError, match="exist in the register"): seq_i.config_slm_mask((3, )) with pytest.raises(ValueError, match="exist in the register"): seq_i.config_slm_mask({3}) with pytest.raises(ValueError, match="exist in the register"): seq_i.config_slm_mask(["0"]) with pytest.raises(ValueError, match="exist in the register"): seq_i.config_slm_mask(("0", )) with pytest.raises(ValueError, match="exist in the register"): seq_i.config_slm_mask({"0"}) targets_i = [0, 2] seq_i.config_slm_mask(targets_i) assert seq_i._slm_mask_targets == {0, 2} with pytest.raises(ValueError, match="configured only once"): seq_i.config_slm_mask(targets_i)
def test_find_indices(): reg = Register(dict(a=(0, 0), c=(5, 0), b=(0, 5))) assert reg.find_indices(["c", "b", "a"]) == [1, 2, 0] with pytest.raises( ValueError, match="IDs list must be selected among" "the IDs of the register's qubits", ): reg.find_indices(["c", "e", "d"])
def test_mask_two_pulses(): """Similar to test_mask_equals_remove, but with more pulses afterwards. Three global pulses act on a three qubit register, with one qubit masked during the first pulse. """ reg_three = Register({"q0": (0, 0), "q1": (10, 10), "q2": (-10, -10)}) reg_two = Register({"q0": (0, 0), "q1": (10, 10)}) pulse = Pulse.ConstantPulse(100, 10, 0, 0) no_pulse = Pulse.ConstantPulse(100, 0, 0, 0) for channel_type in ["mw_global", "rydberg_global", "raman_global"]: # Masked simulation seq_masked = Sequence(reg_three, MockDevice) seq_masked.declare_channel("ch_masked", channel_type) masked_qubits = ["q2"] seq_masked.config_slm_mask(masked_qubits) seq_masked.add(pulse, "ch_masked") # First pulse: masked seq_masked.add(pulse, "ch_masked") # Second pulse: unmasked seq_masked.add(pulse, "ch_masked") # Third pulse: unmasked sim_masked = Simulation(seq_masked) # Unmasked simulation on full register seq_three = Sequence(reg_three, MockDevice) seq_three.declare_channel("ch_three", channel_type) seq_three.add(no_pulse, "ch_three") seq_three.add(pulse, "ch_three") seq_three.add(pulse, "ch_three") sim_three = Simulation(seq_three) # Unmasked simulation on reduced register seq_two = Sequence(reg_two, MockDevice) seq_two.declare_channel("ch_two", channel_type) seq_two.add(pulse, "ch_two") seq_two.add(no_pulse, "ch_two") seq_two.add(no_pulse, "ch_two") sim_two = Simulation(seq_two) ti = seq_masked._slm_mask_time[0] tf = seq_masked._slm_mask_time[1] for t in sim_masked.sampling_times: ham_masked = sim_masked.get_hamiltonian(t) ham_three = sim_three.get_hamiltonian(t) ham_two = sim_two.get_hamiltonian(t) if ti <= t <= tf: assert ham_masked == qutip.tensor(ham_two, qutip.qeye(2)) else: assert ham_masked == ham_three
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 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_noisy_xy(): np.random.seed(15092021) simple_reg = Register.square(2, prefix="atom") detun = 1.0 amp = 3.0 rise = Pulse.ConstantPulse(1500, amp, detun, 0.0) simple_seq = Sequence(simple_reg, MockDevice) simple_seq.declare_channel("ch0", "mw_global") simple_seq.add(rise, "ch0") sim = Simulation(simple_seq, sampling_rate=0.01) with pytest.raises(NotImplementedError, match="mode 'XY' does not support simulation of"): sim.set_config(SimConfig(("SPAM", "doppler"))) sim.set_config(SimConfig("SPAM", eta=0.4)) assert sim._bad_atoms == { "atom0": True, "atom1": False, "atom2": True, "atom3": False, } with pytest.raises(NotImplementedError, match="simulation of noise types: amplitude"): sim.add_config(SimConfig("amplitude"))
def test_get_xy_hamiltonian(): simple_reg = Register.from_coordinates([[0, 10], [10, 0], [0, 0]], prefix="atom") detun = 1.0 amp = 3.0 rise = Pulse.ConstantPulse(1500, amp, detun, 0.0) simple_seq = Sequence(simple_reg, MockDevice) simple_seq.declare_channel("ch0", "mw_global") simple_seq.set_magnetic_field(0, 1.0, 0.0) simple_seq.add(rise, "ch0") assert np.isclose(np.linalg.norm(simple_seq.magnetic_field[0:2]), 1) simple_sim = Simulation(simple_seq, sampling_rate=0.03) with pytest.raises(ValueError, match="less than or equal to the sequence duration"): simple_sim.get_hamiltonian(1650) with pytest.raises(ValueError, match="greater than or equal to 0"): simple_sim.get_hamiltonian(-10) # Constant detuning, so |ud><du| term is C_3/r^3 - 2*detuning for any time simple_ham = simple_sim.get_hamiltonian(143) assert simple_ham[1, 2] == 0.5 * MockDevice.interaction_coeff_xy / 10**3 assert (np.abs(simple_ham[1, 4] - (-2 * 0.5 * MockDevice.interaction_coeff_xy / 10**3)) < 1e-10) assert simple_ham[0, 1] == 0.5 * amp assert simple_ham[3, 3] == -2 * detun
def test_config(): np.random.seed(123) reg = Register.from_coordinates([(0, 0), (0, 5)], prefix="q") seq = Sequence(reg, Chadoq2) seq.declare_channel("ch0", "rydberg_global") duration = 2500 pulse = Pulse.ConstantPulse(duration, np.pi, 0.0 * 2 * np.pi, 0) seq.add(pulse, "ch0") sim = Simulation(seq, config=SimConfig(noise="SPAM")) sim.reset_config() assert sim.config == SimConfig() sim.show_config() with pytest.raises(ValueError, match="not a valid"): sim.set_config("bad_config") clean_ham = sim.get_hamiltonian(123) new_cfg = SimConfig(noise="doppler", temperature=10000) sim.set_config(new_cfg) assert sim.config == new_cfg noisy_ham = sim.get_hamiltonian(123) assert (noisy_ham[0, 0] != clean_ham[0, 0] and noisy_ham[3, 3] == clean_ham[3, 3]) sim.set_config(SimConfig(noise="amplitude")) noisy_amp_ham = sim.get_hamiltonian(123) assert (noisy_amp_ham[0, 0] == clean_ham[0, 0] and noisy_amp_ham[0, 1] != clean_ham[0, 1])
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_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_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()
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_mask_equals_remove(): """Check that masking is equivalent to removing the masked qubits. A global pulse acting on three qubits of which one is masked, should be equivalent to acting on a register with only the two unmasked qubits. """ reg_three = Register({"q0": (0, 0), "q1": (10, 10), "q2": (-10, -10)}) reg_two = Register({"q0": (0, 0), "q1": (10, 10)}) pulse = Pulse.ConstantPulse(100, 10, 0, 0) local_pulse = Pulse.ConstantPulse(200, 10, 0, 0) for channel_type in ["mw_global", "rydberg_global", "raman_global"]: # Masked simulation seq_masked = Sequence(reg_three, MockDevice) if channel_type == "mw_global": seq_masked.set_magnetic_field(0, 1.0, 0.0) else: # Add a local channel acting on a masked qubit (has no effect) seq_masked.declare_channel( "local", channel_type[:-len("global")] + "local", initial_target="q2", ) seq_masked.add(local_pulse, "local") seq_masked.declare_channel("ch_masked", channel_type) masked_qubits = ["q2"] seq_masked.config_slm_mask(masked_qubits) seq_masked.add(pulse, "ch_masked") sim_masked = Simulation(seq_masked) # Simulation on reduced register seq_two = Sequence(reg_two, MockDevice) if channel_type == "mw_global": seq_two.set_magnetic_field(0, 1.0, 0.0) seq_two.declare_channel("ch_two", channel_type) if channel_type != "mw_global": seq_two.delay(local_pulse.duration, "ch_two") seq_two.add(pulse, "ch_two") sim_two = Simulation(seq_two) # Check equality for t in sim_two.sampling_times: ham_masked = sim_masked.get_hamiltonian(t) ham_two = sim_two.get_hamiltonian(t) assert ham_masked == qutip.tensor(ham_two, qutip.qeye(2))
def test_drawing(): with pytest.raises(NotImplementedError, match="register layouts in 2D."): reg_ = Register.from_coordinates([(1, 0, 0), (0, 1, 4)]) reg_.draw() reg = Register.triangular_lattice(3, 8) with patch('matplotlib.pyplot.show'): reg.draw() reg = Register.rectangle(1, 8) with patch('matplotlib.pyplot.show'): reg.draw(blockade_radius=5, draw_half_radius=True, draw_graph=True) with pytest.raises(ValueError, match="'blockade_radius' to draw."): reg.draw(draw_half_radius=True) reg = Register.square(1) with pytest.raises(NotImplementedError, match="Needs more than one atom"): reg.draw(blockade_radius=5, draw_half_radius=True)
def test_register_from_layout(): layout = RegisterLayout([[0, 0], [1, 1], [1, 0], [0, 1]]) reg = layout.define_register(1, 0) assert reg == Register({"q0": [0, 1], "q1": [0, 0]}) seq = Sequence(reg, device=MockDevice) new_reg = encode_decode(seq).register assert reg == new_reg assert new_reg.layout == layout assert new_reg._layout_info.trap_ids == (1, 0)
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_equality_function(): reg1 = Register({"c": (1, 2), "d": (8, 4)}) assert_eq(reg1, reg1) assert_eq(reg1, Register({"d": (8, 4), "c": (1, 2)})) assert_ineq(reg1, Register({"c": (8, 4), "d": (1, 2)})) assert_ineq(reg1, Register({"c": (1, 2), "d": (8, 4), "e": (8, 4)})) assert_ineq(reg1, 10) reg2 = Register3D({"a": (1, 2, 3), "b": (8, 5, 6)}) assert_eq(reg2, reg2) assert_eq(reg2, Register3D({"a": (1, 2, 3), "b": (8, 5, 6)})) assert_ineq(reg2, Register3D({"b": (1, 2, 3), "a": (8, 5, 6)})) assert_ineq(reg2, Register3D({ "a": (1, 2, 3), "b": (8, 5, 6), "e": (8, 5, 6) })) assert_ineq(reg2, 10) assert_ineq(reg1, reg2)
def test_str(): reg_ = Register.rectangle(2, 1, prefix="q") sb = Sequence(reg_, device) sb.declare_channel("ch1", "rydberg_global") with pytest.warns(UserWarning, match="Building a non-parametrized"): seq = sb.build() var = sb.declare_variable("var") pls = Pulse.ConstantPulse(var * 100, var, -1, var) sb.add(pls, "ch1") s = (f"Prelude\n-------\n{str(seq)}Stored calls\n------------\n\n" + "1. add(Pulse.ConstantPulse(mul(var, 100), var, -1, var), ch1)") assert s == str(sb)
def test_hexagon(): # Check number of layers with pytest.raises(ValueError, match="The number of layers"): Register.hexagon(0) # Check spacing with pytest.raises(ValueError, match="Spacing "): Register.hexagon(1, spacing=-1.0) # Check small hexagon (1 layer) reg = Register.hexagon(1, spacing=1.0) assert len(reg.qubits) == 7 atoms = list(reg.qubits.values()) crest_y = np.sqrt(3) / 2 assert np.all(np.isclose(atoms[0], [0.0, 0.0])) assert np.all(np.isclose(atoms[1], [-0.5, crest_y])) assert np.all(np.isclose(atoms[2], [0.5, crest_y])) assert np.all(np.isclose(atoms[3], [1.0, 0.0])) assert np.all(np.isclose(atoms[4], [0.5, -crest_y])) assert np.all(np.isclose(atoms[5], [-0.5, -crest_y])) assert np.all(np.isclose(atoms[6], [-1.0, 0.0])) # Check a few atoms for a bigger hexagon (2 layers) reg = Register.hexagon(2, spacing=1.0) assert len(reg.qubits) == 19 atoms = list(reg.qubits.values()) crest_y = np.sqrt(3) / 2.0 assert np.all(np.isclose(atoms[7], [-1.5, crest_y])) assert np.all(np.isclose(atoms[8], [-1.0, 2.0 * crest_y])) assert np.all(np.isclose(atoms[9], [-0.0, 2.0 * crest_y])) assert np.all(np.isclose(atoms[13], [1.5, -crest_y])) assert np.all(np.isclose(atoms[14], [1.0, -2.0 * crest_y])) assert np.all(np.isclose(atoms[15], [0.0, -2.0 * crest_y]))
def test_expect(): with pytest.raises(TypeError, match="must be a list"): results.expect("bad_observable") with pytest.raises(TypeError, match="Incompatible type"): results.expect(["bad_observable"]) with pytest.raises(ValueError, match="Incompatible shape"): results.expect([np.array(3)]) reg_single = Register.from_coordinates([(0, 0)], prefix="q") seq_single = Sequence(reg_single, Chadoq2) seq_single.declare_channel("ryd", "rydberg_global") seq_single.add(pi, "ryd") sim_single = Simulation(seq_single) results_single = sim_single.run() op = [qutip.basis(2, 0).proj()] exp = results_single.expect(op)[0] assert np.isclose(exp[-1], 1) assert len(exp) == duration + 1 # +1 for the final instant np.testing.assert_almost_equal( results_single._calc_pseudo_density(-1).full(), np.array([[1, 0], [0, 0]]), ) config = SimConfig(noise="SPAM", eta=0) sim_single.set_config(config) sim_single.evaluation_times = "Minimal" results_single = sim_single.run() exp = results_single.expect(op)[0] assert len(exp) == 2 assert isinstance(results_single, CoherentResults) assert results_single._meas_errors == { "epsilon": config.epsilon, "epsilon_prime": config.epsilon_prime, } # Probability of measuring 1 = probability of false positive assert np.isclose(exp[0], config.epsilon) # Probability of measuring 1 = 1 - probability of false negative assert np.isclose(exp[-1], 1 - config.epsilon_prime) np.testing.assert_almost_equal( results_single._calc_pseudo_density(-1).full(), np.array([[1 - config.epsilon_prime, 0], [0, config.epsilon_prime]]), ) seq3dim = Sequence(reg, Chadoq2) seq3dim.declare_channel("ryd", "rydberg_global") seq3dim.declare_channel("ram", "raman_local", initial_target="A") seq3dim.add(pi, "ram") seq3dim.add(pi, "ryd") sim3dim = Simulation(seq3dim) exp3dim = sim3dim.run().expect( [qutip.tensor(qutip.basis(3, 0).proj(), qutip.qeye(3))]) assert np.isclose(exp3dim[0][-1], 1.89690200e-14)
def test_mask_nopulses(): """Check interaction between SLM mask and a simulation with no pulses.""" reg = Register({"q0": (0, 0), "q1": (10, 10), "q2": (-10, -10)}) for channel_type in ["mw_global", "rydberg_global"]: seq_empty = Sequence(reg, MockDevice) if channel_type == "mw_global": seq_empty.set_magnetic_field(0, 1.0, 0.0) seq_empty.declare_channel("ch", channel_type) seq_empty.delay(duration=100, channel="ch") masked_qubits = ["q2"] seq_empty.config_slm_mask(masked_qubits) sim_empty = Simulation(seq_empty) assert seq_empty._slm_mask_time == [] assert sim_empty._seq._slm_mask_time == []
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_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_mappable_register(): layout = TriangularLatticeLayout(100, 5) mapp_reg = layout.make_mappable_register(10) seq = Sequence(mapp_reg, Chadoq2) assert seq.is_register_mappable() reserved_qids = tuple([f"q{i}" for i in range(10)]) assert seq._qids == set(reserved_qids) with pytest.raises(RuntimeError, match="Can't access the qubit info"): seq.qubit_info with pytest.raises(RuntimeError, match="Can't access the sequence's register"): seq.register seq.declare_channel("ryd", "rydberg_global") seq.declare_channel("ram", "raman_local", initial_target="q2") seq.add(Pulse.ConstantPulse(100, 1, 0, 0), "ryd") seq.add(Pulse.ConstantPulse(200, 1, 0, 0), "ram") assert seq._last("ryd").targets == set(reserved_qids) assert seq._last("ram").targets == {"q2"} with pytest.raises(ValueError, match="Can't draw the register"): seq.draw(draw_register=True) # Can draw if 'draw_register=False' with patch("matplotlib.pyplot.show"): seq.draw() with pytest.raises(ValueError, match="'qubits' must be specified"): seq.build() with pytest.raises(ValueError, match="targeted but have not been assigned"): seq.build(qubits={"q0": 1, "q1": 10}) with pytest.warns(UserWarning, match="No declared variables named: a"): seq.build(qubits={"q2": 20, "q0": 10}, a=5) seq_ = seq.build(qubits={"q2": 20, "q0": 10}) assert seq_._last("ryd").targets == {"q2", "q0"} assert not seq_.is_register_mappable() assert seq_.register == Register({ "q0": layout.traps_dict[10], "q2": layout.traps_dict[20] }) with pytest.raises(ValueError, match="already has a concrete register"): seq_.build(qubits={"q2": 20, "q0": 10})
def test_rectangle(): # Check rows with pytest.raises(ValueError, match="The number of rows"): Register.rectangle(0, 2) # Check columns with pytest.raises(ValueError, match="The number of columns"): Register.rectangle(2, 0) # Check spacing with pytest.raises(ValueError, match="Spacing"): Register.rectangle(2, 2, 0.0)