From 8f49f89ec3706b9b21f23441d96813a4eb375bb8 Mon Sep 17 00:00:00 2001 From: Andrea Date: Fri, 26 Jan 2024 15:50:00 +0400 Subject: [PATCH] test: Implement additional tests dumping and loading --- tests/test_platform.py | 51 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/tests/test_platform.py b/tests/test_platform.py index 9a065a2b8..0dca4522b 100644 --- a/tests/test_platform.py +++ b/tests/test_platform.py @@ -7,16 +7,25 @@ import numpy as np import pytest from qibo.models import Circuit +from qibo.result import CircuitResult from qibolab import create_platform from qibolab.backends import QibolabBackend +from qibolab.dummy import create_dummy from qibolab.dummy.platform import FOLDER from qibolab.execution_parameters import ExecutionParameters from qibolab.instruments.qblox.controller import QbloxController from qibolab.instruments.rfsoc.driver import RFSoC +from qibolab.kernels import Kernels from qibolab.platform import Platform, unroll_sequences from qibolab.pulses import PulseSequence, Rectangular -from qibolab.serialize import dump_runcard, load_runcard +from qibolab.serialize import ( + dump_kernels, + dump_platform, + dump_runcard, + load_runcard, + load_settings, +) from .conftest import find_instrument @@ -81,6 +90,46 @@ def test_dump_runcard(platform, tmp_path): assert final_instruments == target_instruments +@pytest.mark.parametrize("has_kernels", [False, True]) +def test_kernels(tmp_path, has_kernels): + """Test dumping and loading of `Kernels`.""" + + platform = create_dummy() + if has_kernels: + for qubit in platform.qubits: + platform.qubits[qubit].kernel = np.random.rand(10) + + dump_kernels(platform, tmp_path) + + if has_kernels: + kernels = Kernels.load(tmp_path) + for qubit in platform.qubits: + np.testing.assert_array_equal(platform.qubits[qubit].kernel, kernels[qubit]) + else: + with pytest.raises(FileNotFoundError): + Kernels.load(tmp_path) + + +@pytest.mark.parametrize("has_kernels", [False, True]) +def test_dump_platform(tmp_path, has_kernels): + """Test platform dump and loading runcard and kernels.""" + + platform = create_dummy() + if has_kernels: + for qubit in platform.qubits: + platform.qubits[qubit].kernel = np.random.rand(10) + + dump_platform(platform, tmp_path) + + settings = load_settings(load_runcard(tmp_path)) + if has_kernels: + kernels = Kernels.load(tmp_path) + for qubit in platform.qubits: + np.testing.assert_array_equal(platform.qubits[qubit].kernel, kernels[qubit]) + + assert settings == platform.settings + + @pytest.fixture(scope="module") def qpu_platform(connected_platform): connected_platform.connect()