Skip to content

Commit

Permalink
Drop circuit-specific dedicated handling for swaps
Browse files Browse the repository at this point in the history
  • Loading branch information
alecandido committed Feb 15, 2023
1 parent 8708b5e commit bac9ad1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
17 changes: 9 additions & 8 deletions src/qibotn/quimb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
from qibo.models import Circuit as QiboCircuit


def from_qibo(circuit: QiboCircuit, swaps: bool = True, psi0=None):
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)

if swaps:
for i in range(nqubits // 2): # TODO: Ignore the barrier indices?
tncirc.apply_gate("SWAP", i, nqubits - i - 1)
tncirc.apply_gate(
gate.name,
*gate.parameters,
*gate.qubits,
parametrize=len(gate.parameters) > 0
)

return tncirc

Expand All @@ -23,15 +24,15 @@ 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", swaps=True):
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, swaps=swaps, psi0=init_state_mps)
circ_quimb = from_qibo(circuit, psi0=init_state_mps)
interim = circ_quimb.psi.full_simplify(seq="DRC")
amplitudes = interim.to_dense(backend=backend).flatten()

Expand Down
1 change: 0 additions & 1 deletion tests/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
class Executor:
backend: str
platform: Optional[str] = None
swaps: bool = True


qibo = Executor(backend="qibojit", platform="numpy")
Expand Down
16 changes: 6 additions & 10 deletions tests/test_qasm_quimb_backend.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
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 create_init_state(nqubits):
Expand Down Expand Up @@ -44,7 +43,7 @@ def test_eval(nqubits: int):
# Test qibo
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=config.qibo.swaps)
lambda: qibo_qft(nqubits, init_state, swaps=True)
)

# Convert to qasm for other backends
Expand All @@ -53,10 +52,7 @@ def test_eval(nqubits: int):
# Test quimb
quimb_time, result_tn = time(
lambda: qibotn.quimb.eval(
qasm_circ,
init_state_tn,
backend=config.quimb.backend,
swaps=config.quimb.swaps,
qasm_circ, init_state_tn, backend=config.quimb.backend
)
)

Expand Down

0 comments on commit bac9ad1

Please sign in to comment.