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?


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.