# Contribute the swap test

**URL:** <https://discuss.pennylane.ai/t/contribute-the-swap-test/4269>\
**Category:** PennyLane Help\
**Created:** [March 13, 2024, 8:19am UTC](https://discuss.pennylane.ai/t/contribute-the-swap-test/4269 "2024-03-13T08:19:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zj-lucky](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/zj-lucky/32/788_2.png) [@zj-lucky](https://discuss.pennylane.ai/u/zj-lucky)\
**Post date:** [March 13, 2024, 8:19am UTC](https://discuss.pennylane.ai/t/contribute-the-swap-test/4269/1 "2024-03-13T08:19:01Z")

</div>

Hello! If we have two vectors with 4 dimensionality, i.e., x\_1 = [1, 2, 3, 4] and x\_2 = [5, 6, 7, 8]，How can we contribute a “SWAP TEST” quantum circuit for evaluate these vectors’ similarity?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [March 14, 2024, 2:24pm UTC](https://discuss.pennylane.ai/t/contribute-the-swap-test/4269/2 "2024-03-14T14:24:59Z")

</div>

Hey @zj-lucky,

Sounds like you’re trying to do the [swap test](https://en.wikipedia.org/wiki/Swap_test) with two 2-qubit states? Is that right?

---

<div class="post-metadata">

**Author:** ![zj-lucky](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/zj-lucky/32/788_2.png) [@zj-lucky](https://discuss.pennylane.ai/u/zj-lucky)\
**Post date:** [March 19, 2024, 2:08am UTC](https://discuss.pennylane.ai/t/contribute-the-swap-test/4269/3 "2024-03-19T02:08:24Z")

</div>

Each vector x\_i can be represented by a two-qubit state; there are two such vectors.

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [March 19, 2024, 11:22pm UTC](https://discuss.pennylane.ai/t/contribute-the-swap-test/4269/4 "2024-03-19T23:22:52Z")

</div>

Ah! Okay. Then this should be straightforward enough I think. Here’s what it looks like to swap two two-qubit states:

```auto
import pennylane as qml
from pennylane import numpy as np

dev = qml.device('default.qubit')

@qml.qnode(dev)
def four_qubit_SWAP(state):
    qml.AmplitudeEmbedding(state, wires=range(4), normalize=True)
    qml.SWAP(wires=[1, 3])
    qml.SWAP(wires=[0, 2])
    return qml.state()

state1 = np.array([0.5, -0.5, -0.5, 0.5])
state2 = np.array([-0.5, 0.5, 0.5, -0.5])
state = np.kron(state1, state2)

# swap state1 with state2
print(state)
print(four_qubit_SWAP(state))
print(qml.draw(four_qubit_SWAP)(state))

```

```auto
[-0.25 0.25 0.25 -0.25 0.25 -0.25 -0.25 0.25 0.25 -0.25 -0.25 0.25
 -0.25 0.25 0.25 -0.25]
[-0.25+0.j 0.25+0.j 0.25+0.j -0.25+0.j 0.25+0.j -0.25+0.j -0.25+0.j
  0.25+0.j 0.25+0.j -0.25+0.j -0.25+0.j 0.25+0.j -0.25+0.j 0.25+0.j
  0.25+0.j -0.25+0.j]
0: ─╭|Ψ⟩───────╭SWAP─┤ State
1: ─├|Ψ⟩─╭SWAP─│─────┤ State
2: ─├|Ψ⟩─│─────╰SWAP─┤ State
3: ─╰|Ψ⟩─╰SWAP───────┤ State

```

It’s just a combination of two different SWAP gates 🙂. Then, to do a SWAP test here, you just need to make a larger controlled operator out of two swap gates.

```auto
@qml.qnode(dev)
def double_swap_test():
    qml.Hadamard(0)
    qml.ctrl(qml.SWAP(wires=[2, 4]) @ qml.SWAP(wires=[1, 3]), control=0)
    qml.Hadamard(0)
    return qml.state()

print(qml.draw(double_swap_test)())

```

```auto
0: ──H─╭●──────────H─┤ State
2: ────├SWAP@SWAP────┤ State
4: ────├SWAP@SWAP────┤ State
1: ────├SWAP@SWAP────┤ State
3: ────╰SWAP@SWAP────┤ State

```
