From 0227d137d2315223b3686bf3ef1a0aabd58ce8b7 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Sat, 20 Jan 2024 09:57:41 +0400 Subject: [PATCH 01/39] function --- src/qibo/quantum_info/utils.py | 46 ++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/qibo/quantum_info/utils.py b/src/qibo/quantum_info/utils.py index eb5a447d8d..192c503434 100644 --- a/src/qibo/quantum_info/utils.py +++ b/src/qibo/quantum_info/utils.py @@ -4,7 +4,7 @@ from itertools import permutations from math import factorial from re import finditer -from typing import Optional +from typing import Optional, Union import numpy as np @@ -13,7 +13,9 @@ from qibo.config import PRECISION_TOL, raise_error -def hamming_weight(bitstring, return_indexes: bool = False): +def hamming_weight( + bitstring: Union[int, list, tuple, np.ndarray], return_indexes: bool = False +): """Calculates the Hamming weight of a bitstring. Args: @@ -31,6 +33,13 @@ def hamming_weight(bitstring, return_indexes: bool = False): f"return_indexes must be type bool, but it is type {type(return_indexes)}", ) + if not isinstance(bitstring, (int, list, tuple, np.ndarray)): + raise_error( + TypeError, + "bitstring must be either type int, list, tuple, or numpy.ndarray. " + f"However, it is type {type(bitstring)}.", + ) + if isinstance(bitstring, int): bitstring = f"{bitstring:b}" elif isinstance(bitstring, (list, tuple, np.ndarray)): @@ -44,6 +53,39 @@ def hamming_weight(bitstring, return_indexes: bool = False): return len(indexes) +def hamming_distance(bitstring_1, bitstring_2, return_indexes: bool = False): + if not isinstance(return_indexes, bool): + raise_error( + TypeError, + f"return_indexes must be type bool, but it is type {type(return_indexes)}", + ) + + if not isinstance(bitstring_1, (int, list, tuple, np.ndarray)): + raise_error( + TypeError, + "bitstring_1 must be either type int, list, tuple, or numpy.ndarray. " + f"However, it is type {type(bitstring_1)}.", + ) + + if not isinstance(bitstring_2, (int, list, tuple, np.ndarray)): + raise_error( + TypeError, + "bitstring_2 must be either type int, list, tuple, or numpy.ndarray. " + f"However, it is type {type(bitstring_2)}.", + ) + + if isinstance(bitstring_1, int): + bitstring_1 = f"{bitstring_1:b}" + + if isinstance(bitstring_2, int): + bitstring_2 = f"{bitstring_2:b}" + + bitstring_1 = np.array(list(bitstring_1), dtype=int) + bitstring_2 = np.array(list(bitstring_2), dtype=int) + + return hamming_weight(bitstring_1 - bitstring_2, return_indexes=return_indexes) + + def hadamard_transform(array, implementation: str = "fast", backend=None): """Calculates the (fast) Hadamard Transform :math:`\\text{HT}` of a :math:`2^{n}`-dimensional vector or :math:`2^{n} \\times 2^{n}` matrix :math:`A`, From eb4721de8a658102b37ad1b76a9ccdb93810e26f Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Sat, 20 Jan 2024 10:04:36 +0400 Subject: [PATCH 02/39] docstring --- src/qibo/quantum_info/utils.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/qibo/quantum_info/utils.py b/src/qibo/quantum_info/utils.py index 192c503434..88b7597dc6 100644 --- a/src/qibo/quantum_info/utils.py +++ b/src/qibo/quantum_info/utils.py @@ -14,7 +14,7 @@ def hamming_weight( - bitstring: Union[int, list, tuple, np.ndarray], return_indexes: bool = False + bitstring: Union[int, str, list, tuple, np.ndarray], return_indexes: bool = False ): """Calculates the Hamming weight of a bitstring. @@ -33,7 +33,7 @@ def hamming_weight( f"return_indexes must be type bool, but it is type {type(return_indexes)}", ) - if not isinstance(bitstring, (int, list, tuple, np.ndarray)): + if not isinstance(bitstring, (int, str, list, tuple, np.ndarray)): raise_error( TypeError, "bitstring must be either type int, list, tuple, or numpy.ndarray. " @@ -54,6 +54,17 @@ def hamming_weight( def hamming_distance(bitstring_1, bitstring_2, return_indexes: bool = False): + """Calculates the Hamming distance ``bitstring_1 - bitstring_2``. + + Args: + bitstring_1 (int or str or list or tuple or ndarray): fisrt bistring. + bitstring_2 (int or str or list or tuple or ndarray): second bitstring. + return_indexes (bool, optional): If ``True``, returns the indexes of the + non-zero elements. Defaults to ``False``. + + Returns: + int or list: Hamming distance or list of indexes of non-zero elements. + """ if not isinstance(return_indexes, bool): raise_error( TypeError, From 235163ded460bf71a116996985c713c2048195bc Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Sat, 20 Jan 2024 10:06:36 +0400 Subject: [PATCH 03/39] api reference --- doc/source/api-reference/qibo.rst | 6 ++++++ src/qibo/quantum_info/utils.py | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/doc/source/api-reference/qibo.rst b/doc/source/api-reference/qibo.rst index bbeeda603c..0cffe7c9cd 100644 --- a/doc/source/api-reference/qibo.rst +++ b/doc/source/api-reference/qibo.rst @@ -2022,6 +2022,12 @@ Hamming weight .. autofunction:: qibo.quantum_info.hamming_weight +Hamming distance +"""""""""""""""" + +.. autofunction:: qibo.quantum_info.hamming_distance + + Hadamard Transform """""""""""""""""" diff --git a/src/qibo/quantum_info/utils.py b/src/qibo/quantum_info/utils.py index 88b7597dc6..5441739976 100644 --- a/src/qibo/quantum_info/utils.py +++ b/src/qibo/quantum_info/utils.py @@ -54,7 +54,9 @@ def hamming_weight( def hamming_distance(bitstring_1, bitstring_2, return_indexes: bool = False): - """Calculates the Hamming distance ``bitstring_1 - bitstring_2``. + """Calculates the Hamming distance between two bistrings. + + This is done by calculating the Hamming weight of ``bitstring_1 - bitstring_2``. Args: bitstring_1 (int or str or list or tuple or ndarray): fisrt bistring. From ef7c5d6c1e2d30a52755a2bc4266f565be68e55a Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Sat, 20 Jan 2024 12:22:53 +0400 Subject: [PATCH 04/39] typing --- src/qibo/quantum_info/utils.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/qibo/quantum_info/utils.py b/src/qibo/quantum_info/utils.py index 5441739976..f744d07ca5 100644 --- a/src/qibo/quantum_info/utils.py +++ b/src/qibo/quantum_info/utils.py @@ -53,7 +53,11 @@ def hamming_weight( return len(indexes) -def hamming_distance(bitstring_1, bitstring_2, return_indexes: bool = False): +def hamming_distance( + bitstring_1: Union[int, str, list, tuple, np.ndarray], + bitstring_2: Union[int, str, list, tuple, np.ndarray], + return_indexes: bool = False, +): """Calculates the Hamming distance between two bistrings. This is done by calculating the Hamming weight of ``bitstring_1 - bitstring_2``. @@ -73,14 +77,14 @@ def hamming_distance(bitstring_1, bitstring_2, return_indexes: bool = False): f"return_indexes must be type bool, but it is type {type(return_indexes)}", ) - if not isinstance(bitstring_1, (int, list, tuple, np.ndarray)): + if not isinstance(bitstring_1, (int, str, list, tuple, np.ndarray)): raise_error( TypeError, "bitstring_1 must be either type int, list, tuple, or numpy.ndarray. " f"However, it is type {type(bitstring_1)}.", ) - if not isinstance(bitstring_2, (int, list, tuple, np.ndarray)): + if not isinstance(bitstring_2, (int, str, list, tuple, np.ndarray)): raise_error( TypeError, "bitstring_2 must be either type int, list, tuple, or numpy.ndarray. " From a80d7e4e016aa4eb1bf8699fe732141a4f2b298c Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Sat, 20 Jan 2024 12:22:59 +0400 Subject: [PATCH 05/39] tests --- tests/test_quantum_info_utils.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/test_quantum_info_utils.py b/tests/test_quantum_info_utils.py index f873078bb4..9d2f45097f 100644 --- a/tests/test_quantum_info_utils.py +++ b/tests/test_quantum_info_utils.py @@ -10,6 +10,7 @@ from qibo.quantum_info.utils import ( haar_integral, hadamard_transform, + hamming_distance, hamming_weight, hellinger_distance, hellinger_fidelity, @@ -33,6 +34,8 @@ def test_hamming_weight(bitstring, kind): with pytest.raises(TypeError): test = hamming_weight("0101", return_indexes="True") + with pytest.raises(TypeError): + test = hamming_weight(2.3) bitstring = f"{bitstring:b}" weight_test = len(bitstring.replace("0", "")) @@ -45,6 +48,32 @@ def test_hamming_weight(bitstring, kind): assert indexes == indexes_test +@pytest.mark.parametrize("bitstring", range(2**5)) +@pytest.mark.parametrize( + "kind", + [ + str, + list, + tuple, + lambda b: np.array(list(b)), + lambda b: int(b, 2), + lambda b: list(map(int, b)), + ], +) +def test_hamming_distance(bitstring, kind): + with pytest.raises(TypeError): + test = hamming_distance("0101", "1010", return_indexes="True") + with pytest.raises(TypeError): + test = hamming_distance(2.3, "1010") + + bitstring = f"{bitstring:b}" + distance = hamming_distance(kind(bitstring), kind(bitstring)) + indexes = hamming_distance(kind(bitstring), kind(bitstring), True) + + assert distance == 0 + assert indexes == [] + + @pytest.mark.parametrize("is_matrix", [False, True]) @pytest.mark.parametrize("implementation", ["fast", "regular"]) @pytest.mark.parametrize("nqubits", [3, 4, 5]) From 627eab75ed212b85c19374bb80a9fa839398f305 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Sat, 20 Jan 2024 12:37:25 +0400 Subject: [PATCH 06/39] improved docstrings --- src/qibo/noise.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/qibo/noise.py b/src/qibo/noise.py index e7ed76909e..9cbbee25f6 100644 --- a/src/qibo/noise.py +++ b/src/qibo/noise.py @@ -1,6 +1,7 @@ import collections from itertools import combinations from math import log2 +from typing import Optional, Union from qibo import gates from qibo.config import raise_error @@ -211,7 +212,13 @@ def __init__(self): self.errors = collections.defaultdict(list) self.noise_model = {} - def add(self, error, gate=None, qubits=None, condition=None): + def add( + self, + error, + gate: Optional[gates.Gate] = None, + qubits: Optional[Union[int, tuple]] = None, + condition=None, + ): """Add a quantum error for a specific gate and qubit to the noise model. Args: @@ -224,13 +231,14 @@ def add(self, error, gate=None, qubits=None, condition=None): :class:`qibo.noise.ReadoutError`, :class:`qibo.noise.ResetError`, :class:`qibo.noise.UnitaryError`, - :class:`qibo.noise.KrausError` and + :class:`qibo.noise.KrausError`, and :class:`qibo.noise.CustomError`. - gate (:class:`qibo.gates.Gate`): gate after which the noise will be added. + gate (:class:`qibo.gates.Gate`, optional): gate after which the noise will be added. If ``None``, the noise will be added after each gate except :class:`qibo.gates.Channel` and :class:`qibo.gates.M`. - qubits (tuple): qubits where the noise will be applied. If ``None``, + qubits (int or tuple, optional): qubits where the noise will be applied. If ``None``, the noise will be added after every instance of the gate. + Defaults to ``None``. condition (callable, optional): function that takes :class:`qibo.gates.Gate` object as an input and returns ``True`` if noise should be added to it. @@ -306,9 +314,8 @@ def apply(self, circuit): circuit (:class:`qibo.models.circuit.Circuit`): quantum circuit Returns: - A (:class:`qibo.models.circuit.Circuit`) which corresponds - to the initial circuit with noise gates added according - to the noise model. + (:class:`qibo.models.circuit.Circuit`): initial circuit with noise gates + added according to the noise model. """ if isinstance(self.noise_model, CompositeNoiseModel): From aee0746ae15c3620f709f6b22e0f8a645265e0ae Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Sat, 20 Jan 2024 12:47:28 +0400 Subject: [PATCH 07/39] fix coverage --- tests/test_quantum_info_utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_quantum_info_utils.py b/tests/test_quantum_info_utils.py index 9d2f45097f..57fb10c4fd 100644 --- a/tests/test_quantum_info_utils.py +++ b/tests/test_quantum_info_utils.py @@ -65,6 +65,8 @@ def test_hamming_distance(bitstring, kind): test = hamming_distance("0101", "1010", return_indexes="True") with pytest.raises(TypeError): test = hamming_distance(2.3, "1010") + with pytest.raises(TypeError): + test = hamming_distance("1010", 2.3) bitstring = f"{bitstring:b}" distance = hamming_distance(kind(bitstring), kind(bitstring)) From d4f85a450879709c6383b8b95d5815d9fdc29ce7 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 22 Jan 2024 22:18:02 +0000 Subject: [PATCH 08/39] Bump pillow from 10.1.0 to 10.2.0 Bumps [pillow](https://github.com/python-pillow/Pillow) from 10.1.0 to 10.2.0. - [Release notes](https://github.com/python-pillow/Pillow/releases) - [Changelog](https://github.com/python-pillow/Pillow/blob/main/CHANGES.rst) - [Commits](https://github.com/python-pillow/Pillow/compare/10.1.0...10.2.0) --- updated-dependencies: - dependency-name: pillow dependency-type: indirect ... Signed-off-by: dependabot[bot] --- poetry.lock | 129 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 74 insertions(+), 55 deletions(-) diff --git a/poetry.lock b/poetry.lock index dff7472a94..d9f70f1364 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2826,70 +2826,88 @@ ptyprocess = ">=0.5" [[package]] name = "pillow" -version = "10.1.0" +version = "10.2.0" description = "Python Imaging Library (Fork)" optional = false python-versions = ">=3.8" files = [ - {file = "Pillow-10.1.0-cp310-cp310-macosx_10_10_x86_64.whl", hash = "sha256:1ab05f3db77e98f93964697c8efc49c7954b08dd61cff526b7f2531a22410106"}, - {file = "Pillow-10.1.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:6932a7652464746fcb484f7fc3618e6503d2066d853f68a4bd97193a3996e273"}, - {file = "Pillow-10.1.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a5f63b5a68daedc54c7c3464508d8c12075e56dcfbd42f8c1bf40169061ae666"}, - {file = "Pillow-10.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:c0949b55eb607898e28eaccb525ab104b2d86542a85c74baf3a6dc24002edec2"}, - {file = "Pillow-10.1.0-cp310-cp310-manylinux_2_28_aarch64.whl", hash = "sha256:ae88931f93214777c7a3aa0a8f92a683f83ecde27f65a45f95f22d289a69e593"}, - {file = "Pillow-10.1.0-cp310-cp310-manylinux_2_28_x86_64.whl", hash = "sha256:b0eb01ca85b2361b09480784a7931fc648ed8b7836f01fb9241141b968feb1db"}, - {file = "Pillow-10.1.0-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:d27b5997bdd2eb9fb199982bb7eb6164db0426904020dc38c10203187ae2ff2f"}, - {file = "Pillow-10.1.0-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:7df5608bc38bd37ef585ae9c38c9cd46d7c81498f086915b0f97255ea60c2818"}, - {file = "Pillow-10.1.0-cp310-cp310-win_amd64.whl", hash = "sha256:41f67248d92a5e0a2076d3517d8d4b1e41a97e2df10eb8f93106c89107f38b57"}, - {file = "Pillow-10.1.0-cp311-cp311-macosx_10_10_x86_64.whl", hash = "sha256:1fb29c07478e6c06a46b867e43b0bcdb241b44cc52be9bc25ce5944eed4648e7"}, - {file = "Pillow-10.1.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:2cdc65a46e74514ce742c2013cd4a2d12e8553e3a2563c64879f7c7e4d28bce7"}, - {file = "Pillow-10.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:50d08cd0a2ecd2a8657bd3d82c71efd5a58edb04d9308185d66c3a5a5bed9610"}, - {file = "Pillow-10.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:062a1610e3bc258bff2328ec43f34244fcec972ee0717200cb1425214fe5b839"}, - {file = "Pillow-10.1.0-cp311-cp311-manylinux_2_28_aarch64.whl", hash = "sha256:61f1a9d247317fa08a308daaa8ee7b3f760ab1809ca2da14ecc88ae4257d6172"}, - {file = "Pillow-10.1.0-cp311-cp311-manylinux_2_28_x86_64.whl", hash = "sha256:a646e48de237d860c36e0db37ecaecaa3619e6f3e9d5319e527ccbc8151df061"}, - {file = "Pillow-10.1.0-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:47e5bf85b80abc03be7455c95b6d6e4896a62f6541c1f2ce77a7d2bb832af262"}, - {file = "Pillow-10.1.0-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:a92386125e9ee90381c3369f57a2a50fa9e6aa8b1cf1d9c4b200d41a7dd8e992"}, - {file = "Pillow-10.1.0-cp311-cp311-win_amd64.whl", hash = "sha256:0f7c276c05a9767e877a0b4c5050c8bee6a6d960d7f0c11ebda6b99746068c2a"}, - {file = "Pillow-10.1.0-cp312-cp312-macosx_10_10_x86_64.whl", hash = "sha256:a89b8312d51715b510a4fe9fc13686283f376cfd5abca8cd1c65e4c76e21081b"}, - {file = "Pillow-10.1.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:00f438bb841382b15d7deb9a05cc946ee0f2c352653c7aa659e75e592f6fa17d"}, - {file = "Pillow-10.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3d929a19f5469b3f4df33a3df2983db070ebb2088a1e145e18facbc28cae5b27"}, - {file = "Pillow-10.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9a92109192b360634a4489c0c756364c0c3a2992906752165ecb50544c251312"}, - {file = "Pillow-10.1.0-cp312-cp312-manylinux_2_28_aarch64.whl", hash = "sha256:0248f86b3ea061e67817c47ecbe82c23f9dd5d5226200eb9090b3873d3ca32de"}, - {file = "Pillow-10.1.0-cp312-cp312-manylinux_2_28_x86_64.whl", hash = "sha256:9882a7451c680c12f232a422730f986a1fcd808da0fd428f08b671237237d651"}, - {file = "Pillow-10.1.0-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:1c3ac5423c8c1da5928aa12c6e258921956757d976405e9467c5f39d1d577a4b"}, - {file = "Pillow-10.1.0-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:806abdd8249ba3953c33742506fe414880bad78ac25cc9a9b1c6ae97bedd573f"}, - {file = "Pillow-10.1.0-cp312-cp312-win_amd64.whl", hash = "sha256:eaed6977fa73408b7b8a24e8b14e59e1668cfc0f4c40193ea7ced8e210adf996"}, - {file = "Pillow-10.1.0-cp38-cp38-macosx_10_10_x86_64.whl", hash = "sha256:fe1e26e1ffc38be097f0ba1d0d07fcade2bcfd1d023cda5b29935ae8052bd793"}, - {file = "Pillow-10.1.0-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:7a7e3daa202beb61821c06d2517428e8e7c1aab08943e92ec9e5755c2fc9ba5e"}, - {file = "Pillow-10.1.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:24fadc71218ad2b8ffe437b54876c9382b4a29e030a05a9879f615091f42ffc2"}, - {file = "Pillow-10.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:fa1d323703cfdac2036af05191b969b910d8f115cf53093125e4058f62012c9a"}, - {file = "Pillow-10.1.0-cp38-cp38-manylinux_2_28_aarch64.whl", hash = "sha256:912e3812a1dbbc834da2b32299b124b5ddcb664ed354916fd1ed6f193f0e2d01"}, - {file = "Pillow-10.1.0-cp38-cp38-manylinux_2_28_x86_64.whl", hash = "sha256:7dbaa3c7de82ef37e7708521be41db5565004258ca76945ad74a8e998c30af8d"}, - {file = "Pillow-10.1.0-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:9d7bc666bd8c5a4225e7ac71f2f9d12466ec555e89092728ea0f5c0c2422ea80"}, - {file = "Pillow-10.1.0-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:baada14941c83079bf84c037e2d8b7506ce201e92e3d2fa0d1303507a8538212"}, - {file = "Pillow-10.1.0-cp38-cp38-win_amd64.whl", hash = "sha256:2ef6721c97894a7aa77723740a09547197533146fba8355e86d6d9a4a1056b14"}, - {file = "Pillow-10.1.0-cp39-cp39-macosx_10_10_x86_64.whl", hash = "sha256:0a026c188be3b443916179f5d04548092e253beb0c3e2ee0a4e2cdad72f66099"}, - {file = "Pillow-10.1.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:04f6f6149f266a100374ca3cc368b67fb27c4af9f1cc8cb6306d849dcdf12616"}, - {file = "Pillow-10.1.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:bb40c011447712d2e19cc261c82655f75f32cb724788df315ed992a4d65696bb"}, - {file = "Pillow-10.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:1a8413794b4ad9719346cd9306118450b7b00d9a15846451549314a58ac42219"}, - {file = "Pillow-10.1.0-cp39-cp39-manylinux_2_28_aarch64.whl", hash = "sha256:c9aeea7b63edb7884b031a35305629a7593272b54f429a9869a4f63a1bf04c34"}, - {file = "Pillow-10.1.0-cp39-cp39-manylinux_2_28_x86_64.whl", hash = "sha256:b4005fee46ed9be0b8fb42be0c20e79411533d1fd58edabebc0dd24626882cfd"}, - {file = "Pillow-10.1.0-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:4d0152565c6aa6ebbfb1e5d8624140a440f2b99bf7afaafbdbf6430426497f28"}, - {file = "Pillow-10.1.0-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:d921bc90b1defa55c9917ca6b6b71430e4286fc9e44c55ead78ca1a9f9eba5f2"}, - {file = "Pillow-10.1.0-cp39-cp39-win_amd64.whl", hash = "sha256:cfe96560c6ce2f4c07d6647af2d0f3c54cc33289894ebd88cfbb3bcd5391e256"}, - {file = "Pillow-10.1.0-pp310-pypy310_pp73-macosx_10_10_x86_64.whl", hash = "sha256:937bdc5a7f5343d1c97dc98149a0be7eb9704e937fe3dc7140e229ae4fc572a7"}, - {file = "Pillow-10.1.0-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b1c25762197144e211efb5f4e8ad656f36c8d214d390585d1d21281f46d556ba"}, - {file = "Pillow-10.1.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl", hash = "sha256:afc8eef765d948543a4775f00b7b8c079b3321d6b675dde0d02afa2ee23000b4"}, - {file = "Pillow-10.1.0-pp310-pypy310_pp73-win_amd64.whl", hash = "sha256:883f216eac8712b83a63f41b76ddfb7b2afab1b74abbb413c5df6680f071a6b9"}, - {file = "Pillow-10.1.0-pp39-pypy39_pp73-macosx_10_10_x86_64.whl", hash = "sha256:b920e4d028f6442bea9a75b7491c063f0b9a3972520731ed26c83e254302eb1e"}, - {file = "Pillow-10.1.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:1c41d960babf951e01a49c9746f92c5a7e0d939d1652d7ba30f6b3090f27e412"}, - {file = "Pillow-10.1.0-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl", hash = "sha256:1fafabe50a6977ac70dfe829b2d5735fd54e190ab55259ec8aea4aaea412fa0b"}, - {file = "Pillow-10.1.0-pp39-pypy39_pp73-win_amd64.whl", hash = "sha256:3b834f4b16173e5b92ab6566f0473bfb09f939ba14b23b8da1f54fa63e4b623f"}, - {file = "Pillow-10.1.0.tar.gz", hash = "sha256:e6bf8de6c36ed96c86ea3b6e1d5273c53f46ef518a062464cd7ef5dd2cf92e38"}, + {file = "pillow-10.2.0-cp310-cp310-macosx_10_10_x86_64.whl", hash = "sha256:7823bdd049099efa16e4246bdf15e5a13dbb18a51b68fa06d6c1d4d8b99a796e"}, + {file = "pillow-10.2.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:83b2021f2ade7d1ed556bc50a399127d7fb245e725aa0113ebd05cfe88aaf588"}, + {file = "pillow-10.2.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6fad5ff2f13d69b7e74ce5b4ecd12cc0ec530fcee76356cac6742785ff71c452"}, + {file = "pillow-10.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:da2b52b37dad6d9ec64e653637a096905b258d2fc2b984c41ae7d08b938a67e4"}, + {file = "pillow-10.2.0-cp310-cp310-manylinux_2_28_aarch64.whl", hash = "sha256:47c0995fc4e7f79b5cfcab1fc437ff2890b770440f7696a3ba065ee0fd496563"}, + {file = "pillow-10.2.0-cp310-cp310-manylinux_2_28_x86_64.whl", hash = "sha256:322bdf3c9b556e9ffb18f93462e5f749d3444ce081290352c6070d014c93feb2"}, + {file = "pillow-10.2.0-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:51f1a1bffc50e2e9492e87d8e09a17c5eea8409cda8d3f277eb6edc82813c17c"}, + {file = "pillow-10.2.0-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:69ffdd6120a4737710a9eee73e1d2e37db89b620f702754b8f6e62594471dee0"}, + {file = "pillow-10.2.0-cp310-cp310-win32.whl", hash = "sha256:c6dafac9e0f2b3c78df97e79af707cdc5ef8e88208d686a4847bab8266870023"}, + {file = "pillow-10.2.0-cp310-cp310-win_amd64.whl", hash = "sha256:aebb6044806f2e16ecc07b2a2637ee1ef67a11840a66752751714a0d924adf72"}, + {file = "pillow-10.2.0-cp310-cp310-win_arm64.whl", hash = "sha256:7049e301399273a0136ff39b84c3678e314f2158f50f517bc50285fb5ec847ad"}, + {file = "pillow-10.2.0-cp311-cp311-macosx_10_10_x86_64.whl", hash = "sha256:35bb52c37f256f662abdfa49d2dfa6ce5d93281d323a9af377a120e89a9eafb5"}, + {file = "pillow-10.2.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:9c23f307202661071d94b5e384e1e1dc7dfb972a28a2310e4ee16103e66ddb67"}, + {file = "pillow-10.2.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:773efe0603db30c281521a7c0214cad7836c03b8ccff897beae9b47c0b657d61"}, + {file = "pillow-10.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:11fa2e5984b949b0dd6d7a94d967743d87c577ff0b83392f17cb3990d0d2fd6e"}, + {file = "pillow-10.2.0-cp311-cp311-manylinux_2_28_aarch64.whl", hash = "sha256:716d30ed977be8b37d3ef185fecb9e5a1d62d110dfbdcd1e2a122ab46fddb03f"}, + {file = "pillow-10.2.0-cp311-cp311-manylinux_2_28_x86_64.whl", hash = "sha256:a086c2af425c5f62a65e12fbf385f7c9fcb8f107d0849dba5839461a129cf311"}, + {file = "pillow-10.2.0-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:c8de2789052ed501dd829e9cae8d3dcce7acb4777ea4a479c14521c942d395b1"}, + {file = "pillow-10.2.0-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:609448742444d9290fd687940ac0b57fb35e6fd92bdb65386e08e99af60bf757"}, + {file = "pillow-10.2.0-cp311-cp311-win32.whl", hash = "sha256:823ef7a27cf86df6597fa0671066c1b596f69eba53efa3d1e1cb8b30f3533068"}, + {file = "pillow-10.2.0-cp311-cp311-win_amd64.whl", hash = "sha256:1da3b2703afd040cf65ec97efea81cfba59cdbed9c11d8efc5ab09df9509fc56"}, + {file = "pillow-10.2.0-cp311-cp311-win_arm64.whl", hash = "sha256:edca80cbfb2b68d7b56930b84a0e45ae1694aeba0541f798e908a49d66b837f1"}, + {file = "pillow-10.2.0-cp312-cp312-macosx_10_10_x86_64.whl", hash = "sha256:1b5e1b74d1bd1b78bc3477528919414874748dd363e6272efd5abf7654e68bef"}, + {file = "pillow-10.2.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:0eae2073305f451d8ecacb5474997c08569fb4eb4ac231ffa4ad7d342fdc25ac"}, + {file = "pillow-10.2.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:b7c2286c23cd350b80d2fc9d424fc797575fb16f854b831d16fd47ceec078f2c"}, + {file = "pillow-10.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:1e23412b5c41e58cec602f1135c57dfcf15482013ce6e5f093a86db69646a5aa"}, + {file = "pillow-10.2.0-cp312-cp312-manylinux_2_28_aarch64.whl", hash = "sha256:52a50aa3fb3acb9cf7213573ef55d31d6eca37f5709c69e6858fe3bc04a5c2a2"}, + {file = "pillow-10.2.0-cp312-cp312-manylinux_2_28_x86_64.whl", hash = "sha256:127cee571038f252a552760076407f9cff79761c3d436a12af6000cd182a9d04"}, + {file = "pillow-10.2.0-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:8d12251f02d69d8310b046e82572ed486685c38f02176bd08baf216746eb947f"}, + {file = "pillow-10.2.0-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:54f1852cd531aa981bc0965b7d609f5f6cc8ce8c41b1139f6ed6b3c54ab82bfb"}, + {file = "pillow-10.2.0-cp312-cp312-win32.whl", hash = "sha256:257d8788df5ca62c980314053197f4d46eefedf4e6175bc9412f14412ec4ea2f"}, + {file = "pillow-10.2.0-cp312-cp312-win_amd64.whl", hash = "sha256:154e939c5f0053a383de4fd3d3da48d9427a7e985f58af8e94d0b3c9fcfcf4f9"}, + {file = "pillow-10.2.0-cp312-cp312-win_arm64.whl", hash = "sha256:f379abd2f1e3dddb2b61bc67977a6b5a0a3f7485538bcc6f39ec76163891ee48"}, + {file = "pillow-10.2.0-cp38-cp38-macosx_10_10_x86_64.whl", hash = "sha256:8373c6c251f7ef8bda6675dd6d2b3a0fcc31edf1201266b5cf608b62a37407f9"}, + {file = "pillow-10.2.0-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:870ea1ada0899fd0b79643990809323b389d4d1d46c192f97342eeb6ee0b8483"}, + {file = "pillow-10.2.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:b4b6b1e20608493548b1f32bce8cca185bf0480983890403d3b8753e44077129"}, + {file = "pillow-10.2.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:3031709084b6e7852d00479fd1d310b07d0ba82765f973b543c8af5061cf990e"}, + {file = "pillow-10.2.0-cp38-cp38-manylinux_2_28_aarch64.whl", hash = "sha256:3ff074fc97dd4e80543a3e91f69d58889baf2002b6be64347ea8cf5533188213"}, + {file = "pillow-10.2.0-cp38-cp38-manylinux_2_28_x86_64.whl", hash = "sha256:cb4c38abeef13c61d6916f264d4845fab99d7b711be96c326b84df9e3e0ff62d"}, + {file = "pillow-10.2.0-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:b1b3020d90c2d8e1dae29cf3ce54f8094f7938460fb5ce8bc5c01450b01fbaf6"}, + {file = "pillow-10.2.0-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:170aeb00224ab3dc54230c797f8404507240dd868cf52066f66a41b33169bdbe"}, + {file = "pillow-10.2.0-cp38-cp38-win32.whl", hash = "sha256:c4225f5220f46b2fde568c74fca27ae9771536c2e29d7c04f4fb62c83275ac4e"}, + {file = "pillow-10.2.0-cp38-cp38-win_amd64.whl", hash = "sha256:0689b5a8c5288bc0504d9fcee48f61a6a586b9b98514d7d29b840143d6734f39"}, + {file = "pillow-10.2.0-cp39-cp39-macosx_10_10_x86_64.whl", hash = "sha256:b792a349405fbc0163190fde0dc7b3fef3c9268292586cf5645598b48e63dc67"}, + {file = "pillow-10.2.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:c570f24be1e468e3f0ce7ef56a89a60f0e05b30a3669a459e419c6eac2c35364"}, + {file = "pillow-10.2.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d8ecd059fdaf60c1963c58ceb8997b32e9dc1b911f5da5307aab614f1ce5c2fb"}, + {file = "pillow-10.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:c365fd1703040de1ec284b176d6af5abe21b427cb3a5ff68e0759e1e313a5e7e"}, + {file = "pillow-10.2.0-cp39-cp39-manylinux_2_28_aarch64.whl", hash = "sha256:70c61d4c475835a19b3a5aa42492409878bbca7438554a1f89d20d58a7c75c01"}, + {file = "pillow-10.2.0-cp39-cp39-manylinux_2_28_x86_64.whl", hash = "sha256:b6f491cdf80ae540738859d9766783e3b3c8e5bd37f5dfa0b76abdecc5081f13"}, + {file = "pillow-10.2.0-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:9d189550615b4948f45252d7f005e53c2040cea1af5b60d6f79491a6e147eef7"}, + {file = "pillow-10.2.0-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:49d9ba1ed0ef3e061088cd1e7538a0759aab559e2e0a80a36f9fd9d8c0c21591"}, + {file = "pillow-10.2.0-cp39-cp39-win32.whl", hash = "sha256:babf5acfede515f176833ed6028754cbcd0d206f7f614ea3447d67c33be12516"}, + {file = "pillow-10.2.0-cp39-cp39-win_amd64.whl", hash = "sha256:0304004f8067386b477d20a518b50f3fa658a28d44e4116970abfcd94fac34a8"}, + {file = "pillow-10.2.0-cp39-cp39-win_arm64.whl", hash = "sha256:0fb3e7fc88a14eacd303e90481ad983fd5b69c761e9e6ef94c983f91025da869"}, + {file = "pillow-10.2.0-pp310-pypy310_pp73-macosx_10_10_x86_64.whl", hash = "sha256:322209c642aabdd6207517e9739c704dc9f9db943015535783239022002f054a"}, + {file = "pillow-10.2.0-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3eedd52442c0a5ff4f887fab0c1c0bb164d8635b32c894bc1faf4c618dd89df2"}, + {file = "pillow-10.2.0-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:cb28c753fd5eb3dd859b4ee95de66cc62af91bcff5db5f2571d32a520baf1f04"}, + {file = "pillow-10.2.0-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl", hash = "sha256:33870dc4653c5017bf4c8873e5488d8f8d5f8935e2f1fb9a2208c47cdd66efd2"}, + {file = "pillow-10.2.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl", hash = "sha256:3c31822339516fb3c82d03f30e22b1d038da87ef27b6a78c9549888f8ceda39a"}, + {file = "pillow-10.2.0-pp310-pypy310_pp73-win_amd64.whl", hash = "sha256:a2b56ba36e05f973d450582fb015594aaa78834fefe8dfb8fcd79b93e64ba4c6"}, + {file = "pillow-10.2.0-pp38-pypy38_pp73-win_amd64.whl", hash = "sha256:d8e6aeb9201e655354b3ad049cb77d19813ad4ece0df1249d3c793de3774f8c7"}, + {file = "pillow-10.2.0-pp39-pypy39_pp73-macosx_10_10_x86_64.whl", hash = "sha256:2247178effb34a77c11c0e8ac355c7a741ceca0a732b27bf11e747bbc950722f"}, + {file = "pillow-10.2.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:15587643b9e5eb26c48e49a7b33659790d28f190fc514a322d55da2fb5c2950e"}, + {file = "pillow-10.2.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:753cd8f2086b2b80180d9b3010dd4ed147efc167c90d3bf593fe2af21265e5a5"}, + {file = "pillow-10.2.0-pp39-pypy39_pp73-manylinux_2_28_aarch64.whl", hash = "sha256:7c8f97e8e7a9009bcacbe3766a36175056c12f9a44e6e6f2d5caad06dcfbf03b"}, + {file = "pillow-10.2.0-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl", hash = "sha256:d1b35bcd6c5543b9cb547dee3150c93008f8dd0f1fef78fc0cd2b141c5baf58a"}, + {file = "pillow-10.2.0-pp39-pypy39_pp73-win_amd64.whl", hash = "sha256:fe4c15f6c9285dc54ce6553a3ce908ed37c8f3825b5a51a15c91442bb955b868"}, + {file = "pillow-10.2.0.tar.gz", hash = "sha256:e87f0b2c78157e12d7686b27d63c070fd65d994e8ddae6f328e0dcf4a0cd007e"}, ] [package.extras] docs = ["furo", "olefile", "sphinx (>=2.4)", "sphinx-copybutton", "sphinx-inline-tabs", "sphinx-removed-in", "sphinxext-opengraph"] +fpx = ["olefile"] +mic = ["olefile"] tests = ["check-manifest", "coverage", "defusedxml", "markdown2", "olefile", "packaging", "pyroma", "pytest", "pytest-cov", "pytest-timeout"] +typing = ["typing-extensions"] +xmp = ["defusedxml"] [[package]] name = "platformdirs" @@ -3530,6 +3548,7 @@ files = [ {file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"}, {file = "PyYAML-6.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:855fb52b0dc35af121542a76b9a84f8d1cd886ea97c84703eaa6d88e37a2ad28"}, {file = "PyYAML-6.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:40df9b996c2b73138957fe23a16a4f0ba614f4c0efce1e9406a184b6d07fa3a9"}, + {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a08c6f0fe150303c1c6b71ebcd7213c2858041a7e01975da3a99aed1e7a378ef"}, {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c22bec3fbe2524cde73d7ada88f6566758a8f7227bfbf93a408a9d86bcc12a0"}, {file = "PyYAML-6.0.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8d4e9c88387b0f5c7d5f281e55304de64cf7f9c0021a3525bd3b1c542da3b0e4"}, {file = "PyYAML-6.0.1-cp312-cp312-win32.whl", hash = "sha256:d483d2cdf104e7c9fa60c544d92981f12ad66a457afae824d146093b8c294c54"}, From 617fc7a6762f2bb98de3efa45d46a67b05bf3185 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Tue, 23 Jan 2024 15:06:06 +0400 Subject: [PATCH 09/39] another fix --- doc/source/api-reference/qibo.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/api-reference/qibo.rst b/doc/source/api-reference/qibo.rst index bbeeda603c..3ee49b39a9 100644 --- a/doc/source/api-reference/qibo.rst +++ b/doc/source/api-reference/qibo.rst @@ -849,7 +849,7 @@ U1q - Pauli-:math:`Z` rotation: :class:`qibo.gates.RZ` - Arbitrary :math:`ZZ` rotation: :class:`qibo.gates.RZZ` - - Fully-entangling :math:`ZZ`-interaction: :math:`R_{ZZ}(\\pi/2)` + - Fully-entangling :math:`ZZ`-interaction: :math:`R_{ZZ}(\pi/2)` _______________________ From d92d4aaf061bd3bd54f35c148028dc5bdb35991f Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Tue, 23 Jan 2024 17:42:56 +0400 Subject: [PATCH 10/39] another minor fix --- src/qibo/quantum_info/metrics.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/qibo/quantum_info/metrics.py b/src/qibo/quantum_info/metrics.py index 4d2632fcaa..eb29f1b486 100644 --- a/src/qibo/quantum_info/metrics.py +++ b/src/qibo/quantum_info/metrics.py @@ -1176,14 +1176,15 @@ def frame_potential( .. math:: \\mathcal{F}_{\\mathcal{U}}^{(t)} = \\int_{U,V \\in \\mathcal{U}} \\, - \\text{d}U \\, \\text{d}V \\, \\text{abs}\\bigl[\\text{tr}(U^{\\dagger} \\, V)\\bigr]^{2t} \\, , + \\text{d}U \\, \\text{d}V \\, \\bigl| \\, \\text{tr}(U^{\\dagger} \\, V) + \\, \\bigr|^{2t} \\, , where :math:`\\mathcal{U}` is the group of unitaries defined by the parametrized circuit. The frame potential is approximated by the average .. math:: \\mathcal{F}_{\\mathcal{U}}^{(t)} \\approx \\frac{1}{N} \\, - \\sum_{k=1}^{N} \\, \\text{abs}\\bigl[ \\text{tr}(U_{k}^{\\dagger} \\, V_{k})\\bigr]^{2t} \\, , + \\sum_{k=1}^{N} \\, \\bigl| \\, \\text{tr}(U_{k}^{\\dagger} \\, V_{k}) \\, \\bigr|^{2t} \\, , where :math:`N` is the number of ``samples``. From 567541dcef96ea2da663464eaa0136d33bf8925d Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Wed, 24 Jan 2024 12:03:55 +0400 Subject: [PATCH 11/39] basis encoding --- src/qibo/models/encodings.py | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/qibo/models/encodings.py b/src/qibo/models/encodings.py index 3bcad17607..39b11240e7 100644 --- a/src/qibo/models/encodings.py +++ b/src/qibo/models/encodings.py @@ -1,6 +1,7 @@ """Module with functions that encode classical data into quantum circuits.""" import math +from typing import Optional, Union import numpy as np from scipy.stats import rv_continuous @@ -10,6 +11,54 @@ from qibo.models.circuit import Circuit +def comp_basis_encoder( + basis_element: Union[int, str, list, tuple], nqubits: Optional[int] = None +): + if not isinstance(basis_element, (int, str, list, tuple)): + raise_error( + TypeError, + "basis_element must be either type int or str or list or tuple, " + + f"but it is type {type(basis_element)}.", + ) + + if isinstance(basis_element, (str, list, tuple)): + if any(elem not in ["0", "1", 0, 1] for elem in basis_element): + raise_error(ValueError, "all elements must be 0 or 1.") + if len(basis_element) > nqubits: + raise_error( + ValueError, + f"nqubits ({nqubits}) must be >= len(basis_element) ({len(basis_element)}).", + ) + + if not isinstance(nqubits, int): + raise_error( + TypeError, f"nqubits must be type int, but it is type {type(nqubits)}." + ) + + if nqubits is None: + if isinstance(basis_element, int): + raise_error( + ValueError, f"nqubits must be specified when basis_element is type int." + ) + else: + nqubits = len(basis_element) + + if isinstance(basis_element, int): + basis_element = f"{basis_element:0{nqubits}b}" + + if isinstance(basis_element, (str, tuple)): + basis_element = list(basis_element) + + basis_element = list(map(int, basis_element)) + + circuit = Circuit(nqubits) + for qubit, elem in enumerate(basis_element): + if elem == 1: + circuit.add(gates.X(qubit)) + + return circuit + + def unary_encoder(data, architecture: str = "tree"): """Creates circuit that performs the unary encoding of ``data``. From ef7f160c34cb607e9910b7a6d17e05f5b05ec69f Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Wed, 24 Jan 2024 17:30:02 +0400 Subject: [PATCH 12/39] fix bug --- src/qibo/models/encodings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/qibo/models/encodings.py b/src/qibo/models/encodings.py index 39b11240e7..80eb6f86cf 100644 --- a/src/qibo/models/encodings.py +++ b/src/qibo/models/encodings.py @@ -30,7 +30,7 @@ def comp_basis_encoder( f"nqubits ({nqubits}) must be >= len(basis_element) ({len(basis_element)}).", ) - if not isinstance(nqubits, int): + if nqubits is not None and not isinstance(nqubits, int): raise_error( TypeError, f"nqubits must be type int, but it is type {type(nqubits)}." ) From 578887760a1d1f631e2f16709f61afe763f0c107 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Wed, 24 Jan 2024 17:30:09 +0400 Subject: [PATCH 13/39] tests --- tests/test_models_encodings.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/test_models_encodings.py b/tests/test_models_encodings.py index 18c7e2af96..d2dd2fbb15 100644 --- a/tests/test_models_encodings.py +++ b/tests/test_models_encodings.py @@ -5,7 +5,11 @@ import pytest from scipy.optimize import curve_fit -from qibo.models.encodings import unary_encoder, unary_encoder_random_gaussian +from qibo.models.encodings import ( + comp_basis_encoder, + unary_encoder, + unary_encoder_random_gaussian, +) def gaussian(x, a, b, c): @@ -13,6 +17,33 @@ def gaussian(x, a, b, c): return np.exp(a * x**2 + b * x + c) +@pytest.mark.parametrize("nqubits", [3]) +@pytest.mark.parametrize( + "basis_element", [5, "101", ["1", "0", "1"], [1, 0, 1], ("1", "0", "1"), (1, 0, 1)] +) +def test_comp_basis_encoder(backend, basis_element, nqubits): + with pytest.raises(TypeError): + circuit = comp_basis_encoder(2.3) + with pytest.raises(ValueError): + circuit = comp_basis_encoder("0b001") + with pytest.raises(ValueError): + circuit = comp_basis_encoder("001", nqubits=2) + with pytest.raises(TypeError): + circuit = comp_basis_encoder("001", nqubits=3.1) + with pytest.raises(ValueError): + circuit = comp_basis_encoder(3) + + zero = np.array([1, 0], dtype=complex) + one = np.array([0, 1], dtype=complex) + target = np.kron(one, np.kron(zero, one)) + target = backend.cast(target, dtype=target.dtype) + + state = comp_basis_encoder(basis_element, nqubits) + state = backend.execute_circuit(state).state() + + backend.assert_allclose(state, target) + + @pytest.mark.parametrize("architecture", ["tree", "diagonal"]) @pytest.mark.parametrize("nqubits", [8]) def test_unary_encoder(backend, nqubits, architecture): From 9bbd4e30bb42be321bc95286ef8edee4e9ddbabe Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Wed, 24 Jan 2024 17:32:56 +0400 Subject: [PATCH 14/39] api reference --- doc/source/api-reference/qibo.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/source/api-reference/qibo.rst b/doc/source/api-reference/qibo.rst index 3ee49b39a9..1f6e65f071 100644 --- a/doc/source/api-reference/qibo.rst +++ b/doc/source/api-reference/qibo.rst @@ -247,6 +247,12 @@ Data Encoders We provide a family of algorithms that encode classical data into quantum circuits. +Computational Basis Encoder +""""""""""""""""""""""""""" + +.. autofunction:: qibo.models.encodings.comp_basis_encoder + + Unary Encoder """"""""""""" From 7348d75124a021ce342fbf4331567df6fe7cd52d Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Wed, 24 Jan 2024 17:42:34 +0400 Subject: [PATCH 15/39] docstring --- src/qibo/models/encodings.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/qibo/models/encodings.py b/src/qibo/models/encodings.py index 80eb6f86cf..2a6e8e0bdc 100644 --- a/src/qibo/models/encodings.py +++ b/src/qibo/models/encodings.py @@ -14,6 +14,22 @@ def comp_basis_encoder( basis_element: Union[int, str, list, tuple], nqubits: Optional[int] = None ): + """Creates circuit that performs encoding of bitstrings into computational basis states. + + Args: + basis_element (int or str or list or tuple): bitstring to be encoded. + If ``int``, ``nqubits`` must be specified. + If ``str``, must be composed of only :math:`0`s and :math:`1`s. + If ``list`` or ``tuple``, must be composed of :math:`0`s and + :math:`1`s as ``int`` or ``str``. + nqubits (int, optional): total number of qubits in the circuit. + If ``basis_element`` is ``int``, ``nqubits`` must be specified. + If ``nqubits`` is ``None``, ``nqubits`` defaults to length of ``basis_element``. + Defaults to ``None``. + + Returns: + :class:`qibo.models.circuit.Circuit`: circuit encoding computational basis element. + """ if not isinstance(basis_element, (int, str, list, tuple)): raise_error( TypeError, From e234255e22dd41993e134e8f216fe036d54e92d8 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Wed, 24 Jan 2024 17:59:10 +0400 Subject: [PATCH 16/39] Fix test failing and remove prints from tests --- tests/test_backends_clifford.py | 6 +----- tests/test_measurements.py | 2 -- tests/test_quantum_info_random.py | 1 - tests/test_transpiler_blocks.py | 3 --- 4 files changed, 1 insertion(+), 11 deletions(-) diff --git a/tests/test_backends_clifford.py b/tests/test_backends_clifford.py index be6518b2f4..6787c366b5 100644 --- a/tests/test_backends_clifford.py +++ b/tests/test_backends_clifford.py @@ -165,10 +165,7 @@ def test_random_clifford_circuit(backend, prob_qubits, binary): numpy_freq = numpy_result.frequencies(binary) clifford_freq = clifford_result.frequencies(binary) - print(numpy_freq) - print(clifford_freq) clifford_freq = {state: clifford_freq[state] for state in numpy_freq.keys()} - print(clifford_freq.values()) assert ( np.sum( np.abs( @@ -176,7 +173,7 @@ def test_random_clifford_circuit(backend, prob_qubits, binary): - np.array(list(clifford_freq.values())) ) ) - < 200 + < 250 ) @@ -224,7 +221,6 @@ def test_initial_state(backend): initial_symplectic_matrix = tmp.symplectic_matrix initial_state = numpy_bkd.execute_circuit(state).state() initial_state = np.outer(initial_state, np.transpose(np.conj(initial_state))) - print(type(initial_state)) c = random_clifford(3, density_matrix=True, backend=backend) numpy_state = numpy_bkd.execute_circuit(c, initial_state=initial_state).state() clifford_state = clifford_bkd.execute_circuit( diff --git a/tests/test_measurements.py b/tests/test_measurements.py index 69d2aa9f9c..17db6df009 100644 --- a/tests/test_measurements.py +++ b/tests/test_measurements.py @@ -457,9 +457,7 @@ def test_measurement_basis_list(backend): c.add(gates.X(3)) c.add(gates.M(0, 1, 2, 3, basis=[gates.X, gates.Z, gates.X, gates.Z])) result = c(nshots=100) - print(result.frequencies()) assert result.frequencies() == {"0011": 100} - print(c.draw()) assert ( c.draw() == """q0: ─H─H───M─ diff --git a/tests/test_quantum_info_random.py b/tests/test_quantum_info_random.py index 01000ab822..ec85c87bed 100644 --- a/tests/test_quantum_info_random.py +++ b/tests/test_quantum_info_random.py @@ -315,7 +315,6 @@ def test_random_density_matrix(backend, dims, pure, metric, basis, normalize): backend.assert_allclose(norm < PRECISION_TOL, True) else: normalization = 1.0 if normalize is False else 1.0 / np.sqrt(dims) - print(state) backend.assert_allclose(state[0], normalization) assert all(np.abs(exp_value) <= normalization for exp_value in state[1:]) diff --git a/tests/test_transpiler_blocks.py b/tests/test_transpiler_blocks.py index af652f72f5..2a193c7a41 100644 --- a/tests/test_transpiler_blocks.py +++ b/tests/test_transpiler_blocks.py @@ -154,7 +154,6 @@ def test_initial_block_decomposition_measurements(): circ.add(gates.H(3)) circ.add(gates.H(4)) blocks = _initial_block_decomposition(circ) - print(blocks[0].gates) assert_gates_equality( blocks[0].gates, [gates.M(0), gates.M(1), gates.H(1), gates.H(0), gates.CZ(0, 1)], @@ -166,7 +165,6 @@ def test_initial_block_decomposition_measurements(): def test_initial_block_decomposition_error(): circ = Circuit(3) circ.add(gates.TOFFOLI(0, 1, 2)) - print(len(circ.queue[0].qubits)) with pytest.raises(BlockingError): blocks = _initial_block_decomposition(circ) @@ -249,7 +247,6 @@ def test_block_decomposition_measurements(): circ.add(gates.CZ(2, 3)) # 4 block circ.add(gates.M(0, 1)) # 3 block blocks = block_decomposition(circ) - print(blocks[0].gates) assert_gates_equality( blocks[0].gates, [gates.H(1), gates.H(0), gates.CZ(0, 1), gates.H(0), gates.M(0), gates.M(1)], From b226374e8c05d34794c874c27f8bb02dc5bd160f Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Wed, 24 Jan 2024 18:14:11 +0400 Subject: [PATCH 17/39] fix coverage --- tests/test_models_encodings.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/test_models_encodings.py b/tests/test_models_encodings.py index d2dd2fbb15..dd78184669 100644 --- a/tests/test_models_encodings.py +++ b/tests/test_models_encodings.py @@ -17,7 +17,6 @@ def gaussian(x, a, b, c): return np.exp(a * x**2 + b * x + c) -@pytest.mark.parametrize("nqubits", [3]) @pytest.mark.parametrize( "basis_element", [5, "101", ["1", "0", "1"], [1, 0, 1], ("1", "0", "1"), (1, 0, 1)] ) @@ -38,7 +37,11 @@ def test_comp_basis_encoder(backend, basis_element, nqubits): target = np.kron(one, np.kron(zero, one)) target = backend.cast(target, dtype=target.dtype) - state = comp_basis_encoder(basis_element, nqubits) + if isinstance(basis_element, int): + state = comp_basis_encoder(basis_element, nqubits=3) + else: + state = comp_basis_encoder(basis_element) + state = backend.execute_circuit(state).state() backend.assert_allclose(state, target) From 267ef1a57ab7cbc203863dd52a102844b1752c30 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Wed, 24 Jan 2024 18:28:54 +0400 Subject: [PATCH 18/39] fix test bug --- tests/test_models_encodings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_models_encodings.py b/tests/test_models_encodings.py index dd78184669..b102f746bf 100644 --- a/tests/test_models_encodings.py +++ b/tests/test_models_encodings.py @@ -20,7 +20,7 @@ def gaussian(x, a, b, c): @pytest.mark.parametrize( "basis_element", [5, "101", ["1", "0", "1"], [1, 0, 1], ("1", "0", "1"), (1, 0, 1)] ) -def test_comp_basis_encoder(backend, basis_element, nqubits): +def test_comp_basis_encoder(backend, basis_element): with pytest.raises(TypeError): circuit = comp_basis_encoder(2.3) with pytest.raises(ValueError): From 35d9f75f23163b384adf52e56db0021b6ed1575d Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Wed, 24 Jan 2024 19:52:48 +0400 Subject: [PATCH 19/39] fix bug --- src/qibo/models/encodings.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/qibo/models/encodings.py b/src/qibo/models/encodings.py index 2a6e8e0bdc..a85024ad3c 100644 --- a/src/qibo/models/encodings.py +++ b/src/qibo/models/encodings.py @@ -40,11 +40,6 @@ def comp_basis_encoder( if isinstance(basis_element, (str, list, tuple)): if any(elem not in ["0", "1", 0, 1] for elem in basis_element): raise_error(ValueError, "all elements must be 0 or 1.") - if len(basis_element) > nqubits: - raise_error( - ValueError, - f"nqubits ({nqubits}) must be >= len(basis_element) ({len(basis_element)}).", - ) if nqubits is not None and not isinstance(nqubits, int): raise_error( From 0d7d8ba8bca6fd9717240330b0cbb3051d0bdeb8 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 25 Jan 2024 09:55:20 +0400 Subject: [PATCH 20/39] api ref --- doc/source/api-reference/qibo.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/doc/source/api-reference/qibo.rst b/doc/source/api-reference/qibo.rst index 3ee49b39a9..2f8e7d9d92 100644 --- a/doc/source/api-reference/qibo.rst +++ b/doc/source/api-reference/qibo.rst @@ -687,6 +687,13 @@ iSwap (iSWAP) :members: :member-order: bysource +Square root of iSwap (SiSWAP) +""""""""""""""""""""""""""""" + +.. autoclass:: qibo.gates.SiSWAP + :members: + :member-order: bysource + f-Swap (FSWAP) """""""""""""" From 4aa44b6539dfb446f7206fa2b3acdd5deb8c4a75 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 25 Jan 2024 09:55:28 +0400 Subject: [PATCH 21/39] matrix --- src/qibo/backends/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/qibo/backends/__init__.py b/src/qibo/backends/__init__.py index d6f0ac18cf..e891ab2235 100644 --- a/src/qibo/backends/__init__.py +++ b/src/qibo/backends/__init__.py @@ -114,6 +114,7 @@ def create(self, dtype): self.CSXDG = self.matrices.CSXDG self.SWAP = self.matrices.SWAP self.iSWAP = self.matrices.iSWAP + self.SiSWAP = self.matrices.SiSWAP self.FSWAP = self.matrices.FSWAP self.ECR = self.matrices.ECR self.SYC = self.matrices.SYC From f8c8ebb29937f0d7c25f44bb69d780e1eee16690 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 25 Jan 2024 09:55:39 +0400 Subject: [PATCH 22/39] matrix --- src/qibo/backends/npmatrices.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/qibo/backends/npmatrices.py b/src/qibo/backends/npmatrices.py index 366c466cd7..364845c514 100644 --- a/src/qibo/backends/npmatrices.py +++ b/src/qibo/backends/npmatrices.py @@ -194,6 +194,18 @@ def iSWAP(self): [[1, 0, 0, 0], [0, 0, 1j, 0], [0, 1j, 0, 0], [0, 0, 0, 1]], dtype=self.dtype ) + @cached_property + def SiSWAP(self): + return self.np.array( + [ + [1, 0, 0, 0], + [0, 1 / self.np.sqrt(2), 1j / self.np.sqrt(2), 0], + [0, 1j / self.np.sqrt(2), 1 / self.np.sqrt(2), 0], + [0, 0, 0, 1], + ], + dtype=self.dtype, + ) + @cached_property def FSWAP(self): return self.np.array( From 06182b93a2b695ea677e97b5094f3ace4dcf6809 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 25 Jan 2024 09:55:45 +0400 Subject: [PATCH 23/39] gate --- src/qibo/gates/gates.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/qibo/gates/gates.py b/src/qibo/gates/gates.py index 0e94225fca..aa0e0d5ede 100644 --- a/src/qibo/gates/gates.py +++ b/src/qibo/gates/gates.py @@ -1476,7 +1476,7 @@ def qasm_label(self): class iSWAP(Gate): - """The iswap gate. + """The iSWAP gate. Corresponds to the following unitary matrix @@ -1507,6 +1507,33 @@ def qasm_label(self): return "iswap" +class SiSWAP(Gate): + """The :math:`\\sqrt{\\text{iSWAP}}}` gate. + + Corresponds to the following unitary matrix + + .. math:: + \\begin{pmatrix} + 1 & 0 & 0 & 0 \\\\ + 0 & 1/\\sqrt{2} & i/\\sqrt{2} & 0 \\\\ + 0 & i/\\sqrt{2} & 1/\\sqrt{2} & 0 \\\\ + 0 & 0 & 0 & 1 \\\\ + \\end{pmatrix} + + Args: + q0 (int): the first qubit to be swapped id number. + q1 (int): the second qubit to be swapped id number. + """ + + def __init__(self, q0, q1): + super().__init__() + self.name = "siswap" + self.draw_label = "si" + self.target_qubits = (q0, q1) + self.init_args = [q0, q1] + self.unitary = True + + class FSWAP(Gate): """The fermionic swap gate. From 8095f28e3456f397a029919327489d35536a295d Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 25 Jan 2024 09:55:51 +0400 Subject: [PATCH 24/39] test --- tests/test_gates_gates.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_gates_gates.py b/tests/test_gates_gates.py index 8bb0e190ea..c15d0954b4 100644 --- a/tests/test_gates_gates.py +++ b/tests/test_gates_gates.py @@ -711,6 +711,16 @@ def test_iswap(backend): assert gates.iSWAP(0, 1).unitary +def test_siswap(backend): + final_state = apply_gates(backend, [gates.X(1), gates.SiSWAP(0, 1)], nqubits=2) + target_state = np.zeros_like(final_state) + target_state[2] = 1.0j + backend.assert_allclose(final_state, target_state) + + assert not gates.SiSWAP(0, 1).clifford + assert gates.SiSWAP(0, 1).unitary + + def test_fswap(backend): nqubits = 2 initial_state = random_statevector(2**nqubits, backend=backend) From c292bdb43b450fe3771981ad0ea7542da1c10931 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 25 Jan 2024 10:02:15 +0400 Subject: [PATCH 25/39] fix test --- tests/test_gates_gates.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_gates_gates.py b/tests/test_gates_gates.py index c15d0954b4..7af61232f3 100644 --- a/tests/test_gates_gates.py +++ b/tests/test_gates_gates.py @@ -714,7 +714,8 @@ def test_iswap(backend): def test_siswap(backend): final_state = apply_gates(backend, [gates.X(1), gates.SiSWAP(0, 1)], nqubits=2) target_state = np.zeros_like(final_state) - target_state[2] = 1.0j + target_state[1] = 1.0 / np.sqrt(2) + target_state[2] = 1.0j / np.sqrt(2) backend.assert_allclose(final_state, target_state) assert not gates.SiSWAP(0, 1).clifford From 0ac4cf6acc8ab68eb366a3d8d343a32e9b1c02ae Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Fri, 26 Jan 2024 09:29:02 +0400 Subject: [PATCH 26/39] fix seed in 'random_clifford' --- tests/test_backends_clifford.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_backends_clifford.py b/tests/test_backends_clifford.py index 6787c366b5..2f882c6704 100644 --- a/tests/test_backends_clifford.py +++ b/tests/test_backends_clifford.py @@ -136,7 +136,7 @@ def test_random_clifford_circuit(backend, prob_qubits, binary): clifford_bkd = construct_clifford_backend(backend) if not clifford_bkd: return - c = random_clifford(5, backend=backend) + c = random_clifford(5, seed=1, backend=backend) c.density_matrix = True c_copy = c.copy() c.add(gates.M(*MEASURED_QUBITS)) @@ -173,7 +173,7 @@ def test_random_clifford_circuit(backend, prob_qubits, binary): - np.array(list(clifford_freq.values())) ) ) - < 250 + < 200 ) From 3d87eb35160b88d92f1e20bddb65e5cf432fe99d Mon Sep 17 00:00:00 2001 From: Stavros Efthymiou <35475381+stavros11@users.noreply.github.com> Date: Fri, 26 Jan 2024 18:50:36 +0400 Subject: [PATCH 27/39] Remove runcard argument from set_backend --- src/qibo/backends/__init__.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/qibo/backends/__init__.py b/src/qibo/backends/__init__.py index e891ab2235..09e1fe1a54 100644 --- a/src/qibo/backends/__init__.py +++ b/src/qibo/backends/__init__.py @@ -8,7 +8,7 @@ from qibo.config import log, raise_error -def construct_backend(backend, platform=None, runcard=None): +def construct_backend(backend, platform=None): if backend == "qibojit": from qibojit.backends import CupyBackend, CuQuantumBackend, NumbaBackend @@ -33,7 +33,7 @@ def construct_backend(backend, platform=None, runcard=None): elif backend == "qibolab": # pragma: no cover from qibolab.backends import QibolabBackend # pylint: disable=E0401 - return QibolabBackend(platform, runcard) + return QibolabBackend(platform) elif backend == "clifford": if platform is not None: # pragma: no cover if platform in ("cupy", "numba", "cuquantum"): @@ -83,13 +83,13 @@ def __new__(cls): return cls._instance @classmethod - def set_backend(cls, backend, platform=None, runcard=None): # pragma: no cover + def set_backend(cls, backend, platform=None): # pragma: no cover if ( cls._instance is None or cls._instance.name != backend or cls._instance.platform != platform ): - cls._instance = construct_backend(backend, platform, runcard) + cls._instance = construct_backend(backend, platform) log.info(f"Using {cls._instance} backend on {cls._instance.device}") @@ -128,8 +128,8 @@ def get_backend(): return str(GlobalBackend()) -def set_backend(backend, platform=None, runcard=None): - GlobalBackend.set_backend(backend, platform, runcard) +def set_backend(backend, platform=None): + GlobalBackend.set_backend(backend, platform) def get_precision(): From bf883f3c2dc520c78c399f88e3126534955f17d1 Mon Sep 17 00:00:00 2001 From: Andrea Papaluca Date: Mon, 29 Jan 2024 09:49:53 +0400 Subject: [PATCH 28/39] test: fixed tests --- tests/test_backends_clifford.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/tests/test_backends_clifford.py b/tests/test_backends_clifford.py index 2f882c6704..b27d33a4ae 100644 --- a/tests/test_backends_clifford.py +++ b/tests/test_backends_clifford.py @@ -166,15 +166,11 @@ def test_random_clifford_circuit(backend, prob_qubits, binary): numpy_freq = numpy_result.frequencies(binary) clifford_freq = clifford_result.frequencies(binary) clifford_freq = {state: clifford_freq[state] for state in numpy_freq.keys()} - assert ( - np.sum( - np.abs( - np.array(list(numpy_freq.values())) - - np.array(list(clifford_freq.values())) - ) - ) - < 200 - ) + assert len(numpy_freq) == len(clifford_freq) + for np_count, clif_count in zip(numpy_freq.values(), clifford_freq.values()): + backend.assert_allclose( + np_count / 1000, clif_count / 1000, atol=1e-1 + ) # nshots = 1000 def test_collapsing_measurements(backend): @@ -249,5 +245,5 @@ def test_set_backend(backend): clifford_bkd = construct_clifford_backend(backend) if not clifford_bkd: return - set_backend("clifford") + set_backend("clifford", platform=str(backend)) assert isinstance(GlobalBackend(), type(clifford_bkd)) From b0878fd809ef40b5425112067104f181402670af Mon Sep 17 00:00:00 2001 From: Andrea Papaluca Date: Mon, 29 Jan 2024 10:07:15 +0400 Subject: [PATCH 29/39] test: fix test set backend --- tests/test_backends_clifford.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_backends_clifford.py b/tests/test_backends_clifford.py index b27d33a4ae..4170b33365 100644 --- a/tests/test_backends_clifford.py +++ b/tests/test_backends_clifford.py @@ -245,5 +245,8 @@ def test_set_backend(backend): clifford_bkd = construct_clifford_backend(backend) if not clifford_bkd: return - set_backend("clifford", platform=str(backend)) + platform = backend.platform + if platform is None: + platform = str(backend) + set_backend("clifford", platform=platform) assert isinstance(GlobalBackend(), type(clifford_bkd)) From a6d3d079aa3159b0caaf9e322516f94d4da175ba Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Mon, 29 Jan 2024 20:03:40 +0400 Subject: [PATCH 30/39] rewrite function --- src/qibo/quantum_info/utils.py | 36 ++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/qibo/quantum_info/utils.py b/src/qibo/quantum_info/utils.py index f744d07ca5..d124ee1c42 100644 --- a/src/qibo/quantum_info/utils.py +++ b/src/qibo/quantum_info/utils.py @@ -14,12 +14,14 @@ def hamming_weight( - bitstring: Union[int, str, list, tuple, np.ndarray], return_indexes: bool = False + bitstring: Union[int, str, list, tuple], return_indexes: bool = False ): """Calculates the Hamming weight of a bitstring. + The Hamming weight of a bistring is the number of :math:'1's that the bistring contains. + Args: - bitstring (int or str or tuple or list or ndarray): bitstring to calculate the + bitstring (int or str or tuple or list): bitstring to calculate the weight, either in binary or integer representation. return_indexes (bool, optional): If ``True``, returns the indexes of the non-zero elements. Defaults to ``False``. @@ -54,17 +56,18 @@ def hamming_weight( def hamming_distance( - bitstring_1: Union[int, str, list, tuple, np.ndarray], - bitstring_2: Union[int, str, list, tuple, np.ndarray], + bitstring_1: Union[int, str, list, tuple], + bitstring_2: Union[int, str, list, tuple], return_indexes: bool = False, ): """Calculates the Hamming distance between two bistrings. - This is done by calculating the Hamming weight of ``bitstring_1 - bitstring_2``. + This is done by calculating the Hamming weight + (:func:`qibo.quantum_info.utils.hamming_weight`) of ``| bitstring_1 - bitstring_2 |``. Args: - bitstring_1 (int or str or list or tuple or ndarray): fisrt bistring. - bitstring_2 (int or str or list or tuple or ndarray): second bitstring. + bitstring_1 (int or str or list or tuple): fisrt bistring. + bitstring_2 (int or str or list or tuple): second bitstring. return_indexes (bool, optional): If ``True``, returns the indexes of the non-zero elements. Defaults to ``False``. @@ -77,30 +80,29 @@ def hamming_distance( f"return_indexes must be type bool, but it is type {type(return_indexes)}", ) - if not isinstance(bitstring_1, (int, str, list, tuple, np.ndarray)): + if not isinstance(bitstring_1, (int, str, list, tuple)): raise_error( TypeError, "bitstring_1 must be either type int, list, tuple, or numpy.ndarray. " f"However, it is type {type(bitstring_1)}.", ) - if not isinstance(bitstring_2, (int, str, list, tuple, np.ndarray)): + if not isinstance(bitstring_2, (int, str, list, tuple)): raise_error( TypeError, "bitstring_2 must be either type int, list, tuple, or numpy.ndarray. " f"However, it is type {type(bitstring_2)}.", ) - if isinstance(bitstring_1, int): - bitstring_1 = f"{bitstring_1:b}" - - if isinstance(bitstring_2, int): - bitstring_2 = f"{bitstring_2:b}" + if isinstance(bitstring_1, (list, tuple)): + bitstring_1 = "".join(bitstring_1) - bitstring_1 = np.array(list(bitstring_1), dtype=int) - bitstring_2 = np.array(list(bitstring_2), dtype=int) + if isinstance(bitstring_2, (list, tuple)): + bitstring_2 = "".join(bitstring_2) - return hamming_weight(bitstring_1 - bitstring_2, return_indexes=return_indexes) + return hamming_weight( + abs(int(bitstring_1, 2) - int(bitstring_2, 2)), return_indexes=return_indexes + ) def hadamard_transform(array, implementation: str = "fast", backend=None): From 7b4a57775809ca1eb036256f285edb27fc910e90 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Mon, 29 Jan 2024 20:03:46 +0400 Subject: [PATCH 31/39] better test --- tests/test_quantum_info_utils.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/test_quantum_info_utils.py b/tests/test_quantum_info_utils.py index 57fb10c4fd..7469a4b823 100644 --- a/tests/test_quantum_info_utils.py +++ b/tests/test_quantum_info_utils.py @@ -48,19 +48,17 @@ def test_hamming_weight(bitstring, kind): assert indexes == indexes_test -@pytest.mark.parametrize("bitstring", range(2**5)) +bitstring_1, bitstring_2 = "11111", "10101" @pytest.mark.parametrize( - "kind", + ["bitstring_1", "bitstring_2"], [ - str, - list, - tuple, - lambda b: np.array(list(b)), - lambda b: int(b, 2), - lambda b: list(map(int, b)), - ], + [bitstring_1, bitstring_2], + [int(bitstring_1, 2), int(bitstring_2, 2)], + [list(bitstring_1), list(bitstring_2)], + [tuple(bitstring_1), tuple(bitstring_2)], + ] ) -def test_hamming_distance(bitstring, kind): +def test_hamming_distance(bitstring_1, bitstring_2): with pytest.raises(TypeError): test = hamming_distance("0101", "1010", return_indexes="True") with pytest.raises(TypeError): @@ -68,12 +66,14 @@ def test_hamming_distance(bitstring, kind): with pytest.raises(TypeError): test = hamming_distance("1010", 2.3) - bitstring = f"{bitstring:b}" - distance = hamming_distance(kind(bitstring), kind(bitstring)) - indexes = hamming_distance(kind(bitstring), kind(bitstring), True) + if isinstance(bitstring_1, int): + bitstring_1, bitstring_2 = f"{bitstring_1:b}", f"{bitstring_2:b}" + + distance = hamming_distance(bitstring_1, bitstring_2) + indexes = hamming_distance(bitstring_1, bitstring_2, return_indexes=True) - assert distance == 0 - assert indexes == [] + assert distance == 2 + assert indexes == [0, 2] @pytest.mark.parametrize("is_matrix", [False, True]) From 9663a0db5e1fe2e8b3001e61a5539fd67993dcf8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 29 Jan 2024 16:04:14 +0000 Subject: [PATCH 32/39] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/qibo/quantum_info/utils.py | 2 +- tests/test_quantum_info_utils.py | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/qibo/quantum_info/utils.py b/src/qibo/quantum_info/utils.py index d124ee1c42..42748a1bda 100644 --- a/src/qibo/quantum_info/utils.py +++ b/src/qibo/quantum_info/utils.py @@ -62,7 +62,7 @@ def hamming_distance( ): """Calculates the Hamming distance between two bistrings. - This is done by calculating the Hamming weight + This is done by calculating the Hamming weight (:func:`qibo.quantum_info.utils.hamming_weight`) of ``| bitstring_1 - bitstring_2 |``. Args: diff --git a/tests/test_quantum_info_utils.py b/tests/test_quantum_info_utils.py index 7469a4b823..a10798c74a 100644 --- a/tests/test_quantum_info_utils.py +++ b/tests/test_quantum_info_utils.py @@ -49,14 +49,16 @@ def test_hamming_weight(bitstring, kind): bitstring_1, bitstring_2 = "11111", "10101" + + @pytest.mark.parametrize( - ["bitstring_1", "bitstring_2"], + ["bitstring_1", "bitstring_2"], [ - [bitstring_1, bitstring_2], + [bitstring_1, bitstring_2], [int(bitstring_1, 2), int(bitstring_2, 2)], [list(bitstring_1), list(bitstring_2)], [tuple(bitstring_1), tuple(bitstring_2)], - ] + ], ) def test_hamming_distance(bitstring_1, bitstring_2): with pytest.raises(TypeError): @@ -68,7 +70,7 @@ def test_hamming_distance(bitstring_1, bitstring_2): if isinstance(bitstring_1, int): bitstring_1, bitstring_2 = f"{bitstring_1:b}", f"{bitstring_2:b}" - + distance = hamming_distance(bitstring_1, bitstring_2) indexes = hamming_distance(bitstring_1, bitstring_2, return_indexes=True) From 4ad2bbe2666a97d78f60a93d938de758e2ba5d71 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Mon, 29 Jan 2024 20:28:23 +0400 Subject: [PATCH 33/39] fix bug --- src/qibo/quantum_info/utils.py | 8 +++++--- tests/test_quantum_info_utils.py | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/qibo/quantum_info/utils.py b/src/qibo/quantum_info/utils.py index 42748a1bda..5e012ba523 100644 --- a/src/qibo/quantum_info/utils.py +++ b/src/qibo/quantum_info/utils.py @@ -100,9 +100,11 @@ def hamming_distance( if isinstance(bitstring_2, (list, tuple)): bitstring_2 = "".join(bitstring_2) - return hamming_weight( - abs(int(bitstring_1, 2) - int(bitstring_2, 2)), return_indexes=return_indexes - ) + nbits = max(len(bitstring_1), len(bitstring_2)) + + difference = abs(int(bitstring_1, 2) - int(bitstring_2, 2)) + + return hamming_weight(f"{difference:{nbits}b}", return_indexes=return_indexes) def hadamard_transform(array, implementation: str = "fast", backend=None): diff --git a/tests/test_quantum_info_utils.py b/tests/test_quantum_info_utils.py index a10798c74a..32aa293ca7 100644 --- a/tests/test_quantum_info_utils.py +++ b/tests/test_quantum_info_utils.py @@ -75,7 +75,7 @@ def test_hamming_distance(bitstring_1, bitstring_2): indexes = hamming_distance(bitstring_1, bitstring_2, return_indexes=True) assert distance == 2 - assert indexes == [0, 2] + assert indexes == [1, 3] @pytest.mark.parametrize("is_matrix", [False, True]) From 796e7a12bb4eeb5adacf13843ef02e4fc9cdd81f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 29 Jan 2024 20:57:00 +0000 Subject: [PATCH 34/39] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/psf/black: 23.12.1 → 24.1.1](https://github.com/psf/black/compare/23.12.1...24.1.1) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 390b5863f6..28dfdd0dfc 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,7 +9,7 @@ repos: - id: check-yaml - id: debug-statements - repo: https://github.com/psf/black - rev: 23.12.1 + rev: 24.1.1 hooks: - id: black - repo: https://github.com/pycqa/isort From abe4cdd2e8e068e4d8d70ece4ae09f1c82e3902e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 29 Jan 2024 20:57:25 +0000 Subject: [PATCH 35/39] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- examples/adiabatic/linear.py | 1 + examples/adiabatic/optimize.py | 1 + examples/adiabatic/trotter_error.py | 1 + examples/benchmarks/evolution.py | 1 + examples/benchmarks/main.py | 1 + examples/benchmarks/qaoa.py | 1 + examples/benchmarks/vqe.py | 1 + examples/mvc/main.py | 1 + src/qibo/backends/clifford.py | 1 + src/qibo/backends/einsum_utils.py | 1 + src/qibo/config.py | 1 + src/qibo/gates/channels.py | 1 + src/qibo/hamiltonians/hamiltonians.py | 1 + src/qibo/models/evolution.py | 1 + src/qibo/parallel.py | 1 + tests/conftest.py | 1 + tests/test_callbacks.py | 1 + tests/test_gates_abstract.py | 1 + tests/test_gates_channels.py | 1 + tests/test_gates_density_matrix.py | 1 + tests/test_gates_gates.py | 1 + tests/test_hamiltonians.py | 1 + tests/test_hamiltonians_from_symbols.py | 1 + tests/test_hamiltonians_models.py | 1 + tests/test_hamiltonians_symbolic.py | 1 + tests/test_hamiltonians_terms.py | 1 + tests/test_hamiltonians_trotter.py | 1 + tests/test_measurements.py | 1 + tests/test_measurements_collapse.py | 1 + tests/test_measurements_probabilistic.py | 1 + tests/test_models_circuit.py | 1 + tests/test_models_circuit_backpropagation.py | 1 + tests/test_models_circuit_features.py | 1 + tests/test_models_circuit_noise.py | 1 + tests/test_models_circuit_parametrized.py | 1 + tests/test_models_circuit_qasm.py | 1 + tests/test_models_circuit_qasm_cirq.py | 1 + tests/test_models_dbi.py | 1 + tests/test_models_distcircuit.py | 1 + tests/test_models_distcircuit_execution.py | 1 + tests/test_models_encodings.py | 1 + tests/test_models_grover.py | 1 + tests/test_models_hep.py | 1 + tests/test_models_iqae.py | 9 +++------ tests/test_models_qgan.py | 1 + tests/test_models_utils.py | 1 + tests/test_models_variational.py | 1 + tests/test_parallel.py | 1 + tests/test_prints.py | 1 + 49 files changed, 51 insertions(+), 6 deletions(-) diff --git a/examples/adiabatic/linear.py b/examples/adiabatic/linear.py index 6cff88ca20..8609a31fcd 100644 --- a/examples/adiabatic/linear.py +++ b/examples/adiabatic/linear.py @@ -1,4 +1,5 @@ """Adiabatic evolution for the Ising Hamiltonian using linear scaling.""" + import argparse from pathlib import Path diff --git a/examples/adiabatic/optimize.py b/examples/adiabatic/optimize.py index d5fdb81675..f81bf51f93 100644 --- a/examples/adiabatic/optimize.py +++ b/examples/adiabatic/optimize.py @@ -1,4 +1,5 @@ """Adiabatic evolution scheduling optimization for the Ising Hamiltonian.""" + import argparse from pathlib import Path diff --git a/examples/adiabatic/trotter_error.py b/examples/adiabatic/trotter_error.py index 5a202d8368..80892db310 100644 --- a/examples/adiabatic/trotter_error.py +++ b/examples/adiabatic/trotter_error.py @@ -1,4 +1,5 @@ """Error of evolution using Trotter decomposition.""" + import argparse import matplotlib diff --git a/examples/benchmarks/evolution.py b/examples/benchmarks/evolution.py index 7756fb6909..990bafd90b 100644 --- a/examples/benchmarks/evolution.py +++ b/examples/benchmarks/evolution.py @@ -1,4 +1,5 @@ """Adiabatic evolution for the Ising Hamiltonian using linear scaling.""" + import argparse import time diff --git a/examples/benchmarks/main.py b/examples/benchmarks/main.py index 7273bd76dd..4809edf044 100644 --- a/examples/benchmarks/main.py +++ b/examples/benchmarks/main.py @@ -3,6 +3,7 @@ The type of the circuit is selected using the ``--type`` flag. """ + import argparse import os import time diff --git a/examples/benchmarks/qaoa.py b/examples/benchmarks/qaoa.py index 2461933334..2cbb704c17 100644 --- a/examples/benchmarks/qaoa.py +++ b/examples/benchmarks/qaoa.py @@ -1,6 +1,7 @@ """ Benchmark Quantum Approximate Optimization Algorithm model. """ + import argparse import time diff --git a/examples/benchmarks/vqe.py b/examples/benchmarks/vqe.py index 5cab1d2593..2b345b044b 100644 --- a/examples/benchmarks/vqe.py +++ b/examples/benchmarks/vqe.py @@ -1,6 +1,7 @@ """ Benchmark Variational Quantum Eigensolver. """ + import argparse import time diff --git a/examples/mvc/main.py b/examples/mvc/main.py index c0513e81e4..2cfe3202f0 100755 --- a/examples/mvc/main.py +++ b/examples/mvc/main.py @@ -1,4 +1,5 @@ """Minimum Vertex Cover""" + import argparse import csv diff --git a/src/qibo/backends/clifford.py b/src/qibo/backends/clifford.py index c81f8760da..f950c04aeb 100644 --- a/src/qibo/backends/clifford.py +++ b/src/qibo/backends/clifford.py @@ -1,4 +1,5 @@ """Module defining the Clifford backend.""" + import collections from functools import reduce from typing import Union diff --git a/src/qibo/backends/einsum_utils.py b/src/qibo/backends/einsum_utils.py index f06feaab4f..347eed8760 100644 --- a/src/qibo/backends/einsum_utils.py +++ b/src/qibo/backends/einsum_utils.py @@ -3,6 +3,7 @@ specifies the contraction indices is created using the following methods and used by :meth:`qibo.backends.numpy.NumpyEngine.apply_gate`. """ + from qibo.config import EINSUM_CHARS, raise_error diff --git a/src/qibo/config.py b/src/qibo/config.py index 33dd39f78b..6cdbb91af5 100644 --- a/src/qibo/config.py +++ b/src/qibo/config.py @@ -1,6 +1,7 @@ """ Define the default circuit, constants and types. """ + import logging import os diff --git a/src/qibo/gates/channels.py b/src/qibo/gates/channels.py index d63ad9a354..5790c6e601 100644 --- a/src/qibo/gates/channels.py +++ b/src/qibo/gates/channels.py @@ -1,4 +1,5 @@ """Define quantum channels.""" + from itertools import product from math import exp, sqrt from typing import Optional, Tuple diff --git a/src/qibo/hamiltonians/hamiltonians.py b/src/qibo/hamiltonians/hamiltonians.py index 2933163354..6354bf316b 100644 --- a/src/qibo/hamiltonians/hamiltonians.py +++ b/src/qibo/hamiltonians/hamiltonians.py @@ -1,4 +1,5 @@ """Module defining Hamiltonian classes.""" + from itertools import chain from typing import Optional diff --git a/src/qibo/models/evolution.py b/src/qibo/models/evolution.py index 9a9cee8885..b40e3577a1 100644 --- a/src/qibo/models/evolution.py +++ b/src/qibo/models/evolution.py @@ -1,4 +1,5 @@ """Models for time evolution of state vectors.""" + from qibo import optimizers, solvers from qibo.callbacks import Gap, Norm from qibo.config import log, raise_error diff --git a/src/qibo/parallel.py b/src/qibo/parallel.py index 3127617d5c..0bbf84f055 100644 --- a/src/qibo/parallel.py +++ b/src/qibo/parallel.py @@ -1,6 +1,7 @@ """ Resources for parallel circuit evaluation. """ + from typing import Iterable from joblib import Parallel, delayed diff --git a/tests/conftest.py b/tests/conftest.py index e68e2166b0..618423bbb6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,6 +3,7 @@ Pytest fixtures. """ + import sys import pytest diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index 6a0a70c75a..4c19b859a9 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -1,4 +1,5 @@ """Test methods defined in `qibo/core/callbacks.py`.""" + import numpy as np import pytest diff --git a/tests/test_gates_abstract.py b/tests/test_gates_abstract.py index 05c4b9a92b..c3e4fabbd2 100644 --- a/tests/test_gates_abstract.py +++ b/tests/test_gates_abstract.py @@ -1,5 +1,6 @@ """Tests methods defined in `qibo/gates/abstract.py` and `qibo/gates/gates.py`.""" + import json from typing import Optional diff --git a/tests/test_gates_channels.py b/tests/test_gates_channels.py index fb3aa0deb5..a512b83831 100644 --- a/tests/test_gates_channels.py +++ b/tests/test_gates_channels.py @@ -1,4 +1,5 @@ """Test channels defined in `qibo/gates.py`.""" + import numpy as np import pytest diff --git a/tests/test_gates_density_matrix.py b/tests/test_gates_density_matrix.py index c40825099c..b83e02a98e 100644 --- a/tests/test_gates_density_matrix.py +++ b/tests/test_gates_density_matrix.py @@ -1,4 +1,5 @@ """Test gates acting on density matrices.""" + import numpy as np import pytest diff --git a/tests/test_gates_gates.py b/tests/test_gates_gates.py index 7af61232f3..e014031eac 100644 --- a/tests/test_gates_gates.py +++ b/tests/test_gates_gates.py @@ -1,4 +1,5 @@ """Test gates defined in `qibo/gates/gates.py`.""" + import numpy as np import pytest diff --git a/tests/test_hamiltonians.py b/tests/test_hamiltonians.py index ff495d19a2..a00bb53828 100644 --- a/tests/test_hamiltonians.py +++ b/tests/test_hamiltonians.py @@ -1,4 +1,5 @@ """Test methods in `qibo/core/hamiltonians.py`.""" + import numpy as np import pytest diff --git a/tests/test_hamiltonians_from_symbols.py b/tests/test_hamiltonians_from_symbols.py index 4208ab5fb7..999f3a7fbc 100644 --- a/tests/test_hamiltonians_from_symbols.py +++ b/tests/test_hamiltonians_from_symbols.py @@ -1,4 +1,5 @@ """Test dense matrix of Hamiltonians constructed using symbols.""" + import numpy as np import pytest import sympy diff --git a/tests/test_hamiltonians_models.py b/tests/test_hamiltonians_models.py index d1178dc70c..8539d0e116 100644 --- a/tests/test_hamiltonians_models.py +++ b/tests/test_hamiltonians_models.py @@ -1,4 +1,5 @@ """Tests methods from `qibo/src/hamiltonians/models.py`.""" + import numpy as np import pytest diff --git a/tests/test_hamiltonians_symbolic.py b/tests/test_hamiltonians_symbolic.py index da1f6af087..e2b214108f 100644 --- a/tests/test_hamiltonians_symbolic.py +++ b/tests/test_hamiltonians_symbolic.py @@ -1,4 +1,5 @@ """Test methods of :class:`qibo.core.hamiltonians.SymbolicHamiltonian`.""" + import numpy as np import pytest import sympy diff --git a/tests/test_hamiltonians_terms.py b/tests/test_hamiltonians_terms.py index f253284e08..1678e6d26e 100644 --- a/tests/test_hamiltonians_terms.py +++ b/tests/test_hamiltonians_terms.py @@ -1,4 +1,5 @@ """Tests methods defined in `qibo/core/terms.py`.""" + import numpy as np import pytest diff --git a/tests/test_hamiltonians_trotter.py b/tests/test_hamiltonians_trotter.py index ad72c5b9da..a3e22551d3 100644 --- a/tests/test_hamiltonians_trotter.py +++ b/tests/test_hamiltonians_trotter.py @@ -1,4 +1,5 @@ """Test Trotter Hamiltonian methods from `qibo/core/hamiltonians.py`.""" + import numpy as np import pytest diff --git a/tests/test_measurements.py b/tests/test_measurements.py index 17db6df009..07ba9d9631 100644 --- a/tests/test_measurements.py +++ b/tests/test_measurements.py @@ -1,4 +1,5 @@ """Test circuit result measurements and measurement gate and as part of circuit.""" + import numpy as np import pytest diff --git a/tests/test_measurements_collapse.py b/tests/test_measurements_collapse.py index 7edb1348f3..2ff1c78428 100644 --- a/tests/test_measurements_collapse.py +++ b/tests/test_measurements_collapse.py @@ -1,4 +1,5 @@ """Test :class:`qibo.gates.M` as standalone and as part of circuit.""" + import numpy as np import pytest diff --git a/tests/test_measurements_probabilistic.py b/tests/test_measurements_probabilistic.py index a4e08720ae..ebe8ec84f3 100644 --- a/tests/test_measurements_probabilistic.py +++ b/tests/test_measurements_probabilistic.py @@ -1,4 +1,5 @@ """Test circuit measurements when outcome is probabilistic.""" + import numpy as np import pytest diff --git a/tests/test_models_circuit.py b/tests/test_models_circuit.py index ca8b191d6d..be68547c51 100644 --- a/tests/test_models_circuit.py +++ b/tests/test_models_circuit.py @@ -1,4 +1,5 @@ """Test all methods defined in `qibo/models/circuit.py`.""" + from collections import Counter import pytest diff --git a/tests/test_models_circuit_backpropagation.py b/tests/test_models_circuit_backpropagation.py index 2458a38b99..12bccc215a 100644 --- a/tests/test_models_circuit_backpropagation.py +++ b/tests/test_models_circuit_backpropagation.py @@ -1,4 +1,5 @@ """Tests Tensorflow's backpropagation when using `tf.Variable` parameters.""" + import numpy as np import pytest diff --git a/tests/test_models_circuit_features.py b/tests/test_models_circuit_features.py index 8944b25b6a..d0e6849145 100644 --- a/tests/test_models_circuit_features.py +++ b/tests/test_models_circuit_features.py @@ -1,4 +1,5 @@ """Test how features defined in :class:`qibo.models.circuit.Circuit` work during circuit execution.""" + from collections import Counter import numpy as np diff --git a/tests/test_models_circuit_noise.py b/tests/test_models_circuit_noise.py index b1710c4520..74a14fb6be 100644 --- a/tests/test_models_circuit_noise.py +++ b/tests/test_models_circuit_noise.py @@ -1,4 +1,5 @@ """Test :class:`qibo.models.circuit.Circuit` for density matrix and noise simulation.""" + import numpy as np import pytest diff --git a/tests/test_models_circuit_parametrized.py b/tests/test_models_circuit_parametrized.py index 2a66947c57..c9ac4990bc 100644 --- a/tests/test_models_circuit_parametrized.py +++ b/tests/test_models_circuit_parametrized.py @@ -1,4 +1,5 @@ """Test :meth:`qibo.models.circuit.Circuit.get_parameters` and :meth:`qibo.models.circuit.Circuit.set_parameters`.""" + import sys import numpy as np diff --git a/tests/test_models_circuit_qasm.py b/tests/test_models_circuit_qasm.py index 24a0f2881e..83f6c6fe47 100644 --- a/tests/test_models_circuit_qasm.py +++ b/tests/test_models_circuit_qasm.py @@ -1,4 +1,5 @@ """Tests creating abstract Qibo circuits from OpenQASM code.""" + import numpy as np import pytest diff --git a/tests/test_models_circuit_qasm_cirq.py b/tests/test_models_circuit_qasm_cirq.py index ea9634d83d..f0e683e88b 100644 --- a/tests/test_models_circuit_qasm_cirq.py +++ b/tests/test_models_circuit_qasm_cirq.py @@ -1,4 +1,5 @@ """Tests executing Qibo circuits created from OpenQASM code.""" + import cirq import numpy as np import pytest diff --git a/tests/test_models_dbi.py b/tests/test_models_dbi.py index a3b22d1bbe..d3e4236191 100644 --- a/tests/test_models_dbi.py +++ b/tests/test_models_dbi.py @@ -1,4 +1,5 @@ """Testing DoubleBracketIteration model""" + import numpy as np import pytest diff --git a/tests/test_models_distcircuit.py b/tests/test_models_distcircuit.py index 53decc00ed..97d1e09c18 100644 --- a/tests/test_models_distcircuit.py +++ b/tests/test_models_distcircuit.py @@ -1,4 +1,5 @@ """Test functions defined in `qibo/models/distcircuit.py`.""" + import pytest from qibo import Circuit, gates diff --git a/tests/test_models_distcircuit_execution.py b/tests/test_models_distcircuit_execution.py index 9ee28a6af7..c726e9a17e 100644 --- a/tests/test_models_distcircuit_execution.py +++ b/tests/test_models_distcircuit_execution.py @@ -1,4 +1,5 @@ """Test :class:`qibo.models.distcircuit.DistributedCircuit` execution.""" + import numpy as np import pytest diff --git a/tests/test_models_encodings.py b/tests/test_models_encodings.py index 18c7e2af96..7a217baf0d 100644 --- a/tests/test_models_encodings.py +++ b/tests/test_models_encodings.py @@ -1,4 +1,5 @@ """Tests for qibo.models.encodings""" + import math import numpy as np diff --git a/tests/test_models_grover.py b/tests/test_models_grover.py index 29640d7905..b0725a80d1 100644 --- a/tests/test_models_grover.py +++ b/tests/test_models_grover.py @@ -1,4 +1,5 @@ """Test Grover model defined in `qibo/models/grover.py`.""" + import pytest from qibo import Circuit, gates diff --git a/tests/test_models_hep.py b/tests/test_models_hep.py index 3b06540cbf..fdf0a46112 100644 --- a/tests/test_models_hep.py +++ b/tests/test_models_hep.py @@ -1,4 +1,5 @@ """Testing HEP models.""" + import numpy as np import pytest diff --git a/tests/test_models_iqae.py b/tests/test_models_iqae.py index 75201c8ae1..891015eb10 100644 --- a/tests/test_models_iqae.py +++ b/tests/test_models_iqae.py @@ -1,4 +1,5 @@ """Test IQAE model defined in `qibo/models/iqae.py`.""" + import numpy as np import pytest @@ -144,9 +145,7 @@ def R_qibo(qc, qx, qx_measure, nbit, b_max, b_min): gates.RY(q=qx_measure, theta=(b_max - b_min) / 2**nbit * 2 * 0.5 + 2 * b_min) ) for i in range(nbit): - qc.add( - gates.CU3(qx[i], qx_measure, 2**i * (b_max - b_min) / 2**nbit * 2, 0, 0) - ) + qc.add(gates.CU3(qx[i], qx_measure, 2**i * (b_max - b_min) / 2**nbit * 2, 0, 0)) def Rinv_qibo(qc, qx, qx_measure, nbit, b_max, b_min): @@ -161,9 +160,7 @@ def Rinv_qibo(qc, qx, qx_measure, nbit, b_max, b_min): """ for i in range(nbit)[::-1]: qc.add( - gates.CU3( - qx[i], qx_measure, -(2**i) * (b_max - b_min) / 2**nbit * 2, 0, 0 - ) + gates.CU3(qx[i], qx_measure, -(2**i) * (b_max - b_min) / 2**nbit * 2, 0, 0) ) qc.add( gates.RY(q=qx_measure, theta=-(b_max - b_min) / 2**nbit * 2 * 0.5 - 2 * b_min) diff --git a/tests/test_models_qgan.py b/tests/test_models_qgan.py index 6e227fd1a6..429137d2fc 100644 --- a/tests/test_models_qgan.py +++ b/tests/test_models_qgan.py @@ -1,4 +1,5 @@ """Test style-qGAN model defined in `qibo/models/qgan.py`.""" + import sys import numpy as np diff --git a/tests/test_models_utils.py b/tests/test_models_utils.py index 1f1bb849bb..5b7ce88ee6 100644 --- a/tests/test_models_utils.py +++ b/tests/test_models_utils.py @@ -1,4 +1,5 @@ """Test `fourier_coefficients' in `qibo/models/utils.py`.""" + import numpy as np import pytest diff --git a/tests/test_models_variational.py b/tests/test_models_variational.py index 30f45565d5..ee269e58df 100644 --- a/tests/test_models_variational.py +++ b/tests/test_models_variational.py @@ -1,6 +1,7 @@ """ Testing Variational Quantum Circuits. """ + import pathlib import numpy as np diff --git a/tests/test_parallel.py b/tests/test_parallel.py index 8bd6ddfb7d..40f22c7cde 100644 --- a/tests/test_parallel.py +++ b/tests/test_parallel.py @@ -1,6 +1,7 @@ """ Testing parallel evaluations. """ + import sys import numpy as np diff --git a/tests/test_prints.py b/tests/test_prints.py index 500cd3e173..fcf94ddf35 100644 --- a/tests/test_prints.py +++ b/tests/test_prints.py @@ -1,4 +1,5 @@ """Test that the source contains no prints.""" + import os import pathlib From 05f994b2156737bf97e8aeded4249df183fe8454 Mon Sep 17 00:00:00 2001 From: MatteoRobbiati Date: Fri, 2 Feb 2024 09:51:52 +0800 Subject: [PATCH 36/39] fix numpy seed --- tests/test_derivative.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_derivative.py b/tests/test_derivative.py index ec7a501720..e90af942fa 100644 --- a/tests/test_derivative.py +++ b/tests/test_derivative.py @@ -33,6 +33,7 @@ def circuit(nqubits=1): def test_standard_parameter_shift(backend, nshots, atol, scale_factor, grads): # initializing the circuit c = circuit(nqubits=1) + np.random.seed(42) # some parameters # we know the derivative's values with these params @@ -88,6 +89,7 @@ def test_standard_parameter_shift(backend, nshots, atol, scale_factor, grads): def test_finite_differences(backend, step_size): # initializing the circuit c = circuit(nqubits=1) + np.random.seed(42) # some parameters test_params = np.linspace(0.1, 1, 3) From c4fddeae61e96c37b561e672ad1a8ea94a0d698d Mon Sep 17 00:00:00 2001 From: Matteo Robbiati <62071516+MatteoRobbiati@users.noreply.github.com> Date: Fri, 2 Feb 2024 13:03:41 +0800 Subject: [PATCH 37/39] Apply suggestions from code review Co-authored-by: Renato Mello --- tests/test_derivative.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_derivative.py b/tests/test_derivative.py index e90af942fa..60ea06244e 100644 --- a/tests/test_derivative.py +++ b/tests/test_derivative.py @@ -33,7 +33,7 @@ def circuit(nqubits=1): def test_standard_parameter_shift(backend, nshots, atol, scale_factor, grads): # initializing the circuit c = circuit(nqubits=1) - np.random.seed(42) + backend.set_seed(42) # some parameters # we know the derivative's values with these params @@ -89,7 +89,7 @@ def test_standard_parameter_shift(backend, nshots, atol, scale_factor, grads): def test_finite_differences(backend, step_size): # initializing the circuit c = circuit(nqubits=1) - np.random.seed(42) + backend.set_seed(42) # some parameters test_params = np.linspace(0.1, 1, 3) From ab8e039c313c816ec007680ccf38a771199f60af Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Fri, 2 Feb 2024 09:22:51 +0400 Subject: [PATCH 38/39] add to doc --- doc/source/api-reference/qibo.rst | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/doc/source/api-reference/qibo.rst b/doc/source/api-reference/qibo.rst index e2b2568eda..5867c9b3ac 100644 --- a/doc/source/api-reference/qibo.rst +++ b/doc/source/api-reference/qibo.rst @@ -250,6 +250,26 @@ We provide a family of algorithms that encode classical data into quantum circui Computational Basis Encoder """"""""""""""""""""""""""" +Given a bitstring :math:`b` of length :math:`n`, this encoder generates of a layer of Pauli-:math:`X`` +gates that creates the quantum state :math:`|\,b\,\rangle`. + +For instance, the following two circuit generations are equivalent: + +.. testsetup:: + + from qibo import Circuit, gates + from qibo.models.encodings import comp_basis_encoder + +.. testcode:: + + b = "101" + circuit_1 = comp_basis_encoder(b) + + circuit_2 = Circuit(3) + circuit_2.add(gates.X(0)) + circuit_2.add(gates.X(2)) + + .. autofunction:: qibo.models.encodings.comp_basis_encoder From 8b85b6413e31232a695f53b94b599d7a501d713a Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Fri, 2 Feb 2024 08:31:36 +0000 Subject: [PATCH 39/39] Update doc/source/api-reference/qibo.rst Co-authored-by: BrunoLiegiBastonLiegi <45011234+BrunoLiegiBastonLiegi@users.noreply.github.com> --- doc/source/api-reference/qibo.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/api-reference/qibo.rst b/doc/source/api-reference/qibo.rst index 5867c9b3ac..ad04550e1c 100644 --- a/doc/source/api-reference/qibo.rst +++ b/doc/source/api-reference/qibo.rst @@ -250,7 +250,7 @@ We provide a family of algorithms that encode classical data into quantum circui Computational Basis Encoder """"""""""""""""""""""""""" -Given a bitstring :math:`b` of length :math:`n`, this encoder generates of a layer of Pauli-:math:`X`` +Given a bitstring :math:`b` of length :math:`n`, this encoder generates a layer of Pauli-:math:`X` gates that creates the quantum state :math:`|\,b\,\rangle`. For instance, the following two circuit generations are equivalent: