From 3d359ebc1f7013c5b225086cf0bfdc9d2d5965f8 Mon Sep 17 00:00:00 2001 From: Nitin Shivaraman Date: Thu, 5 Oct 2023 15:13:49 +0800 Subject: [PATCH 1/5] MPS execution for quimb --- setup.py | 4 +--- src/qibotn/quimb.py | 17 +++++++++++++---- tests/test_qasm_quimb_backend.py | 8 +++++--- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/setup.py b/setup.py index 2b1184ce..3a690c05 100644 --- a/setup.py +++ b/setup.py @@ -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": [], diff --git a/src/qibotn/quimb.py b/src/qibotn/quimb.py index 8414540f..5f5721bb 100644 --- a/src/qibotn/quimb.py +++ b/src/qibotn/quimb.py @@ -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): + 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), + **gate_opt ) return tncirc @@ -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) interim = circ_quimb.psi.full_simplify(seq="DRC") amplitudes = interim.to_dense(backend=backend).flatten() diff --git a/tests/test_qasm_quimb_backend.py b/tests/test_qasm_quimb_backend.py index e9620187..3fd756ed 100644 --- a/tests/test_qasm_quimb_backend.py +++ b/tests/test_qasm_quimb_backend.py @@ -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 @@ -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" From 87cb5673edd49ae7436c9d1d8cd76baa7c785c6c Mon Sep 17 00:00:00 2001 From: Nitin Shivaraman Date: Thu, 5 Oct 2023 15:24:41 +0800 Subject: [PATCH 2/5] Fixed the missing cuquantum libraries --- setup.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/setup.py b/setup.py index 3a690c05..86d29eb3 100644 --- a/setup.py +++ b/setup.py @@ -42,6 +42,8 @@ def version(): "qibo>=0.1.10", "qibojit>=0.0.7", "quimb[tensor]>=1.6.0", + "cupy>=11.6.0", + "cuquantum-python-cu11", ], extras_require={ "docs": [], From 1c043d95f5c1ced043bf7fc02e9fc4167e608d5d Mon Sep 17 00:00:00 2001 From: Liwei Yang Date: Thu, 2 Nov 2023 17:17:56 +0800 Subject: [PATCH 3/5] Minor formatting update --- src/qibotn/quimb.py | 4 ++-- tests/test_qasm_quimb_backend.py | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/qibotn/quimb.py b/src/qibotn/quimb.py index 5f5721bb..50c25ce4 100644 --- a/src/qibotn/quimb.py +++ b/src/qibotn/quimb.py @@ -3,7 +3,7 @@ from qibo.models import Circuit as QiboCircuit -def from_qibo(circuit: QiboCircuit, is_mps: False, psi0=None, method='svd', +def from_qibo(circuit: QiboCircuit, is_mps: False, psi0=None, method='svd', cutoff=1e-6, cutoff_mode='abs'): nqubits = circuit.nqubits gate_opt = {} @@ -12,7 +12,7 @@ def from_qibo(circuit: QiboCircuit, is_mps: False, psi0=None, method='svd', gate_opt["method"] = method gate_opt["cutoff"] = cutoff gate_opt["cutoff_mode"] = cutoff_mode - else: + else: tncirc = qtn.Circuit(nqubits, psi0=psi0) for gate in circuit.queue: diff --git a/tests/test_qasm_quimb_backend.py b/tests/test_qasm_quimb_backend.py index 3fd756ed..f46fbc63 100644 --- a/tests/test_qasm_quimb_backend.py +++ b/tests/test_qasm_quimb_backend.py @@ -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 @@ -42,7 +43,8 @@ def test_eval(nqubits: int, tolerance: float): 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) ) From 2c15613e5eb09556f166596852be908b95c52732 Mon Sep 17 00:00:00 2001 From: Liwei Yang Date: Thu, 2 Nov 2023 17:21:18 +0800 Subject: [PATCH 4/5] Move unnecessary packages away from the required set --- setup.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 86d29eb3..13285eb7 100644 --- a/setup.py +++ b/setup.py @@ -42,8 +42,6 @@ def version(): "qibo>=0.1.10", "qibojit>=0.0.7", "quimb[tensor]>=1.6.0", - "cupy>=11.6.0", - "cuquantum-python-cu11", ], extras_require={ "docs": [], @@ -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"), From 60553bf2e9f65d40ae2ea8500a59c1949ef35946 Mon Sep 17 00:00:00 2001 From: Nitin Shivaraman Date: Fri, 17 Nov 2023 15:21:08 +0800 Subject: [PATCH 5/5] Fixed the comments of the PR --- src/qibotn/quimb.py | 6 +++--- tests/test_qasm_quimb_backend.py | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/qibotn/quimb.py b/src/qibotn/quimb.py index 50c25ce4..a9ac5107 100644 --- a/src/qibotn/quimb.py +++ b/src/qibotn/quimb.py @@ -7,7 +7,7 @@ def from_qibo(circuit: QiboCircuit, is_mps: False, psi0=None, method='svd', cutoff=1e-6, cutoff_mode='abs'): nqubits = circuit.nqubits gate_opt = {} - if (is_mps): + if is_mps: tncirc = qtn.CircuitMPS(nqubits, psi0=psi0) gate_opt["method"] = method gate_opt["cutoff"] = cutoff @@ -33,7 +33,7 @@ def init_state_tn(nqubits, init_state_sv): return qtn.tensor_1d.MatrixProductState.from_dense(init_state_sv, dims) -def eval(qasm: str, init_state, backend="numpy"): +def eval(qasm: str, init_state, is_mps, backend="numpy"): """Evaluate QASM with Quimb backend (quimb): numpy, cupy, jax. Passed to ``opt_einsum``. @@ -41,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, is_mps=True, psi0=init_state_mps) + circ_quimb = from_qibo(circuit, is_mps, psi0=init_state_mps) interim = circ_quimb.psi.full_simplify(seq="DRC") amplitudes = interim.to_dense(backend=backend).flatten() diff --git a/tests/test_qasm_quimb_backend.py b/tests/test_qasm_quimb_backend.py index f46fbc63..f48d48ab 100644 --- a/tests/test_qasm_quimb_backend.py +++ b/tests/test_qasm_quimb_backend.py @@ -30,9 +30,9 @@ def time(func): return time, res -@pytest.mark.parametrize("nqubits, tolerance", - [(1, 1e-6), (2, 1e-6), (5, 1e-3), (10, 1e-3)]) -def test_eval(nqubits: int, tolerance: float): +@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 # implementation @@ -55,7 +55,7 @@ def test_eval(nqubits: int, tolerance: float): # Test quimb quimb_time, result_tn = time( lambda: qibotn.quimb.eval( - qasm_circ, init_state_tn, backend=config.quimb.backend + qasm_circ, init_state_tn, is_mps, backend=config.quimb.backend ) )