@@ -87,13 +87,15 @@ def signature(self) -> 'Signature':
87
87
def on_classical_vals (
88
88
self , x : 'ClassicalValT' , y : 'ClassicalValT'
89
89
) -> 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 ):
91
93
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 } ] '
93
95
)
94
- if not (0 <= y < self .mod ):
96
+ if not (0 <= y < self .mod or special_case ):
95
97
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 } ] '
97
99
)
98
100
99
101
y = (x + y ) % self .mod
@@ -320,7 +322,7 @@ def on_classical_vals(
320
322
321
323
if not (0 <= x < self .mod ):
322
324
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 } ] '
324
326
)
325
327
326
328
x = (x + self .k ) % self .mod
@@ -508,13 +510,15 @@ def on_classical_vals(
508
510
if ctrl != self .cv :
509
511
return {'ctrl' : ctrl , 'x' : x , 'y' : y }
510
512
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 ):
512
516
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 } ] '
514
518
)
515
- if not (0 <= y < self .mod ):
519
+ if not (0 <= y < self .mod or special_case ):
516
520
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 } ] '
518
522
)
519
523
520
524
y = (x + y ) % self .mod
0 commit comments