Skip to content

added Z2 integer support#42

Open
a-b-h-a-y-s-h-i-n-d-e wants to merge 12 commits into
deepforestsci:mainfrom
a-b-h-a-y-s-h-i-n-d-e:Z2_type
Open

added Z2 integer support#42
a-b-h-a-y-s-h-i-n-d-e wants to merge 12 commits into
deepforestsci:mainfrom
a-b-h-a-y-s-h-i-n-d-e:Z2_type

Conversation

@a-b-h-a-y-s-h-i-n-d-e
Copy link
Copy Markdown
Contributor

@a-b-h-a-y-s-h-i-n-d-e a-b-h-a-y-s-h-i-n-d-e commented Apr 3, 2026

This PR adds support for Z2 modulo 2 integer, along with support for XOR, AND operations and boolean flags

example code:

a: Z2 = 1
b: Z2 = 0

# XOR
a + b

# AND
a * b

@a-b-h-a-y-s-h-i-n-d-e a-b-h-a-y-s-h-i-n-d-e force-pushed the Z2_type branch 2 times, most recently from d6b0a34 to 684f2df Compare April 9, 2026 15:43
@JoseAntonioSiguenza
Copy link
Copy Markdown
Collaborator

Please provide a brief description of changes

Comment thread physika/z2.py
# Draft version class


class Z2:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add docstrings and type annotations

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

Comment thread physika/z2.py

Examples
--------
>>> a = Z2(1)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add imports from physika

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

Comment thread physika/z2.py

Returns
-------
None
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to type annotations this returns "Z2", please fix

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

Comment thread physika/z2.py Outdated
"""
return bool(self.val)

def __int__(self) -> int:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are two init methods.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is __int__ method, but now I have removed because we want both types to be Z2 only for doing operations.

Comment thread physika/z2.py
@@ -0,0 +1,121 @@
from typing import Union
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add unit ttests

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

Copy link
Copy Markdown
Contributor

@rbharath rbharath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@a-b-h-a-y-s-h-i-n-d-e Make sure to add rst docs of the new features explaining and providing examples

@a-b-h-a-y-s-h-i-n-d-e
Copy link
Copy Markdown
Contributor Author

image image

@a-b-h-a-y-s-h-i-n-d-e a-b-h-a-y-s-h-i-n-d-e changed the title [DRAFT] added Z2 integer support added Z2 integer support Apr 15, 2026
Comment thread examples/integer_modulo_two.phyk Outdated
# ----------------
# XOR operation
# ----------------
xor_opposite = a + b
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add the types of these decl expressions: xor_opposite, xor_same, and_mixed, etc

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

import torch.optim as optim

from physika.runtime import physika_print
from physika.z2 import Z2
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we want physika.z2 as an import in runtime, instead as a physika type to perform modulo operations with.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this make sense, should I create a new type for Z2 just like we are doing in utils/types.py
e.g:

# Ground scalar types
T_REAL = TScalar("ℝ")
T_NAT = TScalar("ℕ")
T_COMPLEX = TScalar("ℂ")
T_STRING = TScalar("string")

and then handle explicitly in utils/type_checker_utils.py

Copy link
Copy Markdown
Contributor Author

@a-b-h-a-y-s-h-i-n-d-e a-b-h-a-y-s-h-i-n-d-e Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JoseAntonioSiguenza can you check the last commit which I made, I have added Z2 as a standalone type, but since we have z2.py I have to add that in runtime, but in generated code it is not getting included now

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think is better now Z2 as it type, please lets integrate this new type with exisiting ones and test it for type checker.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a new PR for type system integration is better to keep this PR with less changes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay great

return T_COMPLEX
if ts == "string":
return T_STRING
if ts in ("ℤ2", "Z2"):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include this at test_type_checker_utils.py

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

Comment thread physika/utils/types.py
T_NAT = TScalar("ℕ")
T_COMPLEX = TScalar("ℂ")
T_STRING = TScalar("string")
T_Z2 = TScalar("ℤ2")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add this new type to existing tests

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

@a-b-h-a-y-s-h-i-n-d-e
Copy link
Copy Markdown
Contributor Author

@JoseAntonioSiguenza , I have also added Z2 in Types section in language.rst file, we do have separate Z2 examples in same file

image

Comment thread physika/runtime.py
import torch.optim as optim

from physika.utils.print_utils import _from_torch, _infer_type
from physika.z2 import Z2 # noqa
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I think having just this is not adding much. Lets remove Z2 from runtime completly (we can call later if needed)

@JoseAntonioSiguenza
Copy link
Copy Markdown
Collaborator

JoseAntonioSiguenza commented Apr 24, 2026

Great work!! This PR got pretty large, so lets split in 2 to facilitate final review:

  • main Z2 changes PR
  • Z2 type integration PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants