Quantum Arithmetic Operator Constraints¶
This document details the constraints of the quantum arithmetic operators pre-implemented in QRAM-Simulator, their unitarity guarantees, and their usage rules.
Contents¶
Overview¶
QRAM-Simulator adopts the “Register Level Programming” paradigm: every operator acts directly on registers. Each operator is carefully designed to preserve the quantum unitary property.
Key Concepts¶
Out-of-place operations: the result is stored in a separate output register; unitarity is guaranteed via XOR
In-place operations: the result modifies the input register directly; an explicit dagger method is required to guarantee reversibility (Section 6 (the
_InPlacecontract))SelfAdjoint operators: satisfy U† = U; applying the operator twice is the identity
Width and Truncation Conventions¶
This section is the authoritative contract for the width behavior of every arithmetic operator (established 2026-09). Principle: semantics are carried by the operator name, not by the build mode; validation of register declared types runs only as an additional step in debug builds.
LSB alignment: all operands and outputs are aligned at the least significant bit; there are no implicit shifts.
Read extension is decided by the name slots: a
_UInt_slot zero-extends the operand into the computation domain; an_SInt_slot sign-extends in two’s complement; a_Bool_slot reads a single bit; anAnyIntslot extends according to the register’s declared type (UInt → zero-extension, SInt → sign-extension). The extension logic is hard-coded, so release builds behave identically. (mirrored by the slot-carried semantics rule in the naming conventions)Computation domain: intermediate quantities are evaluated at full precision — multiplication goes through a 64-bit high/low-half decomposition (equivalent to 128-bit precision), addition/subtraction/comparison hold equivalently over the unsigned 64-bit wrapping domain, and the quotient domain of division/square root is ≤ 64 bits.
Write: the result is reduced
mod 2^out_widthand then XOR-ed into the output register. The output width may differ from any input width; the values of input registers are never modified by an out-of-place operator.Totalization outside the domain: reversibility requires the operator to be a deterministic function on every basis state, so the core operators never raise domain exceptions —
Div_UInt_UIntreturns quotient 0 when the divisor is zero;Sqrt_UIntaccepts only magnitude (non-negative) inputs. Domain/overflow information is invariably reported by dedicated flag operators.Flag predicates: flag outputs are XOR-ed into 1-bit Boolean registers; predicates are evaluated over the full-precision domain. Flag operators that take
outas a parameter (Carry_UInt_UIntetc.) take only that register’s width and never read its value, so they make no assumption about the initial content of the output register. (predicate-operator naming follows Section 4 of the naming conventions)Width bounds: 1…64 are all supported. Every
pow2(w)/1<<wundefined behavior at w=64 is replaced bywidth_mask(with a special case for 64); operators always read widths at execution time viaSystem::size_ofand never snapshot them at construction time.Aliasing constraint: the output (including flags) must not alias any input — an always-on check, independent of debug/release.
Exceptions to the contract:
GetMid_UInt_UIntandSwap_General_Generalkeep their equal-width requirement (the semantics of midpoint overflow and whole-register exchange inherently depend on equal widths); they are the only two exceptions to this contract.Legacy compatibility: the existing Add family (zero-extended reads + writes
mod 2^dst) already conforms to this contract. The only historical behavior correction isAdd_AnyInt_AnyInt_InPlace: from this contract onward, the AnyInt slot extends according to the register’s declared type (previously it was always read as an unsigned bit pattern), and thelhs == rhsaliasing rejection plus execution-time width reads were added.
Operator Classification¶
1. Out-of-place Operators (SelfAdjointOperator)¶
Operator |
Operation |
Input Type |
Output Type |
Constraints |
|---|---|---|---|---|
res ^= lhs + rhs |
UnsignedInteger |
UnsignedInteger |
all register sizes arbitrary, result truncated |
|
res ^= lhs + const |
UnsignedInteger |
UnsignedInteger |
all register sizes arbitrary, result truncated |
|
res ^= lhs * const |
UnsignedInteger |
UnsignedInteger |
const should be odd to keep the map bijective |
|
reg2 ^= reg1 |
any |
same as reg1 |
widths arbitrary, reg2 truncated to mod 2^reg2_w |
|
outputs comparison flags |
UnsignedInteger |
Boolean |
output register size is 1 |
|
outputs the less-than flag |
UnsignedInteger |
Boolean |
output register size is 1 |
|
|
mid ^= (l+r)/2 |
UnsignedInteger |
UnsignedInteger |
the three registers must have the same size |
reg ^= ~reg |
UnsignedInteger/SignedInteger |
- |
negates all bits |
|
swaps a single bit |
any |
any |
bit index within the valid range |
|
|
swaps entire registers |
any |
any |
the two registers must have the same size |
res ^= arccos(√(l/r)) |
UnsignedInteger |
Rational |
lhs < rhs |
|
res ^= arccos(l/√r) |
SignedInteger, UnsignedInteger |
Rational |
|lhs| ≤ √rhs |
|
res ^= atan2(r,l)/2π |
integer types |
Rational |
result in the range [0,1) |
|
res ^= lhs − rhs |
UnsignedInteger |
UnsignedInteger |
widths arbitrary (generic contract row) |
|
res ^= 0 − reg |
UnsignedInteger |
UnsignedInteger |
widths arbitrary |
|
res ^= |sext(reg)| |
SignedInteger |
UnsignedInteger |
two’s-complement minimum wraps to itself |
|
res ^= lhs · rhs |
UnsignedInteger |
UnsignedInteger |
widths arbitrary, result mod 2^res_w |
|
|
res ^= lhs // rhs |
UnsignedInteger |
UnsignedInteger |
division by zero → quotient 0 (totalization outside the domain) |
|
res ^= isqrt(reg) |
UnsignedInteger |
UnsignedInteger |
integer square root (floor) |
res ^= cond ? lhs : rhs |
Boolean, UInt×2 |
UnsignedInteger |
cond width is 1 |
|
bitwise AND/OR/XOR |
UnsignedInteger×2 |
UnsignedInteger |
widths arbitrary |
|
flag ^= sext(lhs) < sext(rhs) |
SignedInteger×2 |
Boolean |
signed comparison |
|
|
flag ^= lhs+rhs ≥ 2^res_w |
UnsignedInteger×2 |
Boolean |
res only supplies its width; never read or written |
flag ^= signed addition overflow |
SignedInteger×2 |
Boolean |
res only supplies its width; evaluated internally at the res width |
|
flag ^= lhs·rhs ≥ 2^res_w (full precision) |
UnsignedInteger×2 |
Boolean |
res only supplies its width |
|
flag ^= reg == 0 |
UnsignedInteger |
Boolean |
— |
|
flag ^= sext(reg) < 0 |
SignedInteger |
Boolean |
— |
|
res ^= func(inputs) |
any |
any |
func must be a deterministic function |
2. In-place Operators (BaseOperator)¶
Operator |
Operation |
Input Type |
Dagger Implementation |
Constraints |
|---|---|---|---|---|
rhs += lhs |
UnsignedInteger |
rhs += (2^N - lhs) |
widths arbitrary; lhs zero-extended, rhs wraps mod 2^rhs_w |
|
res += lhs * const (lhs unchanged) |
UnsignedInteger |
res -= lhs * const |
lhs unchanged, only res is updated |
|
reg += const |
UnsignedInteger/SignedInteger |
reg += (2^N - const) |
wraps mod 2^N |
|
y = y * a^(2^x) mod N |
UnsignedInteger |
y = y * a^(-2^x) mod N |
gcd(a, N) = 1, register ≥ ⌈log₂(N)⌉ |
|
|
lhs += rhs |
integer types |
lhs -= rhs (mod 2^N) |
mixed types; rhs extends per its declared type (SInt sign-extends); lhs≠rhs |
rotate left |
UnsignedInteger/SignedInteger |
rotate right by the same amount |
shift amount ≤ register size |
|
rotate right |
UnsignedInteger/SignedInteger |
rotate left by the same amount |
shift amount ≤ register size |
Detailed Operator Descriptions¶
Add_UInt_UInt¶
Operation: res ^= lhs + rhs (out-of-place)
Unitarity guarantee: realized through XOR. res ⊕ (lhs+rhs) ⊕ (lhs+rhs) = res
Constraints:
lhs,rhs,resmust be of UnsignedInteger typeall registers must be active
the sum is truncated to the size of the
resregister
Example:
auto lhs = System::add_register("lhs", UnsignedInteger, 4);
auto rhs = System::add_register("rhs", UnsignedInteger, 4);
auto res = System::add_register("res", UnsignedInteger, 4);
Init_Unsafe(lhs, 3);
Init_Unsafe(rhs, 5);
// res = 0 ⊕ (3 + 5) = 8
Add_UInt_UInt("lhs", "rhs", "res");
Add_UInt_UInt_InPlace¶
Operation: rhs += lhs (mod 2^N) (in-place)
Unitarity guarantee: the dagger is realized through modular arithmetic. rhs = (rhs + (2^N - lhs)) mod 2^N restores the original value.
Constraints:
lhs,rhsmust be of UnsignedInteger typeusing registers of the same size is strongly recommended
all registers must be active
Overflow behavior: the result wraps modulo 2^N at the size of the rhs register
Example:
auto lhs = System::add_register("lhs", UnsignedInteger, 4);
auto rhs = System::add_register("rhs", UnsignedInteger, 4);
Init_Unsafe(lhs, 7);
Init_Unsafe(rhs, 3);
// rhs = (3 + 7) % 16 = 10
Add_UInt_UInt_InPlace("lhs", "rhs");
// dagger: rhs = (10 + 9) % 16 = 3 (restores the original value)
op.dag(state);
ShiftLeft_InPlace / ShiftRight_InPlace¶
Operation: circular shift
Unitarity guarantee: a circular shift is a bijection; ShiftLeft_InPlace and ShiftRight_InPlace are each other’s dagger. ShiftLeft_InPlace::dag() invokes ShiftRight_InPlace, and vice versa.
Constraints:
the register must be of UnsignedInteger or SignedInteger type
the shift amount must be ≤ the register size
a shift amount equal to the size is the identity operation
Example:
auto reg = System::add_register("reg", UnsignedInteger, 4);
Init_Unsafe(reg, 0b1010); // 10
// rotate left by 1: 0b0101 = 5
ShiftLeft_InPlace("reg", 1);
// rotate right by 1 returns to the original value
ShiftRight_InPlace("reg", 1);
// or use .dag():
ShiftLeft_InPlace("reg", 1).dag(state); // undo
Mult_UInt_ConstUInt¶
Operation: res ^= lhs * mult (out-of-place)
Unitarity guarantee: realized through XOR. Note: the multiplication is bijective only when mult is coprime to 2^N.
Constraints:
lhs,resmust be of UnsignedInteger typefor unitarity,
multshould be odd (coprime to 2^N)the product is truncated to the size of the
resregister
Warning: if mult is even, the multiplication is not bijective and can lose information — multiplying by 2, for example, loses the lowest bit.
Assign¶
Operation: reg2 ^= reg1 (out-of-place)
Unitarity guarantee: realized through XOR; it is a self-adjoint operation.
Semantics note: this is not an assignment in the classical sense. If reg2 starts at 0, the effect is a copy; otherwise it is an XOR update.
Constraints:
the two registers must have the same size
the types may differ (the bit pattern is copied)
Compare_UInt_UInt¶
Operation: |l>|r>|0>|0> → |l>|r>|l<r?>|l==r?>
Unitarity guarantee: the flags are set through XOR; it is a self-adjoint operation.
Constraints:
left,rightmust be UnsignedIntegercompare_less,compare_equalmust be Boolean (size 1)
CustomArithmetic¶
Operation: outputs ^= func(inputs) (out-of-place)
Unitarity guarantee: realized through XOR, but the user must ensure that func is deterministic.
Constraints:
funcmust be a pure function (the same inputs always produce the same outputs)funcmust not have side effectsthe user is responsible for the correctness of
func
Unitarity Guarantee Mechanisms¶
1. The XOR Mechanism (Out-of-place)¶
All out-of-place operators write their results via XOR:
output.value ^= compute_result(input_values);
Because x ⊕ y ⊕ y = x, applying the operation twice restores the original value, guaranteeing U† = U.
2. The Modular-Arithmetic Mechanism (In-place)¶
In-place operators guarantee reversibility through modular arithmetic:
Forward operation:
reg.value = (reg.value + value) % (1ULL << N);
Dagger operation:
reg.value = (reg.value + ((1ULL << N) - value)) % (1ULL << N);
Because (x + y) + (2^N - y) ≡ x (mod 2^N), the operation and its dagger cancel out.
3. Bijection Verification¶
For out-of-place operators, the verify_outofplace_unitarity template verifies:
for all possible inputs, apply the operation twice
check that the result equals the original state
For in-place operators, the verify_inplace_unitarity template verifies:
for all possible inputs, apply the operation and then the dagger
check that the result equals the original state
Type Safety and Overflow Behavior¶
Debug-Mode Checks¶
In any mode other than QRAM_Release, every operator constructor checks:
whether the input/output register types meet the requirements
whether the register sizes match (for operations that require matching)
whether the operands are within the valid range
Overflow Behavior¶
Addition/subtraction: wraps mod 2^N
Multiplication: truncated to the output register size
Shifts: circular shifts (overflow bits wrap around to the other side)
Type Conversions¶
UnsignedInteger: the raw value is used directly
SignedInteger: two’s-complement representation
Rational: encoded as a fixed-point fraction
Boolean: treated as a 1-bit UnsignedInteger
Usage Recommendations¶
1. Choosing Register Sizes¶
make sure the output register has enough bits to hold the result
for in-place operations, registers of the same size are strongly recommended
consider how overflow behavior affects the correctness of your algorithm
2. Unitarity Verification¶
use the provided test templates to verify the unitarity of custom operators
for critical operations, verify state normalization with
CheckNormalizationinside the algorithm
3. Performance¶
debug-mode type checks carry runtime overhead; Release mode removes them automatically
out-of-place operations are usually easier to optimize than in-place ones
consider
CustomArithmeticto reduce register allocation
4. Common Pitfalls¶
Non-bijective multiplication: an even multiplier loses information
In-place size mismatch: values in the smaller register are truncated
XOR semantics: the output of an out-of-place operator is XOR-ed into the target register; it is not a plain assignment
Signed overflow: signed integer overflow is undefined behavior in C++; use it with care