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

Parse OpenQASM #495

Merged
merged 65 commits into from
Dec 18, 2019
Merged

Parse OpenQASM #495

merged 65 commits into from
Dec 18, 2019

Conversation

notmgsk
Copy link
Member

@notmgsk notmgsk commented Dec 4, 2019

This (late) PR implements all functionality described in the OpenQASM spec.

The following is a somewhat complex example, demonstrating a ripple-carry adder OpenQASM program:

(qvm-app::print-classical-memory (qvm:run-program 10 (parse-qasm-string "
// quantum ripple-carry adder from Cuccaro et al, quant-ph/0410184
OPENQASM 2.0;
include \"qelib1.inc\";
gate majority a,b,c 
{ 
  cx c,b; 
  cx c,a; 
  ccx a,b,c; 
}
gate unmaj a,b,c 
{ 
  ccx a,b,c; 
  cx c,a; 
  cx a,b; 
}
qreg cin[1];
qreg a[4];
qreg b[4];
qreg cout[1];
creg ans[5];
// set input states
x a[0]; // a = 0001
x b;    // b = 1111
// add a to b, storing result in b
majority cin[0],b[0],a[0];
majority a[0],b[1],a[1];
majority a[1],b[2],a[2];
majority a[2],b[3],a[3];
cx a[3],cout[0];
unmaj a[2],b[3],a[3];
unmaj a[1],b[2],a[2];
unmaj a[0],b[1],a[1];
unmaj cin[0],b[0],a[0];
measure b[0] -> ans[0];
measure b[1] -> ans[1];
measure b[2] -> ans[2];
measure b[3] -> ans[3];
measure cout[0] -> ans[4];")))
Classical memory (low -> high indexes):
    ans: 0 0 0 0 1

There are some things to address:

  • There should be more error checking, e.g. making sure the sizes of registers are appropriate for the gate application.
  • In parse-application I explicitly define all of the gates defined in OpenQASM's qelib1.inc. This could be simplified (and instead just define the fundamental gates U and CX), but I'm not sure (because I haven't tried) whether that will Just Work.
  • Automatically invoke the parser from parse-quil if the first (non-comment) line is OPENQASM 2.0.

I think the test suite is generally in a good state. It covers all positive behaviours listed in the spec but it doesn't test for misuse.

@notmgsk notmgsk requested a review from a team as a code owner December 4, 2019 00:41
Copy link
Member

@stylewarning stylewarning left a comment

Choose a reason for hiding this comment

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

First round of review.

src/ast.lisp Outdated Show resolved Hide resolved
src/cl-quil.lisp Outdated Show resolved Hide resolved
src/package.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Show resolved Hide resolved
src/qasm.lisp Show resolved Hide resolved
src/qasm.lisp Outdated
(destructuring-bind (if-toks . rest-lines)
tok-lines
(pop if-toks)
(q2q-check-token-type (pop if-toks) ':LEFT-PAREN)
Copy link
Member

Choose a reason for hiding this comment

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

sounds like we can maybe use token destructuring?

src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Show resolved Hide resolved
Copy link
Contributor

@ecpeterson ecpeterson left a comment

Choose a reason for hiding this comment

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

Commenting to let you know that I've looked through this, & (TODOs aside) things look pretty good.

@stylewarning
Copy link
Member

@notmgsk Ping when re-review is desired.

Copy link
Contributor

@appleby appleby left a comment

Choose a reason for hiding this comment

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

Made it about half-way through. All tuckered out. Time for Christmas cookies.

tests/qasm-tests.lisp Show resolved Hide resolved
tests/qasm-tests.lisp Outdated Show resolved Hide resolved
tests/qasm-tests.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated
;; a[0] -> b[0]`.
;;
;; TODO I wrote this while very tired. Untested.
(let ((ts (mapcar #'alexandria:ensure-list (remove '_ token-idents))))
Copy link
Contributor

Choose a reason for hiding this comment

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

feels like there should be a smattering of once-onlys but I didn't check carefully

src/qasm.lisp Show resolved Hide resolved
src/qasm.lisp Show resolved Hide resolved
@notmgsk notmgsk force-pushed the feature/qasm branch 2 times, most recently from 0717f2a to b26089e Compare December 13, 2019 19:21
Copy link
Contributor

@appleby appleby left a comment

Choose a reason for hiding this comment

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

There is some trailing whitespace in both qasm.lisp and qasm-tests.lisp. M-x whitespace-cleanup and/or M-x delete-trailing-whitespace to the rescue.

I don't feel like any of these are blockers per se, but I'll delay approval to maintain Code Review Best Practices®️

I'm also vaguely uneasy about *creg-names* and *qreg-names* but I don't know why. maybe I'm just hungry. time for lunch.

src/qasm.lisp Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
@notmgsk
Copy link
Member Author

notmgsk commented Dec 16, 2019

@appleby @stylewarning ping for re-review at you leisure

Copy link
Contributor

@appleby appleby left a comment

Choose a reason for hiding this comment

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

cool stuff

src/qasm.lisp Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/cl-quil.lisp Outdated Show resolved Hide resolved
src/cl-quil.lisp Outdated Show resolved Hide resolved
Copy link
Member

@stylewarning stylewarning left a comment

Choose a reason for hiding this comment

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

Some critical things, some not so critical things.

I think this PR has been a little too liberal with TODOs. On one hand they're good first issues for newcomers, on the other hand they're debt that people may not feel motivated to fix.

src/cl-quil.lisp Outdated Show resolved Hide resolved
src/cl-quil.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
src/qasm.lisp Outdated Show resolved Hide resolved
Copy link
Contributor

@appleby appleby left a comment

Choose a reason for hiding this comment

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

Previously approved. Just chiming in to say the latest round of changes lgtm.

src/qasm.lisp Show resolved Hide resolved
src/cl-quil.lisp Show resolved Hide resolved
src/qasm.lisp Show resolved Hide resolved
Copy link
Contributor

@jmbr jmbr left a comment

Choose a reason for hiding this comment

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

Comments (1/N).

src/cl-quil.lisp Show resolved Hide resolved
src/qasm.lisp Show resolved Hide resolved
src/qasm.lisp Show resolved Hide resolved
src/qasm.lisp Show resolved Hide resolved
src/qasm.lisp Show resolved Hide resolved
src/qasm.lisp Show resolved Hide resolved
Copy link
Contributor

@jmbr jmbr left a comment

Choose a reason for hiding this comment

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

Comments (N/N).

LGTM.

(defun parse-qasm (string)
"Parse STRING into a PARSED-PROGRAM object, applying all transforms."
(let ((pp (quil::resolve-objects (parse-qasm-into-raw-program string))))
(setf pp (quil::transform 'quil::expand-circuits pp))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: fuse setf forms.

Comment on lines +793 to +794
(code (parse-qasm-body string)))
(setf code (quil::process-includes code))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: compose parse-qasm-body with quil::process-includes in the let* form above and discard the setf.

Copy link
Contributor

@braised-babbage braised-babbage left a comment

Choose a reason for hiding this comment

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

LGTM

*qreg-names*
*qubit-count*)))

(defun parse-qasm (string)
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor request: a keyword argument transforms which defaults a list of the three you currently use

@stylewarning stylewarning merged commit 8ae5c87 into master Dec 18, 2019
@stylewarning stylewarning deleted the feature/qasm branch December 18, 2019 19:24
@notmgsk notmgsk mentioned this pull request Dec 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants