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 operator support for new assert_equal function: Group 2 #5648

Open
albi3ro opened this issue May 3, 2024 · 1 comment · May be fixed by #5780
Open

Improve operator support for new assert_equal function: Group 2 #5648

albi3ro opened this issue May 3, 2024 · 1 comment · May be fixed by #5780
Labels
unitaryhack Dedicated issue for Unitary Fund open-source hackathon

Comments

@albi3ro
Copy link
Contributor

albi3ro commented May 3, 2024

Context

PennyLane currently has a qml.equal function which can be used to check the equality of operators, measurements, and tapes. While this has been extremely useful in testing and validation, it can be difficult to figure out why two objects aren't equal.

This is why we are currently have added the framework for an assert_equal function:

def assert_equal(

>>> from pennylane.ops.functions.equal import assert_equal
>>> assert_equal(qml.S(0), qml.T(0))
AssertionError: op1 and op2 are of different types.  Got <class 'pennylane.ops.qubit.non_parametric_ops.S'> and <class 'pennylane.ops.qubit.non_parametric_ops.T'>.

The framework for this function was added in PR #5634 , but the implementation still needs to be updated.

Implementation Details

Comparison currently relies on a _equal single dispatch function, with different "kernels" registered for different types of operators. Currently this function returns True or False. We are changing it to return True, or a string that explains why the two operators are not equal. This change has already been made for BasisRotation:

def _equal_basis_rotation(

This task is to update _equal for

While more single dispatch kernels exist for _equal, they will be updated as part of separate tasks.

After this PR and one from Issue #5644 , we should be able to do something like:

>>> assert_equal(2.0 * qml.X(0), 2.0 * qml.X(1))
AssertionError: op1 and op2 have different base operations because op1 in on <Wires = [0]> and op2 is on <Wires = [1]>
>>> assert_equal(qml.X(0) + qml.Y(0), qml.X(0) + qml.Y(0) + qml.Z(0))
AssertionError: op1 and op2 have different lengths.
>>> asset_equal(qml.S(0) @ qml.SX(0), qml.SX(0) @ qml.S(0))
AssertionError: sorted op 0 is different because op1 and op2 are of different types. Got pennylane.ops.qubit.non_parametric_ops.S and pennylane.ops.qubit.non_parametric_ops.SX.
>>> op1 = qml.operation.Tensor(qml.X(0), qml.Y(1))
>>> op2 = qml.operation.Tensor(qml.Y(0), qml.X(1))
>>> assert_equal(op1, op2)
AsssetionError: op1 and op2 have different _obs_data outputs.

Use your best judgement as to the contents of the string and we will help refine the message if needed. The output may recursively depend on _equal when comparing component operations.

Additional Requirements

To fulfill this task, a PR should:

  • Update _equal kernels for the above cases
  • Update tests in test_equal.py. Existing tests like, test_adjoint_comparison can be extended, or new tests can be written.
  • Add a changelog entry
  • Pass black, pylint, and isort checks
@trbromley trbromley changed the title Improve nested operator support for new assert_equal function: Pt 2 Improve operator support for new assert_equal function: Group 2 May 6, 2024
@trbromley trbromley added the unitaryhack Dedicated issue for Unitary Fund open-source hackathon label May 29, 2024
@Tarun-Kumar07 Tarun-Kumar07 linked a pull request Jun 1, 2024 that will close this issue
@Tarun-Kumar07
Copy link
Contributor

Hi @albi3ro and @trbromley,

I’ve submitted PR #5780. Once the PR is merged, could you please assign this issue to me before closing it?

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
unitaryhack Dedicated issue for Unitary Fund open-source hackathon
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants