def test_incorrect_number_of_measurements(self, measure_op, measure_name): """Test that an exception is raised if the compiler is called with a device spec with an incorrect number of measurements""" class DummyCompiler(Compiler): """A circuit with 2 modes""" interactive = True primitives = {"MeasureHomodyne", "MeasureHeterodyne", "MeasureFock"} decompositions = set() # set maximum number of measurements to 2, and measure 3 in prog below device_dict = { "target": "simulon_gaussian", "modes": {"pnr_max": 2, "homodyne_max": 2, "heterodyne_max": 2}, "layout": "", "gate_parameters": {}, "compiler": ["gaussian"], } spec = sf.DeviceSpec(spec=device_dict) prog = sf.Program(3) with prog.context as q: for reg in q: measure_op | reg with pytest.raises(program.CircuitError, match=f"contains 3 {measure_name} measurements"): prog.compile(device=spec, compiler=DummyCompiler())
def test_run_optimizations(self): """Test that circuit is optimized when optimize is True""" class DummyCircuit(Compiler): """A circuit with 2 modes""" interactive = True primitives = {"Rgate"} decompositions = set() device_dict = { "target": "dummy_target", "modes": 3, "layout": "", "gate_parameters": {}, "compiler": ["gaussian"], } spec = sf.DeviceSpec(spec=device_dict) prog = sf.Program(3) with prog.context as q: ops.Rgate(0.3) | q[0] ops.Rgate(0.4) | q[0] new_prog = prog.compile( compiler=DummyCircuit(), optimize=True, ) assert new_prog.circuit[0].__str__() == "Rgate(0.7) | (q[0])"
def test_incorrect_modes(self): """Test that an exception is raised if the compiler is called with a device spec with an incorrect number of modes""" class DummyCompiler(Compiler): """A circuit with 2 modes""" interactive = True primitives = {"S2gate", "Interferometer"} decompositions = set() device_dict = { "target": "simulon_gaussian", "modes": 2, "layout": "", "gate_parameters": {}, "compiler": ["gaussian"], } spec = sf.DeviceSpec(spec=device_dict) prog = sf.Program(3) with prog.context as q: ops.S2gate(0.6) | [q[0], q[1]] ops.S2gate(0.6) | [q[1], q[2]] with pytest.raises( program.CircuitError, match="program contains 3 modes, but the device 'simulon_gaussian' only supports a 2-mode program", ): new_prog = prog.compile(device=spec, compiler=DummyCompiler())
def test_assert_max_number_of_measurements_wrong_entry(self): """Check that the correct error is raised when calling `prog.assert_number_of_measurements` with the incorrect type of device spec mode entry.""" device_dict = { "target": "simulon_gaussian", "modes": 2, "layout": "", "gate_parameters": {}, "compiler": ["gaussian"], } spec = sf.DeviceSpec(spec=device_dict) prog = sf.Program(3) with prog.context as q: ops.S2gate(0.6) | [q[0], q[1]] ops.S2gate(0.6) | [q[1], q[2]] with pytest.raises(KeyError, match="Expected keys for the maximum allowed number of PNR"): prog.assert_max_number_of_measurements(spec)
def test_assert_max_number_of_measurements(self, measure_op, measure_name): """Check that the correct error is raised when calling `prog.assert_number_of_measurements` with the incorrect number of measurements in the circuit.""" # set maximum number of measurements to 2, and measure 3 in prog below device_dict = { "target": "simulon_gaussian", "modes": {"pnr_max": 2, "homodyne_max": 2, "heterodyne_max": 2}, "layout": "", "gate_parameters": {}, "compiler": ["gaussian"], } spec = sf.DeviceSpec(spec=device_dict) prog = sf.Program(3) with prog.context as q: for reg in q: measure_op | reg with pytest.raises(program.CircuitError, match=f"contains 3 {measure_name} measurements"): prog.assert_max_number_of_measurements(spec)
def test_keyerror_assert_max_number_of_measurements(self): """Check that the correct error is raised when calling `prog.assert_number_of_measurements` with an incorrect device spec modes entry.""" # set maximum number of measurements to 2, and measure 3 in prog below device_dict = { "target": "simulon_gaussian", "modes": {"max": {"pnr": 2, "homodyne": 2, "heterodyne": 2}}, "layout": "", "gate_parameters": {}, "compiler": ["gaussian"], } spec = sf.DeviceSpec(spec=device_dict) prog = sf.Program(3) with prog.context as q: for reg in q: ops.MeasureFock() | reg match = "Expected keys for the maximum allowed number of PNR" with pytest.raises(KeyError, match=match): prog.assert_max_number_of_measurements(spec)
def test_validate_parameters(self): """Test that the parameters are validated in the compile method""" mock_layout = textwrap.dedent( """\ name mock version 1.0 S2gate({squeezing_amplitude_0}, 0.0) | [0, 1] """ ) device_dict = { "target": None, "layout": mock_layout, "modes": 2, "compiler": ["DummyCompiler"], "gate_parameters": { "squeezing_amplitude_0": [0, 1], }, } class DummyCircuit(Compiler): """A circuit with 2 modes""" interactive = True primitives = {"S2gate"} decompositions = set() spec = sf.DeviceSpec(spec=device_dict) prog = sf.Program(2) with prog.context as q: ops.S2gate(1.5) | q # invalid value 1.5 with pytest.raises(ValueError, match="has invalid value"): new_prog = prog.compile( device=spec, compiler=DummyCircuit(), )
def test_assert_number_of_modes(self): """Check that the correct error is raised when calling `prog.assert_number_of_modes` with the incorrect number of modes.""" device_dict = { "target": "abc", "modes": 2, "layout": "", "gate_parameters": {}, "compiler": ["DummyCompiler"], } spec = sf.DeviceSpec(spec=device_dict) prog = sf.Program(3) with prog.context as q: ops.S2gate(0.6) | [q[0], q[1]] ops.S2gate(0.6) | [q[1], q[2]] with pytest.raises( program.CircuitError, match="program contains 3 modes, but the device 'abc' only supports a 2-mode program", ): prog.assert_number_of_modes(spec)