# control gate with 3 inputs, two control and rotation gate

My question is about if there is any way to represent a circuit that take 3 inputs and applies a rotation gate on the third qubit if the first two qubits is similar (has the same state)?

• Thank you for your respond. I want to build a circuit that take a qubit in superposition, another qubit for input (unknown that I will give it to the circuit at run time) and I want to flip the third qubit value |0> to |1> if the first two has the same value? – Ebtehal Ali Jul 7 at 21:00

 #This line creates the circuit instance
circuit = cirq.Circuit()

#These lines creates the cubits you want to use from a given length
length = 3
qubits = [cirq.LineQubit(i) for i in range(length)]

#Here you assign the control qubits (the first two) and then the target qubit
controlled_rotation_on_Z = cirq.Z.controlled_by(*qubits[:-1])
circuit.append(controlled_rotation_on_Z(qubits[-1]))

Yielding the following circuit:

0: ───@───
│
1: ───@───
│
2: ───Z───


It is worth noting that it can be more than just 3 qubits. You can have N available qubits and you can develop a control gate, i.e. a Toffoli gate, that controls N-1 qubits and the very last qubit is the target qubit. So, it doesn't have to just rotation gates. Something to keep in mind.

Take a look at this question: How to create an $n$-qubit normally controlled gate?

And there are more of these questions in the exchange. I personally asked a couple of weeks ago about an N-Toffoli gate and was helped out greatly by that answer!

Hope it helps!

• Thank you for your respond. I want to build a circuit that take a qubit in superposition, another qubit for input (unknown that I will give it to the circuit at run time) and I want to flip the third qubit value |0> to |1> if the first two has the same value? – Ebtehal Ali Jul 7 at 21:12
• Ok, then you need to unpack the following: 1. You need to provide one of the qubits to be in superposition. So I think you would need a H-gate to act on the qubit prior to be acted upon by this controlled gate. 2. I assume you want the other qubit unchanged. 3. I think if you want to bit flip on the third qubit, you should take a look at this: en.wikipedia.org/wiki/Toffoli_gate – Enrique Segura Jul 7 at 21:14
• toffoli gate flip the third qubit if the first two is |1>. I want a gate that flips the third qubit if the first two is similar (|11> or |00>) – Ebtehal Ali Jul 7 at 21:32