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 2 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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def version():
install_requires=[
"qibo>=0.1.10",
"qibojit>=0.0.7",
"quimb[tensor]>=1.4.0",
"quimb[tensor]>=1.6.0",
"cupy>=11.6.0",
"cuquantum-python-cu11",
],
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
8 changes: 5 additions & 3 deletions tests/test_qasm_quimb_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,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 @@ -57,4 +58,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