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

Tools to disable op validation #5530

Merged
merged 8 commits into from
Jun 17, 2022

Conversation

95-martin-orion
Copy link
Collaborator

Fixes #5517.

Does what it says on the tin: turns off op validation in the provided context. The try/except block in the manager ensures that validation is always re-enabled, regardless of what terrible things happen in the context.

@95-martin-orion 95-martin-orion requested review from a team, vtomole and cduck as code owners June 15, 2022 22:31
@CirqBot CirqBot added the size: M 50< lines changed <250 label Jun 15, 2022
Copy link
Contributor

@maffoo maffoo left a comment

Choose a reason for hiding this comment

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

Consider using a ContextVar rather than modifying global state.

Comment on lines 38 to 42
except:
raw_types._validate_qid_shape = temp
raise
finally:
raw_types._validate_qid_shape = temp
Copy link
Contributor

Choose a reason for hiding this comment

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

Only need the finally clause here. It'll run on failure too.

from cirq.ops import raw_types

temp = raw_types._validate_qid_shape
raw_types._validate_qid_shape = lambda *args: None
Copy link
Contributor

Choose a reason for hiding this comment

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

We might consider using contextvars instead:

# raw_types.py
validate_qids: contextvars.ContextVar[bool] = contextvars.ContextVar('validate_qids', default=True)

def _validate_qid_shape(...) -> None:
    if validate_qids.get():
        return
    ...

# here
def disable_op_validation(...):
    ...
    token = raw_types.validate_qids.set(False)
    try:
        yield
    finally:
        raw_types.validate_qids.reset(token)

This would be more scoped than modifying global state, which could otherwise cause hard-to-debug issues with threads or asynchrony.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done. My only misgiving with this is that it exposes the hack in raw_types.py, which could lead to users applying it in ill-advised ways. Left a comment to mitigate this somewhat.

Copy link
Contributor

Choose a reason for hiding this comment

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

After thinking about it, I think I agree with you that it's better to keep the hack itself contained and not modify raw_types.py. If this turns out to be useful and we can't otherwise optimize the qid validation then we could migrate it into raw_types in the future. So feel free to ignore my suggestions and revert to your original version.

Copy link
Contributor

@maffoo maffoo left a comment

Choose a reason for hiding this comment

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

Can revert the contextvars suggestion, then LGTM.

@95-martin-orion
Copy link
Collaborator Author

Ended up needing to twiddle the tests to get coverage happy, but changes were fairly minor.

@95-martin-orion 95-martin-orion added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Jun 16, 2022
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Jun 16, 2022
@CirqBot
Copy link
Collaborator

CirqBot commented Jun 16, 2022

Automerge cancelled: A status check is failing.

@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Jun 16, 2022
@MichaelBroughton MichaelBroughton added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Jun 17, 2022
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Jun 17, 2022
@CirqBot CirqBot merged commit 525e7e4 into quantumlib:master Jun 17, 2022
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Jun 17, 2022
@95-martin-orion 95-martin-orion deleted the cirq-assume-valid branch June 17, 2022 18:49
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
Fixes quantumlib#5517.

Does what it says on the tin: turns off op validation in the provided context. The try/except block in the manager ensures that validation is **always** re-enabled, regardless of what terrible things happen in the context.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Context manager for disabling validation
4 participants