Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add XPow/YPow/ZPow and factor out CirqGateAsBloqBase #469

Merged
merged 7 commits into from
Nov 2, 2023

Conversation

tanujkhattar
Copy link
Collaborator

@tanujkhattar tanujkhattar commented Oct 31, 2023

This PR does a bunch of changes/improvements in cirq interop

  • Introduces a new CirqGateAsBloqBase abstract class which has all the functionality to auto populate the bloq methods using underlying Cirq gates. There is one abstract method called cirq_gate which can be over-riden by derived classes.
  • The wrapper class CirqGateAsBloq simply derives from CirqGateAsBloqBase and uses the user specified self.gate as the corresponding cirq_gate.
  • However, users can now also derive from CirqGateAsBloqBase class and specify a custom cirq gate as the cirq_gate property. For commonly used gates in Cirq-Core, this inheritance based method for wrapping cirq gates solves the discoverability issue and reduces boilerplace code.
  • This is now used in the rotations.py file to wrap Rx / Ry / Rz / XPow / YPow / ZPow gates. As you can see, the wrapper code is now much simpler.
  • Another drive-thru change is to change the default signature of wrapped gates from Register(qubits, shape=(nqubits), bitsize=1) to Register('q', shape=(nqubits), bitsize=1) if nqubits > 1 or Register('q', shape=(), bitsize=1) if nqbits = 1. Changing qubits to q is nicer because we don't need to worry about singular / plural when number of qubits are > 1 or = 1.

The XPow/YPow/ZPow gates have an option to configure the global phase and follow a pattern similar to Cirq. These gates are useful when implementing phase gradient states. Docstrings for these gates are copied over from Cirq.

As a follow up, we can also consider making these new gates implementation more robust so we can get rid of XGate / YGate / ZGate that also currently exist.

@fdmalone
Copy link
Collaborator

fdmalone commented Oct 31, 2023

This LGTM % nits. The more standard rotation bloqs are preserved, which is good!

@mpharrigan
Copy link
Collaborator

should we remove/replace the existing Rx,Ry,Rz? I'm not a huge fan of the "pow" nomenclature since I don't think this has really caught on outside of Cirq; but this matches cirq so I can see why we'd want to keep the name

@fdmalone
Copy link
Collaborator

Why would we remove them? It's convenient having the textbook rotation gates around no?

@tanujkhattar
Copy link
Collaborator Author

Why would we remove them? It's convenient having the textbook rotation gates around no?

Yeah, I don't think we should remove them. We can probably make them derive from XPow / YPow etc. like Cirq does but then it becomes harder to continue to keep them frozen dataclasses.

@tanujkhattar
Copy link
Collaborator Author

@mpharrigan @fdmalone This is ready for a re-review

@fdmalone fdmalone self-requested a review November 2, 2023 22:42
Copy link
Collaborator

@fdmalone fdmalone left a comment

Choose a reason for hiding this comment

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

I think this LGTM. It took me a second to get the inheritance straight in my head but I think this is nice. Presumably we may want to replace various other single-qubit bloqs this way?

@tanujkhattar
Copy link
Collaborator Author

Presumably we may want to replace various other single-qubit bloqs this way?

That's right, and this approach can be used for multi qubit cirq-core gates as well (like XX, YY, ZZ, etc.); the exact same code will work.

@tanujkhattar tanujkhattar enabled auto-merge (squash) November 2, 2023 22:45
@tanujkhattar tanujkhattar merged commit 1532cb6 into main Nov 2, 2023
@@ -53,34 +54,36 @@ def _get_cirq_quregs(signature: Signature, qm: InteropQubitManager):
return ret


@frozen
class CirqGateAsBloq(GateWithRegisters):
class CirqGateAsBloqBase(GateWithRegisters):
Copy link
Collaborator

Choose a reason for hiding this comment

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

don't you need to mark this as abstract? even if the metaclass is inherited, it would probably help for documenting

gate: cirq.Gate
@property
@abc.abstractmethod
def cirq_gate(self) -> cirq.Gate:
Copy link
Collaborator

Choose a reason for hiding this comment

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

update class docstring .. this is no longer a wrapper, right? it's something else

@mpharrigan mpharrigan changed the title Add XPow/YPow/ZPow rotations gate Add XPow/YPow/ZPow and factor out CirqGateAsBloqBase Nov 3, 2023
@mpharrigan mpharrigan changed the title Add XPow/YPow/ZPow and factor out CirqGateAsBloqBase Add XPow/YPow/ZPow and factor out CirqGateAsBloqBase Nov 3, 2023
Comment on lines +124 to +125
class CirqGateAsBloq(CirqGateAsBloqBase):
gate: cirq.Gate
Copy link
Collaborator

Choose a reason for hiding this comment

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

docstring!

@mpharrigan
Copy link
Collaborator

post-hoc review; would appreciate if you could open a small follow-up pr

@mpharrigan
Copy link
Collaborator

@tanujkhattar ^

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants