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

Merged
merged 25 commits into from
Feb 7, 2025
Merged

PT + ZNE tutorial #2601

merged 25 commits into from
Feb 7, 2025

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 (c243b83).
Report is 28 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.

@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!

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!

**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.

Copy link
Member

Choose a reason for hiding this comment

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

Pauli twirling's goal is to transform some noise channel into a local depolarizing noise channel.

Right, yes, good point. That's the main goal. When adding gates surrounding each CNOT, you do it in such a way so that the CNOT is still the logical operation (up to global phase). In that sense are we not adding gates that end up compiling out (should you compile, which, you shouldn't)?

Copy link
Collaborator Author

@purva-thakre purva-thakre Feb 4, 2025

Choose a reason for hiding this comment

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

I am confused about the 'compile to identity' phrase.

If we start out with a CNOT subjected to a coherent noise channel, after Pauli twirling it's a CNOT gate with incoherent noise. That's what I was trying to show with the equations. Pauli twirling is equivalent to a local depoalrizing 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 am confused about the 'compile to identity' phrase.

Yeah that was sloppy phrasing on my part, sorry! I think we agree that the effect of pauli twirling on an ideal CNOT is the identity map (PT(ideal CNOT) = ideal CNOT).

Pauli twirling is equivalent to a local depoalrizing channel.

Is that true in all cases? I think Pauli twirling (in the infinite samples limit) only guarantees a Pauli channel is spit out, right?

BTW thanks for going back and forth on this a few times. It's really helpful for my understanding!

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 think Pauli twirling (in the infinite samples limit) only guarantees a Pauli channel is spit out, right?

Depends on the noise strength, the number of Pauli twirled circuits etc. If you look at the gif example, I ended up at a finite number of pauli twirled circuits that gave us the ideal CNOT PTM. I'll edit the lines to discuss this.

BTW thanks for going back and forth on this a few times. It's really helpful for my understanding!

No worries!

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

@purva-thakre
Copy link
Collaborator Author

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

Good luck with FOSDEM!

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.

Ready to go once my comments are addressed (and Alessandro's incoming).

@purva-thakre
Copy link
Collaborator Author

purva-thakre commented Feb 7, 2025

@natestemen I know you already approved this. Pinging you again in case you want to take a look at the changes in aeb9d8f

I added a new sub-section to add more information about the quadratic and linear error rates.

@cosenal PTAL.

Copy link
Contributor

@cosenal cosenal left a comment

Choose a reason for hiding this comment

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

LGTM


executor=partial(execute, noise_level=NOISE_LEVEL)

def combine_zne_pt(list_circuits, executor) -> List[float]:
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉

@purva-thakre purva-thakre merged commit 9ba9617 into main Feb 7, 2025
14 of 15 checks passed
@purva-thakre purva-thakre deleted the pt_zne_tutorial branch February 7, 2025 16:38
gluonhiggs pushed a commit to gluonhiggs/mitiq that referenced this pull request Feb 21, 2025
* add md file

* add comparison plot

* add thumbnail image

* correct plot typo

* update plot

* nate/alessandro feedback

* correct typo

* Apply suggestions from code review

Co-authored-by: nate stemen <[email protected]>

* check build: set heatmap bounds and y axis label

* nate's suggestions to code, update thumbnail image

* ordering phrasing

* some suggested changes and example twirled ptms

* Update docs/source/examples/pt_zne.md

Co-authored-by: nate stemen <[email protected]>

* remaining suggestions

* add 100 twirled circuits plot

* typo lets to let's

* change to gif

* typo 300 to 100 circuits

* Apply suggestions from code review

Co-authored-by: nate stemen <[email protected]>

* remaining suggestions (nate) + remove links to images in favor of gif

* erroneously capitalized Paulis in function defs

* some of Alessandro's comments

* remaining alessandro's comments

* typos

* nate's comment

---------

Co-authored-by: nate stemen <[email protected]>
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