def test_fill_template(self, connection, monkeypatch): """Test that the fill_template method works""" prog = sf.tdm.tdmprogram.TDMProgram(2) with prog.context([1, 2], [3, 4]) as (p, q): sf.ops.Sgate(0.7, 0) | q[1] sf.ops.BSgate(p[0]) | (q[0], q[1]) sf.ops.MeasureHomodyne(p[1]) | q[0] spec = DeviceSpec(connection=connection, spec=device_dict_tdm, target="abc") spec.fill_template(prog) assert spec.layout == textwrap.dedent("""\ name template_td2 version 1.0 target abc (shots=1) type tdm (temporal_modes=2) float array p0[1, 2] = {rs_array} float array p1[1, 2] = {bs_array} float array p2[1, 2] = {r_array} float array p3[1, 2] = {m_array} Sgate(p0) | 1 BSgate(p1) | (1, 0) Rgate(p2) | 1 MeasureHomodyne(p3) | 0 """)
def test_refresh(self, connection, monkeypatch): """Tests that the refresh method refreshes the device spec""" spec = DeviceSpec(connection=connection, spec=device_dict, target="abc") assert spec.modes == device_dict["modes"] new_spec_dict = device_dict.copy() new_spec_dict["modes"] = 42 monkeypatch.setattr(connection, "_get_device_dict", lambda target: new_spec_dict) spec.refresh() assert spec.modes != device_dict["modes"] assert spec.modes == 42
def test_initialization(self, connection): """Test that the device spec class initializes correctly.""" spec = DeviceSpec(connection=None, spec=device_dict, target="abc") assert spec.target == "abc" assert spec.layout == device_dict["layout"] assert spec.modes == device_dict["modes"] assert spec.compiler == device_dict["compiler"]
def test_gate_parameters(self): """Test that gate_parameters outputs the correctly parsed parameters""" true_params = { "squeezing_amplitude_0": Ranges([0], [1], variable_name="squeezing_amplitude_0"), "phase_0": Ranges([0], [0, 6.3], variable_name="phase_0"), "phase_1": Ranges([0.5, 1.4], variable_name="phase_1"), } spec_params = DeviceSpec(connection=None, spec=device_dict, target="abc").gate_parameters assert true_params == spec_params
def test_create_program(self, monkeypatch): """Test that the program creation works""" circuit = [ "S2gate(0, 0) | (q[0], q[1])", "MZgate(1.23, 0.5) | (q[0], q[1])", "MeasureFock | (q[0], q[1])", ] params = {"phase_0": 1.23} prog = DeviceSpec(connection=None, spec=device_dict, target="abc").create_program(**params) assert prog.target is None assert prog.name == "mock" assert [str(cmd) for cmd in prog.circuit] == circuit
def test_compile_device_invalid_device_error(self, prog, monkeypatch, caplog): """Tests that an error is raised if the program was compiled for another device and recompilation was not requested.""" caplog.set_level(logging.INFO) test_device_dict = mock_device_dict.copy() test_device_dict["compiler"] = [] monkeypatch.setattr(Connection, "create_job", lambda self, target, program, run_options: program) monkeypatch.setattr(Connection, "_get_device_dict", lambda *args: test_device_dict) monkeypatch.setattr(Program, "compile", lambda *args, **kwargs: self.MockProgram()) # Setting compile_info with a dummy devicespec and compiler name X8_spec = DeviceSpec(target="DummyDevice", connection=None, spec=None) prog._compile_info = (X8_spec, "dummy_compiler") engine = sf.RemoteEngine("X8") with pytest.raises(ValueError, match="Cannot use program compiled"): program = engine.run_async(prog, shots=10)
def test_recompilation_precompiled(self, prog, monkeypatch, caplog): """Test that recompilation happens when: 1. the program was precompiled 2. but the recompile keyword argument was set to True. The program is considered to be precompiled if program.compile_info was set (setting it in the test case). """ caplog.set_level(logging.INFO) test_device_dict = mock_device_dict.copy() test_device_dict["compiler"] = [] monkeypatch.setattr(Connection, "create_job", lambda self, target, program, run_options: program) monkeypatch.setattr(Connection, "_get_device_dict", lambda *args: test_device_dict) monkeypatch.setattr(Program, "compile", lambda *args, **kwargs: self.MockProgram()) # Setting compile_info prog._compile_info = (None, "dummy_compiler") # Setting compile_info with a dummy devicespec and compiler name X8_spec = DeviceSpec(target="DummyDevice", connection=None, spec=None) prog._compile_info = (X8_spec, "dummy_compiler") engine = sf.RemoteEngine("X8") compile_options = None # Setting recompile in keyword arguments program = engine.run_async(prog, shots=10, compile_options=compile_options, recompile=True) assert isinstance(program, self.MockProgram) assert caplog.records[ -1].message == "Recompiling program for device X8_01 using compiler Xunitary."
def device_spec(self): return DeviceSpec(target="X8", spec=mock_device_dict, connection=None)
def test_unknown_parameter(self, params): """Test that error is raised when an unknown parameter is supplied""" with pytest.raises(ValueError, match="not a valid parameter for this device"): DeviceSpec(connection=None, spec=device_dict, target="abc").create_program(**params)
def test_invalid_parameter_value(self, params): """Test that error is raised when an invalid parameter value is supplied""" with pytest.raises(ValueError, match="has invalid value"): DeviceSpec(connection=None, spec=device_dict, target="abc").create_program(**params)
"phase_8": [0, [0, 6.283185307179586]], "phase_9": [0, [0, 6.283185307179586]], "phase_10": [0, [0, 6.283185307179586]], "phase_11": [0, [0, 6.283185307179586]], "final_phase_0": [0, [0, 6.283185307179586]], "final_phase_1": [0, [0, 6.283185307179586]], "final_phase_2": [0, [0, 6.283185307179586]], "final_phase_3": [0, [0, 6.283185307179586]], "final_phase_4": [0, [0, 6.283185307179586]], "final_phase_5": [0, [0, 6.283185307179586]], "final_phase_6": [0, [0, 6.283185307179586]], "final_phase_7": [0, [0, 6.283185307179586]], }, } X8_spec = DeviceSpec(target="X8_01", connection=None, spec=test_spec) def generate_X8_params(r, p): return { "squeezing_amplitude_0": r, "squeezing_amplitude_1": r, "squeezing_amplitude_2": r, "squeezing_amplitude_3": r, "phase_0": p, "phase_1": p, "phase_2": p, "phase_3": p, "phase_4": p, "phase_5": p, "phase_6": p,