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

PT + ZNE tutorial #2601

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open

PT + ZNE tutorial #2601

wants to merge 18 commits into from

Conversation

purva-thakre
Copy link
Collaborator

Description

Fixes #2374


License

  • I license this contribution under the terms of the GNU GPL, version 3 and grant Unitary Fund the right to provide additional permissions as described in section 7 of the GNU GPL, version 3.

Before opening the PR, please ensure you have completed the following where appropriate.

@purva-thakre
Copy link
Collaborator Author

@natestemen Do you mind taking a quick look at the general outline of this tutorial? I have to clean up the code a bit. So, you could ignore the code blocks for now.

I am not too sure if I need to describe something in excruciating detail as the PT user guide is still WIP.

Copy link

codecov bot commented Dec 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.74%. Comparing base (0262bfc) to head (ca4b9c8).
Report is 25 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2601      +/-   ##
==========================================
+ Coverage   98.72%   98.74%   +0.01%     
==========================================
  Files          92       93       +1     
  Lines        4168     4219      +51     
==========================================
+ Hits         4115     4166      +51     
  Misses         53       53              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@natestemen
Copy link
Member

Took a quick look through things and this looks great! I think it's a nice split of theory and practice. I can imagine some of the material working its way in to the user guide, but lets not worry about that for now.

@purva-thakre
Copy link
Collaborator Author

@natestemen The comparison plot is missing data related to only ZNE values.

This is because the circuit I am using is very small for ZNE. If I increase the circuit size used for the comparison plot, the time taken by the tutorial during execution is a lot.

@natestemen natestemen requested a review from cosenal December 17, 2024 16:55
Copy link
Member

@natestemen natestemen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't finished reading through everything just yet, but wanted to get feedback soon so we can start iterating.

Overall this is a really great mix of theory and practice. Well done setting the bones up.

docs/source/_thumbnails/pt_zne.png Outdated Show resolved Hide resolved
docs/source/examples/examples.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Show resolved Hide resolved
@cosenal
Copy link
Contributor

cosenal commented Dec 18, 2024

I restarted the RTD build; having a rendered version will make it easier to review.

Copy link
Member

@natestemen natestemen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really solid tutorial! Can't wait to get it up!

docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
plt.ylabel("Expectation Values Difference")
plt.title("Comparison of expectation values with ideal as a function of noise strength")
plt.legend()
plt.show()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we understand why ZNE is so good when the noise strength = 0.5?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not too sure for the circuit used in this example.

If I use a smaller GHZ circuit of 2 qubits, the noise does not do anything as the final state vector before and after adding coherent noise is the same. Interestingly, this behavior is not preserved as the number of qubits in the initial ghz circuit is increased. Worth looking into!

Copy link
Member

@natestemen natestemen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments, but hopefully nothing major!

docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
**Incoherent noise** is a process that results in the quantum system entangling with its environment i.e. this type of noise is irreversible. The system and the environment end up in a mixed state. It scales linearly in the small error limit.
The noise channel can be described using Pauli operators which makes it easy to analyze and simulate. Worst case error rate is directly proportional to the **average gate infidelity**.

For example, a depolarizing noise channel is a stochastic noise channel where a noiseless process is probabilistically mixed with orthogonal errors. If $\rho$ is a single qubit state, $p$ is the probabilistic error rate and $\mathcal{E}(\rho)$ is the noise channel:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great example, but it needs a connection back to (in)coherent noise. Can you add something, maybe at the end?

Do you think we should have an example for coherent noise too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The depolarizing noise example is to show how Pauli twirling is equivalent to tailoring the noise into a depolarizing noise channel.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite see how this relates to pauli twirling. Can you help spell it out?

From my perspective the main idea in pauli twirling is to add gates that compile to the identity, but change the impact of noise. So when you say the depolarizing channel can be written in terms of paulis, I don't see how that is pauli twirling.

Copy link
Collaborator Author

@purva-thakre purva-thakre Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my perspective the main idea in pauli twirling is to add gates that compile to the identity,

Uh, no. Pauli twirling's goal is to transform some noise channel into a local depolarizing noise channel. The circuits average over to a mixed noise channel which is what I was trying to show in my example.

docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved
print("Example noisy twirled circuit", noisy_twirled_circuits[-1], sep="\n")
```

The twirled PTM is averaged over each noisy twirled circuit such that the new PTM is close to that of the PTM of incoherent noise. We skip the step in this section as we require a very large number of twirled circuits to demonstrate the desired effect of averaging over multiple numpy arrays.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit anti-climatic, I was looking forward to seeing the effect of this on those PTM heatmaps :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, well, this increases the notebook execution time by a lot. The coherent noise that I used is obviously not included in the noisy options allowed by generate_pauli_twirl_variants. We should create an issue to clean this portion of pauli twirling.

So, if I want to generate 100 pauli twirled circuits, I have to splice each of the circuits to make sure the noise is inserted in the correct spot and the pauli twirling gates are also inserted accordingly. Then, the next step is to get their PTM's and sum them up.

Copy link
Member

@natestemen natestemen Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we included an image of the twirled PTM heatmap saying "this is what would happen if you generated the twirled PTM"?

Copy link
Collaborator Author

@purva-thakre purva-thakre Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added 3 4 examples. See 2fbf94f.

I also changed the color palette to make the heatmap easy to read.

Ideally, I would prefer this to be a gif (something like this) to show how the heatmap changes as the number of pauli twirled circuits is increased. We could keep this as a unitary hackathon issue if there are not a lot of non-quantum issues. WDYT?

I manually created a gif in 818a864 Hopefully, this fixes Alessandro's comment.

I was looking forward to seeing the effect of this on those PTM heatmaps

docs/source/examples/pt_zne.md Outdated Show resolved Hide resolved

We are going to add coherent noise to this circuit and then get the error-mitigated expectation value. For a detailed discussion on this, refer to the [ZNE user guide](../guide/zne-1-intro.md).

As we are using a simulator, we have to make sure the noise model adds coherent noise to CZ/CNOT gates in our circuit. For this, `get_noise_model` is used to add noise to CZ/CNOT gates. See [PT user guide](../guide/pt-1-intro.md) for more.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

solid function that get_noise_model 📦 😃

docs/source/examples/pt_zne.md Show resolved Hide resolved
@purva-thakre
Copy link
Collaborator Author

@natestemen @cosenal This is ready. Whenever you guys are free...

Good luck with FOSDEM!

print("Example noisy twirled circuit", noisy_twirled_circuits[-1], sep="\n")
```

The twirled PTM is averaged over each noisy twirled circuit such that the new PTM is close to that of the PTM of incoherent noise. We skip the step in this section as we require a very large number of twirled circuits to demonstrate the desired effect of averaging over multiple numpy arrays. The variations in pauli twirled PTMs are shown below when averaged over a different number of pauli twirled circuits: [3](../img/pt_zne_3_circuits.png), [5](../img/pt_zne_5_circuits.png), [30](../img/pt_zne_30_circuits.png) and [300](../img/pt_zne_100_circuits.png).
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDK if there's a better way to provide a link to a couple of png images. These appear as images a user will have to download.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tutorial: Composing techniques: Pauli Twirling and Zero Noise Extrapolation
3 participants