Assume I have tree qubits (A,B, and C)
I would like to flip qubit C if (A xor B)=1. The gate CCNOT(A,B,C) will flip C if A and B are both one.
Please let me know if there is a solution.
Quantum Computing Stack Exchange is a question and answer site for engineers, scientists, programmers, and computing professionals interested in quantum computing. It only takes a minute to sign up.
Sign up to join this communityAssume I have tree qubits (A,B, and C)
I would like to flip qubit C if (A xor B)=1. The gate CCNOT(A,B,C) will flip C if A and B are both one.
Please let me know if there is a solution.
why not just use two CNOTs to get the parity of qubits 0 and 1 on qubit 2? This circuit will set q_2 = q_0 XOR q_1:
from qiskit.circuit import QuantumCircuit
from qiskit import Aer, transpile
backend_sim = Aer.get_backend('statevector_simulator')
c = QuantumCircuit(3, 3)
# you can try here all 4 combinations
c.x(0)
c.x(1)
# the entanglement circuit
c.cnot(0, 2)
c.cnot(1, 2)
c.measure(range(3), range(3))
print(c.draw())
backend_sim.run(transpile(c, backend_sim)).result().get_counts()
I am not aware of such gate in qiskit's standard library, but you can create it as a custom gate.
As stated in the comment, the gate you are looking for is
I am not sure of such gate exists in the qiskit's standard library. But you can build it as a custom gate. First note that
Then you can do this as
from qiskit import QuantumCircuit
my_gate = QuantumCircuit(3)
my_gate.x(1)
my_gate.ccx(0,1,2)
my_gate.x(1)
my_gate.x(0)
my_gate.ccx(0,1,2)
my_gate.x(0)
custom_gate = my_gate.to_gate()
Quirk has a concept of "combined parity controls" for this. But you can also just decompose into a pair of CNOTs or X-controlled Zs.
More abstractly, if you generalize the concept of one operation controlling another, then the operation you want is $\text{Control}(Z_1 Z_2, X_3)$ where
$$\text{Control}(A, B) = \exp\left(-\frac{i}{\pi} \ln(A) \ln(B)\right)$$