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

Rewrite cirq.stratified_circuit following new Transformer API and primitives. #4944

Merged
merged 7 commits into from
Feb 10, 2022

Conversation

tanujkhattar
Copy link
Collaborator

@tanujkhattar tanujkhattar commented Feb 3, 2022

@tanujkhattar tanujkhattar requested review from cduck, vtomole and a team as code owners February 3, 2022 12:27
@tanujkhattar tanujkhattar requested a review from viathor February 3, 2022 12:27
@CirqBot CirqBot added the size: L 250< lines changed <1000 label Feb 3, 2022
@MichaelBroughton MichaelBroughton self-assigned this Feb 3, 2022
Copy link
Collaborator

@MichaelBroughton MichaelBroughton left a comment

Choose a reason for hiding this comment

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

Looks great. Took a quick look around and this transformer doesn't seem to get heavy use in the library (it also has exponential runtime in the number of categories).

Should we just cut it outright ? If not, I've left a few comments and we should be good to merge.

Args:
circuit: The circuit whose operations should be re-arranged.
context: `cirq.TransformerContext` storing common configurable options for transformers.
categories: A list of classifiers picking out certain operations.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we tighten this up to reflect the types that you are ultimately allowed to pass in here ? It seems like this is always getting promoted to a lambda function of some kind in the _category_to_classifier to function, maybe we just require lambdas from the get go ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We already describe all the valid types in the description of this arg?

There are several ways to specify a classifier. You can pass in a gate instance 
(e.g. `cirq.X`), a gate type (e.g. `cirq.XPowGate`), an operation instance 
(e.g. `cirq.X(cirq.LineQubit(0))`), an operation type (e.g.`cirq.CircuitOperation`), 
or an arbitrary operation predicate (e.g. `lambda op: len(op.qubits) == 2`).

I think it's useful to allow these types instead of directly requiring lambda's because an iterable of lambda's would be messy to write. For example:

>>> categories = [cirq.X, cirq.Y, cirq.Z] # Way-1: Much cleaner 
>>> categories = [lambda op: isinstance(op.gate, cirq.X), lambda op: isinstance(op.gate, cirq.Y), lambda op: isinstance(op.gate, cirq.Z)] # Way-2: Much worse 

cirq-core/cirq/transformers/stratify.py Outdated Show resolved Hide resolved
cirq-core/cirq/transformers/stratify.py Show resolved Hide resolved
cirq-core/cirq/transformers/stratify.py Outdated Show resolved Hide resolved
cirq-core/cirq/transformers/stratify.py Outdated Show resolved Hide resolved
cirq-core/cirq/transformers/stratify.py Outdated Show resolved Hide resolved
assert cirq.stratified_circuit(input_circuit, categories=[cirq.X]) == expected
cirq.testing.assert_same_circuits(
cirq.stratified_circuit(input_circuit, categories=[cirq.X]), expected
)


Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe add a CCO test or two just so we have it and know what happens and then when we get around to well-defining their behavior in transformers, we know where we are starting from ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

CCOs are indeed tricky! Fixed a bug, added a test and filed #4976 -- which I'm sure is going to be a cause of bugs with CCOs at a lot more places in cirq :))

@tanujkhattar
Copy link
Collaborator Author

Should we just cut it outright ? If not, I've left a few comments and we should be good to merge.

The number of categories in practice are usually pretty small, for example: align all 1 and 2q gates in separate moments. Therefore, I think this is a useful transformer to keep.

I've addressed your comments. PTAL.

Copy link
Collaborator

@MichaelBroughton MichaelBroughton left a comment

Choose a reason for hiding this comment

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

LGTM

@MichaelBroughton
Copy link
Collaborator

Could you try pushing a no-op to the branch here to retrigger the CI ? I can't seem to get it to go.....

@tanujkhattar tanujkhattar merged commit 4dc6e25 into quantumlib:master Feb 10, 2022
@tanujkhattar tanujkhattar deleted the stratify_transformer branch February 10, 2022 19:28
95-martin-orion pushed a commit to 95-martin-orion/Cirq that referenced this pull request Mar 2, 2022
…rimitives. (quantumlib#4944)

* Rewrite stratified_circuit following new Transformer API and primitives.

* Reorder arguments lists

* Address Mike's Feedback
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
…rimitives. (quantumlib#4944)

* Rewrite stratified_circuit following new Transformer API and primitives.

* Reorder arguments lists

* Address Mike's Feedback
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
…rimitives. (quantumlib#4944)

* Rewrite stratified_circuit following new Transformer API and primitives.

* Reorder arguments lists

* Address Mike's Feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: L 250< lines changed <1000
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants