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 missing factor of pi to parameterized Quil gate output #3206

Merged
merged 6 commits into from
Aug 12, 2020

Conversation

karalekas
Copy link
Contributor

This PR addresses a bug in Quil output.

The current code doesn't distinguish between Cirq's PowGate exponents and the angle parameters for Quil gates, but they differ by a factor of pi. This is verified by comparing program unitaries before and after conversion to Quil (see below).

Adds a test for this change that depends on pyQuil, so pyquil is added to pip-list-dev-tools.txt, but in the event that pyQuil is not available for import, the test is skipped.

Additionally sneaks in a change to Quil output for ISwapPowGate -- uses the native XY Quil gate when the exponent of the ISwapPowGate does not equal 1.

In [1]: import numpy as np
   ...: from pyquil import Program
   ...: from pyquil.simulation.tools import program_unitary
   ...:
   ...: from cirq import Circuit, LineQubit, QuilOutput, rx
   ...:
   ...: q0 = LineQubit(0)
   ...:
   ...: # rx(pi/2) creates an XPowGate with exponent 0.5 (and global_shift -0.5) 
   ...: circ = Circuit(rx(np.pi/2)(q0))
   ...: print(circ)
   ...:
   ...: pyquil_program = Program(str(QuilOutput(circ, (q0,))))
   ...: print(pyquil_program)
   ...:
   ...: pyquil_unitary = program_unitary(pyquil_program, n_qubits=1)
   ...: print(pyquil_unitary)
   ...:
   ...: cirq_unitary = circ.unitary()
   ...: print(cirq_unitary)
   ...:
   ...: np.allclose(pyquil_unitary, cirq_unitary)

on master:

0: ───Rx(0.5π)───
RX(0.5) 0

[[0.96891242+0.j         0.        -0.24740396j]
 [0.        -0.24740396j 0.96891242+0.j        ]]
[[0.70710678+0.j         0.        -0.70710678j]
 [0.        -0.70710678j 0.70710678+0.j        ]]
Out[1]: False

this PR:

0: ───Rx(0.5π)───
RX(pi/2) 0

[[0.70710678+0.j         0.        -0.70710678j]
 [0.        -0.70710678j 0.70710678+0.j        ]]
[[0.70710678+0.j         0.        -0.70710678j]
 [0.        -0.70710678j 0.70710678+0.j        ]]
Out[1]: True

Related to #1742, #2386, #2983

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@karalekas
Copy link
Contributor Author

@googlebot I signed it!

@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added cla: yes Makes googlebot stop complaining. and removed cla: no labels Aug 12, 2020
@vtomole vtomole self-requested a review August 12, 2020 16:10
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.

Thank you for all the fixes @karalekas, this looks good to me.

Copy link
Collaborator

@vtomole vtomole left a comment

Choose a reason for hiding this comment

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

LGTM

@vtomole vtomole added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Aug 12, 2020
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Aug 12, 2020
@CirqBot CirqBot merged commit b878d06 into quantumlib:master Aug 12, 2020
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Aug 12, 2020
@karalekas karalekas deleted the bugfix-quil-output-pi branch August 12, 2020 19:04
exAClior added a commit to exAClior/Cirq that referenced this pull request Aug 13, 2020
exAClior added a commit to exAClior/Cirq that referenced this pull request Aug 13, 2020
tonybruguier pushed a commit to tonybruguier/Cirq that referenced this pull request Aug 23, 2020
…b#3206)

This PR addresses a bug in Quil output.

The current code doesn't distinguish between Cirq's `PowGate` exponents and the angle parameters for Quil gates, but they differ by a factor of pi. This is verified by comparing program unitaries before and after conversion to Quil (see below).

Adds a test for this change that depends on pyQuil, so `pyquil` is added to `pip-list-dev-tools.txt`, but in the event that pyQuil is not available for import, the test is [skipped](https://docs.pytest.org/en/latest/skipping.html#skipping-on-a-missing-import-dependency).

Additionally sneaks in a change to Quil output for `ISwapPowGate` -- uses the native `XY` Quil gate when the exponent of the `ISwapPowGate` does not equal 1.

```python
In [1]: import numpy as np
   ...: from pyquil import Program
   ...: from pyquil.simulation.tools import program_unitary
   ...:
   ...: from cirq import Circuit, LineQubit, QuilOutput, rx
   ...:
   ...: q0 = LineQubit(0)
   ...:
   ...: # rx(pi/2) creates an XPowGate with exponent 0.5 (and global_shift -0.5) 
   ...: circ = Circuit(rx(np.pi/2)(q0))
   ...: print(circ)
   ...:
   ...: pyquil_program = Program(str(QuilOutput(circ, (q0,))))
   ...: print(pyquil_program)
   ...:
   ...: pyquil_unitary = program_unitary(pyquil_program, n_qubits=1)
   ...: print(pyquil_unitary)
   ...:
   ...: cirq_unitary = circ.unitary()
   ...: print(cirq_unitary)
   ...:
   ...: np.allclose(pyquil_unitary, cirq_unitary)
```

on `master`:

```
0: ───Rx(0.5π)───
RX(0.5) 0

[[0.96891242+0.j         0.        -0.24740396j]
 [0.        -0.24740396j 0.96891242+0.j        ]]
[[0.70710678+0.j         0.        -0.70710678j]
 [0.        -0.70710678j 0.70710678+0.j        ]]
Out[1]: False
```

this PR:

```
0: ───Rx(0.5π)───
RX(pi/2) 0

[[0.70710678+0.j         0.        -0.70710678j]
 [0.        -0.70710678j 0.70710678+0.j        ]]
[[0.70710678+0.j         0.        -0.70710678j]
 [0.        -0.70710678j 0.70710678+0.j        ]]
Out[1]: True
```

Related to quantumlib#1742, quantumlib#2386, quantumlib#2983
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.

None yet

5 participants