Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mps for quimb #20

Merged
merged 6 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ def version():
install_requires=[
"qibo>=0.1.10",
"qibojit>=0.0.7",
"quimb[tensor]>=1.4.0",
"cupy>=11.6.0",
"cuquantum-python-cu11",
"quimb[tensor]>=1.6.0",
],
extras_require={
"docs": [],
Expand All @@ -55,6 +53,10 @@ def version():
"analysis": [
"pylint>=2.16.0",
],
"cuda": [
"cupy>=11.6.0",
"cuquantum-python-cu11>=23.3.0",
],
},
python_requires=">=3.8.0",
long_description=(HERE / "README.md").read_text(encoding="utf-8"),
Expand Down
17 changes: 13 additions & 4 deletions src/qibotn/quimb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@
from qibo.models import Circuit as QiboCircuit


def from_qibo(circuit: QiboCircuit, psi0=None):
def from_qibo(circuit: QiboCircuit, is_mps: False, psi0=None, method='svd',
cutoff=1e-6, cutoff_mode='abs'):
nqubits = circuit.nqubits
tncirc = qtn.Circuit(nqubits, psi0=psi0)
gate_opt = {}
if (is_mps):
nitinshivaraman marked this conversation as resolved.
Show resolved Hide resolved
tncirc = qtn.CircuitMPS(nqubits, psi0=psi0)
gate_opt["method"] = method
gate_opt["cutoff"] = cutoff
gate_opt["cutoff_mode"] = cutoff_mode
else:
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
parametrize=False if is_mps else (len(gate.parameters) > 0),
nitinshivaraman marked this conversation as resolved.
Show resolved Hide resolved
**gate_opt
)

return tncirc
Expand All @@ -32,7 +41,7 @@ def eval(qasm: str, init_state, backend="numpy"):
"""
circuit = QiboCircuit.from_qasm(qasm)
init_state_mps = init_state_tn(circuit.nqubits, init_state)
circ_quimb = from_qibo(circuit, psi0=init_state_mps)
circ_quimb = from_qibo(circuit, is_mps=True, psi0=init_state_mps)
nitinshivaraman marked this conversation as resolved.
Show resolved Hide resolved
interim = circ_quimb.psi.full_simplify(seq="DRC")
amplitudes = interim.to_dense(backend=backend).flatten()

Expand Down
14 changes: 9 additions & 5 deletions tests/test_qasm_quimb_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@


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 @@ -29,8 +30,9 @@ def time(func):
return time, res


@pytest.mark.parametrize("nqubits", [1, 2, 5, 10])
def test_eval(nqubits: int):
@pytest.mark.parametrize("nqubits, tolerance",
[(1, 1e-6), (2, 1e-6), (5, 1e-3), (10, 1e-3)])
def test_eval(nqubits: int, tolerance: float):
# hack quimb to use the correct number of processes
# TODO: remove completely, or at least delegate to the backend
# implementation
Expand All @@ -41,7 +43,8 @@ def test_eval(nqubits: int):
init_state_tn = copy.deepcopy(init_state)

# 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, (qibo_circ, result_sv) = time(
lambda: qibo_qft(nqubits, init_state, swaps=True)
)
Expand All @@ -57,4 +60,5 @@ def test_eval(nqubits: int):
)

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