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

Add kwargs for equal functions #5668

Merged
merged 10 commits into from
May 15, 2024

Conversation

kenya-sk
Copy link
Contributor

@kenya-sk kenya-sk commented May 7, 2024

Before submitting

Please complete the following checklist when submitting a PR:

  • All new features must include a unit test.
    If you've fixed a bug or added code that should be tested, add a test to the
    test directory!

  • All new functions and code must be clearly commented and documented.
    If you do make documentation changes, make sure that the docs build and
    render correctly by running make docs.

  • Ensure that the test suite passes, by running make test.

  • Add a new entry to the doc/releases/changelog-dev.md file, summarizing the
    change, and including a link back to the PR.

  • The PennyLane source code conforms to
    PEP8 standards.
    We check all of our code against Pylint.
    To lint modified files, simply pip install pylint, and then
    run pylint pennylane/path/to/file.py.

When all the above are checked, delete everything above the dashed
line and fill in the pull request template.


Context:
The kwargs (check_interface, check_trainability, atol and rtol) were not available for _equal_pow, _equal_adjoint, _equal_exp and _equal_sprod, which are overrides of qml.equal. Therefore, comparisons that allowed for interfacing and numerical errors were not possible.

Description of the Change:
Changed to allow kwargs to be used in override of qml.equal.

Benefits:
The following interfaces and comparisons with tolerance for numerical errors are possible.

>>> op1 = qml.RX(1.2, wires=0)
>>> op2 = qml.RX(1.2 + 1e-4, wires=0)
>>> qml.equal(op1 **2, op2 ** 2, atol=1e-3)
True
>>> qml.equal(op1 **2, op2 ** 2, atol=1e-6)
False
>>> qml.equal(qml.adjoint(op1), qml.adjoint(op2), atol=1e-3)
True
>>> qml.equal(qml.adjoint(op1), qml.adjoint(op2), atol=1e-6)
False
>>> op3 = qml.exp( qml.s_prod(2j, qml.X(0)))
>>> op4 = qml.exp(qml.s_prod(qml.numpy.array(2j), qml.X(0)))
>>> qml.equal(op3, op4, check_interface=True)
False
>>> qml.equal(op3, op4, check_interface=False)
True

Possible Drawbacks:

Related GitHub Issues:
#5408

@albi3ro
Copy link
Contributor

albi3ro commented May 7, 2024

Thanks for opening this PR @kenya-sk ! Looking great 🎉

I looked into the jax failure in test_jit_simplification. This is a simple by-product of the fact we are now actually checking the ML framework of the coefficient, and correctly stating that jax.numpy.array(2.0) is not equal to 2.0. A simple update to the relevant test should do the job.

This kind of failure is exactly why we are now in the process of adding an assert_equal function to explain these things.

Now other comments from my side. I'll check back later.

Copy link

codecov bot commented May 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.67%. Comparing base (f960c0e) to head (1bf4644).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5668      +/-   ##
==========================================
- Coverage   99.68%   99.67%   -0.01%     
==========================================
  Files         414      414              
  Lines       38819    38550     -269     
==========================================
- Hits        38695    38425     -270     
- Misses        124      125       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kenya-sk kenya-sk marked this pull request as ready for review May 8, 2024 07:48
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

Looks perfect 🎉 I'm happy to approve.

Copy link
Contributor

@dwierichs dwierichs left a comment

Choose a reason for hiding this comment

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

Hi @kenya-sk, thanks for this upgrade! 🎉
I had a few very small comments/suggestions, some of which are optional.
And a question to briefly check in on with @albi3ro.

doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
pennylane/ops/functions/equal.py Show resolved Hide resolved
tests/ops/functions/test_equal.py Outdated Show resolved Hide resolved
tests/ops/functions/test_equal.py Outdated Show resolved Hide resolved
tests/ops/functions/test_equal.py Show resolved Hide resolved
@kenya-sk
Copy link
Contributor Author

kenya-sk commented May 9, 2024

@dwierichs @albi3ro
Thanks insight full comment!
I will prepare the PR again reflecting your comments.

@kenya-sk
Copy link
Contributor Author

@dwierichs @albi3ro
I have reflected your comments in the code!
I have skipped the handling of numerical errors in _equal_pow that need to be discussed in the internals. If implementation is needed, please let me know so that I can address it immediately.

Also, since _equal_exp and others execute the equal method again by passing kwargs to nested base operators (return qml.equal(op1.base, op2.base, **kwargs)), the test code was created by separating the evaluation of Exp and Base Operator.

Copy link
Contributor

@dwierichs dwierichs left a comment

Choose a reason for hiding this comment

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

Thank you @kenya-sk for this thorough work! 💯

pennylane/ops/functions/equal.py Show resolved Hide resolved
tests/ops/functions/test_equal.py Show resolved Hide resolved
@albi3ro albi3ro enabled auto-merge (squash) May 15, 2024 15:08
@albi3ro albi3ro merged commit daf2d76 into PennyLaneAI:master May 15, 2024
38 checks passed
Shiro-Raven pushed a commit that referenced this pull request May 15, 2024
### Before submitting

Please complete the following checklist when submitting a PR:

- [x] All new features must include a unit test.
If you've fixed a bug or added code that should be tested, add a test to
the
      test directory!

- [x] All new functions and code must be clearly commented and
documented.
If you do make documentation changes, make sure that the docs build and
      render correctly by running `make docs`.

- [x] Ensure that the test suite passes, by running `make test`.

- [x] Add a new entry to the `doc/releases/changelog-dev.md` file,
summarizing the
      change, and including a link back to the PR.

- [x] The PennyLane source code conforms to
      [PEP8 standards](https://www.python.org/dev/peps/pep-0008/).
We check all of our code against [Pylint](https://www.pylint.org/).
      To lint modified files, simply `pip install pylint`, and then
      run `pylint pennylane/path/to/file.py`.

When all the above are checked, delete everything above the dashed
line and fill in the pull request template.


------------------------------------------------------------------------------------------------------------

**Context:**
The kwargs (`check_interface`, `check_trainability`, `atol` and `rtol`)
were not available for `_equal_pow`, `_equal_adjoint`, `_equal_exp` and
`_equal_sprod`, which are overrides of `qml.equal`. Therefore,
comparisons that allowed for interfacing and numerical errors were not
possible.

**Description of the Change:**
Changed to allow kwargs to be used in override of `qml.equal`.

**Benefits:**
The following interfaces and comparisons with tolerance for numerical
errors are possible.

```
>>> op1 = qml.RX(1.2, wires=0)
>>> op2 = qml.RX(1.2 + 1e-4, wires=0)
>>> qml.equal(op1 **2, op2 ** 2, atol=1e-3)
True
>>> qml.equal(op1 **2, op2 ** 2, atol=1e-6)
False
>>> qml.equal(qml.adjoint(op1), qml.adjoint(op2), atol=1e-3)
True
>>> qml.equal(qml.adjoint(op1), qml.adjoint(op2), atol=1e-6)
False
>>> op3 = qml.exp( qml.s_prod(2j, qml.X(0)))
>>> op4 = qml.exp(qml.s_prod(qml.numpy.array(2j), qml.X(0)))
>>> qml.equal(op3, op4, check_interface=True)
False
>>> qml.equal(op3, op4, check_interface=False)
True
```

**Possible Drawbacks:**

**Related GitHub Issues:**
#5408

---------

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants