Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update classical action of addition gates and fix classical action bug in Join #1174

Merged
merged 7 commits into from
Jul 23, 2024
Merged

Update classical action of addition gates and fix classical action bug in Join #1174

merged 7 commits into from
Jul 23, 2024

Conversation

NoureldinYosri
Copy link
Contributor

No description provided.


Addition of signed integers can result in overflow. In most classical programming languages (e.g. C++)
what happens when an overflow happens is left as an implementation detail for compiler designers.
However for quantum subtraction the operation should be unitary and that means that the unitary of
Copy link
Collaborator

Choose a reason for hiding this comment

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

wrap lines

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ptal



def signed_addition(a: int, b: int, N: int, is_signed: bool) -> int:
"""Performs addition mod N of (un)signed in a reversible way.
Copy link
Collaborator

Choose a reason for hiding this comment

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

addition of signed? recommend: addition of two integers

def test_signed_addition_signed(x, y, n_bits):
half_n = 1 << (n_bits - 1)
# Addition of signed ints `x` and `y`` is a cyclic rotation of the interval [-2^(n-1), 2^n) by `y`.
R = [*range(-(2 ** (n_bits - 1)), 2 ** (n_bits - 1))]
Copy link
Collaborator

Choose a reason for hiding this comment

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

is there a good reason why we're not using pep-8 variable names here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

renamed

@@ -265,3 +265,20 @@ def format_classical_truth_table(
for invals, outvals in truth_table
]
return '\n'.join([heading] + entries)


def signed_addition(a: int, b: int, N: int, is_signed: bool) -> int:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Args section to document the arguments; I'd suggest renaming N to mod, and I'd suggest changing mod and is_signed to be keyword-only arguments (by putting ..., *, mod, is_signed). Baring that, I would strongly recommend using keyword args when calling this function to make the flags obvious

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done ... though I prefer to pass num_bits rather than N or mod

@@ -95,7 +94,7 @@ def my_tensors(
]

def on_classical_vals(self, reg: 'NDArray[np.uint]') -> Dict[str, int]:
return {'reg': bits_to_ints(reg)[0]}
return {'reg': self.dtype.from_bits(reg.tolist())}
Copy link
Collaborator

Choose a reason for hiding this comment

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

why is tolist required?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mypy

Copy link
Collaborator

Choose a reason for hiding this comment

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

it doesn't recognize ndarray as Sequence?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

expects Sequence[int] but got ndarray[np.int64]

Copy link
Collaborator

@mpharrigan mpharrigan left a comment

Choose a reason for hiding this comment

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

Thanks! Some style things and there's a failing test but then I think this is a nice improvement!

@NoureldinYosri
Copy link
Contributor Author

NoureldinYosri commented Jul 23, 2024

@mpharrigan I modified my change to the classical action of Join for QFxp to retain the old behaviour ... the behaviour for QFxp will be fixed by #1142

@NoureldinYosri NoureldinYosri enabled auto-merge (squash) July 23, 2024 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants