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_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_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_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_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_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_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_creation(): empty_dict = {} with pytest.raises(ValueError, match="Cannot create a Register with"): Register(empty_dict) coords = [(0, 0), (1, 0)] ids = ("q0", "q1") qubits = dict(zip(ids, coords)) with pytest.raises(TypeError): Register(coords) Register(ids) with pytest.raises(ValueError, match="vectors of size 2"): Register.from_coordinates([(0, 1, 0, 1)]) with pytest.raises(NotImplementedError, match="a prefix and a set of labels"): Register.from_coordinates(coords, prefix="a", labels=["a", "b"]) with pytest.raises(ValueError, match="vectors of size 3"): Register3D.from_coordinates([((1, 0), ), ((-1, 0), )]) reg1 = Register(qubits) reg2 = Register.from_coordinates(coords, center=False, prefix="q") assert np.all(np.array(reg1._coords) == np.array(reg2._coords)) assert reg1._ids == reg2._ids reg2b = Register.from_coordinates(coords, center=False, labels=["a", "b"]) assert reg2b._ids == ("a", "b") with pytest.raises(ValueError, match="Label length"): Register.from_coordinates(coords, center=False, labels=["a", "b", "c"]) reg3 = Register.from_coordinates(np.array(coords), prefix="foo") coords_ = np.array([(-0.5, 0), (0.5, 0)]) assert reg3._ids == ("foo0", "foo1") assert np.all(reg3._coords == coords_) assert not np.all(coords_ == coords) reg4 = Register.rectangle(1, 2, spacing=1) assert np.all(reg4._coords == coords_) reg5 = Register.square(2, spacing=2) coords_ = np.array([(-1, -1), (1, -1), (-1, 1), (1, 1)], dtype=float) assert np.all(np.array(reg5._coords) == coords_) reg6 = Register.triangular_lattice(2, 2, spacing=4) coords_ = np.array([ (-3, -np.sqrt(3)), (1, -np.sqrt(3)), (-1, np.sqrt(3)), (3, np.sqrt(3)), ]) assert np.all(np.array(reg6._coords) == coords_) with pytest.raises(ValueError, match="must only be 'layout' and 'trap_ids'"): Register(qubits, spacing=10, layout="square", trap_ids=(0, 1, 3))
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_effective_size_disjoint(): simple_reg = Register.square(2, prefix="atom") rise = Pulse.ConstantPulse(1500, 0, 0, 0) for channel_type in ["mw_global", "rydberg_global", "raman_global"]: np.random.seed(15092021) seq = Sequence(simple_reg, MockDevice) seq.declare_channel("ch0", channel_type) seq.add(rise, "ch0") seq.config_slm_mask(["atom1"]) sim = Simulation(seq, sampling_rate=0.01) sim.set_config(SimConfig("SPAM", eta=0.4)) assert sim._bad_atoms == { "atom0": True, "atom1": False, "atom2": True, "atom3": False, } assert sim.get_hamiltonian(0) == 0 * sim.build_operator( [("I", "global")])
def test_creation(): coords = [(0, 0), (1, 0)] ids = ['q0', 'q1'] qubits = dict(zip(ids, coords)) with pytest.raises(TypeError): Register(coords) Register(ids) with pytest.raises(ValueError, match="vectors of size 2 or 3"): Register.from_coordinates([(0, 1, 0, 1)]) with pytest.raises(ValueError, match="vectors of size 2 or 3"): Register.from_coordinates([((1, 0),), ((-1, 0),)]) reg1 = Register(qubits) reg2 = Register.from_coordinates(coords, center=False, prefix='q') assert np.all(np.array(reg1._coords) == np.array(reg2._coords)) assert reg1._ids == reg2._ids reg3 = Register.from_coordinates(np.array(coords), prefix='foo') coords_ = np.array([(-0.5, 0), (0.5, 0)]) assert reg3._ids == ['foo0', 'foo1'] assert np.all(reg3._coords == coords_) assert not np.all(coords_ == coords) reg4 = Register.rectangle(1, 2, spacing=1) assert np.all(reg4._coords == coords_) reg5 = Register.square(2, spacing=2) coords_ = np.array([(-1, -1), (1, -1), (-1, 1), (1, 1)], dtype=float) assert np.all(np.array(reg5._coords) == coords_) reg6 = Register.triangular_lattice(2, 2, spacing=4) coords_ = np.array([(-3, -np.sqrt(3)), (1, -np.sqrt(3)), (-1, np.sqrt(3)), (3, np.sqrt(3))]) assert np.all(np.array(reg6._coords) == coords_)
def test_rotation(): 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_))