from qiskit import QuantumCircuit from qiskit.circuit import QuantumRegister, ClassicalRegister import math def ghz(n): qreg = QuantumRegister(n) circ = QuantumCircuit(qreg) if n > 0 : circ.h(qreg[0]) for i in range(n-1): circ.cx(qreg[i], qreg[i+1]) return circ # print(ghz(10)) def ex_or(x, y, circ): q = QuantumRegister(1) circ.add_register(q) circ.cx(x, q[0]) circ.cx(y, q[0]) return (circ, q[0]) # def ex_or2(circ): # q = QuantumRegister(1) # circ.add_register(q) # circ.cx(, q[0]) # circ.cx(, q[0]) # return circ # qreg_tst = QuantumRegister(2) # circ_tst = QuantumCircuit(qreg_tst) # Note that the output will have 3 qubits. # print(ex_or(qreg_tst[0],qreg_tst[1], circ_tst)[1]) def g(x, y, z, circ): (circ, xy) = ex_or(x, y, circ) circ.barrier() (circ, yz) = ex_or(y, z, circ) circ.barrier() (circ, xz) = ex_or(x, z, circ) circ.barrier() (circ, xyz) = ex_or(x, yz, circ) return (circ, x, y,z, xy, yz, xz, xyz) def ccz(x, y, z, circ): (circ, x, y,z, xy, yz, xz, xyz) = g(x, y, z, circ) circ.barrier() g_dg = circ.inverse() circ.t(x) circ.t(y) circ.t(z) circ.tdg(xy) circ.tdg(yz) circ.tdg(xz) circ.t(xyz) circ = circ.compose(g_dg) return circ qreg_tst1 = QuantumRegister(3) a, b, c = qreg_tst1 circ_tst1 = QuantumCircuit(qreg_tst1) print(ccz(a,b,c,circ_tst1))