From 380d5cf9e82158fb33dcc86b8b57134f17ef07c5 Mon Sep 17 00:00:00 2001 From: Jacfomg Date: Thu, 21 Sep 2023 11:39:22 +0400 Subject: [PATCH 1/4] patch for RB --- src/qibo/gates/abstract.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/qibo/gates/abstract.py b/src/qibo/gates/abstract.py index 19ebb7ab1f..4fcd4735a5 100644 --- a/src/qibo/gates/abstract.py +++ b/src/qibo/gates/abstract.py @@ -1,4 +1,5 @@ import collections +import json from typing import List, Sequence, Tuple import sympy @@ -50,6 +51,26 @@ def __init__(self): self.device_gates = set() self.original_gate = None + def toJSON(self): + encoded = self.__dict__ + + required_fields = ["name", "init_kwargs", "_target_qubits", "_control_qubits"] + encoded_simple = { + key: value for key, value in encoded.items() if key in required_fields + } + + required_fields_init_kwargs = ["theta", "phi", "lam"] + encoded_simple["init_kwargs"] = { + key: value + for key, value in encoded_simple["init_kwargs"].items() + if key in required_fields_init_kwargs + } + + for value in encoded_simple: + if isinstance(encoded[value], set): + encoded_simple[value] = list(encoded_simple[value]) + return json.dumps(encoded_simple) + @property def target_qubits(self) -> Tuple[int]: """Tuple with ids of target qubits.""" From c1b442fe2fdf2c9f330346ac1f0b9c47fc55da54 Mon Sep 17 00:00:00 2001 From: Jacfomg Date: Fri, 22 Sep 2023 11:24:55 +0400 Subject: [PATCH 2/4] tests and name --- src/qibo/gates/abstract.py | 2 +- tests/test_gates_abstract.py | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/qibo/gates/abstract.py b/src/qibo/gates/abstract.py index 4fcd4735a5..5b0f1e2cee 100644 --- a/src/qibo/gates/abstract.py +++ b/src/qibo/gates/abstract.py @@ -51,7 +51,7 @@ def __init__(self): self.device_gates = set() self.original_gate = None - def toJSON(self): + def to_json(self): encoded = self.__dict__ required_fields = ["name", "init_kwargs", "_target_qubits", "_control_qubits"] diff --git a/tests/test_gates_abstract.py b/tests/test_gates_abstract.py index 133f103ca0..e4f459fa0e 100644 --- a/tests/test_gates_abstract.py +++ b/tests/test_gates_abstract.py @@ -1,6 +1,7 @@ """Tests methods defined in `qibo/gates/abstract.py` and `qibo/gates/gates.py`.""" import numpy as np import pytest +import json from qibo import gates, matrices from qibo.config import PRECISION_TOL @@ -13,6 +14,19 @@ def test_one_qubit_gates_init(gatename): gate = getattr(gates, gatename)(0) assert gate.target_qubits == (0,) +@pytest.mark.parametrize( + "gatename", ["H", "X", "Y", "Z", "S", "SDG", "T", "TDG", "I", "Align"] +) +def test_one_qubit_gates_to_json(gatename): + gate = getattr(gates, gatename)(0) + + json_general = '{"name": {}, "init_kwargs": {}, "_target_qubits": [0], "_control_qubits": []}' + + json_gate = json.loads(json_general) + json_gate["name"] = gate.name + + assert gate.to_json() == json.dumps(json_gate) + @pytest.mark.parametrize( "controls,instance", [((1,), "CNOT"), ((1, 2), "TOFFOLI"), ((1, 2, 4), "X")] @@ -92,6 +106,29 @@ def test_one_qubit_rotations_init(gatename, params): assert gate.target_qubits == (0,) assert gate.parameters == params +@pytest.mark.parametrize( + "gatename,params", + [ + ("RX", (0.1234,)), + ("RY", (0.1234,)), + ("RZ", (0.1234,)), + ("U1", (0.1234,)), + ("U2", (0.1234, 0.4321)), + ("U3", (0.1234, 0.4321, 0.5678)), + ], +) +def test_one_qubit_rotations_to_json(gatename, params): + gate = getattr(gates, gatename)(0, *params) + + json_general = '{"name": {}, "init_kwargs": {}, "_target_qubits": [0], "_control_qubits": []}' + + json_gate = json.loads(json_general) + json_gate["name"] = gate.name + json_gate["init_kwargs"] = gate.init_kwargs + del(json_gate["init_kwargs"]["trainable"]) + + assert gate.to_json() == json.dumps(json_gate) + @pytest.mark.parametrize( "gatename,params", From 833a612f2055272d363605a5891c9972c18567c7 Mon Sep 17 00:00:00 2001 From: Jacfomg Date: Fri, 22 Sep 2023 11:25:13 +0400 Subject: [PATCH 3/4] pre-commit --- tests/test_gates_abstract.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/tests/test_gates_abstract.py b/tests/test_gates_abstract.py index e4f459fa0e..ab488c4831 100644 --- a/tests/test_gates_abstract.py +++ b/tests/test_gates_abstract.py @@ -1,7 +1,8 @@ """Tests methods defined in `qibo/gates/abstract.py` and `qibo/gates/gates.py`.""" +import json + import numpy as np import pytest -import json from qibo import gates, matrices from qibo.config import PRECISION_TOL @@ -14,19 +15,22 @@ def test_one_qubit_gates_init(gatename): gate = getattr(gates, gatename)(0) assert gate.target_qubits == (0,) + @pytest.mark.parametrize( "gatename", ["H", "X", "Y", "Z", "S", "SDG", "T", "TDG", "I", "Align"] ) def test_one_qubit_gates_to_json(gatename): gate = getattr(gates, gatename)(0) - - json_general = '{"name": {}, "init_kwargs": {}, "_target_qubits": [0], "_control_qubits": []}' - + + json_general = ( + '{"name": {}, "init_kwargs": {}, "_target_qubits": [0], "_control_qubits": []}' + ) + json_gate = json.loads(json_general) json_gate["name"] = gate.name assert gate.to_json() == json.dumps(json_gate) - + @pytest.mark.parametrize( "controls,instance", [((1,), "CNOT"), ((1, 2), "TOFFOLI"), ((1, 2, 4), "X")] @@ -106,6 +110,7 @@ def test_one_qubit_rotations_init(gatename, params): assert gate.target_qubits == (0,) assert gate.parameters == params + @pytest.mark.parametrize( "gatename,params", [ @@ -119,16 +124,18 @@ def test_one_qubit_rotations_init(gatename, params): ) def test_one_qubit_rotations_to_json(gatename, params): gate = getattr(gates, gatename)(0, *params) - - json_general = '{"name": {}, "init_kwargs": {}, "_target_qubits": [0], "_control_qubits": []}' - + + json_general = ( + '{"name": {}, "init_kwargs": {}, "_target_qubits": [0], "_control_qubits": []}' + ) + json_gate = json.loads(json_general) json_gate["name"] = gate.name json_gate["init_kwargs"] = gate.init_kwargs - del(json_gate["init_kwargs"]["trainable"]) + del json_gate["init_kwargs"]["trainable"] assert gate.to_json() == json.dumps(json_gate) - + @pytest.mark.parametrize( "gatename,params", From 70450212c071d8ecd1a33739c352153e94b036de Mon Sep 17 00:00:00 2001 From: Jacfomg Date: Tue, 26 Sep 2023 18:42:55 +0400 Subject: [PATCH 4/4] comments --- src/qibo/gates/abstract.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/qibo/gates/abstract.py b/src/qibo/gates/abstract.py index 5b0f1e2cee..d1582d6243 100644 --- a/src/qibo/gates/abstract.py +++ b/src/qibo/gates/abstract.py @@ -7,6 +7,9 @@ from qibo.backends import GlobalBackend from qibo.config import raise_error +REQUIRED_FIELDS = ["name", "init_kwargs", "_target_qubits", "_control_qubits"] +REQUIRED_FIELDS_INIT_KWARGS = ["theta", "phi", "lam"] + class Gate: """The base class for gate implementation. @@ -54,16 +57,14 @@ def __init__(self): def to_json(self): encoded = self.__dict__ - required_fields = ["name", "init_kwargs", "_target_qubits", "_control_qubits"] encoded_simple = { - key: value for key, value in encoded.items() if key in required_fields + key: value for key, value in encoded.items() if key in REQUIRED_FIELDS } - required_fields_init_kwargs = ["theta", "phi", "lam"] encoded_simple["init_kwargs"] = { key: value for key, value in encoded_simple["init_kwargs"].items() - if key in required_fields_init_kwargs + if key in REQUIRED_FIELDS_INIT_KWARGS } for value in encoded_simple: