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

Add two-qubit unitary decomposition for (inverse) sqrt-iSWAP #4213

Merged
merged 23 commits into from Jul 3, 2021

Conversation

cduck
Copy link
Collaborator

@cduck cduck commented Jun 16, 2021

Part of #4083.

  • Adds the method cirq.two_qubit_matrix_to_sqrt_iswap_operations() to optimally decompose with SQRT_ISWAP or SQRT_ISWAP_INV gates.
  • Adds cirq.SQRT_ISWAP and cirq.SQRT_ISWAP_INV (aliases for cirq.ISWAP**0.5 and cirq.ISWAP**-0.5).

@cduck cduck requested review from vtomole and a team as code owners June 16, 2021 09:08
@cduck cduck requested a review from dstrain115 June 16, 2021 09:08
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Jun 16, 2021
@cduck cduck force-pushed the sqiswap branch 2 times, most recently from 8004fda to 8fd5c4e Compare June 17, 2021 09:55
@cduck cduck changed the title [WIP] Add two-qubit unitary decomposition for sqrt-iSWAP Add two-qubit unitary decomposition for sqrt-iSWAP Jun 17, 2021
@cduck
Copy link
Collaborator Author

cduck commented Jun 17, 2021

This PR depends on the bug fix in #4219 and should be merged afterward.

I need to rename the SQISWAP gate but otherwise this PR is ready to review.

Copy link
Collaborator

@tanujkhattar tanujkhattar left a comment

Choose a reason for hiding this comment

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

Left a first round of comments. Still need to take a closer look at the math in _decomp_2sqrt_iswap_matrices and _decomp_3sqrt_iswap_matrices.

cirq-core/cirq/optimizers/two_qubit_to_sqrt_iswap.py Outdated Show resolved Hide resolved
cirq-core/cirq/optimizers/two_qubit_to_sqrt_iswap.py Outdated Show resolved Hide resolved
cirq-core/cirq/optimizers/two_qubit_to_sqrt_iswap.py Outdated Show resolved Hide resolved
cirq-core/cirq/optimizers/two_qubit_to_sqrt_iswap.py Outdated Show resolved Hide resolved
cirq-core/cirq/optimizers/two_qubit_to_sqrt_iswap.py Outdated Show resolved Hide resolved
cirq-core/cirq/optimizers/two_qubit_to_sqrt_iswap.py Outdated Show resolved Hide resolved
@cduck
Copy link
Collaborator Author

cduck commented Jun 22, 2021

PTAL @tanujkhattar @dstrain115 @vtomole.

Thanks for all the comments. I removed the conditional gate cleanup code and always output nice XPow/YPow/ZPow gates but no global phase. Let me know if I missed updating any doc strings.

@cduck
Copy link
Collaborator Author

cduck commented Jun 24, 2021

Since no one re-reviewed this yet, I added the SQRT_ISWAP_INV gate to go along with the new SQRT_ISWAP in this PR (suggested here).

@cduck cduck changed the title Add two-qubit unitary decomposition for sqrt-iSWAP Add two-qubit unitary decomposition for (inverse) sqrt-iSWAP Jun 24, 2021
@cduck
Copy link
Collaborator Author

cduck commented Jun 25, 2021

Ping.

@balopat
Copy link
Contributor

balopat commented Jul 1, 2021

As Tanuj is on reduced schedule this week, I can help out with the review. I'll get to this today.

Copy link
Contributor

@balopat balopat left a comment

Choose a reason for hiding this comment

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

LGTM, I only have a couple of nits.

@balopat balopat dismissed tanujkhattar’s stale review July 2, 2021 04:26

Tanuj's comments were addressed

Copy link
Collaborator

@tanujkhattar tanujkhattar left a comment

Choose a reason for hiding this comment

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

LGTM % nits

cirq-core/cirq/optimizers/two_qubit_to_sqrt_iswap.py Outdated Show resolved Hide resolved
# No error tolerance needed
ieq1 = y > np.pi / 8
ieq2 = z < 0
if ieq1:
Copy link
Collaborator

Choose a reason for hiding this comment

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

IIUC, this is different (and better :)) from the reference implementation given in the paper in Algorithm-1. where they keep track of the intermediate single qubit rotations during canonicalization of the interaction coefficients. Instead of keeping track of the rotations, we simply recompute the kak and recurse for both single & two qubit case here.

I think it's worth adding a small description explaining the property of kak vectors that we can compose unitaries corresponding to (x1, y1, z1) and (x2, y2, z2) to form unitary corresponding to (x1 + x2, y1 + y2, z1 + z2), which is used here to break the problem into subtasks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll do that. I couldn't get this part of the algorithm from the paper to work so this is actually an improvement over the paper that supports decomposing any gate into 3-sqrt-iSWAP.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

To clarify your statement, this doesn't "recompute" kak, it just uses kak_canonicalize_vector to do the canonicalization automatically instead of manually like in Algorithm 1.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Comment added.

*,
single_qubit_gate_types=(cirq.ZPowGate, cirq.XPowGate, cirq.YPowGate),
two_qubit_gate=cirq.SQRT_ISWAP,
atol=1e-6,
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be nice if the default atol in cirq.allclose_up_to_global_phase is enough. Right now, the default atol returns false in many cases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agreed. There is some source of numerical error that I couldn't track down. It doesn't help that some the Cirq linalg functions don't pass through atol to half of the functions they use internally (so default to 1e-8).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a comment. It may be worth looking into the cause of this and also fixing various linalg functions that don't pass atol to their sub-functions.

@cduck
Copy link
Collaborator Author

cduck commented Jul 2, 2021

Thanks for the reviews. I'll try to do these minor fixes tomorrow otherwise early next week.

@cduck cduck added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Jul 3, 2021
@cduck cduck merged commit c80e463 into quantumlib:master Jul 3, 2021
@cduck cduck deleted the sqiswap branch July 3, 2021 22:55
CirqBot pushed a commit that referenced this pull request Jul 7, 2021
Follow up to #4213.  Fixes #4083.  (Also contains a one-line change to fix #4225.)

I'm open to name suggestions for `MergeInteractionsToSqrtIswap`.
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
…lib#4213)

- Add the method cirq.two_qubit_matrix_to_sqrt_iswap_operations() to optimally decompose with SQRT_ISWAP or SQRT_ISWAP_INV gates.
- Add cirq.SQRT_ISWAP and cirq.SQRT_ISWAP_INV (aliases for cirq.ISWAP**0.5 and cirq.ISWAP**-0.5).
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
Follow up to quantumlib#4213.  Fixes quantumlib#4083.  (Also contains a one-line change to fix quantumlib#4225.)

I'm open to name suggestions for `MergeInteractionsToSqrtIswap`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Tells CirqBot to sync and merge this PR. (If it's running.) cla: yes Makes googlebot stop complaining.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants