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 initial support for converting Quil programs into Cirq circuits #3211

Merged
merged 8 commits into from
Aug 13, 2020

Conversation

karalekas
Copy link
Contributor

@karalekas karalekas commented Aug 13, 2020

This PR adds Quil import support in a way analogous to the existing QASM import functionality, by leveraging the Quil parser that already exists in pyQuil.

By using circuit_from_quil from the cirq.contrib.quil_import module, all Quil standard gates, measurements, and simple (non-parameterized) DEFGATEs can be converted to Cirq operations (see below).

Additionally adds Quil output for special cases of TwoQubitDiagonalGate, and fixes a bug in the way DEFGATE blocks were being generated by the Quil outputter.

In [1]: from cirq.contrib.quil_import import circuit_from_quil

In [2]: circuit_from_quil("""
   ...: DECLARE ro BIT[3]
   ...: I 0
   ...: I 1
   ...: I 2
   ...: X 0
   ...: Y 1
   ...: Z 2
   ...: H 0
   ...: S 1
   ...: T 2
   ...: PHASE(pi/8) 0
   ...: PHASE(pi/8) 1
   ...: PHASE(pi/8) 2
   ...: RX(pi/2) 0
   ...: RY(pi/2) 1
   ...: RZ(pi/2) 2
   ...: CZ 0 1
   ...: CNOT 1 2
   ...: CPHASE(pi/2) 0 1
   ...: SWAP 1 2
   ...: ISWAP 0 1
   ...: XY(pi/2) 1 2
   ...: CCNOT 0 1 2
   ...: CSWAP 0 1 2
   ...: MEASURE 0 ro[0]
   ...: MEASURE 1 ro[1]
   ...: MEASURE 2 ro[2]
   ...: """)
Out[2]:
0: ───I───X───H───Z^(1/8)───Rx(0.5π)───@───────@───────────iSwap───────────────@───@───M('ro[0]')───
                                       │       │           │                   │   │
1: ───I───Y───S───Z^(1/8)───Ry(0.5π)───@───@───@^0.5───×───iSwap───iSwap───────@───×───M('ro[1]')───
                                           │           │           │           │   │
2: ───I───Z───T───Z^(1/8)───Rz(0.5π)───────X───────────×───────────iSwap^0.5───X───×───M('ro[2]')───

In [3]: circuit_from_quil("""
   ...: DEFGATE MYZ:
   ...:     1,0
   ...:     0,-1
   ...:
   ...: MYZ 0
   ...: """)
Out[3]:
      ┌     ┐
0: ───│ 1  0│───
      │ 0 -1│
      └     ┘

Related to #1742, #2386, #2983

@googlebot googlebot added the cla: yes Makes googlebot stop complaining. label Aug 13, 2020
@vtomole vtomole self-requested a review August 13, 2020 17:18
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
Copy link
Collaborator

vtomole commented Aug 13, 2020

@karalekas Please add some docs to https://github.com/quantumlib/Cirq/blob/master/docs/interop.ipynb

@vtomole vtomole added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Aug 13, 2020
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Aug 13, 2020
@CirqBot CirqBot merged commit 1aabcb7 into quantumlib:master Aug 13, 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 13, 2020
@karalekas karalekas deleted the contrib-quil-import branch August 13, 2020 18:59
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
…uantumlib#3211)

This PR adds Quil import support in a way analogous to the existing QASM import functionality, by leveraging the Quil parser that already exists in pyQuil.

By using `circuit_from_quil` from the `cirq.contrib.quil_import` module, all Quil standard gates, measurements, and simple (non-parameterized) DEFGATEs can be converted to Cirq operations (see below).

Additionally adds Quil output for special cases of `TwoQubitDiagonalGate`, and fixes a bug in the way DEFGATE blocks were being generated by the Quil outputter.

```python
In [1]: from cirq.contrib.quil_import import circuit_from_quil

In [2]: circuit_from_quil("""
   ...: DECLARE ro BIT[3]
   ...: I 0
   ...: I 1
   ...: I 2
   ...: X 0
   ...: Y 1
   ...: Z 2
   ...: H 0
   ...: S 1
   ...: T 2
   ...: PHASE(pi/8) 0
   ...: PHASE(pi/8) 1
   ...: PHASE(pi/8) 2
   ...: RX(pi/2) 0
   ...: RY(pi/2) 1
   ...: RZ(pi/2) 2
   ...: CZ 0 1
   ...: CNOT 1 2
   ...: CPHASE(pi/2) 0 1
   ...: SWAP 1 2
   ...: ISWAP 0 1
   ...: XY(pi/2) 1 2
   ...: CCNOT 0 1 2
   ...: CSWAP 0 1 2
   ...: MEASURE 0 ro[0]
   ...: MEASURE 1 ro[1]
   ...: MEASURE 2 ro[2]
   ...: """)
Out[2]:
0: ───I───X───H───Z^(1/8)───Rx(0.5π)───@───────@───────────iSwap───────────────@───@───M('ro[0]')───
                                       │       │           │                   │   │
1: ───I───Y───S───Z^(1/8)───Ry(0.5π)───@───@───@^0.5───×───iSwap───iSwap───────@───×───M('ro[1]')───
                                           │           │           │           │   │
2: ───I───Z───T───Z^(1/8)───Rz(0.5π)───────X───────────×───────────iSwap^0.5───X───×───M('ro[2]')───

In [3]: circuit_from_quil("""
   ...: DEFGATE MYZ:
   ...:     1,0
   ...:     0,-1
   ...:
   ...: MYZ 0
   ...: """)
Out[3]:
      ┌     ┐
0: ───│ 1  0│───
      │ 0 -1│
      └     ┘
```

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

4 participants