unitaryfund / mitiq

Mitiq is an open source toolkit for implementing error mitigation techniques on most current intermediate-scale quantum computers.
https://mitiq.readthedocs.io
GNU General Public License v3.0
344 stars 145 forks source link

Simulate noise for CNOT and CZ gates in Pauli Twirling #2397

Closed EmilianoG-byte closed 2 weeks ago

EmilianoG-byte commented 4 weeks ago

Description

Currently, the pauli_twirl_circuit function does not wrap correctly noise applied on CNOT and CZ gates when used in a simulator. Instead, it adds the twirled operators in between the noise and the two-qubit gates.

This PR implements an argument in pauli_twirl_circuit to pass a noise_op acting between CNOT/CZ gates and the twirled layers. Thus, it allows to properly Taylor the noise using Pauli Twirling on a simulated backend.

Example:

Circuit without noise:

0: ───H───@───@───
          │   │
1: ───────X───@───

Pauli Twirling before this PR:

0: ───H────I───@───I───D(0.01)───I───@───Z───D(0.01)───
               │                     │
1: ────────X───X───X───D(0.01)───Y───@───Y───D(0.01)───

Pauli Twirling after this PR:

0: ───H───I───@───D(0.01)───I───I───@───D(0.01)───Z───
              │                     │
1: ───────X───X───D(0.01)───X───Y───@───D(0.01)───Y────

Contribution as part of the Unitary Hack 2024


License

Closes [#2346]

codecov[bot] commented 4 weeks ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 98.31%. Comparing base (03b3548) to head (a6995f1). Report is 4 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #2397 +/- ## ========================================== + Coverage 98.30% 98.31% +0.01% ========================================== Files 87 87 Lines 4003 4042 +39 ========================================== + Hits 3935 3974 +39 Misses 68 68 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

Misty-W commented 2 weeks ago

hi @EmilianoG-byte good news, we will accept your solution for unitaryHACK, only change needed is to fix the mypy failures (we will give hints if needed). If you're interested in solving the multi-platform problem for fun, that would be awesome but not required to win the bounty.

EmilianoG-byte commented 2 weeks ago

hi @EmilianoG-byte good news, we will accept your solution for unitaryHACK, only change needed is to fix the mypy failures (we will give hints if needed). If you're interested in solving the multi-platform problem for fun, that would be awesome but not required to win the bounty.

Thank you @Misty-W , sounds great 😄. I have fixed the mypy issues, "except" for one where I ignored the type hinting since I am passing **kwargs to the chosen noise function.

I am actually intrigued on what you and the other developers at Mitiq think about it. Do you agree with my solution being the best we can do (not just for the unitary hack)?

Misty-W commented 2 weeks ago

I am actually intrigued on what you and the other developers at Mitiq think about it. Do you agree with my solution being the best we can do (not just for the unitary hack)?

@EmilianoG-byte the alternative we discussed is to have the user specify a noise model in the simulator backend such that twirling gates surround the noisy CNOT and noisy CZ gates instead of the ideal CNOT and CZ. It's not clear whether we can get that approach to work on all Mitiq-supported platforms either, though.

EmilianoG-byte commented 2 weeks ago

@EmilianoG-byte the alternative we discussed is to have the user specify a noise model in the simulator backend such that twirling gates surround the noisy CNOT and noisy CZ gates instead of the ideal CNOT and CZ. It's not clear whether we can get that approach to work on all Mitiq-supported platforms either, though.

@Misty-W so that still means mapping them "manually" as I am doing right? Since there is no automatic QASM conversion for noise models (?).

Misty-W commented 2 weeks ago

@Misty-W so that still means mapping them "manually" as I am doing right? Since there is no automatic QASM conversion for noise models (?).

I don't believe there is QASM conversion for noise models, but if the noise is added after converting the twirled circuit back to the native frontend, then the noise model wouldn't have to be converted. I haven't tested this idea, so I might be missing something that prevents this flow from working as intended.