diff --git a/.github/workflows/rules.yml b/.github/workflows/rules.yml index e087400..4b2cb05 100644 --- a/.github/workflows/rules.yml +++ b/.github/workflows/rules.yml @@ -18,4 +18,5 @@ jobs: os: ${{ matrix.os }} python-version: ${{ matrix.python-version }} environment: "qibotn" + pip-extras: "analysis,tests" secrets: inherit diff --git a/.gitignore b/.gitignore index 6a256b3..68bc17f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,129 +1,160 @@ # Byte-compiled / optimized / DLL files - __pycache__/ - *.py[cod] - *$py.class +__pycache__/ +*.py[cod] +*$py.class - # C extensions - *.so +# C extensions +*.so - # Distribution / packaging - .Python - build/ - develop-eggs/ - dist/ - downloads/ - eggs/ - .eggs/ - lib/ - lib64/ - parts/ - sdist/ - var/ - wheels/ - pip-wheel-metadata/ - share/python-wheels/ - *.egg-info/ - .installed.cfg - *.egg - MANIFEST +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST - # PyInstaller - # Usually these files are written by a python script from a template - # before PyInstaller builds the exe, so as to inject date/other infos into it. - *.manifest - *.spec +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec - # Installer logs - pip-log.txt - pip-delete-this-directory.txt +# Installer logs +pip-log.txt +pip-delete-this-directory.txt - # Unit test / coverage reports - htmlcov/ - .tox/ - .nox/ - .coverage - .coverage.* - .cache - nosetests.xml - coverage.xml - *.cover - *.py,cover - .hypothesis/ - .pytest_cache/ +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ - # Translations - *.mo - *.pot +# Translations +*.mo +*.pot - # Django stuff: - *.log - local_settings.py - db.sqlite3 - db.sqlite3-journal +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal - # Flask stuff: - instance/ - .webassets-cache +# Flask stuff: +instance/ +.webassets-cache - # Scrapy stuff: - .scrapy +# Scrapy stuff: +.scrapy - # Sphinx documentation - docs/_build/ +# Sphinx documentation +docs/_build/ - # PyBuilder - target/ +# PyBuilder +.pybuilder/ +target/ - # Jupyter Notebook - .ipynb_checkpoints +# Jupyter Notebook +.ipynb_checkpoints - # IPython - profile_default/ - ipython_config.py +# IPython +profile_default/ +ipython_config.py - # pyenv - .python-version +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version - # pipenv - # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. - # However, in case of collaboration, if having platform-specific dependencies or dependencies - # having no cross-platform support, pipenv may install dependencies that don't work, or not - # install all needed dependencies. - #Pipfile.lock +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock - # PEP 582; used by e.g. github.com/David-OConnor/pyflow - __pypackages__/ +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock - # Celery stuff - celerybeat-schedule - celerybeat.pid +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml - # SageMath parsed files - *.sage.py +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ - # Environments - .env - .venv - env/ - venv/ - ENV/ - env.bak/ - venv.bak/ +# Celery stuff +celerybeat-schedule +celerybeat.pid - # Spyder project settings - .spyderproject - .spyproject +# SageMath parsed files +*.sage.py - # Rope project settings - .ropeproject +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ - # mkdocs documentation - /site +# Spyder project settings +.spyderproject +.spyproject - # mypy - .mypy_cache/ - .dmypy.json - dmypy.json +# Rope project settings +.ropeproject - # Pyre type checker - .pyre/ +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/pyproject.toml b/pyproject.toml index 5693b81..f2546e4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,3 +4,4 @@ output-format = "colorized" [tool.pytest.ini_options] testpaths = ["tests/"] addopts = ["--cov=qibotn", "--cov-report=xml"] +env = ["D:NUMBA_DISABLE_JIT=1"] diff --git a/setup.py b/setup.py index 1fa40bf..08b5515 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,3 @@ -# Installation script for python from setuptools import setup, find_packages import pathlib @@ -24,12 +23,20 @@ setup( "Topic :: Scientific/Engineering :: Physics", ], install_requires=[ - "quimb", - "qibo", + "qibo>=0.1.10", + "qibojit>=0.0.7", + "quimb[tensor]>=1.4.0", ], extras_require={ "docs": [], - "tests": ["pytest", "pytest-cov"], + "tests": [ + "pytest>=7.2.0", + "pytest-cov>=4.0.0", + "pytest-env>=0.8.1", + ], + "analysis": [ + "pylint>=2.16.0", + ], }, python_requires=">=3.7.0", long_description=LONG_DESCRIPTION, diff --git a/src/qibotn/__init__.py b/src/qibotn/__init__.py index a4e2017..f102a9c 100644 --- a/src/qibotn/__init__.py +++ b/src/qibotn/__init__.py @@ -1 +1 @@ -__version__ = "0.1" +__version__ = "0.0.1" diff --git a/src/qibotn/__main__.py b/src/qibotn/__main__.py new file mode 100644 index 0000000..2dbf82e --- /dev/null +++ b/src/qibotn/__main__.py @@ -0,0 +1,19 @@ +import argparse +from qibotn import qasm_quimb + + +def parser(): + parser = argparse.ArgumentParser() + parser.add_argument( + "--nqubits", default=10, type=int, help="Number of quibits in the circuits." + ) + return parser.parse_args() + + +def main(args: argparse.Namespace): + print("Testing for %d nqubits" % (args.nqubits)) + qasm_quimb.eval_QI_qft(args.nqubits) + + +if __name__ == "__main__": + main(parser()) diff --git a/src/qibotn/qasm_quimb.py b/src/qibotn/qasm_quimb.py new file mode 100644 index 0000000..1d7a5ce --- /dev/null +++ b/src/qibotn/qasm_quimb.py @@ -0,0 +1,214 @@ +import re +import copy +from timeit import default_timer as timer + +import numpy as np +import quimb as qu +import quimb.tensor as qtn + +import qibo +from qibo.models import QFT as qibo_qft + + +def get_gate_params(operation): + if "h " in operation: + qbit_no = [int(re.findall(r"\d+", operation)[0])] + qbit_no.insert(0, "H") + elif "x " in operation: + qbit_no = [int(re.findall(r"\d+", operation)[0])] + qbit_no.insert(0, "X") + elif "y " in operation: + qbit_no = [int(re.findall(r"\d+", operation)[0])] + qbit_no.insert(0, "Y") + elif "z " in operation: + qbit_no = [int(re.findall(r"\d+", operation)[0])] + qbit_no.insert(0, "Z") + elif "s " in operation: + qbit_no = [int(re.findall(r"\d+", operation)[0])] + qbit_no.insert(0, "S") + elif "t " in operation: + qbit_no = [int(re.findall(r"\d+", operation)[0])] + qbit_no.insert(0, "T") + elif "cu1" in operation: + lamda = float( + ".".join(re.findall(r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0])) + ) + qbit_no = re.findall(r"\d+", operation.split(" ")[1]) + qbit_no = [int(x) for x in qbit_no] + qbit_no[0:0] = ["CU1", lamda] + elif "cu2" in operation: + angles = re.findall(r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0]) + phi = float(".".join(angles[0:2])) + lamba = float(".".join(angles[2:])) + qbit_no = re.findall(r"\d+", operation.split(" ")[1]) + qbit_no = [int(x) for x in qbit_no] + qbit_no[0:0] = ["CU2", phi, lamda] + elif "cu3" in operation: + angles = re.findall(r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0]) + theta = float(".".join(angles[0:2])) + phi = float(".".join(angles[2:4])) + lamba = float(".".join(angles[4:])) + qbit_no = re.findall(r"\d+", operation.split(" ")[1]) + qbit_no = [int(x) for x in qbit_no] + qbit_no[0:0] = ["CU3", theta, phi, lamda] + elif " cx " in operation: + qbit_no = re.findall(r"\d+", operation.split(" ")[1]) + qbit_no = [int(x) for x in qbit_no] + qbit_no.insert(0, "CX") + elif " cy " in operation: + qbit_no = re.findall(r"\d+", operation.split(" ")[1]) + qbit_no = [int(x) for x in qbit_no] + qbit_no.insert(0, "CY") + elif " cz " in operation: + qbit_no = re.findall(r"\d+", operation.split(" ")[1]) + qbit_no = [int(x) for x in qbit_no] + qbit_no.insert(0, "CZ") + elif " ccx " in operation: + qbit_no = re.findall(r"\d+", operation.split(" ")[1]) + qbit_no = [int(x) for x in qbit_no] + qbit_no.insert(0, "CCX") + elif " ccy " in operation: + qbit_no = re.findall(r"\d+", operation.split(" ")[1]) + qbit_no = [int(x) for x in qbit_no] + qbit_no.insert(0, "CCY") + elif " ccz " in operation: + qbit_no = re.findall(r"\d+", operation.split(" ")[1]) + qbit_no = [int(x) for x in qbit_no] + qbit_no.insert(0, "CCZ") + elif " rx " in operation: + theta = float( + ".".join(re.findall(r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0])) + ) + qbit_no = [int(re.findall(r"\d+", operation)[0])] + qbit_no[0:0] = ["RX", theta] + elif "^ry " in operation: + theta = float( + ".".join(re.findall(r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0])) + ) + qbit_no = [int(re.findall(r"\d+", operation)[0])] + qbit_no[0:0] = ["RY", theta] + elif "^rz " in operation: + theta = float( + ".".join(re.findall(r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0])) + ) + qbit_no = [int(re.findall(r"\d+", operation)[0])] + qbit_no[0:0] = ["RZ", theta] + elif "^rzz " in operation: + theta = float( + ".".join(re.findall(r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0])) + ) + qbit_no = re.findall(r"\d+", operation.split(" ")[1]) + qbit_no = [int(x) for x in qbit_no] + qbit_no[0:0] = ["RZZ", theta] + elif "^u1 " in operation: + lamda = float( + ".".join(re.findall(r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0])) + ) + qbit_no = [int(re.findall(r"\d+", operation)[0])] + qbit_no[0:0] = ["U1", lamda] + elif "^u2 " in operation: + angles = re.findall(r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0]) + phi = float(".".join(angles[0:2])) + lamba = float(".".join(angles[2:])) + qbit_no = int(re.findall(r"\d+", operation)[0]) + qbit_no[0:0] = ["U2", phi, lamda] # pylint: disable=E1137 + elif "^u3 " in operation: + angles = re.findall(r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0]) + theta = float(".".join(angles[0:2])) + phi = float(".".join(angles[2:4])) + lamba = float(".".join(angles[4:])) + qbit_no = int(re.findall(r"\d+", operation)[0]) + qbit_no[0:0] = ["U3", theta, phi, lamda] # pylint: disable=E1137 + else: + assert "Unsupported gate" + + return qbit_no + + +def get_gate_functions(qasm_str, start_idx): + func_list = [] + result = [] + idx_inc = 0 + for line in qasm_str[start_idx:]: + if "gate " in line: + result = re.findall("[^,\s()]+", line) + elif result and "{" not in line and "}" not in line: + params = get_gate_params(line) + func_list.append(*params) + elif "}" in line: + print("Returning the list") + print(func_list) + return func_list, idx_inc + idx_inc += 1 + + +def qasm_QFT(nqubits: int, qasm_str: str, with_swaps: bool = True, psi0=None): + circ = qtn.Circuit(nqubits, psi0=psi0) + + qasm_str = qasm_str.split("\n") + for idx, line in enumerate(qasm_str): + command = line.split(" ")[0] + if re.search("include|//|OPENQASM", command): + continue + elif "qreg" in command: + nbits = int(re.findall(r"\d+", line)[0]) + assert nbits == nqubits + elif "swap" in command: + break + elif "gate" in command: # TODO: Complete gate handling + gate_func, increment = get_gate_functions(qasm_str, idx) + pass + elif "barrier" in command: # TODO: Complete barrier handling + pass + elif "measure" in command: # TODO: Complete measure handling + pass + else: + params = get_gate_params(line) + circ.apply_gate(*params) + + if with_swaps: + for i in range(nqubits // 2): # TODO: Ignore the barrier indices? + circ.apply_gate("SWAP", i, nqubits - i - 1) + + return circ + + +def eval_QI_qft(nqubits, backend="numpy", qibo_backend="qibojit", with_swaps=True): + # backend (quimb): numpy, cupy, jax. Passed to ``opt_einsum``. + # qibo_backend: qibojit, qibotf, tensorflow, numpy + + # generate random statevector as initial state + 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()) + init_state_quimb = copy.deepcopy(init_state) + + # Qibo circuit + # qibo.set_backend(backend=qibo_backend, platform="numba") + qibo.set_backend(backend=qibo_backend, platform="numpy") + + start = timer() + circ_qibo = qibo_qft(nqubits, with_swaps) + amplitudes_reference = np.array(circ_qibo(init_state)) + end = timer() + print("qibo time is " + str(end - start)) + qasm_circ = circ_qibo.to_qasm() + + ##################################################################### + # Quimb circuit + qu.core.pnjit() + # convert vector to MPS + dims = tuple(2 * np.ones(nqubits, dtype=int)) + init_state_MPS = qtn.tensor_1d.MatrixProductState.from_dense(init_state_quimb, dims) + + # construct quimb qft circuit + start = timer() + circ_quimb = qasm_QFT(nqubits, qasm_circ, with_swaps, psi0=init_state_MPS) + + interim = circ_quimb.psi.full_simplify(seq="DRC") + + result = interim.to_dense(backend=backend) + amplitudes = result.flatten() + end = timer() + quimb_qft_time = end - start + print("quimb time is " + str(quimb_qft_time)) + assert np.allclose(amplitudes, amplitudes_reference, atol=1e-06) diff --git a/src/qibotn/test_qasm_quimb_backend.py b/src/qibotn/test_qasm_quimb_backend.py deleted file mode 100644 index 548b95d..0000000 --- a/src/qibotn/test_qasm_quimb_backend.py +++ /dev/null @@ -1,221 +0,0 @@ -import argparse -import quimb as qu -import quimb.tensor as qtn -import numpy as np -import re, copy - -import qibo -from qibo.models import QFT as qibo_qft - -from timeit import default_timer as timer - -def get_gate_params(operation): - if "h " in operation: - qbit_no = [int(re.findall(r'\d+', operation)[0])] - qbit_no.insert(0, "H") - elif "x " in operation: - qbit_no = [int(re.findall(r'\d+', operation)[0])] - qbit_no.insert(0, "X") - elif "y " in operation: - qbit_no = [int(re.findall(r'\d+', operation)[0])] - qbit_no.insert(0, "Y") - elif "z " in operation: - qbit_no = [int(re.findall(r'\d+', operation)[0])] - qbit_no.insert(0, "Z") - elif "s " in operation: - qbit_no = [int(re.findall(r'\d+', operation)[0])] - qbit_no.insert(0, "S") - elif "t " in operation: - qbit_no = [int(re.findall(r'\d+', operation)[0])] - qbit_no.insert(0, "T") - elif "cu1" in operation: - lamda = float('.'.join(re.findall(r'\b\d+(?:[Ee][+-]?\d+)?', \ - operation.split(" ")[0]))) - qbit_no = re.findall(r'\d+', operation.split(" ")[1]) - qbit_no = [int(x) for x in qbit_no] - qbit_no[0:0] = ["CU1", lamda] - elif "cu2" in operation: - angles = re.findall(r'\b\d+(?:[Ee][+-]?\d+)?',operation.split(" ")[0]) - phi = float('.'.join(angles[0:2])) - lamba = float('.'.join(angles[2:])) - qbit_no = re.findall(r'\d+', operation.split(" ")[1]) - qbit_no = [int(x) for x in qbit_no] - qbit_no[0:0] = ["CU2", phi, lamda] - elif "cu3" in operation: - angles = re.findall(r'\b\d+(?:[Ee][+-]?\d+)?',operation.split(" ")[0]) - theta = float('.'.join(angles[0:2])) - phi = float('.'.join(angles[2:4])) - lamba = float('.'.join(angles[4:])) - qbit_no = re.findall(r'\d+', operation.split(" ")[1]) - qbit_no = [int(x) for x in qbit_no] - qbit_no[0:0] = ["CU3", theta, phi, lamda] - elif " cx " in operation: - qbit_no = re.findall(r'\d+', operation.split(" ")[1]) - qbit_no = [int(x) for x in qbit_no] - qbit_no.insert(0, "CX") - elif " cy " in operation: - qbit_no = re.findall(r'\d+', operation.split(" ")[1]) - qbit_no = [int(x) for x in qbit_no] - qbit_no.insert(0, "CY") - elif " cz " in operation: - qbit_no = re.findall(r'\d+', operation.split(" ")[1]) - qbit_no = [int(x) for x in qbit_no] - qbit_no.insert(0, "CZ") - elif " ccx " in operation: - qbit_no = re.findall(r'\d+', operation.split(" ")[1]) - qbit_no = [int(x) for x in qbit_no] - qbit_no.insert(0, "CCX") - elif " ccy " in operation: - qbit_no = re.findall(r'\d+', operation.split(" ")[1]) - qbit_no = [int(x) for x in qbit_no] - qbit_no.insert(0, "CCY") - elif " ccz " in operation: - qbit_no = re.findall(r'\d+', operation.split(" ")[1]) - qbit_no = [int(x) for x in qbit_no] - qbit_no.insert(0, "CCZ") - elif " rx " in operation: - theta = float('.'.join(re.findall(r'\b\d+(?:[Ee][+-]?\d+)?', \ - operation.split(" ")[0]))) - qbit_no = [int(re.findall(r'\d+', operation)[0])] - qbit_no[0:0] = ["RX", theta] - elif "^ry " in operation: - theta = float('.'.join(re.findall(r'\b\d+(?:[Ee][+-]?\d+)?', \ - operation.split(" ")[0]))) - qbit_no = [int(re.findall(r'\d+', operation)[0])] - qbit_no[0:0] = ["RY", theta] - elif "^rz " in operation: - theta = float('.'.join(re.findall(r'\b\d+(?:[Ee][+-]?\d+)?', \ - operation.split(" ")[0]))) - qbit_no = [int(re.findall(r'\d+', operation)[0])] - qbit_no[0:0] = ["RZ", theta] - elif "^rzz " in operation: - theta = float('.'.join(re.findall(r'\b\d+(?:[Ee][+-]?\d+)?', \ - operation.split(" ")[0]))) - qbit_no = re.findall(r'\d+', operation.split(" ")[1]) - qbit_no = [int(x) for x in qbit_no] - qbit_no[0:0] = ["RZZ", theta] - elif "^u1 " in operation: - lamda = float('.'.join(re.findall(r'\b\d+(?:[Ee][+-]?\d+)?', \ - operation.split(" ")[0]))) - qbit_no = [int(re.findall(r'\d+', operation)[0])] - qbit_no[0:0] = ["U1", lamda] - elif "^u2 " in operation: - angles = re.findall(r'\b\d+(?:[Ee][+-]?\d+)?',operation.split(" ")[0]) - phi = float('.'.join(angles[0:2])) - lamba = float('.'.join(angles[2:])) - qbit_no = int(re.findall(r'\d+', operation)[0]) - qbit_no[0:0] = ["U2", phi, lamda] - elif "^u3 " in operation: - angles = re.findall(r'\b\d+(?:[Ee][+-]?\d+)?',operation.split(" ")[0]) - theta = float('.'.join(angles[0:2])) - phi = float('.'.join(angles[2:4])) - lamba = float('.'.join(angles[4:])) - qbit_no = int(re.findall(r'\d+', operation)[0]) - qbit_no[0:0] = ["U3", theta, phi, lamda] - else: - assert("Unsupported gate") - - return qbit_no - - -def get_gate_functions(qasm_str, start_idx): - func_list = [] - result = [] - idx_inc = 0 - for line in qasm_str[start_idx:]: - if "gate " in line: - result = re.findall("[^,\s()]+", line) - elif result and "{" not in line and "}" not in line: - params = get_gate_params(line) - func_list.append(*params) - elif "}" in line: - print("Returning the list") - print(func_list) - return func_list, idx_incsss - idx_inc += 1 - -def qasm_QFT(nqubits:int, qasm_str:str, with_swaps: bool = True, psi0 = None): - circ = qtn.Circuit(nqubits, psi0 = psi0) - - qasm_str = qasm_str.split('\n') - for idx, line in enumerate(qasm_str): - command = line.split(" ")[0] - if re.search("include|//|OPENQASM", command): - continue - elif "qreg" in command: - nbits = int(re.findall(r'\d+', line)[0]) - assert(nbits == nqubits) - elif "swap" in command: - break - elif "gate" in command: # TODO: Complete gate handling - gate_func, increment = get_gate_functions(qasm_str, idx) - pass - elif "barrier" in command: # TODO: Complete barrier handling - pass - elif "measure" in command: # TODO: Complete measure handling - pass - else: - params = get_gate_params(line) - circ.apply_gate(*params) - - if with_swaps: - for i in range(nqubits // 2): # TODO: Ignore the barrier indices? - circ.apply_gate('SWAP', i, nqubits - i - 1) - - return circ - - -def eval_QI_qft(nqubits, backend='numpy', qibo_backend='qibojit', \ - with_swaps=True): - # backend (quimb): numpy, cupy, jax. Passed to ``opt_einsum``. - # qibo_backend: qibojit, qibotf, tensorflow, numpy - - # generate random statevector as initial state - 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()) - init_state_quimb = copy.deepcopy(init_state) - - # Qibo circuit - # qibo.set_backend(backend=qibo_backend, platform="numba") - qibo.set_backend(backend=qibo_backend, platform="numpy") - - start = timer() - circ_qibo = qibo_qft(nqubits, with_swaps) - amplitudes_reference = np.array(circ_qibo(init_state)) - end = timer() - print("qibo time is " + str(end-start)) - qasm_circ = circ_qibo.to_qasm() - - - ##################################################################### - # Quimb circuit - qu.core.pnjit() - ## convert vector to MPS - dims = tuple(2*np.ones(nqubits, dtype=int)) - init_state_MPS = qtn.tensor_1d.MatrixProductState.from_dense \ - (init_state_quimb, dims) - - - # construct quimb qft circuit - start = timer() - circ_quimb = qasm_QFT(nqubits, qasm_circ, with_swaps, psi0=init_state_MPS) - - interim = circ_quimb.psi.full_simplify(seq="DRC") - - result = interim.to_dense(backend=backend) - amplitudes = result.flatten() - end = timer() - quimb_qft_time = end-start - print("quimb time is " + str(quimb_qft_time)) - assert(np.allclose(amplitudes,amplitudes_reference,atol=1e-06)) - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.add_argument("--nqubits", default=10, type=int, - help="Number of quibits in the circuits.") - - args = parser.parse_args() - - print("Testing for %d nqubits" % (args.nqubits)) - result = eval_QI_qft(args.nqubits) diff --git a/tests/test_qasm_quimb_backend.py b/tests/test_qasm_quimb_backend.py new file mode 100644 index 0000000..ba99dcb --- /dev/null +++ b/tests/test_qasm_quimb_backend.py @@ -0,0 +1,12 @@ +import os + +import pytest + + +@pytest.mark.parametrize("nqubits", [1, 2, 5, 10]) +def test_eval(nqubits: int): + os.environ["QUIMB_NUM_PROCS"] = str(os.cpu_count()) + from qibotn import qasm_quimb + + print(f"Testing for {nqubits} nqubits") + result = qasm_quimb.eval_QI_qft(nqubits)