diff --git a/src/qibotn/eval_qu.py b/src/qibotn/eval_qu.py index 4d06034c..df9425f6 100644 --- a/src/qibotn/eval_qu.py +++ b/src/qibotn/eval_qu.py @@ -46,8 +46,9 @@ def dense_vector_tn_qu(qasm: str, initial_state, mps_opts, backend="numpy"): return amplitudes - -def expectation_qu(qasm: str, pauli_string_pattern, initial_state, mps_opts, backend="numpy"): +def expectation_qu( + qasm: str, pauli_string_pattern, initial_state, mps_opts, backend="numpy" +): qasm_mod, nqubits = modify_qasm(qasm) if initial_state is not None: @@ -58,32 +59,34 @@ def expectation_qu(qasm: str, pauli_string_pattern, initial_state, mps_opts, bac qasm, psi0=initial_state, gate_opts=mps_opts ) - obs = pauli_string_gen(nqubits-1, pauli_string_pattern) + obs = pauli_string_gen(nqubits - 1, pauli_string_pattern) opt = ctg.ReusableHyperOptimizer( - # just do a few runs + # just do a few runs max_repeats=32, - # only use the basic greedy optimizer ... - methods=['greedy'], - # ... but pair it with reconfiguration + # only use the basic greedy optimizer ... + methods=["greedy"], + # ... but pair it with reconfiguration reconf_opts={}, - # just uniformly sample the space - optlib='random', - # terminate search if contraction is cheap - max_time='rate:1e6', - # account for both flops and write - usually wise for practical performance - minimize='combo-64', - # persist paths found in here - directory='cotengra_cache_eco', + # just uniformly sample the space + optlib="random", + # terminate search if contraction is cheap + max_time="rate:1e6", + # account for both flops and write - usually wise for practical performance + minimize="combo-64", + # persist paths found in here + directory="cotengra_cache_eco", + ) + expectation = circ_quimb.local_expectation( + obs, where=list(range(nqubits - 1)), optimize=opt, simplify_sequence="DRC" ) - expectation = circ_quimb.local_expectation(obs, where=list(range(nqubits-1)), optimize=opt, - simplify_sequence ="DRC") - + return expectation + def modify_qasm(qasm_circ): - lines =qasm_circ.split("\n") - qasm_circ_mod =[] + lines = qasm_circ.split("\n") + qasm_circ_mod = [] while lines: line = lines.pop(0).strip() sta = re.compile(r"qreg\s+(\w+)\s*\[(\d+)\];") @@ -94,7 +97,8 @@ def modify_qasm(qasm_circ): else: qasm_circ_mod.append(line) qasm_circ_mod = "\n".join(qasm_circ_mod) - return qasm_circ_mod, int(nqubits)+1 + return qasm_circ_mod, int(nqubits) + 1 + def pauli_string_gen(nqubits, pauli_string_pattern): """Used internally to generate the string based on given pattern and number @@ -117,11 +121,10 @@ def pauli_string_gen(nqubits, pauli_string_pattern): for i in range(nqubits): char_to_add = pauli_string_pattern[i % len(pauli_string_pattern)] result += char_to_add - for i, c in enumerate(result): - if (i==0): + if i == 0: obs = qu.pauli(c) else: - obs = obs&qu.pauli(c) + obs = obs & qu.pauli(c) return obs