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_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_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_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)
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. import copy import numpy as np import pytest from pulser import Pulse, Register, Sequence from pulser.devices import Chadoq2, MockDevice from pulser.parametrized import Variable from pulser.parametrized.variable import VariableItem from pulser.waveforms import BlackmanWaveform reg = Register.rectangle(4, 3) device = Chadoq2 def test_var_declarations(): sb = Sequence(reg, device) assert sb.declared_variables == {} var = sb.declare_variable("var", size=1) assert sb.declared_variables == {"var": var} assert isinstance(var, Variable) assert var.dtype == float assert var.size == 1 with pytest.raises(ValueError, match="already being used"): sb.declare_variable("var", dtype=int, size=10) var3 = sb.declare_variable("var3") assert sb.declared_variables["var3"] == var3.var