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

Gate.on().gate should be consistent #2626

Closed
kevinsung opened this issue Dec 5, 2019 · 7 comments
Closed

Gate.on().gate should be consistent #2626

kevinsung opened this issue Dec 5, 2019 · 7 comments
Assignees
Labels
area/gates complexity/medium introduces/modifies 3-5 concepts, takes max up to a month for an advanced contributor kind/health For CI/testing/release process/refactoring/technical debt items skill-level/advanced One or more of the areas need a solid understanding. triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on

Comments

@kevinsung
Copy link
Collaborator

Right now the return type of Gate.on is Operation, not GateOperation. I have come across situations where I want to assume that the result of Gate.on has a non-None gate attribute. This should be a safe assumption, but the types currently don't work out.

Changing the return type of Gate.on from Operation to GateOperation results in the following mypy errors:

cirq/ops/fourier_transform.py:194: error: Incompatible types in assignment (expression has type "Operation", variable has type "GateOperation")
cirq/ops/dense_pauli_string.py:268: error: Return type of "on" incompatible with supertype "Gate"
cirq/ops/controlled_gate.py:116: error: Return type of "on" incompatible with supertype "Gate"
cirq/ops/common_gates.py:845: error: Signature of "on" incompatible with supertype "Gate"
cirq/google/optimizers/convert_to_sycamore_gates.py:262: error: Argument 1 to "extend" of "list" has incompatible type "Iterator[Operation]"; expected "Iterable[GateOperation]"

I think the only tricky ones here are DensePauliString and ControlledGate. DensePauliString can be fixed by making PauliString a GateOperation. ControlledGate can be fixed by defining ControlledGateOperation. It makes sense to have both ControlledOperation and ControlledGateOperation given that both Operation and GateOperation exist.

@dabacon
Copy link
Collaborator

dabacon commented May 29, 2020

I've tried this refactor and indeed run into problems with ControlledOperation and PauliString. These feel like signs that these operations are not properly designed.

@balopat balopat added area/gates kind/health For CI/testing/release process/refactoring/technical debt items triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on complexity/medium introduces/modifies 3-5 concepts, takes max up to a month for an advanced contributor skill-level/advanced One or more of the areas need a solid understanding. labels Sep 18, 2020
@dabacon
Copy link
Collaborator

dabacon commented Mar 28, 2022

I'm not sure if this is possible any more, but pre 1.0 should dedide if we want to do this.

@daxfohl
Copy link
Collaborator

daxfohl commented Mar 28, 2022

I'd looked at this and it would be a pretty big change. Alternately, we may want a consistency test that gate == gate.on(...).gate that is automatically run on all gates defined in the project. This should work on all our gates now. The first sentence of this issue reflects that that is the actual problem, so perhaps update the issue title.

@dabacon
Copy link
Collaborator

dabacon commented Mar 29, 2022

OK so if we reduce the scope to testing this, I think the challenge is going to be generating instances of all of the gates. I thought about using the json test data for this, anyone have a better idea?

@dstrain115 dstrain115 added the triage/discuss Needs decision / discussion, bring these up during Cirq Cynque label May 11, 2022
@dstrain115
Copy link
Collaborator

Adding discuss label to see if we want to do this. Given the comments and the corresponding difficulty of deprecating TaggedOperations, I think this is too big of an issue (with not as much benefit) to tackle before 1.0.

As suggested above, we could limit the scope to adding consistency that gate == gate.on(...).gate for all gates and then reduce the scope of the issue.

@dstrain115 dstrain115 changed the title Gate.on should return GateOperation Gate.on().gate should be consistent May 11, 2022
@dstrain115 dstrain115 removed the triage/discuss Needs decision / discussion, bring these up during Cirq Cynque label May 11, 2022
@dabacon
Copy link
Collaborator

dabacon commented May 19, 2022

#5354 adds tests for round tripping. There is at least one more gate that needs to be serialized to get complete coverage.

@dabacon
Copy link
Collaborator

dabacon commented May 19, 2022

Closing this issue as gates without json serialization is tracked in #5353

@dabacon dabacon closed this as completed May 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/gates complexity/medium introduces/modifies 3-5 concepts, takes max up to a month for an advanced contributor kind/health For CI/testing/release process/refactoring/technical debt items skill-level/advanced One or more of the areas need a solid understanding. triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on
Projects
None yet
Development

No branches or pull requests

5 participants