-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add noise amplification transformer #6665
Conversation
Co-authored-by: Noureldin <[email protected]>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #6665 +/- ##
=======================================
Coverage 97.82% 97.82%
=======================================
Files 1066 1068 +2
Lines 91864 91919 +55
=======================================
+ Hits 89862 89917 +55
Misses 2002 2002 ☔ View full report in Codecov by Sentry. |
self, | ||
p: float | Mapping[tuple[ops.Qid, ops.Qid], float], | ||
target_gate: ops.Gate = ops.CZ, | ||
rng: np.random.Generator | None = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you move rng
to be an argument of __call__
?
Returns: | ||
The transformed circuit. | ||
|
||
Raises: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you move the validation of p
to the constructor? ... this way you don't need to do it here
import numpy as np | ||
|
||
|
||
def _gate_in_moment(gate: ops.Gate, moment: circuits.Moment) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit for performance:
return any(op.gate == gate for op in moment)
transformed_circuit_p1 = na.DepolerizingNoiseTransformer(1.0)(circuit) | ||
assert len(transformed_circuit_p1) == 20 | ||
transformed_circuit_p0_03 = na.DepolerizingNoiseTransformer(0.03)(circuit) | ||
assert 10 <= len(transformed_circuit_p0_03) <= 20 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't the expected len of the circuit num_moment * (1 + p) ? so here we expect ~$10 * 1.03 = 13$ and 20 correspends to the extreme
"p must either be a float or a mapping from" # pragma: no cover | ||
+ "sorted qubit pairs to floats" # pragma: no cover | ||
) # pragma: no cover | ||
self.p = p |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can resolve p
here and then use it in call as self.p_func(pair)
self.p = p | |
self.p_func = lambda _: p if isinstance(p, (int, float)) else lambda pair: p.get(pair, 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with one optional suggestion
p_i = self.p_func(pair_sorted_tuple) | ||
apply = rng.choice([True, False], p=[p_i, 1 - p_i]) | ||
if apply: | ||
choices = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[optional suggestion] this creates 17 objects for each pair, 16 pauli pairs + the numpy array. consider doing it this way
pauli_a_idx = np.choice(4)
if pauli_a_idx == 0:
pauli_b_idx = np.choice(3) + 1
else:
pauli_b_idx = np.choice(4)
paulit_to_apply = paulis[pauli_a_idx](pair[0]), paulis[pauli_b_idx](pair[1])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, Nour. I agree that this would be more efficient code, but I'm going to leave it as is for now so that I can move on. (It is already very fast as written.)
* Add noise amplification transformer * add _gate_in_moment * add copyright notice * add raises documentation, remove import cirq, fix coverage * transformer api * * after `circuit` input Co-authored-by: Noureldin <[email protected]> * format * update test * Convert to a class * types and lint * suggestions from Nour * lint * suggestion from Nour * add () * types * lint --------- Co-authored-by: Noureldin <[email protected]>
Adds a tool for adding depolarizing noise after the specified gate, which can be used on hardware for probabilistic error amplification and zero noise extrapolation.