Exemplo n.º 1
0
    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"],
        }
        device = sf.Device(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=device, compiler=DummyCompiler())
Exemplo n.º 2
0
    def test_validate_gate_parameters_bb_program_invalid_param(self):
        """Test the ``.sf.program_utils.validate_gate_parameters`` function with a ``BlackbirdProgram``
        when a parameter value is invalid"""

        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],
            },
        }

        mock_prog = bb.loads(mock_layout.format(squeezing_amplitude_0=42))
        device = sf.Device(spec=device_dict)

        with pytest.raises(ValueError, match="has invalid value 42"):
            validate_gate_parameters(mock_prog, device)
Exemplo n.º 3
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"],
        }
        device = sf.Device(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=device, compiler=DummyCompiler())
Exemplo n.º 4
0
    def test_validate_gate_parameters_sf_program_compiled(self):
        """Test the ``.sf.program_utils.validate_gate_parameters`` function with a compiled ``sf.Program``
        when no device is passed"""

        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],
            },
        }

        mock_prog = sf.Program(2)
        with mock_prog.context as q:
            ops.S2gate(1) | q

        device = sf.Device(spec=device_dict)

        mock_prog._compile_info = (device, "compiler")

        assert validate_gate_parameters(mock_prog)
Exemplo n.º 5
0
    def test_keyerror_assert_modes_dict(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"],
        }
        device = sf.Device(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_modes(device)
Exemplo n.º 6
0
    def test_assert_modes_dict(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"],
        }
        device = sf.Device(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_modes(device)
Exemplo n.º 7
0
    def test_missing_layout(self):
        """Test that an error is raised if the device spec is missing a layout when compiled"""
        device_dict = {
            "target": None,
            "layout": None,
            "modes": 2,
            "compiler": ["DummyCompiler"],
            "gate_parameters": {
                "squeezing_amplitude_0": [0, 1],
            },
        }

        class DummyCompiler(Compiler):
            """A circuit with 2 modes"""

            interactive = True
            primitives = {"S2gate"}
            decompositions = set()

        device = sf.Device(spec=device_dict)

        prog = sf.Program(2)
        with prog.context as q:
            ops.S2gate(0.6) | [q[0], q[1]]

        with pytest.raises(ValueError, match="missing a circuit layout"):
            new_prog = prog.compile(
                device=device,
                compiler=DummyCompiler(),
            )
Exemplo n.º 8
0
    def test_missing_gate_parameters(self, gate_params):
        """Test that an error is raised if the device spec is missing gate parameters when compiled"""
        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":
            gate_params,  # no gate_parameters, so any value is valid
        }

        class DummyCompiler(Compiler):
            """A circuit with 2 modes"""

            interactive = True
            primitives = {"S2gate"}
            decompositions = set()

        device = sf.Device(spec=device_dict)

        prog = sf.Program(2)
        with prog.context as q:
            ops.S2gate(123) | q

        new_prog = prog.compile(
            device=device,
            compiler=DummyCompiler(),
        )

        assert len(new_prog) == 1
        assert device.gate_parameters == gate_params

        # test gates are correct
        circuit = new_prog.circuit
        assert circuit[0].op.__class__.__name__ == "S2gate"
Exemplo n.º 9
0
    def test_assert_modes_dict_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": "pnr",
            "layout": "",
            "gate_parameters": {},
            "compiler": ["gaussian"],
        }
        device = sf.Device(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_modes(device)
Exemplo n.º 10
0
    def test_assert_number_of_modes(self):
        """Check that the correct error is raised when calling `prog.assert_modes`
        with the incorrect number of modes."""
        device_dict = {
            "target": "abc",
            "modes": 2,
            "layout": "",
            "gate_parameters": {},
            "compiler": ["DummyCompiler"],
        }
        device = sf.Device(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_modes(device)
Exemplo n.º 11
0
    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 DummyCompiler(Compiler):
            """A circuit with 2 modes"""

            interactive = True
            primitives = {"S2gate"}
            decompositions = set()

        device = sf.Device(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"):
            prog.compile(
                device=device,
                compiler=DummyCompiler(),
            )
Exemplo n.º 12
0
    def test_validate_gate_parameters_bb_program(self):
        """Test the ``.sf.program_utils.validate_gate_parameters`` function with a ``BlackbirdProgram``"""

        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],
            },
        }

        mock_prog = bb.loads(mock_layout.format(squeezing_amplitude_0=1))
        device = sf.Device(spec=device_dict)

        assert validate_gate_parameters(mock_prog, device)