def test_no_shots(self, prog, connection):
        """Test that if the number of shots is not provided, an
        exception is raised"""
        engine = RemoteEngine("X8", connection=connection)

        with pytest.raises(ValueError, match="Number of shots must be specified"):
            engine.run_async(prog)
Esempio n. 2
0
 def test_run_async_without_shots(self, prog):
     """Tests that a ValueError is raised if no shots are specified when a
     remote engine is instantiated.
     """
     engine = RemoteEngine("X8")
     with pytest.raises(ValueError, match=r"Number of shots must be specified"):
         engine.run_async(prog)
Esempio n. 3
0
 def test_run_async_options_from_kwargs(self, prog, blackbird):
     """Tests that :meth:`RemoteEngine.run_async` passes all keyword
     argument backend and runtime options to the Blackbird program.
     """
     engine = RemoteEngine("X8", backend_options={"cutoff_dim": 12})
     engine.run_async(prog, shots=1234)
     assert blackbird._target["options"] == {"cutoff_dim": 12, "shots": 1234}
    def test_no_shots(self, prog, connection, monkeypatch):
        """Test that if the number of shots is not provided, an
        exception is raised"""
        monkeypatch.setattr(Connection, "_get_device_dict",
                            lambda *args: mock_device_dict)
        engine = RemoteEngine("X8", connection=connection)

        with pytest.raises(ValueError,
                           match="Number of shots must be specified"):
            engine.run_async(prog)
Esempio n. 5
0
    def test_run_async_options_from_program(self, prog, blackbird):
        """Test that :meth:`RemoteEngine.run_async` correctly parses runtime
        options compiled into the program.
        """
        engine = RemoteEngine("X8")

        prog = prog.compile(device=engine.device_spec, shots=15)
        assert prog.run_options == {"shots": 15}

        engine.run_async(prog)
        assert blackbird._target["options"] == {"shots": 15}
Esempio n. 6
0
    def test_default_compiler(self, prog, infolog, device):
        """Test that the Xunitary compiler is used by default if a device does
        not explicitly provide a default compiler.
        """
        device.specification["compiler"] = []

        engine = RemoteEngine("X8")
        engine.run_async(prog, shots=10)

        assert engine.device_spec.default_compiler == "Xunitary"

        want_message = "Compiling program for device X8_01 using compiler Xunitary."
        assert infolog.records[-1].message == want_message
    def test_run_options_from_kwargs(self, prog, monkeypatch):
        """Test that the remote engine run_async method correctly
        passes all keyword argument backend and runtime options to the create_job
        method."""
        monkeypatch.setattr(Connection, "create_job", lambda *args: args)
        engine = RemoteEngine("X8", backend_options={"cutoff_dim": 12})
        _, _, _, run_options = engine.run_async(prog, shots=1234)
        assert run_options == {"shots": 1234, "cutoff_dim": 12}

        # run options from keyword arguments overwrite
        # run options provided by the program object
        prog = prog.compile("X8", shots=15)
        _, _, _, run_options = engine.run_async(prog, shots=1234)
        assert run_options == {"shots": 1234, "cutoff_dim": 12}
Esempio n. 8
0
    def test_compile(self, prog, infolog, device):
        """Tests that compilation happens by default if no compile_info is
        specified when :meth:`RemoteEngine.run_async` is invoked.
        """
        device.specification["compiler"] = []

        # Leaving compile_info as None.
        assert prog.compile_info == None

        engine = RemoteEngine("X8")
        engine.run_async(prog, shots=10)

        want_message = "Compiling program for device X8_01 using compiler Xunitary."
        assert infolog.records[-1].message == want_message
    def test_compilation(self, prog, monkeypatch, caplog):
        """Test that the remote engine correctly compiles a program
        for the intended backend"""
        caplog.set_level(logging.INFO)
        monkeypatch.setattr(Connection, "create_job", lambda *args: args)
        monkeypatch.setattr(Connection, "_get_device_dict",
                            lambda *args: mock_device_dict)

        engine = RemoteEngine("X8")
        _, target, res_prog, _ = engine.run_async(prog, shots=10)

        assert caplog.records[
            -1].message == "Compiling program for device X8_01 using compiler fock."
        assert target == RemoteEngine.DEFAULT_TARGETS["X8"]

        # check program is compiled to match the chip template
        expected = prog.compile(device=engine.device_spec).circuit
        res = res_prog.circuit

        for cmd1, cmd2 in zip(res, expected):
            # loop through all commands in res and expected

            # check gates are the same
            assert type(cmd1.op) is type(cmd2.op)
            # check modes are the same
            assert all(i.ind == j.ind for i, j in zip(cmd1.reg, cmd2.reg))
            # check parameters are the same
            assert all(p1 == p2 for p1, p2 in zip(cmd1.op.p, cmd2.op.p))
    def test_run_options_from_program(self, prog, monkeypatch):
        """Test that the remote engine run_async method correctly
        parses runtime options compiled into the program"""
        monkeypatch.setattr(Connection, "create_job", lambda *args: args)
        engine = RemoteEngine("X8")

        prog = prog.compile("X8", shots=15)
        assert prog.run_options == {"shots": 15}

        _, _, _, run_options = engine.run_async(prog)
        assert run_options == {"shots": 15}
