microquantum.stdlib.numbers¶
Standard library: numeric and angle utilities (Phase 117).
Small, dependency-light helpers for the angle arithmetic that shows up everywhere in quantum programming (rotation gates, phase accumulation, gradient shifts):
mod_2pi()— reduce an angle to its canonical representative in[0, 2*pi).wrap_angle()— symmetric principal value in[-pi, pi].is_angle_close()— equality modulo2*piwithin a tolerance (two rotations are equivalent when their angles differ by a full turn).is_identity_angle()— whether a rotation by the angle is the identity (its reduced angle is0modulo2*pi).
Numpy scalar types are accepted and coerced to float; NaN and infinities
are rejected. Complex numbers and strings are rejected.
Module Contents¶
- microquantum.stdlib.numbers.mod_2pi(angle)[source]¶
Reduce an angle to its canonical representative in
[0, 2*pi).- Parameters:
angle (Any) – Any real-valued angle (int, float or numpy scalar).
- Returns:
anglemodulo2*piin[0, 2*pi).0.0stays0.0.- Raises:
TypeError – If
angleis complex or not a real number.ValueError – If
angleis NaN or infinite.
- Return type:
- microquantum.stdlib.numbers.wrap_angle(angle)[source]¶
Reduce an angle to its symmetric principal value in
[-pi, pi].Uses
math.remainder(), so the result is the numerically closest representative modulo2*pi:0.0maps to0.0, angles just belowpistay positive and angles just abovepibecome slightly negative.- Parameters:
angle (Any) – Any real-valued angle (int, float or numpy scalar).
- Returns:
The principal value in
[-pi, pi].- Raises:
TypeError – If
angleis complex or not a real number.ValueError – If
angleis NaN or infinite.
- Return type:
- microquantum.stdlib.numbers.is_angle_close(left, right, *, tol=1e-10)[source]¶
Check whether two angles are equal modulo
2*piwithintol.Two angles are close when rotating either by any whole number of full turns brings them within
tolof each other — i.e. the rotations they describe are the same.- Parameters:
left (Any) – First real-valued angle.
right (Any) – Second real-valued angle.
tol (float) – Absolute tolerance on the modulo-
2*pidifference.
- Returns:
Truewhen the angles describe equivalent rotations.- Raises:
TypeError – If an angle is complex or not a real number.
ValueError – If an angle is NaN/infinite, or
tolis not a real non-negative number.
- Return type:
- microquantum.stdlib.numbers.is_identity_angle(angle, *, tol=1e-09)[source]¶
Check whether a rotation by
angleequals the identity.A rotation is the identity when its reduced angle is
0modulo2*pi(including a full turn), up to tolerancetol.- Parameters:
angle (Any) – Any real-valued angle.
tol (float) – Absolute tolerance applied against the reduced angle.
- Returns:
Truewhen rotating byangleis (approximately) the identity.- Raises:
TypeError – If
angleis complex or not a real number.ValueError – If
angleis NaN/infinite, ortolis not a real non-negative number.
- Return type: