Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/unitary_hack/issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
- [Improvements to the API docs](https://github.com/PsiQ/bartiq/issues/193)
- [Add function for calculating `circuit_volume`](https://github.com/PsiQ/bartiq/issues/150)
- [Fix displaying resource name `local_highwater`.](https://github.com/PsiQ/bartiq/issues/163)
- [Passing a non-integer into nlz results in unhelpful error message.](https://github.com/PsiQ/bartiq/issues/180)
- [Passing a non-integer into ntz results in unhelpful error message.](https://github.com/PsiQ/bartiq/issues/180)
- [Improve visualisation methods in Bartiq](https://github.com/PsiQ/bartiq/issues/188)
- [Add convenience functions for finding subroutines](https://github.com/PsiQ/bartiq/issues/185)
22 changes: 18 additions & 4 deletions src/bartiq/symbolics/sympy_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import operator
import warnings
from typing import Any

from sympy import (
Expand Down Expand Up @@ -143,30 +144,42 @@ def eval(cls, p, n):
return orig_multiplicity(p, n) if isinstance(p, Integer) and isinstance(n, Integer) else None


class nlz(Function):
class ntz(Function):
@classmethod
def eval(cls, n):
"""
Returns the number of trailing zeros in the binary representation of n.
Only defined for non-negative integers.
Returns 0 for input 0.
For symbolic input, returns unevaluated nlz(n).
For symbolic input, returns unevaluated ntz(n).
Raises:
TypeError: If input is not an integer (when numeric).
ValueError: If input is a negative integer.
"""
# Numeric evaluation
if n.is_number:
if not n.is_integer:
raise TypeError(f"nlz requires integer argument; found {n}")
raise TypeError(f"ntz requires integer argument; found {n}")
n = int(n)
if n < 0:
raise ValueError(f"nlz requires non-negative integer; found {n}")
raise ValueError(f"ntz requires non-negative integer; found {n}")
if n == 0:
return 0
return (n & -n).bit_length() - 1


class nlz(ntz):
"""Deprecated alias for ntz; use ntz instead."""

def __new__(cls, n, *args, **kwargs):
warnings.warn(
"nlz is deprecated and will be removed in a future release; use ntz instead",
DeprecationWarning,
stacklevel=2,
)
return super().__new__(cls, n, *args, **kwargs)


class Max(Function):
"""A custom implementation of Max.

Expand Down Expand Up @@ -242,6 +255,7 @@ def eval(cls, *args):
"gamma": gamma,
"heaviside": Heaviside,
"multiplicity": multiplicity,
"ntz": ntz,
"nlz": nlz,
}

Expand Down
Loading