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

Update non-gate quantum ops to have gates #4683

Closed
daxfohl opened this issue Nov 13, 2021 · 9 comments · Fixed by #4702
Closed

Update non-gate quantum ops to have gates #4683

daxfohl opened this issue Nov 13, 2021 · 9 comments · Fixed by #4702
Labels
area/gates kind/design-issue A conversation around design triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on

Comments

@daxfohl
Copy link
Collaborator

daxfohl commented Nov 13, 2021

Describe your design idea/issue

It would be nice to make things consistent such that quantum operations (as opposed to control operation classes like subcircuits and feedforward) all have associated gates. Most do. The ones I see that don't are:

  • BooleanHamiltonean: This seems straightforward to convert: instead of qubit_map, the BooleanHamiltoneanGate.init would just take a list of names, and then implement the decompose_once_with_qubits protocol to create the qubit_map on demand using self.names and the passed in qubits.
  • PauliStringPhasor and PauliStringGateOperation: Haven't looked at these.
  • GlobalPhaseOperation: This is always an oddball. But we could make a GlobalPhaseGate easily enough and it would fit better in some ways. Granted, GlobalPhaseGate().on(tuple()) is weird. But it does play well with other gates, like if someone wanted a function that zipped up gates and qubits, this would allow them to add global phases to that.

