-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add __cirq_debug__
flag and conditionally disable qid validations in gates and operations
#6000
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
Changes from 3 commits
dc0c826
8fcfdf8
48b31a2
5a20969
6fad36a
b24e606
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,6 +151,30 @@ def test_op_validate(): | |
op2.validate_args([cirq.LineQid(1, 2), cirq.LineQid(1, 2)]) | ||
|
||
|
||
def test_disable_op_validation(): | ||
q0, q1 = cirq.LineQubit.range(2) | ||
h_op = cirq.H(q0) | ||
|
||
# Fails normally. | ||
with pytest.raises(ValueError, match='Wrong number'): | ||
_ = cirq.H(q0, q1) | ||
with pytest.raises(ValueError, match='Wrong number'): | ||
h_op.validate_args([q0, q1]) | ||
|
||
# Passes, skipping validation. | ||
cirq.__cirq_debug__.set(False) | ||
op = cirq.H(q0, q1) | ||
assert op.qubits == (q0, q1) | ||
h_op.validate_args([q0, q1]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it'd be nice to add a context manager for changing the @contextlib.contextmanager
def with_debug(value: bool) -> Iterator[None]:
token = __cirq_debug__.set(value)
try:
yield
finally:
token.reset() Then can use that in these tests: with cirq.with_debug(False):
op = cirq.H(q0, q1)
assert op.qubits == (q0, q1)
h_op.valudate_args([q0, q1]) I think we should encourage using a mechanism like this and discourage setting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think adding the context manager is fine, but I'd still like to set the default value to What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added a context manager, PTAL |
||
|
||
# Fails again when validation is re-enabled. | ||
cirq.__cirq_debug__.set(True) | ||
with pytest.raises(ValueError, match='Wrong number'): | ||
_ = cirq.H(q0, q1) | ||
with pytest.raises(ValueError, match='Wrong number'): | ||
h_op.validate_args([q0, q1]) | ||
|
||
|
||
def test_default_validation_and_inverse(): | ||
class TestGate(cirq.Gate): | ||
def _num_qubits_(self): | ||
|
@@ -787,6 +811,11 @@ def matrix(self): | |
test_non_qubits = [str(i) for i in range(3)] | ||
with pytest.raises(ValueError): | ||
_ = g.on_each(*test_non_qubits) | ||
|
||
cirq.__cirq_debug__.set(False) | ||
assert g.on_each(*test_non_qubits)[0].qubits == ('0',) | ||
|
||
cirq.__cirq_debug__.set(True) | ||
with pytest.raises(ValueError): | ||
_ = g.on_each(*test_non_qubits) | ||
|
||
|
@@ -853,6 +882,11 @@ def test_on_each_two_qubits(): | |
g.on_each([(a,)]) | ||
with pytest.raises(ValueError, match='Expected 2 qubits'): | ||
g.on_each([(a, b, a)]) | ||
|
||
cirq.__cirq_debug__.set(False) | ||
assert g.on_each([(a, b, a)])[0].qubits == (a, b, a) | ||
cirq.__cirq_debug__.set(True) | ||
|
||
with pytest.raises(ValueError, match='Expected 2 qubits'): | ||
g.on_each(zip([a, a])) | ||
with pytest.raises(ValueError, match='Expected 2 qubits'): | ||
|
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.
nit: remove blank line and sort imports since
contextvars
is part of the stdlib.