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

Improve use of keyword args in qml.equal comparison #5408

Closed
albi3ro opened this issue Mar 19, 2024 · 2 comments
Closed

Improve use of keyword args in qml.equal comparison #5408

albi3ro opened this issue Mar 19, 2024 · 2 comments
Assignees
Labels
good first issue Good for newcomers

Comments

@albi3ro
Copy link
Contributor

albi3ro commented Mar 19, 2024

While qml.equal has the keyword arguments check_interface, check_trainability, atol, and rtol, not all specialized kernels use them when they should.

These operators are:

Pow:

def _equal_pow(op1: Pow, op2: Pow, **kwargs):

Adjoint:

def _equal_adjoint(op1: Adjoint, op2: Adjoint, **kwargs):

Exp:

def _equal_exp(op1: Exp, op2: Exp, **kwargs):

SProd:

return qml.equal(op1.base, op2.base)

After this change, we should have:

>>> 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

Relevant tests should be added to:

https://github.com/PennyLaneAI/pennylane/blob/master/tests/ops/functions/test_equal.py

A changelog entry should be added under Community Contributions

<h4>Community contributions 🥳</h4>

And your name should be added to the list of contributors at the bottom of the changelog:

<h3>Contributors ✍️</h3>

@albi3ro albi3ro added the good first issue Good for newcomers label Mar 19, 2024
@kenya-sk
Copy link
Contributor

@albi3ro
I am working on another issue, but am also interested in this one.
Can you please assign it to me?

albi3ro added a commit that referenced this issue 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 <[email protected]>
Shiro-Raven pushed a commit that referenced this issue 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 <[email protected]>
@Alex-Preciado
Copy link
Contributor

Alex-Preciado commented May 23, 2024

Closed with #5668

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants