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

Json serialize cirq.ion.ion_gates.MSGate #4603

Closed
vtomole opened this issue Oct 27, 2021 · 5 comments · Fixed by #4605
Closed

Json serialize cirq.ion.ion_gates.MSGate #4603

vtomole opened this issue Oct 27, 2021 · 5 comments · Fixed by #4605
Assignees
Labels
area/json area/serialization good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. kind/feature-request Describes new functionality no QC knowledge needed Want to contribute to Cirq, but don't know quantum computing? This issue is for you.

Comments

@vtomole
Copy link
Collaborator

vtomole commented Oct 27, 2021

Replace the existing class:

class MSGate(ops.XXPowGate):

with

class MSGate(ops.XXPowGate):
    """The Mølmer–Sørensen gate, a native two-qubit operation in ion traps.
    A rotation around the XX axis in the two-qubit bloch sphere.
    The gate implements the following unitary:
        exp(-i t XX) = [ cos(t)   0        0       -isin(t)]
                       [ 0        cos(t)  -isin(t)  0      ]
                       [ 0       -isin(t)  cos(t)   0      ]
                       [-isin(t)  0        0        cos(t) ]
    """

    def __init__(self, *, rads: float):  # Forces keyword args.
        ops.XXPowGate.__init__(self, exponent=rads * 2 / np.pi, global_shift=-0.5)
        self.rads = rads

    def _with_exponent(self: 'MSGate', exponent: value.TParamVal) -> 'MSGate':
        return type(self)(rads=exponent * np.pi / 2)

    def _circuit_diagram_info_(
        self, args: 'cirq.CircuitDiagramInfoArgs'
    ) -> Union[str, 'protocols.CircuitDiagramInfo']:
        angle_str = self._format_exponent_as_angle(args, order=4)
        symbol = f'MS({angle_str})'
        return protocols.CircuitDiagramInfo(wire_symbols=(symbol, symbol))

    def __str__(self) -> str:
        if self._exponent == 1:
            return 'MS(π/2)'
        return f'MS({self._exponent!r}π/2)'

    def __repr__(self) -> str:
        if self._exponent == 1:
            return 'cirq.ms(np.pi/2)'
        return f'cirq.ms({self._exponent!r}*np.pi/2)'

    def _json_dict_(self) -> Dict[str, Any]:
        return cirq.protocols.obj_to_dict_helper(self, ["rads"])

    @classmethod
    def _from_json_dict_(cls, rads: float, **kwargs: Any) -> Any:
        return cls(rads=rads)
@vtomole vtomole added good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. kind/feature-request Describes new functionality area/serialization area/json labels Oct 27, 2021
@vtomole vtomole changed the title Json serilize cirq.ion.ion_gates.MSGate Json serialize cirq.ion.ion_gates.MSGate Oct 27, 2021
@kyteinsky
Copy link

I don't understand this. If you got all the code ready, why don't you just commit it yourself 😄. Sorry if I'm being naive here.

@vtomole vtomole added the no QC knowledge needed Want to contribute to Cirq, but don't know quantum computing? This issue is for you. label Oct 28, 2021
@vtomole
Copy link
Collaborator Author

vtomole commented Oct 28, 2021

Fair question. The committed code will have to pass continuous integration checks.

@vtomole
Copy link
Collaborator Author

vtomole commented Oct 28, 2021

Took your advice :) #4605

@vtomole vtomole self-assigned this Oct 28, 2021
@kyteinsky
Copy link

Oh man, I just forked the repo. You beat me to it. 🤣

@vtomole
Copy link
Collaborator Author

vtomole commented Oct 28, 2021

Feel free to take on any of these: good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue.

CirqBot pushed a commit that referenced this issue Oct 29, 2021
rht pushed a commit to rht/Cirq that referenced this issue May 1, 2023
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this issue Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/json area/serialization good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. kind/feature-request Describes new functionality no QC knowledge needed Want to contribute to Cirq, but don't know quantum computing? This issue is for you.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants