Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 13, 2024
1 parent c119272 commit 3a30d10
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 53 deletions.
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/qibotn/MPSUtils.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
3 changes: 1 addition & 2 deletions src/qibotn/QiboCircuitConvertor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
)
Expand Down
4 changes: 2 additions & 2 deletions src/qibotn/QiboCircuitToMPS.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
13 changes: 7 additions & 6 deletions src/qibotn/cutn.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/qibotn/mps_contraction_helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from cuquantum import contract, contract_path, CircuitToEinsum, tensor
from cuquantum import CircuitToEinsum, contract, contract_path, tensor


class MPSContractionHelper:
Expand Down
10 changes: 8 additions & 2 deletions src/qibotn/quimb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
38 changes: 17 additions & 21 deletions tests/test_cuquantum_cutensor_backend.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
32 changes: 16 additions & 16 deletions tests/test_qasm_quimb_backend.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import os

import config
import numpy as np
import pytest
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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"

0 comments on commit 3a30d10

Please sign in to comment.