Esempio n. 11
0
    def test_compilation(self, prog, blackbird, infolog):
        """Test that :class:`RemoteEngine` can compile a program for the
        intended backend.
        """
        engine = RemoteEngine("X8")
        engine.run_async(prog, shots=10)

        program = blackbird.mock_calls[0].args[0]

        want_message = "Compiling program for device X8_01 using compiler fock."
        assert infolog.records[-1].message == want_message

        # Check that the program is compiled to match the chip template.
        expected = prog.compile(device=engine.device_spec).circuit
        res = program.circuit

        for cmd1, cmd2 in zip(res, expected):
            # Check that the gates are the same.
            assert type(cmd1.op) is type(cmd2.op)
            # Check that the modes are the same.
            assert all(i.ind == j.ind for i, j in zip(cmd1.reg, cmd2.reg))
            # Check that the parameters are the same.
            assert all(p1 == p2 for p1, p2 in zip(cmd1.op.p, cmd2.op.p))
    def test_run_async(self, connection, prog, job_to_complete):
        """Tests a successful non-blocking job execution."""

        engine = RemoteEngine("X8_01", connection=connection)
        job = engine.run_async(prog, shots=10)
        assert job.status == JobStatus.OPEN.value

        for _ in range(MockServer.REQUESTS_BEFORE_COMPLETED):
            job.refresh()

        assert job.status == JobStatus.COMPLETED.value
        assert np.array_equal(job.result.samples, np.array([[1, 2], [3, 4]]))

        with pytest.raises(
            AttributeError, match="The state is undefined for a stateless computation."
        ):
            job.result.state
    def test_default_compiler(self, prog, monkeypatch, caplog):
        """Test that if the device does not provide a default compiler,
        that Xcov is used by default."""
        caplog.set_level(logging.INFO)
        test_device_dict = mock_device_dict.copy()
        test_device_dict["compiler"] = []

        monkeypatch.setattr(Connection, "create_job", lambda *args: args)
        monkeypatch.setattr(Connection, "_get_device_dict",
                            lambda *args: test_device_dict)

        engine = RemoteEngine("X8")
        _, target, res_prog, _ = engine.run_async(prog, shots=10)

        assert engine.device_spec.default_compiler == "Xcov"
        assert caplog.records[
            -1].message == "Compiling program for device X8_01 using compiler Xcov."
Esempio n. 14
0
    def test_run_async(self, prog):
        """Tests that a non-blocking job execution can succeed."""
        engine = RemoteEngine("X8_01")
        job = engine.run_async(prog, shots=10)

        # job.status calls job.finished, incrementing the request counter
        assert job.status == "open"

        for _ in range(REQUESTS_BEFORE_COMPLETED - 1):
            assert job.finished is False
        assert job.finished is True

        assert job.status == "complete"
        assert np.array_equal(job.result["foo"], [np.array([5, 6])])
        assert np.array_equal(job.result["output"], [np.array([[1, 2], [3, 4]])])

        result = Result(job.result)
        result.state is None
    def test_compilation(self, prog, monkeypatch):
        """Test that the remote engine correctly compiles a program
        for the intended backend"""
        monkeypatch.setattr(Connection, "create_job", lambda *args: args)

        engine = RemoteEngine("X8")
        _, target, res_prog, _ = engine.run_async(prog, shots=10)

        assert target == RemoteEngine.DEFAULT_TARGETS["X8"]

        # check program is compiled to match the chip template
        expected = prog.compile("X8").circuit
        res = res_prog.circuit

        for cmd1, cmd2 in zip(res, expected):
            # loop through all commands in res and expected

            # check gates are the same
            assert type(cmd1.op) is type(cmd2.op)
            # check modes are the same
            assert all(i.ind == j.ind for i, j in zip(cmd1.reg, cmd2.reg))
            # check parameters are the same
            assert all(p1 == p2 for p1, p2 in zip(cmd1.op.p, cmd2.op.p))
Esempio n. 16
0
    def test_compile(self, prog, monkeypatch, caplog):
        """Tests that compilation happens by default if no compile_info was
        specified when call run_async."""
        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())

        # Leaving compile_info as None
        assert prog.compile_info == None

        engine = RemoteEngine("X8")
        program = engine.run_async(prog, shots=10)

        assert isinstance(program, self.MockProgram)
        assert caplog.records[
            -1].message == "Compiling program for device X8_01 using compiler Xunitary."