-
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.
renamed function 'eval' to 'dense_vector_tn_qu'
- Loading branch information
vinitha-balachandran
committed
Feb 6, 2024
1 parent
4fe1684
commit a757419
Showing
1 changed file
with
49 additions
and
0 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,49 @@ | ||
import numpy as np | ||
import quimb.tensor as qtn | ||
from qibo.models import Circuit as QiboCircuit | ||
|
||
|
||
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: | ||
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=False if is_mps else (len(gate.parameters) > 0), | ||
**gate_opt | ||
) | ||
|
||
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 dense_vector_tn_qu(qasm: str, init_state, is_mps, backend="numpy"): | ||
"""Evaluate QASM with Quimb | ||
backend (quimb): numpy, cupy, jax. Passed to ``opt_einsum``. | ||
""" | ||
circuit = QiboCircuit.from_qasm(qasm) | ||
if init_state is not None: | ||
init_state = init_state_tn(circuit.nqubits, init_state) | ||
circ_quimb = from_qibo(circuit, is_mps, psi0=init_state) | ||
interim = circ_quimb.psi.full_simplify(seq="DRC") | ||
amplitudes = interim.to_dense(backend=backend).flatten() | ||
|
||
return amplitudes |