Skip to content

Handle all possible 1-qubit Clifford gates in Clifford simulator #2803

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

Merged
merged 40 commits into from
Mar 29, 2020

Conversation

fedimser
Copy link
Contributor

@fedimser fedimser commented Mar 3, 2020

I used the fact that all 1-qubit gates can be generated by H and S gates, and there are only 24 of them (up to phase) - proof.
So, I can explicitly generate all those 24 gates as sequences of H and S gates (see generation code).
Then, I can compare given gate with all of those 24 gates and check if they differ only in phase. If so, I can implement given gate using existing implementation of S and H gates.

So, this PR ensures that Clifford simulator supports all possible 1-qubit Clifford gates.

Also updated function in mux.py which checks if circuit is Clifford circuit.
There was one test of determinism in mux_test.py which started failing, because now it's simulated on Clifford simulator. Currently Clifford simulator doesn't support taking seed parameter, so I changed that test to use another gate, which is not Clifford gate.

@googlebot googlebot added the cla: yes Makes googlebot stop complaining. label Mar 3, 2020
@fedimser
Copy link
Contributor Author

fedimser commented Mar 3, 2020

This PR is related to issue #2423.

@fedimser
Copy link
Contributor Author

fedimser commented Mar 3, 2020

@smitsanghavi, please have a look (looks like I can't add reviewers to this PR).

Copy link
Collaborator

@smitsanghavi smitsanghavi left a comment

Choose a reason for hiding this comment

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

Added a few high level comments before diving deeper.

@@ -47,8 +48,16 @@ def __init__(self):
self.init = True

@staticmethod
def get_supported_gates() -> List['cirq.Gate']:
return [cirq.X, cirq.Y, cirq.Z, cirq.H, cirq.S, cirq.CNOT, cirq.CZ]
def is_supported_operation(op: 'cirq.Operation') -> bool:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This handling is being replaced with a has_stabilizer_effect protocol in #2760

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That PR have not been merged yet. As soon as it's merged, I'll update this PR to extend _has_stabilizer_effect_ on all matrices and to use it to check if gate can be simulated by Clifford simulator.

By the way, I think there is more elgeant way of implementing this - instead of defining _has_stabilizer_effect_ at every gate, we can do the following:
In has_stabilizer_effect_protocol.py check if gate has 2x2 uniary matrix (name it U).
If so, check that U@[email protected]().T and U@[email protected] are in set X, -X, Y, -Y, Z, -Z.

This is also better than trying to match unitary with 24 matrices, as I am doing in this PR.

@fedimser fedimser requested a review from smitsanghavi March 11, 2020 10:20
Copy link
Contributor

@Strilanc Strilanc 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 functionality belongs on cirq.SingleQubitCliffordGate. Define a static method on that class called from_unitary which takes a 2x2 unitary matrix or object with a 2x2 unitary matrix and returns an instance of cirq.SingleQubitCliffordGate.

You can also define a cirq.SingleQubitCliffordGate.ALL field which enumerates the 24 possible instances.

@fedimser
Copy link
Contributor Author

I added a method from_unitary to SingleQubitCliffordGate.
Since SingleQubitCliffordGate already has method decompose_rotation, and we know how to decompose rotations into H and S gates, I don't need precalculated list of all possible Clifford gates anymore.
Please take another look.

@fedimser fedimser requested a review from Strilanc March 15, 2020 21:13
@fedimser fedimser requested a review from smitsanghavi March 18, 2020 08:34
Copy link
Collaborator

@smitsanghavi smitsanghavi left a comment

Choose a reason for hiding this comment

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

LGTM. Please wait for Craig's approval too.

cduck
cduck previously requested changes Mar 19, 2020
Copy link
Collaborator

@cduck cduck left a comment

Choose a reason for hiding this comment

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

I suggest also adding tests for the Clifford operations GlobalPhaseOperation and PauliString. I think these may hit corner cases that aren't handled yet.

@fedimser
Copy link
Contributor Author

I added tests for PauliString and GlobalPhaseOperation.

GlobalPhaseOperation doesn't have any qubits, so it wasn't handled by my current implementation (which works for ops on exactly 1 qubit). So I just handled it separately (without involving single qubit Clifford gate).

@fedimser fedimser requested a review from cduck March 19, 2020 22:58
@cduck cduck dismissed their stale review March 20, 2020 21:52

Fixed.

@fedimser
Copy link
Contributor Author

Craig, I can't merge this PR because you requested changes, but I addressed them. I think that some action from you is required to acknowledge that changes were made.
@Strilanc

Copy link
Contributor

@Strilanc Strilanc left a comment

Choose a reason for hiding this comment

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

LGTM (after merge conflict is fixed)

@fedimser
Copy link
Contributor Author

Can we please merge this now?
I understand that I also need to update has_stabilizer_effect (because PR #2760 is now merged) and use that method to determine whether operation is supported by the CLifford simulator, but I'd rather do it in a separate, simple PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Makes googlebot stop complaining.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants