Skip to content

Commit

Permalink
Exposed various options in RCP entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bramathon authored and stylewarning committed Sep 7, 2022
1 parent f7fcd81 commit 09b2456
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 11 deletions.
12 changes: 11 additions & 1 deletion app/src/entry-point.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@
&key
protoquil
state-aware
enable-approximate-compilation
compressor-passes
rewriting-peephole-size
global-queue-tolerance-threshold
deterministic
verbose
gate-whitelist
gate-blacklist)
Expand All @@ -482,7 +487,12 @@ Returns a values tuple (PROCESSED-PROGRAM, STATISTICS), where PROCESSED-PROGRAM
(let* ((statistics (make-hash-table :test #'equal))
(cl-quil::*compiler-noise* verbose)
(*random-state* (make-random-state t))
(cl-quil::*enable-state-prep-compression* state-aware))
(quil::*enable-state-prep-compression* state-aware)
(quil::*enable-approximate-compilation* enable-approximate-compilation)
(quil::*compressor-passes* (or compressor-passes quil::*compressor-passes*))
(quil::*rewriting-peephole-size* (or rewriting-peephole-size quil::*rewriting-peephole-size*))
(quil::*global-queue-tolerance-threshold* (or global-queue-tolerance-threshold quil::*global-queue-tolerance-threshold*))
)
;; do the compilation
(multiple-value-bind (processed-program topological-swaps)
(compiler-hook program chip-specification :protoquil protoquil :destructive t)
Expand Down
29 changes: 23 additions & 6 deletions app/src/rpc-server.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@
:|topological_swaps| (gethash "topological_swaps" statistics)
:|qpu_runtime_estimation| (gethash "qpu_runtime_estimation" statistics)))

;; TODO: rework the structure of process-program so that the JSON junk is only
;; done in web-server.lisp, and this doesn't have to do back-translation.
(defun quil-to-native-quil-handler (request &key protoquil state-aware)
"Traditional QUILC invocation: compiles a Quil program to native Quil, as specified by an ISA."
(defun quil-to-native-quil-handler (request &key protoquil state-aware enable-approximate-compilation compressor-passes rewriting-peephole-size global-queue-tolerance-threshold seed)
"Traditional QUILC invocation: compiles a Quil program to native Quil, as specified by an ISA but with more options."
(check-type request rpcq::|NativeQuilRequest|)
(let* ((quil-program (safely-parse-quil (rpcq::|NativeQuilRequest-quil| request)))
(target-device (rpcq::|NativeQuilRequest-target_device| request))
Expand All @@ -66,7 +64,21 @@
(state-aware (ecase state-aware
((nil) *state-aware*)
(:false nil)
(t t))))
(t t)))
(enable-approximate-compilation (ecase enable-approximate-compilation
((nil) quil::*enable-approximate-compilation*)
(:false nil)
(t t)))
(compressor-passes (etypecase compressor-passes
(null quil::*compressor-passes*)
(integer compressor-passes)))
(rewriting-peephole-size (etypecase rewriting-peephole-size
(null quil::*rewriting-peephole-size*)
(integer rewriting-peephole-size)))
(global-queue-tolerance-threshold (etypecase global-queue-tolerance-threshold
(null quil::*global-queue-tolerance-threshold*)
(integer global-queue-tolerance-threshold)))
)
(unless (slot-boundp cache 'addresser-state)
(setf (cached-chip-addresser-state cache)
(make-instance cl-quil::*default-addresser-state-class*
Expand All @@ -76,7 +88,12 @@
(process-program quil-program chip-specification
:protoquil protoquil
:state-aware state-aware
:verbose cl-quil::*compiler-noise*)
:enable-approximate-compilation enable-approximate-compilation
:compressor-passes compressor-passes
:rewriting-peephole-size rewriting-peephole-size
:global-queue-tolerance-threshold global-queue-tolerance-threshold
:verbose quil::*compiler-noise*
)
(when protoquil
(setf (gethash "qpu_runtime_estimation" statistics-dict)
(runtime-estimation processed-program)))
Expand Down
48 changes: 48 additions & 0 deletions app/tests/rpcq-tests.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,54 @@
(rpcq::|NativeQuilResponse-metadata| server-response))))
(is (quil::matrix-equality mat1 mat2)))))))

(deftest test-quil-to-native-quil-options ()
"Test that the \"quil-to-native-quil\" endpoint compiles to a program whose matrix representation is equivalent to the input program for all optional settings."
(flet ((plist-isa-subtable (&rest p) (a:plist-hash-table p :test #'equalp)))
(with-random-rpc-client (client)
(let* ((quil "H 0")
(isa (plist-isa-subtable
"1Q" (plist-isa-subtable
"0" (make-hash-table)
"1" (make-hash-table))
"2Q" (plist-isa-subtable
"0-1" (make-hash-table))))
(specs (plist-isa-subtable
"1Q" (plist-isa-subtable
"0" (plist-isa-subtable "f1QRB" 0.98)
"1" (plist-isa-subtable "f1QRB" 0.98))
"2Q" (plist-isa-subtable
"0-1" (make-hash-table))))
(target-device (make-instance 'rpcq::|TargetDevice|
:|isa| isa
:|specs| specs))
(server-payload (make-instance 'rpcq::|NativeQuilRequest|
:|quil| quil
:|target_device| target-device))
(pp (quil::parse-quil quil))
)
(labels ((verify-programs-match (candidate-program reference-program)
(multiple-value-bind (mat1 mat2)
(quil::matrix-rescale (quil::make-matrix-from-quil
(coerce (quil:parsed-program-executable-code reference-program) 'list))
(quil::make-matrix-from-quil
(coerce (quil:parsed-program-executable-code candidate-program) 'list)))
(setf mat1 (quil::scale-out-matrix-phases mat1 mat2))
(is (quil::matrix-equality mat1 mat2)))))
(and (mapcar (lambda (options)
(destructuring-bind (protoquil state-aware compressor-passes rewriting-peephole-size global-queue-tolerance-threshold) options
(let ((candidate-program
(quil::parse-quil
(rpcq::|NativeQuilResponse-quil|
(rpcq:rpc-call client
"quil-to-native-quil" server-payload
:protoquil protoquil
:state-aware state-aware
:compressor-passes compressor-passes
:rewriting-peephole-size rewriting-peephole-size
:global-queue-tolerance-threshold global-queue-tolerance-threshold)))))
(verify-programs-match candidate-program pp))))
(a:map-product #'list '(nil t) '(nil t) '(0 1 2) '(0 2 4) '(0 2 4)))))))))

(deftest test-quil-to-native-quil-protoquil-endpoint ()
"Test that the \"quil-to-native-quil\" endpoint will compile protoquil when given :PROTOQUIL T."
(with-random-rpc-client (client)
Expand Down
11 changes: 11 additions & 0 deletions src/ast.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,17 @@ Examples:
(dolist (defn defns)
(print-instruction defn stream)
(terpri stream))))

;; Ensure that any non-standard gates in the program are defined
;; TODO: handle non-simple gates
(let ((defined-gate-names (append (mapcar #'gate-definition-name (parsed-program-gate-definitions pp)) (loop for k being the hash-key of **default-gate-definitions** collect k)))
(defgates (parsed-program-gate-definitions pp))
(simple-gates (map 'list #'gate-application-gate (remove-if-not (lambda (inst) (and (typep inst 'gate-application) (typep (gate-application-gate inst) 'simple-gate))) (parsed-program-executable-code pp)))))
(loop for gate in simple-gates
when (not (member (slot-value gate 'name) defined-gate-names))
do (push (make-instance 'static-gate-definition :name (slot-value gate 'name) :entries (coerce (slot-value (simple-gate-matrix gate) 'magicl::storage) 'list)) defgates))
(setf (parsed-program-gate-definitions pp) defgates))


(print-definitions (parsed-program-memory-definitions pp))
;; instructions and single-line definitions (e.g. DECLARE) do not introduce newlines
Expand Down
5 changes: 1 addition & 4 deletions src/compressor/compressor.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,7 @@ other's."
instructions
:relabeling (standard-qubit-relabeler qubits-on-obj))))
(expand-to-native-instructions
(list (make-instance 'gate-application
:operator #.(named-operator "WHOLEPROGRAM")
:arguments (mapcar #'qubit qubits-on-obj)
:gate (make-instance 'simple-gate :matrix matrix)))
(list (apply 'anon-gate (append (list "UNITARY" matrix) (mapcar #'qubit qubits-on-obj))))
chip-specification))))

(destructuring-bind (start-wf wf-qc)
Expand Down
1 change: 1 addition & 0 deletions tests/qpu-test-files/Aspen-11-40Q-A.qpu

Large diffs are not rendered by default.

Loading

0 comments on commit 09b2456

Please sign in to comment.