-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add support for importing custom gates in Quirk URLS #2473
Conversation
- Define `CompositeCell` - Add abstract `with_qubits` and `gate_count` methods to `Cell` - Add `_replace_qubit[s]` utility methods to `Cell` - Included defenses against billion laughs attack, test that they work - Add QFT example test
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.
Mypy is upset and so tests didn't run. See below for a fix and some comments. I haven't finished the review yet.
gate_count = sum(0 if cell is None else cell.gate_count() | ||
for col in parsed_cols | ||
for cell in col) | ||
|
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.
Can we raise ValueError
for billion laughs here instead of l.208? Then we never have to instantiate a CompositeCell
with dangerous cell structure and don't need the extra complexity to ensure lazy traversals.
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.
what's l.208
? Later?
I tried doing this, and the runtime of the test to detect the attack increased from a fraction of a section to tens of seconds. The problem is that this approach performs O(MAX_OP_COUNT) work instead of O(INPUT LENGTH) work when defending against an attack, because the largest composite gate under the limit is still built.
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.
Sorry, by l.208 I meant line 208 above:
raise ValueError(
f'Quirk URL specifies a circuit with {comp.gate_count()} '
f'operations, but max_operation_count={max_operation_count}.')
You're right, though about my suggestion above. It doesn't solve the problem.
The reason I raised that suggestion was that the current API of CompositeCell
bothers me a little. Basically, the fact that we have to add the caution warnings in the docstring suggests that our abstraction isn't great.
WDYT about making the max operation limit a class property of CompositeCell
and raising the exception in CompositeCell._transform_cells()
? Then we could remove the caution warnings and the requirement that sub_cell_cols_generator
be a generator (it could be any iterable whether fully materialized or not). We could also remove associated machinery to support the lazy processing, i.e. _iterator_to_iterable
.
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.
Ready for another pass
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.
This looks good to me overall, but I have one more suggestion I think we should consider. Sorry for dragging the review on.
gate_count = sum(0 if cell is None else cell.gate_count() | ||
for col in parsed_cols | ||
for cell in col) | ||
|
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.
Sorry, by l.208 I meant line 208 above:
raise ValueError(
f'Quirk URL specifies a circuit with {comp.gate_count()} '
f'operations, but max_operation_count={max_operation_count}.')
You're right, though about my suggestion above. It doesn't solve the problem.
The reason I raised that suggestion was that the current API of CompositeCell
bothers me a little. Basically, the fact that we have to add the caution warnings in the docstring suggests that our abstraction isn't great.
WDYT about making the max operation limit a class property of CompositeCell
and raising the exception in CompositeCell._transform_cells()
? Then we could remove the caution warnings and the requirement that sub_cell_cols_generator
be a generator (it could be any iterable whether fully materialized or not). We could also remove associated machinery to support the lazy processing, i.e. _iterator_to_iterable
.
I would prefer for it to be configurable at call time. I think the only proper way to remove the generator shenanigans is to split parsing into two phases: size estimation then circuit construction. That would be quite a lot of work. |
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.
Alright. CompositeCell is imperfect, but it is strictly an improvement. LGTM.
CompositeCell
with_qubits
andgate_count
methods toCell
_replace_qubit[s]
utility methods toCell