Skip to content

Commit

Permalink
Optimization for the MCX Recursive Gate
Browse files Browse the repository at this point in the history
Change the recursive method for the Lemma 9 of arXiv:1501.06911, first shown in Lemma 7.3 of https://link.aps.org/doi/10.1103/PhysRevA.52.3457

Co-Authored-By: Rafaella Vale <26910380+rafaella-vale@users.noreply.github.com>
Co-Authored-By: Jefferson Deyvis <67497412+jeffersondeyvis@users.noreply.github.com>
  • Loading branch information
3 people committed Sep 7, 2022
1 parent 30899cf commit 507d5b3
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions qiskit/circuit/library/standard_gates/x.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,8 +1093,22 @@ def _define(self):
qc._append(C4XGate(), q[:], [])
self.definition = qc
else:
for instr, qargs, cargs in self._recurse(q[:-1], q_ancilla=q[-1]):
qc._append(instr, qargs, cargs)
num_ctrl_qubits = len(q) - 1
q_ancilla = q[-1]
q_target = q[-2]
middle = ceil(num_ctrl_qubits / 2)
first_half = [*q[:middle]]
second_half = [*q[middle:num_ctrl_qubits - 1], q_ancilla]

qc._append(MCXVChain(num_ctrl_qubits=len(first_half), dirty_ancillas=True), \
[*first_half, q_ancilla, *q[middle:middle + len(first_half) - 2]], [])
qc._append(MCXVChain(num_ctrl_qubits=len(second_half), dirty_ancillas=True), \
[*second_half, q_target, *q[:len(second_half) - 2]], [])
qc._append(MCXVChain(num_ctrl_qubits=len(first_half), dirty_ancillas=True), \
[*first_half, q_ancilla, *q[middle:middle + len(first_half) - 2]], [])
qc._append(MCXVChain(num_ctrl_qubits=len(second_half), dirty_ancillas=True), \
[*second_half, q_target, *q[:len(second_half) - 2]], [])

self.definition = qc

def _recurse(self, q, q_ancilla=None):
Expand Down

0 comments on commit 507d5b3

Please sign in to comment.