-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from qiboteam/quimb-qasm-parser
Refactor quimb backend
- Loading branch information
Showing
5 changed files
with
109 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters