def multi_cz(qubits):
## This will perform a CCCCCZ on as many qubits as we want,
## as long as we have enough scratch qubits
multi_cx(qubits, do_cz=True)
def multi_cx(qubits, do_cz=False):
## This will perform a CCCCCX with as many conditions as we want,
## as long as we have enough scratch qubits
## The last qubit in the list is the target.
target = qubits[-1]
conds = qubits[:-1]
scratch_index = 0
ops = []
while len(conds) > 2:
new_conds = []
for i in range(len(conds)//2):
ops.append((conds[i * 2], conds[i * 2 + 1], scratch[scratch_index]))
new_conds.append(scratch[scratch_index])
scratch_index += 1
if len(conds) & 1:
new_conds.append(conds[-1])
conds = new_conds
for op in ops:
qc.ccx(op[0], op[1], op[2])
if do_cz:
qc.h(target)
if len(conds) == 0:
qc.x(target)
elif len(conds) == 1:
qc.cx(conds[0], target)
else:
qc.ccx(conds[0], conds[1], target)
if do_cz:
qc.h(target)
ops.reverse()
for op in ops:
qc.ccx(op[0], op[1], op[2])
main()