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_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_triangular_lattice(): # Check rows with pytest.raises(ValueError, match="The number of rows"): Register.triangular_lattice(0, 2) # Check columns with pytest.raises(ValueError, match="The number of atoms per row"): Register.triangular_lattice(2, 0) # Check spacing with pytest.raises(ValueError, match="Spacing"): Register.triangular_lattice(2, 2, 0.0)
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_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_)
# 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 Sequence, Pulse, Register from pulser.devices import Chadoq2, MockDevice from pulser.devices._pasqal_device import PasqalDevice from pulser.sequence import _TimeSlot from pulser.waveforms import BlackmanWaveform reg = Register.triangular_lattice(4, 7, spacing=5, prefix='q') device = Chadoq2 def test_init(): fake_device = PasqalDevice("fake", 2, 10, 10, 1, Chadoq2._channels) with pytest.raises(ValueError, match='imported from pasqal.devices'): Sequence(reg, fake_device) seq = Sequence(reg, device) assert seq.qubit_info == reg.qubits assert seq.declared_channels == {} assert seq.available_channels.keys() == device.channels.keys() def test_channel_declaration():