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

Add support for importing custom gates in Quirk URLS #2473

Merged
merged 10 commits into from
Nov 7, 2019
Merged

Conversation

Strilanc
Copy link
Contributor

@Strilanc Strilanc commented Nov 1, 2019

  • 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

- 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
@Strilanc Strilanc requested a review from viathor November 1, 2019 21:03
@googlebot googlebot added the cla: yes Makes googlebot stop complaining. label Nov 1, 2019
Copy link
Collaborator

@viathor viathor left a 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.

cirq/contrib/quirk/cells/input_rotation_cells.py Outdated Show resolved Hide resolved
cirq/contrib/quirk/cells/cell.py Outdated Show resolved Hide resolved
cirq/contrib/quirk/cells/cell.py Outdated Show resolved Hide resolved
cirq/contrib/quirk/cells/cell.py Show resolved Hide resolved
cirq/contrib/quirk/cells/cell.py Show resolved Hide resolved
cirq/contrib/quirk/cells/cell.py Outdated Show resolved Hide resolved
cirq/contrib/quirk/url_to_circuit.py Outdated Show resolved Hide resolved
cirq/contrib/quirk/cells/cell.py Show resolved Hide resolved
cirq/contrib/quirk/cells/cell.py Outdated Show resolved Hide resolved
cirq/contrib/quirk/cells/composite_cell.py Outdated Show resolved Hide resolved
cirq/contrib/quirk/cells/composite_cell.py Outdated Show resolved Hide resolved
cirq/contrib/quirk/cells/composite_cell.py Outdated Show resolved Hide resolved
cirq/contrib/quirk/cells/composite_cell.py Outdated Show resolved Hide resolved
cirq/contrib/quirk/cells/composite_cell.py Show resolved Hide resolved
cirq/contrib/quirk/cells/composite_cell.py Show resolved Hide resolved
cirq/contrib/quirk/cells/cell.py Show resolved Hide resolved
gate_count = sum(0 if cell is None else cell.gate_count()
for col in parsed_cols
for cell in col)

Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

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.

cirq/contrib/quirk/cells/composite_cell.py Show resolved Hide resolved
cirq/contrib/quirk/cells/composite_cell.py Outdated Show resolved Hide resolved
Copy link
Contributor Author

@Strilanc Strilanc left a 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

Copy link
Collaborator

@viathor viathor left a 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)

Copy link
Collaborator

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.

@Strilanc
Copy link
Contributor Author

Strilanc commented Nov 6, 2019

making the max operation limit a class property of CompositeCell and raising the exception in CompositeCell._transform_cells()

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.

Copy link
Collaborator

@viathor viathor left a 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.

@Strilanc Strilanc added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Nov 6, 2019
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Nov 6, 2019
@CirqBot CirqBot merged commit 1cdf343 into master Nov 7, 2019
@CirqBot CirqBot removed the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Nov 7, 2019
@CirqBot CirqBot deleted the quirk_toolingE branch November 7, 2019 00:05
@CirqBot CirqBot removed the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Nov 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Makes googlebot stop complaining.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants