[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import copy
|
||||
import os
|
||||
|
||||
import config
|
||||
import numpy as np
|
||||
import pytest
|
||||
@@ -8,8 +9,7 @@ from qibo.models import QFT
|
||||
|
||||
|
||||
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"
|
||||
|
||||
Reference in New Issue
Block a user