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

Implement comparison between QuantumScript classes with qml.equal #4894

Closed
albi3ro opened this issue Nov 28, 2023 · 5 comments
Closed

Implement comparison between QuantumScript classes with qml.equal #4894

albi3ro opened this issue Nov 28, 2023 · 5 comments
Assignees
Labels
good first issue Good for newcomers

Comments

@albi3ro
Copy link
Contributor

albi3ro commented Nov 28, 2023

Currently, we can use qml.equal to compare operations and measurements:

>>> qml.equal(qml.RX(1.2, wires=0), qml.RX(1.2, wires=0))
True
>>> qml.equal(qml.probs(wires=(0,1)), qml.probs(wires=(0,1)) )
True

but we cannot currently compare our circuit object, qml.tape.QuantumScript.

To add comparison support, we would need to register a new dispatch for the _equal single dispatch function:

This function

def _equal(op1 : qml.tape.QuantumScript, op2 : qml.tape.QuantumScript,
    check_interface=True,
    check_trainability=True,
    rtol=1e-5,
    atol=1e-9,
    ) -> bool :

Should return True if:

And False otherwise.

A PR implementing this should also update the docstring for qml.equal and include unit tests for comparison.

Example of desired behavior:

>>> tape1 = qml.tape.QuantumScript([qml.PauliX(0), qml.RX(1.2, wires=0)], [qml.expval(qml.PauliZ(0))], shots=10)
>>> tape2 = qml.tape.QuantumScript([qml.PauliX(0)], [qml.expval(qml.PauliZ(0))], shots=10)
>>> qml.equal(tape1, tape2)
False
>>> qml.equal(tape1, tape1)
True
>>> tape3 = qml.tape.QuantumScript([qml.PauliX(0)], [qml.expval(qml.PauliZ(0))], shots=None)
>>> qml.equal(tape2, tape3)
False
>>> tape4 = qml.tape.QuantumScript([qml.PauliX(0), qml.RX(1.2+1e-6, wires=0)], [qml.expval(qml.PauliZ(0))], shots=10)
>>> qml.equal(tape1, tape4, atol=1e-5)
True
>>> qml.equal(tape1, tape4, atol=1e-7)
False
@minhtriet
Copy link
Contributor

I would like to work on this if you don't mind

@albi3ro
Copy link
Contributor Author

albi3ro commented Nov 29, 2023

Thanks @minhtriet . Let me know if you have any further questions.

@minhtriet
Copy link
Contributor

Hi @albi3ro, in your example

>>> qml.equal(tape1, tape4, atol=1e-7)
False

did you mean, perhaps

>>> qml.equal(tape1, tape4, rtol=0, atol=1e-7)
False

Right now rtol is default to 1e-5, which makes the test return True

@minhtriet
Copy link
Contributor

I think we could close it this @albi3ro :)

@albi3ro
Copy link
Contributor Author

albi3ro commented Dec 5, 2023

Thanks for the PR @minhtriet 🎉

@albi3ro albi3ro closed this as completed Dec 5, 2023
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

2 participants