Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect stated effect of CNOT gate when viewed as a XOR gate in the docs #13348

Open
tnemoz opened this issue Oct 21, 2024 · 3 comments
Open
Labels
bug Something isn't working

Comments

@tnemoz
Copy link
Contributor

tnemoz commented Oct 21, 2024

Environment

Not relevant.

What is happening?

The current documentation for the CXGate states that its effect is:
$$|a,b\rangle\to|a,a\oplus b\rangle$$
However, this is the effect using the textbook bit ordering. But for instance, the CXGate sends $|0,1\rangle$ to $|1,1\rangle$. Indeed, the actual effect of the CXGate is:
$$|a,b\rangle\to|a\oplus b,b\rangle$$.

How can we reproduce the issue?

Not relevant.

What should happen?

The documentation should correct the effect from $|a,a\oplus b\rangle$ to $|a\oplus b,b\rangle$.

Any suggestions?

No response

@tnemoz tnemoz added the bug Something isn't working label Oct 21, 2024
@alexanderivrii
Copy link
Contributor

Thanks @tnemoz for starting this discussion. Do you mean the documentation here: https://docs.quantum.ibm.com/api/qiskit/qiskit.circuit.library.CXGate?

Note that what it says is:

In the computational basis, this gate flips the target qubit if the control qubit is in the ∣1⟩∣1⟩ state. In this sense it is similar to a classical XOR gate. ‘∣a,b⟩→∣a,a⊕b⟩‘

This is actually correct, because when we define the CXGate(a, b) gate, the first argument a is the control, and the second argument b is the target. Here Qiskit's little endian convention is not important.

However, Qiskit's convention is important when describing the 4x4 unitary operator corresponding to the CXGate, note its matrix (from the same referenced page) is:

1 0 0 0
0 0 0 1
0 0 1 0
0 1 0 0

which is different from a more standard big endian convention, which would give

1 0 0 0
0 1 0 0
0 0 0 1
0 0 1 0

I hope this clears possible confusion.

@tnemoz
Copy link
Contributor Author

tnemoz commented Oct 21, 2024

Thanks @tnemoz for starting this discussion. Do you mean the documentation here: https://docs.quantum.ibm.com/api/qiskit/qiskit.circuit.library.CXGate?

I do!

Note that what it says is:

In the computational basis, this gate flips the target qubit if the control qubit is in the ∣1⟩∣1⟩ state. In this sense it is similar to a classical XOR gate. ‘∣a,b⟩→∣a,a⊕b⟩‘

when we define the CXGate(a, b) gate, the first argument a is the control, and the second argument b is the target.

I also agree with this. The CXGate(0, 1) is the "standard" CNOT gate, with the qubit number 0 being the control and the qubit number 1 being the target. However, this means that when seen with ket notation, the CXGate(0, 1) effect is:
$$|0,0\rangle\to|0,0\rangle,\quad|0,1\rangle\to|1,1\rangle,\quad|1,0\rangle\to|1,0\rangle,\quad|1,1\rangle\to|0,1\rangle.$$
This is also what is described by the matrix representation you linked. Indeed, the second column for instance represents the image of $|1\rangle=|0,1\rangle$ and is given by $|3\rangle=|1, 1\rangle$. This is why I think Qiskit's convention is also important when writing out the effect in ket notation.

Another way to see it is that when using Qiskit's convention, the observable representing the computational basis is represented as $Z_1\otimes Z_0$, meaning that the qubit number 0, which is the control, is placed on the right of the tensor product.

@alexanderivrii
Copy link
Contributor

Hmm, I think I see your point of view, indeed the effect of a CXGate on the computation basis states $|0,0\rangle$, $|0,1\rangle$, $|1,0\rangle$, $|1,1\rangle$ with little endian convention are exactly as you describe, agreeing with the operator representation. Nevertheless, I am personally interpreting the sentence on the similarity to the classical XOR gate as $(control, target) \to (control, control \oplus target)$, where of course $target$ is getting XORed with $control$, not the other way around, i.e. it should not be $(control, target) \to (control \oplus target, target)$. But I do now agree that writing the above in the ket notation may lead to some confusion. Maybe we should change that sentence to use $(a, b)$ instead of $|a, b\rangle$? What do other think about this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants