How to implement such a gate where one input bit is zero and the other is one and the output should be one?


I have created 10 qubits, where I have set 2 in superposition by applying Hadamard gate to each of the 2 qubits. So the 2 qubits state can be $|00\rangle$, $|01\rangle$, $|10\rangle$, $|11\rangle$. I need to assign values based on the values present in 2 superpositioned qubits. For eg, if the 2 qubits are in $|00\rangle$ I need to set the other qubits as $|00011110\rangle$, if the 2 qubits are in $|01\rangle$ states, I need to set the other qubits as $|10101010\rangle$ and if the 2 qubits are in $|11\rangle$ I need to set the other qubits as $|11110000\rangle$. So in the broad sense after applying H gate to 2 qubits when they have totally 4 states for each state I need to assign qubits other values. I saw I can apply the Toffoli gate if the state is |11⟩. However, if the state is $|01\rangle$ or $|00\rangle$ then what is the way?


Posted 2019-03-14T17:08:41.197

Reputation: 33

Hi. Welcome to Quantum Computing SE! It is preferable that you use MathJax to typeset your posts. Review How to write a good question?. I've [edit]ed it on your behalf this time.

– Sanchayan Dutta – 2019-03-14T17:12:37.433



I believe the question boils down to "How to apply a controlled version of the X gate if the control qubits should be in states other than $|11\rangle$?"

Toffoli gate CCNOT applies an X gate to the target qubit if both control qubits are in the $|1\rangle$ state. If you need to apply an X gate if the control qubits are in some different state, you can do the following:

  1. Convert the control qubits to $|11\rangle$ state using X gates on qubits which have to be in $|0\rangle$ state.
  2. Apply CCNOT gate.
  3. Convert the control qubits back to their original state, using the same sequence of X gates as in step 1.

For example, if you need to apply an X gate to target if control qubits are in $|01\rangle$ state, you can do

CCNOT(control[0], control[1], target);

You can use the same approach when implementing gates controlled on any pattern of control qubit states.

This question looks very similar to problem A2 from the recent Q# contest. If you're looking to implement this in Q#, check ControlledOnBitString library function which does exactly this. You can find the full code for the problem in the Superposition kata.

Mariia Mykhailova

Posted 2019-03-14T17:08:41.197

Reputation: 6 616