I have a 2 qubit (+1 ancillary qubit) circuit in Qiskit which calculates the classical OR gate (q0 or q1) as follows:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer
for input in ['00','01','10','11']:
mycircuit1 = QuantumCircuit(3,1)
#Initialization - Note qiskit order
if input[0] == '1':
mycircuit1.x(1)
if input[1] == '1':
mycircuit1.x(0)
mycircuit1.cx(0,2)
mycircuit1.cx(1,2)
mycircuit1.ccx(0,1,2)
mycircuit1.measure(2,0)
job = execute(mycircuit1, Aer.get_backend('qasm_simulator'), shots = 1000)
counts = job.result().get_counts(mycircuit1)
print("Input:", input, "Output:", counts)
that returns:
Input: 00 Output: {'0': 1000}
Input: 01 Output: {'1': 1000}
Input: 10 Output: {'1': 1000}
Input: 11 Output: {'1': 1000}
and and I want to expand it to a 3 qubit(+1 ancillary qubit) circuit that calculates (q0 or q1 or q2) but I do not know how. One way of thinking is that (q0 OR q1 OR q2) = (qo OR q1) OR q2 so I can put the gates from the previous circuit and repeat the process. However, I do not know how to bind these two together. Here is the circuit that I have at the moment:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer
for input in ['000','100','010','001', '110','011','101','111']:
mycircuit1 = QuantumCircuit(4,1)
#Initialization - Note qiskit order
if input[0] == '1':
mycircuit1.x(1)
if input[1] == '1':
mycircuit1.x(0)
mycircuit1.cx(0,2)
mycircuit1.cx(1,2)
mycircuit1.ccx(0,1,2)
mycircuit1.barrier()
mycircuit1.cx(1,3)
mycircuit1.cx(2,3)
mycircuit1.ccx(1,2,3)
mycircuit1.measure(3,0)
job = execute(mycircuit1, Aer.get_backend('qasm_simulator'), shots = 1000)
counts = job.result().get_counts(mycircuit1)
print("Input:", input, "Output:", counts)
that returns:
Input: 000 Output: {'0': 1000}
Input: 100 Output: {'1': 1000}
Input: 010 Output: {'1': 1000}
Input: 001 Output: {'0': 1000}
Input: 110 Output: {'1': 1000}
Input: 011 Output: {'1': 1000}
Input: 101 Output: {'1': 1000}
Input: 111 Output: {'1': 1000}
I am stuck at this point and I do not know what to do next. As demonstrated in the results if q0 or q1 equals one, the final result would be one, so if I can fix the problem in the 001 input, I can say I implemented the classical OR gate for three inputs.