Skip to content

Commit

Permalink
generators for for loops in qeb.py
Browse files Browse the repository at this point in the history
  • Loading branch information
damarkian committed Nov 7, 2024
1 parent 1958939 commit 677bd38
Showing 1 changed file with 10 additions and 35 deletions.
45 changes: 10 additions & 35 deletions src/qibochem/ansatz/qeb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 677bd38

Please sign in to comment.