-
Notifications
You must be signed in to change notification settings - Fork 575
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
Skip over identity operations in "default.qubit"
#2356
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2356 +/- ##
=======================================
Coverage 99.42% 99.42%
=======================================
Files 242 242
Lines 18341 18343 +2
=======================================
+ Hits 18235 18237 +2
Misses 106 106
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @albi3ro, great catch! It would be great to cover this functionality with tests too. For example, one could check that no internal methods (e.g., _apply_unitary_einsum
) are being called for applying identities.
@@ -242,6 +242,8 @@ def _apply_operation(self, state, operation): | |||
Returns: | |||
array[complex]: output state | |||
""" | |||
if operation.base_name == "Identity": | |||
return state |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any other "no-ops" to include here? e.g., things like a barrier
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Barrier
and WireCut
don't define a matrix, so we don't have to worry about matrix operations with them. They get expanded away to nothing prior to execution. Identity
, on the other hand, is an explicitly supported gate, so it won't get expanded away.
spy_unitary = mocker.spy(dev, "_apply_unitary") | ||
|
||
res = dev._apply_operation(starting_state, op) | ||
assert res is starting_state |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is
makes sure that the state isn't just approximately the same values, but was literally unaffected by the method.
@antalszava test is added. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! 💯 Great catch. 🎉 Would this be applicable for default.mixed
too?
* `"default.qubit"` now skips over identity operators instead performing matrix multiplication | ||
with the identity. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* `"default.qubit"` now skips over identity operators instead performing matrix multiplication | |
with the identity. | |
* `default.qubit` now skips over identity operators instead of performing matrix multiplication | |
with the identity. |
@@ -2341,6 +2341,24 @@ def compute_matrix(*params, **hyperparams): | |||
assert np.allclose(res_mat, op.get_matrix()) | |||
assert np.allclose(res_wires, wires) | |||
|
|||
def test_identity_skipped(self, mocker): | |||
"""Test identity operation does not perform additional computations.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"""Test identity operation does not perform additional computations.""" | |
"""Test that applying the identity operation does not perform any additional computations.""" |
Currently,
"default.qubit"
handles identity operations by performingeinsum
with the identity.This PR makes it so that identity operations are merely passed over.