Skip to content

Commit

Permalink
Merge pull request #13 from qiboteam/quimb-qasm-parser
Browse files Browse the repository at this point in the history
Refactor quimb backend
  • Loading branch information
liweintu authored Feb 16, 2023
2 parents ed624c1 + 662adfe commit 176db90
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 251 deletions.
25 changes: 25 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-toml
- id: check-merge-conflict
- id: debug-statements
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
args: ["--profile", "black"]
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
211 changes: 0 additions & 211 deletions src/qibotn/qasm_quimb.py

This file was deleted.

39 changes: 39 additions & 0 deletions src/qibotn/quimb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import numpy as np
import quimb.tensor as qtn
from qibo.models import Circuit as QiboCircuit


def from_qibo(circuit: QiboCircuit, psi0=None):
nqubits = circuit.nqubits
tncirc = qtn.Circuit(nqubits, psi0=psi0)

for gate in circuit.queue:
tncirc.apply_gate(
gate.name,
*gate.parameters,
*gate.qubits,
parametrize=len(gate.parameters) > 0
)

return tncirc


def init_state_tn(nqubits, init_state_sv):
dims = tuple(2 * np.ones(nqubits, dtype=int))

return qtn.tensor_1d.MatrixProductState.from_dense(init_state_sv, dims)


def eval(qasm: str, init_state, backend="numpy"):
"""Evaluate QASM with Quimb
backend (quimb): numpy, cupy, jax. Passed to ``opt_einsum``.
"""
circuit = QiboCircuit.from_qasm(qasm)
init_state_mps = init_state_tn(circuit.nqubits, init_state)
circ_quimb = from_qibo(circuit, psi0=init_state_mps)
interim = circ_quimb.psi.full_simplify(seq="DRC")
amplitudes = interim.to_dense(backend=backend).flatten()

return amplitudes
22 changes: 12 additions & 10 deletions tests/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
qibo = dict(
backend = 'qibojit',
platform = 'numpy',
swaps = True
)

quimb = dict(
backend = 'numpy',
swaps = True
)
from dataclasses import dataclass
from typing import Optional


@dataclass
class Executor:
backend: str
platform: Optional[str] = None


qibo = Executor(backend="qibojit", platform="numpy")
quimb = Executor(backend="numpy")
63 changes: 33 additions & 30 deletions tests/test_qasm_quimb_backend.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import copy
import os
from timeit import default_timer as timer

import config
import numpy as np
import pytest
import qibo
from qibo.models import QFT
import numpy as np
import copy
from timeit import default_timer as timer

import config


def init_state_sv(nqubits):
init_state = np.random.random(2**nqubits) + \
1j * np.random.random(2**nqubits)
def create_init_state(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())
# An unmodified init_state has to be converted to tn format
init_state_for_tn = copy.deepcopy(init_state)
return init_state, init_state_for_tn
return init_state


def qibo_qft(nqubits, init_state, swaps):
Expand All @@ -25,33 +21,40 @@ def qibo_qft(nqubits, init_state, swaps):
return circ_qibo, state_vec


def time(func):
start = timer()
res = func()
end = timer()
time = end - start
return time, res


@pytest.mark.parametrize("nqubits", [1, 2, 5, 10])
def test_eval(nqubits: int):
# hack quimb to use the correct number of processes
# TODO: remove completely, or at least delegate to the backend
# implementation
os.environ["QUIMB_NUM_PROCS"] = str(os.cpu_count())
from qibotn import qasm_quimb
import qibotn.quimb

init_state_qibo, init_state_for_tn = init_state_sv(nqubits=nqubits)
init_state = create_init_state(nqubits=nqubits)
init_state_tn = copy.deepcopy(init_state)

# Test qibo
qibo.set_backend(backend=config.qibo['backend'],
platform=config.qibo['platform'])
start_time = timer()
qibo_circ, result_sv = qibo_qft(nqubits, init_state=init_state_qibo,
swaps=config.qibo['swaps'])
end_time = timer()
qibo_time = end_time - start_time
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)
)

# Convert to qasm for other backends
qasm_circ = qibo_circ.to_qasm()

# Test quimb
start_time = timer()
result_tn = qasm_quimb.eval_QI_qft(nqubits=nqubits, qasm_circ=qasm_circ,
init_state=init_state_for_tn,
backend=config.quimb['backend'],
swaps=config.quimb['swaps'])
end_time = timer()
quimb_time = end_time - start_time

assert np.allclose(result_sv, result_tn), \
"Resulting dense vectors do not match"
quimb_time, result_tn = time(
lambda: qibotn.quimb.eval(
qasm_circ, init_state_tn, backend=config.quimb.backend
)
)

assert 1e-2 * qibo_time < quimb_time < 1e2 * qibo_time
assert np.allclose(result_sv, result_tn), "Resulting dense vectors do not match"

0 comments on commit 176db90

Please sign in to comment.