The advantages of doing this are:

  • Gates are more reusable (you can have my_x=X**0.8 and then apply that to different qubits, etc. With an op you'd have to **0.8 each time). With things like BooleanHamiltonean this could be nice too.
  • They fit cleaner in gatesets (GlobalPhaseGate would likely allow for cleanup of some existing gateset code)
  • You can typically operate on them at a lower level, e.g. with state vectors and axes, rather than requiring qubits and full simulators.
  • Consistency for consistency's sake.

Disadvantages:

  • e.g. GateOperation(PauliStringPhasorGate(), q) is not the same as PauliStringPhasorGate.on(q): we still want the latter to return a PauliStringPhasorOperation because it has extra functionality. So maybe this is confusing.
@daxfohl daxfohl added the kind/design-issue A conversation around design label Nov 13, 2021
@tanujkhattar
Copy link
Collaborator

There's also ArithmeticOperation; QuirkInputRotationOperation.

@tanujkhattar tanujkhattar added area/gates triage/discuss Needs decision / discussion, bring these up during Cirq Cynque labels Nov 16, 2021
@tanujkhattar
Copy link
Collaborator

tanujkhattar commented Nov 17, 2021

Regarding the disadvantage mentioned:

There are many operations which are not gate operations but have a .gate property. The confusing case that you mentioned would be applicable to all those cases. Eg: controlled_gate.on(*q) is not same as GateOperation(controlled_gate, q). Another prominent example is PauliString (operation) vs DensePauliGate (corresponding gate)

@tanujkhattar
Copy link
Collaborator

Gates are more reusable (you can have my_x=X**0.8 and then apply that to different qubits, etc. With an op you'd have to **0.8 each time). With things like BooleanHamiltonean this could be nice too.

With ops you can do ops.with_qubits() to apply them to different qubits.

@95-martin-orion
Copy link
Collaborator

Related: #1561

@tanujkhattar tanujkhattar added triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on and removed triage/discuss Needs decision / discussion, bring these up during Cirq Cynque labels Nov 17, 2021
@daxfohl
Copy link
Collaborator Author

daxfohl commented Nov 22, 2021

@tanujkhattar: It looks like ArithmeticOperation will be possible to make into a gate. I don't think QuirkInputRotationOperation will be possible though, since it wraps another Operation. To make a gate, we'd have to redesign the thing so that it wraps a Gate instead, which, IDK if that fits with what QuirkInputRotationOperation is supposed to do. If so, then probably it's possible to change that to a gate too, but IDK if the ROI is there.

@daxfohl
Copy link
Collaborator Author

daxfohl commented Nov 23, 2021

@tanujkhattar Looking again, I believe the QuirkInputRotationOperation would be a valid candidate. It can wrap a gate instead of an operation. Of course, that means it's limited to wrapping gates, but the point of this project is to push as many things down to the gate level as it makes sense to do. So, it won't be able to wrap a CircuitOperation or a ClassicallyControlledOperation. Which makes sense, and is probably better.

@tanujkhattar
Copy link
Collaborator

tanujkhattar commented Dec 13, 2021

This would be useful towards the Cirq 1.0 roadmap item for organizing gate relationships.

xref #3242

CirqBot pushed a commit that referenced this issue Dec 20, 2021
BREAKING CHANGE: PauliStringPhasor no longer inherits from PauliStringGateOperation.

Implements PauliStringPhasor in terms of a GateOperation on a new class PauliStringPhasorGate.

Mostly involved moving existing functions from the operation to the gate, and then having the operation call those methods under the hood.

Closes #1561, xref #4683

@tanujkhattar
MichaelBroughton pushed a commit to MichaelBroughton/Cirq that referenced this issue Jan 22, 2022
BREAKING CHANGE: PauliStringPhasor no longer inherits from PauliStringGateOperation.

Implements PauliStringPhasor in terms of a GateOperation on a new class PauliStringPhasorGate.

Mostly involved moving existing functions from the operation to the gate, and then having the operation call those methods under the hood.

Closes quantumlib#1561, xref quantumlib#4683

@tanujkhattar
@daxfohl
Copy link
Collaborator Author

daxfohl commented Feb 25, 2022

@tanujkhattar can you add the pre-1.0 tag to this one? #4705 and #4702 are still pending review.

@vtomole
Copy link
Collaborator

vtomole commented Feb 25, 2022

I've added it.

CirqBot pushed a commit that referenced this issue May 5, 2022
Migrate ArithmeticOperation to ArithmeticGate.

As implemented, ArithmeticGate.on(qubits) returns a GateOperation. In other words, ArithmeticGate is completely unrelated to ArithmeticOperation. They can be considered two different ways to do the same thing. This was done in order to enable ArithmeticOperation to be deprecated.

Additionally this PR implements QuirkArithmeticGate, deprecating QuirkArithmeticOperation, and rewrites ModularExp as a gate without a deprecation cycle since it's in /examples.

The code for ArithmeticGate (and subclasses) is *basically* identical with ArithmeticOperation, except instead of `Sequence[Qid]]`, a quantum register is a `Sequence[int]`, where the int represents the dimension. It implements the _qid_shape_ protocol from this data, and the GateOperation constructor already has logic to ensure the appropriate number/dimension of qubits are applied.

Tests are added to that effect.

Closes #4683
rht pushed a commit to rht/Cirq that referenced this issue May 1, 2023
BREAKING CHANGE: PauliStringPhasor no longer inherits from PauliStringGateOperation.

Implements PauliStringPhasor in terms of a GateOperation on a new class PauliStringPhasorGate.

Mostly involved moving existing functions from the operation to the gate, and then having the operation call those methods under the hood.

Closes quantumlib#1561, xref quantumlib#4683

@tanujkhattar
rht pushed a commit to rht/Cirq that referenced this issue May 1, 2023
Migrate ArithmeticOperation to ArithmeticGate.

As implemented, ArithmeticGate.on(qubits) returns a GateOperation. In other words, ArithmeticGate is completely unrelated to ArithmeticOperation. They can be considered two different ways to do the same thing. This was done in order to enable ArithmeticOperation to be deprecated.

Additionally this PR implements QuirkArithmeticGate, deprecating QuirkArithmeticOperation, and rewrites ModularExp as a gate without a deprecation cycle since it's in /examples.

The code for ArithmeticGate (and subclasses) is *basically* identical with ArithmeticOperation, except instead of `Sequence[Qid]]`, a quantum register is a `Sequence[int]`, where the int represents the dimension. It implements the _qid_shape_ protocol from this data, and the GateOperation constructor already has logic to ensure the appropriate number/dimension of qubits are applied.

Tests are added to that effect.

Closes quantumlib#4683
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this issue Oct 31, 2024
BREAKING CHANGE: PauliStringPhasor no longer inherits from PauliStringGateOperation.

Implements PauliStringPhasor in terms of a GateOperation on a new class PauliStringPhasorGate.

Mostly involved moving existing functions from the operation to the gate, and then having the operation call those methods under the hood.

Closes quantumlib#1561, xref quantumlib#4683

@tanujkhattar
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this issue Oct 31, 2024
Migrate ArithmeticOperation to ArithmeticGate.

As implemented, ArithmeticGate.on(qubits) returns a GateOperation. In other words, ArithmeticGate is completely unrelated to ArithmeticOperation. They can be considered two different ways to do the same thing. This was done in order to enable ArithmeticOperation to be deprecated.

Additionally this PR implements QuirkArithmeticGate, deprecating QuirkArithmeticOperation, and rewrites ModularExp as a gate without a deprecation cycle since it's in /examples.

The code for ArithmeticGate (and subclasses) is *basically* identical with ArithmeticOperation, except instead of `Sequence[Qid]]`, a quantum register is a `Sequence[int]`, where the int represents the dimension. It implements the _qid_shape_ protocol from this data, and the GateOperation constructor already has logic to ensure the appropriate number/dimension of qubits are applied.

Tests are added to that effect.

Closes quantumlib#4683
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/gates kind/design-issue A conversation around design triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants