# Question on last tasks on "Modeling the toric code on a quantum computer" demo

**URL:** <https://discuss.pennylane.ai/t/question-on-last-tasks-on-modeling-the-toric-code-on-a-quantum-computer-demo/8386>\
**Category:** Demos\
**Created:** [April 11, 2025, 9:14am UTC](https://discuss.pennylane.ai/t/question-on-last-tasks-on-modeling-the-toric-code-on-a-quantum-computer-demo/8386 "2025-04-11T09:14:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jag](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/jag/32/3705_2.png) [@jag](https://discuss.pennylane.ai/u/jag)\
**Post date:** [April 11, 2025, 9:14am UTC](https://discuss.pennylane.ai/t/question-on-last-tasks-on-modeling-the-toric-code-on-a-quantum-computer-demo/8386/1 "2025-04-11T09:14:24Z")

</div>

Hi,

At the end of notebook [Modeling the toric code on a quantum computer | PennyLane Demos](https://pennylane.ai/qml/demos/tutorial_toric_code), there are these tasks which I’m trying to do.

```auto
Note
I encourage dedicated readers to calculate the phase accumulated by exchanging:
1. A Z Group excitation and a Z Group Excitation
2. An X Group excitation and an X Group Excitation
3. A combination Ψ particle and an X Group excitation
4. A combination Ψ particle and a Z Group excitation
5. A combination Ψ particle with another Ψ particle

The combination particle should behave like a standard fermion. You can create and move combination particles by applying PauliY operations.

```

For task 1, i.e. A Z Group excitation and a Z Group Excitation, this is my code

```python
prep1 = [(1, 1), (2, 1)]
prep2 = [(1, 3)]
loop1 = [(1,2),(2,2),(3,2),(3,1),(2,1),(2,2)]

# WITHOUT LOOPING

expvals = excitations(prep1 + prep2, [])
x_expvals, z_expvals = separate_expvals(expvals)

fig, ax = excitation_plot(x_expvals, z_expvals)

ax.plot(*zip(*(prep1)), color="maroon", linewidth=10)
ax.plot(*zip(*(prep2)), color="maroon", linewidth=10)

display(plt.show())
z_around_z = hadamard_test(prep1 + prep2, [], [], [])
print("WITHOUT LOOPING - Mutual exchange statistics - Move z excitation around z excitation: ", z_around_z)

# WITH LOOPING
expvals = excitations(prep1 + prep2 + loop1, [])
x_expvals, z_expvals = separate_expvals(expvals)

fig, ax = excitation_plot(x_expvals, z_expvals)

ax.plot(*zip(*(prep1)), color="maroon", linewidth=10)
ax.plot(*zip(*(prep2 + loop1)), color="maroon", linewidth=10)

display(plt.show())
z_around_z = hadamard_test(prep1 + prep2, [], loop1, [])
print("WITH LOOPING - Mutual exchange statistics - Move z excitation around z excitation: ", z_around_z)

```

I did with and without looping to compare the output of the layout and hadamard test.

 ![z_around_z](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/a/a23d6768ae41de0afca22d412f0cfb5b030fff42.png)

Question 1: Without any looping, I’m getting +1 from the Hadamard test and 0.0 with looping. Can I know which one is the correct output? Is my looping path correct since I see some “extra” excitations in my loop path?

Question 2: I modified the excitation and Hadamard test code to work with combination Ψ particle using PauliY operator. Then, I did the task 3, i.e. A combination Ψ particle and an X Group excitation. The output is as below (with and without looping). Similar to the previous question’s observations, without looping, I’m getting +1 from Hadamard test and 0.0 with looping. Can I know which one is the correct output? Is my looping path correct since I see some “extra” excitations in my loop path?

 ![y_around_x](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/a/a79ca604fb5106dfa5d0cfe006673fefc976750b.png)

Thank you.

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [April 14, 2025, 8:49pm UTC](https://discuss.pennylane.ai/t/question-on-last-tasks-on-modeling-the-toric-code-on-a-quantum-computer-demo/8386/2 "2025-04-14T20:49:14Z")

</div>

Hi @jag , thanks for your questions!

I’m checking with the team. We’ll get back to you as soon as we have capacity. We may need a couple of days since we’re working on a new PennyLane version that will be released tomorrow! 😎

---

<div class="post-metadata">

**Author:** ![jag](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/jag/32/3705_2.png) [@jag](https://discuss.pennylane.ai/u/jag)\
**Post date:** [April 15, 2025, 4:07am UTC](https://discuss.pennylane.ai/t/question-on-last-tasks-on-modeling-the-toric-code-on-a-quantum-computer-demo/8386/3 "2025-04-15T04:07:04Z")

</div>

Hi @CatalinaAlbornoz , thank you for looking into my questions. I’m learning a lot from Pennylane especially the tutorials. Looking forward to the new Pennylane release too. 🙂

---

<div class="post-metadata">

**Author:** ![christina](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/christina/32/583_2.png) [@christina](https://discuss.pennylane.ai/u/christina)\
**Post date:** [April 23, 2025, 3:07pm UTC](https://discuss.pennylane.ai/t/question-on-last-tasks-on-modeling-the-toric-code-on-a-quantum-computer-demo/8386/4 "2025-04-23T15:07:16Z")

</div>

Thanks for the question @jag .

When we are calculating the statistics, we need to make sure to bring the particles back to the same spot. If the excitations are in a different place, we are in a completely different state. When we bring the excitations back to the same spot, we still have the same probability distribution but the state just has a different phase.

Since the excitations are in completely different places, the hadamard test gives us 0 because there’s no overlap.

With this set of prep and loop:

```auto

prep1 = [(1, 1), (2, 1), (0,2)]
prep2 = [(1, 3)]
loop1 = [(2,3), (3,3), (4,2), (4,1), (3,0), (2,0), (1,1), (1,2)]

```

 ![Screenshot 2025-04-23 at 11.03.18 AM](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/7/73d55f8c360db06452e579d92c43ce9bcc3a489c.png)

We keep the excitations in exactly the same locations at the end, but one of the excitations made a full circuit around the other.

In this case, we can see that the hadamard test gives us a relative phase offset of `0`, as `cos(phi) = 1`. The X and Z excitations on their own are bosonic. It’s only when they interact that things get interesting.

A similar approach needs to be taken for the composite particle too.
