-
Notifications
You must be signed in to change notification settings - Fork 558
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
Implement qml.equal
comparison for BasisRotation
#4893
Labels
good first issue
Good for newcomers
Comments
I would like to work on this if you don't mind |
I've assigned you the issue @minhtriet . Feel free to combine this and #4894 if that is easiest for you. |
albi3ro
added a commit
that referenced
this issue
Dec 12, 2023
**Context:** Add comparison for `BasisRotation` **Description of the Change:** **Benefits:** **Possible Drawbacks:** As correctly stated in #4893, hashing a numpy array is not possible. One potential solution is to write a custom `_flatten()` and `_unflatten()` for BasisRotation, in which the matrix is serialized/deserialized to/from JSON. Specifically ```python rotation_mat = np.array( [ [-0.618452, -0.68369054 - 0.38740723j], [-0.78582258, 0.53807284 + 0.30489424j], ] ) # serialize u = ';'.join(str(__) for _ in rotation_mat for __ in _) # gives '(-0.618452+0j);(-0.68369054-0.38740723j);(-0.78582258+0j);(0.53807284+0.30489424j)' # deserialize v = [complex(_) for _ in u.split(';')] np.array(v).reshape([int(np.sqrt(len(v)))]*2) ``` This relies on the fact that rotational matrix is small and a square matrix **Related GitHub Issues:** #4893 @albi3ro --------- Co-authored-by: Christina Lee <chrissie.c.l@gmail.com> Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com> Co-authored-by: Christina Lee <christina@xanadu.ai>
Closed by #4919 |
mudit2812
pushed a commit
that referenced
this issue
Jan 19, 2024
**Context:** Add comparison for `BasisRotation` **Description of the Change:** **Benefits:** **Possible Drawbacks:** As correctly stated in #4893, hashing a numpy array is not possible. One potential solution is to write a custom `_flatten()` and `_unflatten()` for BasisRotation, in which the matrix is serialized/deserialized to/from JSON. Specifically ```python rotation_mat = np.array( [ [-0.618452, -0.68369054 - 0.38740723j], [-0.78582258, 0.53807284 + 0.30489424j], ] ) # serialize u = ';'.join(str(__) for _ in rotation_mat for __ in _) # gives '(-0.618452+0j);(-0.68369054-0.38740723j);(-0.78582258+0j);(0.53807284+0.30489424j)' # deserialize v = [complex(_) for _ in u.split(';')] np.array(v).reshape([int(np.sqrt(len(v)))]*2) ``` This relies on the fact that rotational matrix is small and a square matrix **Related GitHub Issues:** #4893 @albi3ro --------- Co-authored-by: Christina Lee <chrissie.c.l@gmail.com> Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com> Co-authored-by: Christina Lee <christina@xanadu.ai>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
BasisRotation
template cannot currently be compared withqml.equal
or==
due the presence of a numpy array as a hyperparameter.For example, if we run:
To fix this issue, we need to register a custom comparison with the single dispatch function
_equal
defined herepennylane/pennylane/ops/functions/equal.py
Line 160 in 9781f11
Examples of custom kernels can be found in that file. Ths
op.hyperparameters['unitary_matrix']
hyperparameter should be compared withqml.math.allclose
to the requested relative and absolute tolerances (atol
,rtol
).The code for
BasisRotation
can be found:pennylane/pennylane/templates/subroutines/basis_rotation.py
Line 25 in 9781f11
Once custom comparison is implemented, the
xfail
mark can be removed from this test that runsqml.ops.functions.assert_valid
:pennylane/tests/templates/test_subroutines/test_basis_rotation.py
Line 23 in 9781f11
If the standard validity checks still fail after custom comparison is implemented, we can reassess any further steps.
A PR for this issue should:
_equal
forBasisRotation
xfail
mark from the standard validity testdocs/releases/changelog-dev.md
The text was updated successfully, but these errors were encountered: