Adding feature to pass MPS parameters in quimb
This commit is contained in:
@@ -16,9 +16,11 @@ class QuimbBackend(NumpyBackend):
|
|||||||
|
|
||||||
mps_enabled_value = runcard.get("MPS_enabled")
|
mps_enabled_value = runcard.get("MPS_enabled")
|
||||||
if mps_enabled_value is True:
|
if mps_enabled_value is True:
|
||||||
self.MPS_enabled = True
|
self.mps_opts = {"method": "svd", "cutoff": 1e-6, "cutoff_mod": "abs"}
|
||||||
elif mps_enabled_value is False:
|
elif mps_enabled_value is False:
|
||||||
self.MPS_enabled = False
|
self.mps_opts = False
|
||||||
|
elif isinstance(mps_enabled_value, dict):
|
||||||
|
self.mps_opts = mps_enabled_value
|
||||||
else:
|
else:
|
||||||
raise TypeError("MPS_enabled has an unexpected type")
|
raise TypeError("MPS_enabled has an unexpected type")
|
||||||
|
|
||||||
@@ -74,7 +76,7 @@ class QuimbBackend(NumpyBackend):
|
|||||||
)
|
)
|
||||||
|
|
||||||
state = eval.dense_vector_tn_qu(
|
state = eval.dense_vector_tn_qu(
|
||||||
circuit.to_qasm(), initial_state, is_mps=self.MPS_enabled, backend="numpy"
|
circuit.to_qasm(), initial_state, self.mps_opts, backend="numpy"
|
||||||
)
|
)
|
||||||
|
|
||||||
if return_array:
|
if return_array:
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ def init_state_tn(nqubits, init_state_sv):
|
|||||||
return qtn.tensor_1d.MatrixProductState.from_dense(init_state_sv, dims)
|
return qtn.tensor_1d.MatrixProductState.from_dense(init_state_sv, dims)
|
||||||
|
|
||||||
|
|
||||||
def dense_vector_tn_qu(qasm: str, initial_state, is_mps, backend="numpy"):
|
def dense_vector_tn_qu(qasm: str, initial_state, mps_opts, backend="numpy"):
|
||||||
"""Evaluate QASM with Quimb.
|
"""Evaluate QASM with Quimb.
|
||||||
|
|
||||||
backend (quimb): numpy, cupy, jax. Passed to ``opt_einsum``.
|
backend (quimb): numpy, cupy, jax. Passed to ``opt_einsum``.
|
||||||
@@ -20,14 +20,9 @@ def dense_vector_tn_qu(qasm: str, initial_state, is_mps, backend="numpy"):
|
|||||||
nqubits = int(np.log2(len(initial_state)))
|
nqubits = int(np.log2(len(initial_state)))
|
||||||
initial_state = init_state_tn(nqubits, initial_state)
|
initial_state = init_state_tn(nqubits, initial_state)
|
||||||
|
|
||||||
if is_mps:
|
if mps_opts:
|
||||||
gate_opt = {}
|
|
||||||
gate_opt["method"] = "svd"
|
|
||||||
gate_opt["cutoff"] = 1e-6
|
|
||||||
gate_opt["cutoff_mode"] = "abs"
|
|
||||||
|
|
||||||
circ_quimb = qtn.circuit.CircuitMPS.from_openqasm2_str(
|
circ_quimb = qtn.circuit.CircuitMPS.from_openqasm2_str(
|
||||||
qasm, psi0=initial_state, gate_opts=gate_opt
|
qasm, psi0=initial_state, gate_opts=mps_opts
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -50,9 +50,18 @@ def test_eval(nqubits: int, tolerance: float, is_mps: bool):
|
|||||||
qasm_circ = qibo_circ.to_qasm()
|
qasm_circ = qibo_circ.to_qasm()
|
||||||
|
|
||||||
# Test quimb
|
# Test quimb
|
||||||
result_tn = qibotn.eval_qu.dense_vector_tn_qu(
|
if is_mps:
|
||||||
qasm_circ, init_state_tn, is_mps, backend=config.quimb.backend
|
gate_opt = {}
|
||||||
).flatten()
|
gate_opt["method"] = "svd"
|
||||||
|
gate_opt["cutoff"] = 1e-6
|
||||||
|
gate_opt["cutoff_mode"] = "abs"
|
||||||
|
result_tn = qibotn.eval_qu.dense_vector_tn_qu(
|
||||||
|
qasm_circ, init_state_tn, gate_opt, backend=config.quimb.backend
|
||||||
|
).flatten()
|
||||||
|
else:
|
||||||
|
result_tn = qibotn.eval_qu.dense_vector_tn_qu(
|
||||||
|
qasm_circ, init_state_tn, is_mps, backend=config.quimb.backend
|
||||||
|
).flatten()
|
||||||
|
|
||||||
assert np.allclose(
|
assert np.allclose(
|
||||||
result_sv, result_tn, atol=tolerance
|
result_sv, result_tn, atol=tolerance
|
||||||
|
|||||||
Reference in New Issue
Block a user