From 677bd382fdbc78e379a39a4636383e8beb55e222 Mon Sep 17 00:00:00 2001 From: Adrian Mak Date: Thu, 7 Nov 2024 09:06:26 +0800 Subject: [PATCH] generators for for loops in qeb.py --- src/qibochem/ansatz/qeb.py | 45 +++++++++----------------------------- 1 file changed, 10 insertions(+), 35 deletions(-) diff --git a/src/qibochem/ansatz/qeb.py b/src/qibochem/ansatz/qeb.py index 5ef8e37..acf5bd5 100644 --- a/src/qibochem/ansatz/qeb.py +++ b/src/qibochem/ansatz/qeb.py @@ -24,45 +24,20 @@ def qeb_circuit(n_qubits, excitation, theta=0.0, trotter_steps=1) -> Circuit: assert n_orbitals % 2 == 0, f"{excitation} must have an even number of items" n_tuples = len(excitation) // 2 - i_array = excitation[0:n_tuples] + i_array = excitation[:n_tuples] a_array = excitation[n_tuples:] - - ry_angle = 2.0 * theta - - mcry_controls = excitation[0:-1] - - fwd_gates = [] - - for _i in reversed(i_array[0:-1]): - fwd_gates.append(gates.CNOT(i_array[-1], _i)) - - for _a in reversed(a_array[0:-1]): - fwd_gates.append(gates.CNOT(a_array[-1], _a)) - + fwd_gates = [gates.CNOT(i_array[-1], _i) for _i in i_array[-2::-1]] + fwd_gates += [gates.CNOT(a_array[-1], _a) for _a in a_array[-2::-1]] fwd_gates.append(gates.CNOT(a_array[-1], i_array[-1])) - + fwd_gates += [gates.X(_ia) for _ia in excitation if _ia not in (i_array[-1], a_array[-1])] + circuit = Circuit(n_qubits) + circuit.add(gate for gate in fwd_gates) # MCRY # multi-controlled RY gate, # negative controls i, a # positive control on i_n - for _ia in excitation: - if _ia not in [i_array[-1], a_array[-1]]: - fwd_gates.append(gates.X(_ia)) - - mcry_gate = [] - mcry_gate.append(gates.RY(a_array[-1], ry_angle).controlled_by(*mcry_controls)) - - gate_list = [] - - for _g in fwd_gates: - gate_list.append(_g) - for _g in mcry_gate: - gate_list.append(_g) - for _g in reversed(fwd_gates): - gate_list.append(_g) - - circuit = Circuit(n_qubits) - for _g in gate_list: - circuit.add(_g) - + mcry_controls = excitation[:-1] + ry_angle = 2.0 * theta + circuit.add(gates.RY(a_array[-1], ry_angle).controlled_by(*mcry_controls)) + circuit.add(gate for gate in fwd_gates[::-1]) return circuit