diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 177c408f..76230b3f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ # See https://pre-commit.com/hooks.html for more hooks repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v4.5.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer @@ -11,15 +11,15 @@ repos: - id: check-merge-conflict - id: debug-statements - repo: https://github.com/psf/black - rev: 23.1.0 + rev: 24.2.0 hooks: - id: black - repo: https://github.com/pycqa/isort - rev: 5.12.0 + rev: 5.13.2 hooks: - id: isort args: ["--profile", "black"] - repo: https://github.com/asottile/pyupgrade - rev: v3.3.1 + rev: v3.15.0 hooks: - id: pyupgrade diff --git a/setup.py b/setup.py index 13285eb7..e44a9063 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,7 @@ -from setuptools import setup, find_packages -import re import pathlib +import re + +from setuptools import find_packages, setup HERE = pathlib.Path(__file__).parent.absolute() PACKAGE = "qibotn" diff --git a/src/qibotn/MPSUtils.py b/src/qibotn/MPSUtils.py index fd1b4c73..138fb965 100644 --- a/src/qibotn/MPSUtils.py +++ b/src/qibotn/MPSUtils.py @@ -1,6 +1,6 @@ import cupy as cp -from cuquantum.cutensornet.experimental import contract_decompose from cuquantum import contract +from cuquantum.cutensornet.experimental import contract_decompose def initial(num_qubits, dtype): diff --git a/src/qibotn/QiboCircuitConvertor.py b/src/qibotn/QiboCircuitConvertor.py index d72a09cb..8ebfe5fe 100644 --- a/src/qibotn/QiboCircuitConvertor.py +++ b/src/qibotn/QiboCircuitConvertor.py @@ -94,8 +94,7 @@ def init_intermediate_circuit(self, circuit): required_shape = self.op_shape_from_qubits(len(gate_qubits)) self.gate_tensors.append( ( - cp.asarray(gate.matrix(), dtype=self.dtype).reshape( - required_shape), + cp.asarray(gate.matrix(), dtype=self.dtype).reshape(required_shape), gate_qubits, ) ) diff --git a/src/qibotn/QiboCircuitToMPS.py b/src/qibotn/QiboCircuitToMPS.py index d51093f5..58fdd70e 100644 --- a/src/qibotn/QiboCircuitToMPS.py +++ b/src/qibotn/QiboCircuitToMPS.py @@ -1,9 +1,9 @@ import cupy as cp import numpy as np - from cuquantum import cutensornet as cutn + +from qibotn.MPSUtils import apply_gate, initial from qibotn.QiboCircuitConvertor import QiboCircuitToEinsum -from qibotn.MPSUtils import initial, apply_gate class QiboCircuitToMPS: diff --git a/src/qibotn/cutn.py b/src/qibotn/cutn.py index eb0e0d49..6b8582fe 100644 --- a/src/qibotn/cutn.py +++ b/src/qibotn/cutn.py @@ -1,12 +1,13 @@ -from qibotn.QiboCircuitConvertor import QiboCircuitToEinsum -from cuquantum import contract -from cuquantum import cutensornet as cutn import multiprocessing -from cupy.cuda.runtime import getDeviceCount + import cupy as cp +from cupy.cuda.runtime import getDeviceCount +from cuquantum import contract +from cuquantum import cutensornet as cutn -from qibotn.QiboCircuitToMPS import QiboCircuitToMPS from qibotn.mps_contraction_helper import MPSContractionHelper +from qibotn.QiboCircuitConvertor import QiboCircuitToEinsum +from qibotn.QiboCircuitToMPS import QiboCircuitToMPS def eval(qibo_circ, datatype): @@ -49,7 +50,7 @@ def eval_tn_MPI(qibo_circ, datatype, n_samples=8): return result, rank - + def eval_mps(qibo_circ, gate_algo, datatype): myconvertor = QiboCircuitToMPS(qibo_circ, gate_algo, dtype=datatype) mps_helper = MPSContractionHelper(myconvertor.num_qubits) diff --git a/src/qibotn/mps_contraction_helper.py b/src/qibotn/mps_contraction_helper.py index ee8e4e42..0366a34a 100644 --- a/src/qibotn/mps_contraction_helper.py +++ b/src/qibotn/mps_contraction_helper.py @@ -1,4 +1,4 @@ -from cuquantum import contract, contract_path, CircuitToEinsum, tensor +from cuquantum import CircuitToEinsum, contract, contract_path, tensor class MPSContractionHelper: diff --git a/src/qibotn/quimb.py b/src/qibotn/quimb.py index a9ac5107..0349f58a 100644 --- a/src/qibotn/quimb.py +++ b/src/qibotn/quimb.py @@ -3,8 +3,14 @@ from qibo.models import Circuit as QiboCircuit -def from_qibo(circuit: QiboCircuit, is_mps: False, psi0=None, method='svd', - cutoff=1e-6, cutoff_mode='abs'): +def from_qibo( + circuit: QiboCircuit, + is_mps: False, + psi0=None, + method="svd", + cutoff=1e-6, + cutoff_mode="abs", +): nqubits = circuit.nqubits gate_opt = {} if is_mps: diff --git a/tests/test_cuquantum_cutensor_backend.py b/tests/test_cuquantum_cutensor_backend.py index 3a87206a..b3a1c053 100644 --- a/tests/test_cuquantum_cutensor_backend.py +++ b/tests/test_cuquantum_cutensor_backend.py @@ -1,8 +1,8 @@ from timeit import default_timer as timer import config -import numpy as np import cupy as cp +import numpy as np import pytest import qibo from qibo.models import QFT @@ -35,18 +35,14 @@ def test_eval(nqubits: int, dtype="complex128"): import qibotn.cutn # Test qibo - qibo.set_backend(backend=config.qibo.backend, - platform=config.qibo.platform) - qibo_time, (qibo_circ, result_sv) = time( - lambda: qibo_qft(nqubits, swaps=True)) + qibo.set_backend(backend=config.qibo.backend, platform=config.qibo.platform) + qibo_time, (qibo_circ, result_sv) = time(lambda: qibo_qft(nqubits, swaps=True)) # Test Cuquantum - cutn_time, result_tn = time( - lambda: qibotn.cutn.eval(qibo_circ, dtype).flatten()) + cutn_time, result_tn = time(lambda: qibotn.cutn.eval(qibo_circ, dtype).flatten()) assert 1e-2 * qibo_time < cutn_time < 1e2 * qibo_time - assert np.allclose( - result_sv, result_tn), "Resulting dense vectors do not match" + assert np.allclose(result_sv, result_tn), "Resulting dense vectors do not match" @pytest.mark.gpu @@ -62,25 +58,25 @@ def test_mps(nqubits: int, dtype="complex128"): import qibotn.cutn # Test qibo - qibo.set_backend(backend=config.qibo.backend, - platform=config.qibo.platform) + qibo.set_backend(backend=config.qibo.backend, platform=config.qibo.platform) - qibo_time, (circ_qibo, result_sv) = time( - lambda: qibo_qft(nqubits, swaps=True)) + qibo_time, (circ_qibo, result_sv) = time(lambda: qibo_qft(nqubits, swaps=True)) result_sv_cp = cp.asarray(result_sv) # Test of MPS - gate_algo = {'qr_method': False, - 'svd_method': { - 'partition': 'UV', - 'abs_cutoff': 1e-12, - }} + gate_algo = { + "qr_method": False, + "svd_method": { + "partition": "UV", + "abs_cutoff": 1e-12, + }, + } cutn_time, result_tn = time( - lambda: qibotn.cutn.eval_mps(circ_qibo, gate_algo, dtype).flatten()) + lambda: qibotn.cutn.eval_mps(circ_qibo, gate_algo, dtype).flatten() + ) - print( - f"State vector difference: {abs(result_tn - result_sv_cp).max():0.3e}") + print(f"State vector difference: {abs(result_tn - result_sv_cp).max():0.3e}") assert cp.allclose(result_tn, result_sv_cp) diff --git a/tests/test_qasm_quimb_backend.py b/tests/test_qasm_quimb_backend.py index 54171159..6abfb7f1 100644 --- a/tests/test_qasm_quimb_backend.py +++ b/tests/test_qasm_quimb_backend.py @@ -1,5 +1,6 @@ import copy import os + import config import numpy as np import pytest @@ -8,8 +9,7 @@ def create_init_state(nqubits): - init_state = np.random.random(2**nqubits) + \ - 1j * np.random.random(2**nqubits) + init_state = np.random.random(2**nqubits) + 1j * np.random.random(2**nqubits) init_state = init_state / np.sqrt((np.abs(init_state) ** 2).sum()) return init_state @@ -20,8 +20,10 @@ def qibo_qft(nqubits, init_state, swaps): return circ_qibo, state_vec -@pytest.mark.parametrize("nqubits, tolerance, is_mps", - [(1, 1e-6, True), (2, 1e-6, False), (5, 1e-3, True), (10, 1e-3, False)]) +@pytest.mark.parametrize( + "nqubits, tolerance, is_mps", + [(1, 1e-6, True), (2, 1e-6, False), (5, 1e-3, True), (10, 1e-3, False)], +) def test_eval(nqubits: int, tolerance: float, is_mps: bool): # hack quimb to use the correct number of processes # TODO: remove completely, or at least delegate to the backend @@ -33,22 +35,20 @@ def test_eval(nqubits: int, tolerance: float, is_mps: bool): init_state_tn = copy.deepcopy(init_state) # Test qibo - qibo.set_backend(backend=config.qibo.backend, - platform=config.qibo.platform) - #qibo_time, (qibo_circ, result_sv) = time( - #lambda: qibo_qft(nqubits, init_state, swaps=True) - #) - qibo_circ, result_sv= qibo_qft(nqubits, init_state, swaps=True) - + qibo.set_backend(backend=config.qibo.backend, platform=config.qibo.platform) + # qibo_time, (qibo_circ, result_sv) = time( + # lambda: qibo_qft(nqubits, init_state, swaps=True) + # ) + qibo_circ, result_sv = qibo_qft(nqubits, init_state, swaps=True) # Convert to qasm for other backends qasm_circ = qibo_circ.to_qasm() # Test quimb result_tn = qibotn.quimb.eval( - qasm_circ, init_state_tn, is_mps, backend=config.quimb.backend - ) - + qasm_circ, init_state_tn, is_mps, backend=config.quimb.backend + ) - assert np.allclose(result_sv, result_tn, - atol=tolerance), "Resulting dense vectors do not match" + assert np.allclose( + result_sv, result_tn, atol=tolerance + ), "Resulting dense vectors do not match"