r/QuantumComputing • • Jun 28 '20

SWAP/CNOT identity?

Post image
60 Upvotes

9 comments sorted by

12

u/cgranade Jun 29 '20

One way to think of what's happening here is to make an analogy to the common programming interview question about how to swap two integers without allocating any additional space.

x = a;
y = b;
x = x ^ y; // x is now a ⊕ b
y = y ^ x; // y is now b ⊕ (a ⊕ b) = a
x = x ^ y; // x is now (a ⊕ b) ⊕ a = b

The CNOT operation acts like a single-bit XOR on the computational basis, |𝑥⟩|𝑦⟩ ↦ |𝑥⟩|𝑦 ⊕ 𝑥⟩, so that the same argument works in the quantum case as well. Since you can apply that argument for every member of the computational basis, it works for all input states by linearity.

If you're interested to learn more, checkout my book with u/crazy4pi314, Learn Quantum Computing with Python and Q#!

2

u/[deleted] Jun 28 '20

I’m reading Quantum Computing: An Applied Approach by Hidary. I’m not able to make sense of this identity. First, they are both symmetric, so the right hand side of the equation can be written as (CNOT_ij)3. But also, just trying it for i=j=4, SWAP_44 = 1, where CNOT_44 = 0. I must be completely misunderstanding what the ij indices are. Are they not the row and column matrix indices?

7

u/dahdar Jun 28 '20 edited Jun 28 '20

cnot can be written as CNOT_ij

1, 0, 0, 0
0, 1, 0, 0
0, 0, 0, 1
0, 0, 1, 0

or CNOT_ji

1, 0, 0, 0
0, 0, 0, 1
0, 0, 1, 0
0, 1, 0, 0

Depending on if the first qubit or the second qubit is the control, in this case the indices are indicating which qubit is the control. That identity holds if you switch i and j

Edit: You can check this by thinking about a CNOT with control on qubit 0, which is effectively the definition you provided. If I wanted to switch it so that qubit 1 is the control, i could put a swap before and after that gate, which would make qubit 1 be the effective control for that single gate. When you multiply SWAP * CNOT * SWAP you get the second matrix I posted.

3

u/[deleted] Jun 28 '20 edited Jun 28 '20

Ahhh, so it’s not a transpose but a switch in which bit is the control. That makes sense now. If I take your first matrix times your second, then again times your first, I indeed get the SWAP matrix. Thanks!

Edit (because of your edit): That’s cool! And it makes sense. So I guess the identity is

SWAP = CNOT SWAP CNOT SWAP CNOT

or, since they are their own inverse,

SWAP CNOT SWAP = CNOT SWAP CNOT

1

u/[deleted] Jul 21 '24

that edit makes soo much intuitive sense! thanks a million man!

2

u/Sy-Zygy Jun 28 '20

Check out the Generalizing Xor-Swapping into Axis-Swapping section here: https://algassert.com/post/1717

2

u/[deleted] Jun 28 '20

Thanks. I’m coming to this from a particle physics background, and woefully underprepared w.r.t. computer science. I think a lot of this gate stuff is going to take some time to settle into my brain.

1

u/7_hermits Jun 29 '20

Three CNOTS are used to swap the two elements.

Here it is shown how it is done.

0

u/wnoise Jun 29 '20 edited Jun 29 '20

These are classical gates. Just work through the truth table.

The permutation representation of the gates is a bit obtuse in this case, especially compared to just keeping track of the values of the two variables, and running through each case.

CNOT is "y ^= x" in C notation, or, "y = y ^ x".