Conversation
hsemenenko
left a comment
There was a problem hiding this comment.
Here are some initial comments. I think we need some discussion about this fits into an architecture, especially if we expect other rz implementations to be added in the future.
I haven't reviewed the tests in detail.
| /// S dagger gate. | ||
| sdg, | ||
| /// Rz gate with angle in radians. | ||
| adaptive_rz, |
There was a problem hiding this comment.
I'm not convinced that the HUGR op should be called adaptive_rz. Could it be a more generic rz op in the HUGR extension, that is then implemented with the adaptive routine? What I don't want is requiring a new rz op for every different implementation.
| @custom_type( | ||
| rotation().get_type("rotation").instantiate([]), copyable=True, droppable=True | ||
| ) | ||
| class _Rotation: | ||
| """TKET rotation argument used by the Rz encoder.""" | ||
|
|
||
| @hugr_op(lambda ty, _inst, _ctx: ExtOp(rotation().get_op("to_halfturns"), ty, [])) | ||
| @no_type_check | ||
| def halfturns(self: "_Rotation") -> float: ... |
| emit ``adaptive_rz_dephasing_limit_hit=True`` once per affected call. | ||
| The round limit still stops execution with an error. | ||
|
|
||
| These settings do not add simulator noise. |
There was a problem hiding this comment.
This seems like an odd comment and not required.
| """Settings for adaptive Steane rotations. | ||
|
|
||
| Attributes: | ||
| tolerance: Allowed residual angle in radians. |
There was a problem hiding this comment.
epsilon seems more appropriate than tolerance
| if -tolerance <= remaining and remaining <= tolerance: | ||
| return total_dephasing | ||
| if rounds == max_rounds: | ||
| panic("Adaptive Rz exceeded max_rounds") |
There was a problem hiding this comment.
Is panic right here? I think the difference between exit and panic is that exit will continue to the next shot, while panic will exit and not continue to future shots.
| # NaN and infinity both make this subtraction NaN. | ||
| if phase - phase != 0.0: | ||
| panic("Adaptive Rz requires a finite angle") |
There was a problem hiding this comment.
I don't think this is necessary in Guppy.
|
|
||
| @guppy | ||
| @no_type_check | ||
| def rotate_and_correct_rz(blk: LogicalBlock[7], physical_angle: float) -> bool: |
There was a problem hiding this comment.
Could this be made private? It's only used here and in tests. Do we expect users to need it?
| blk: LogicalBlock[7], | ||
| phase: float, | ||
| tolerance: float, | ||
| max_rounds: int, | ||
| dephasing: float, | ||
| max_dephasing: float, | ||
| abort_on_dephasing: bool, |
There was a problem hiding this comment.
Which of these arguments will be variable at runtime, and which will be statically known at compile time? We should consider marking @ comptime for args that will be known at compile time to reduce runtime computation.
| if abort_on_dephasing: | ||
| panic("Adaptive Rz exceeded max_dephasing") | ||
| if not dephasing_limit_hit: | ||
| output("adaptive_rz_dephasing_limit_hit", True) |
There was a problem hiding this comment.
Primitives shouldn't include output.
| output("adaptive_rz_dephasing_limit_hit", True) |
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class AdaptiveRzConf: |
There was a problem hiding this comment.
If we expect other Rz implementations to be added in the future, we could consider having a more general RzConf instead.
| lo = -float(pi) / 2.0 | ||
| hi = float(pi) / 2.0 | ||
| ratio = y / x | ||
| for _ in range(52): |
There was a problem hiding this comment.
Instead of always repeating 52 times, this could be made more efficient by iterating until two successive values of mid are close enough (within 1e-15 or so).
However, copying the implementation from go.dev (as we do for tan) would be much more efficient still.
Adds adaptive Rz synthesis to the Steane encoder, enabled with
SteaneBuilder().with_adaptive_rz(...). This supports small, arbitrary rotation angles through ordinary Guppyrzgates or direct logicaladaptive_rzcalls to a Steane code blck.Each round applies physical rotations and checks the error syndromes, adapting the next coherent angle induced depending on the syndrome measured.
AdaptiveRzConflets users set:adaptive_rz_dephasing_limit_hitand continues. The maximum round limit still stops execution in either mode.The implementation draws on:
The noise model assumes independent Z errors and ideal Clifford gates and syndrome measurements. Setting
dephasingdoes not add simulation noise or similar, but just informs the controller what to expect to try to account for accidental overrotation.