@@ -87,13 +87,15 @@ def signature(self) -> 'Signature':
8787 def on_classical_vals (
8888 self , x : 'ClassicalValT' , y : 'ClassicalValT'
8989 ) -> Dict [str , 'ClassicalValT' ]:
90- if not (0 <= x < self .mod ):
90+ # The construction still works when at most one of inputs equals `mod`.
91+ special_case = (x == self .mod ) ^ (y == self .mod )
92+ if not (0 <= x < self .mod or special_case ):
9193 raise ValueError (
92- f'{ x = } is outside the valid interval for modular addition [0, { self .mod } ) '
94+ f'{ x = } is outside the valid interval for modular addition [0, { self .mod } ] '
9395 )
94- if not (0 <= y < self .mod ):
96+ if not (0 <= y < self .mod or special_case ):
9597 raise ValueError (
96- f'{ y = } is outside the valid interval for modular addition [0, { self .mod } ) '
98+ f'{ y = } is outside the valid interval for modular addition [0, { self .mod } ] '
9799 )
98100
99101 y = (x + y ) % self .mod
@@ -320,7 +322,7 @@ def on_classical_vals(
320322
321323 if not (0 <= x < self .mod ):
322324 raise ValueError (
323- f'{ x = } is outside the valid interval for modular addition [0, { self .mod } ) '
325+ f'{ x = } is outside the valid interval for modular addition [0, { self .mod } ] '
324326 )
325327
326328 x = (x + self .k ) % self .mod
@@ -508,13 +510,15 @@ def on_classical_vals(
508510 if ctrl != self .cv :
509511 return {'ctrl' : ctrl , 'x' : x , 'y' : y }
510512
511- if not (0 <= x < self .mod ):
513+ # The construction still works when at most one of inputs equals `mod`.
514+ special_case = (x == self .mod ) ^ (y == self .mod )
515+ if not (0 <= x < self .mod or special_case ):
512516 raise ValueError (
513- f'{ x = } is outside the valid interval for modular addition [0, { self .mod } ) '
517+ f'{ x = } is outside the valid interval for modular addition [0, { self .mod } ] '
514518 )
515- if not (0 <= y < self .mod ):
519+ if not (0 <= y < self .mod or special_case ):
516520 raise ValueError (
517- f'{ y = } is outside the valid interval for modular addition [0, { self .mod } ) '
521+ f'{ y = } is outside the valid interval for modular addition [0, { self .mod } ] '
518522 )
519523
520524 y = (x + y ) % self .mod
0 commit comments