Skip to content

Commit

Permalink
rework some of the standard gates init logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Spin1Half committed Apr 8, 2022
1 parent 31fbf5e commit c5601f3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
3 changes: 2 additions & 1 deletion cl-quil.asd
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@
(:file "fusion")
(:file "simplify-arithmetic")
(:file "validate-sequence-gate")
(:file "simplification-grab-bag")))))
(:file "simplification-grab-bag")))
(:file "initialize-standard-gates")))

;;; Contribs

Expand Down
17 changes: 3 additions & 14 deletions src/gates.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -382,20 +382,9 @@ The Pauli sum is recorded as a list of PAULI-TERM objects, stored in the TERMS s

;;;;;;;;;;;;;;;;;;;;;; Default Gate Definitions ;;;;;;;;;;;;;;;;;;;;;;

;;; Load all of the standard gates from src/quil/stdgates.quil
(global-vars:define-global-var **default-gate-definitions**
(let ((stdgates-file (asdf:system-relative-pathname
"cl-quil" "src/quil/stdgates.quil")))
(format t "~&; loading standard gates from ~A~%" stdgates-file)
(let ((table (make-hash-table :test 'equal))
(gate-defs
(remove-if-not (lambda (obj) (typep obj 'gate-definition))
(parse-quil-into-raw-program
(a:read-file-into-string stdgates-file)))))
(dolist (gate-def gate-defs table)
(setf (gethash (gate-definition-name gate-def) table)
gate-def))))
"A table of default gate definitions, mapping string name to a GATE-DEFINITION object.")
;;; This will be initialized to a table of default gate definitions in
;;; initialize-standard-gates.lisp
(defvar **default-gate-definitions**)

(defun standard-gate-names ()
"Query for the list of standard Quil gate names."
Expand Down
33 changes: 33 additions & 0 deletions src/initialize-standard-gates.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
;;;; initialize-standard-gates.lisp
;;;;
;;;; Author: Parker Williams

(in-package #:cl-quil.frontend)

(defun read-standard-gates-from-file (&optional (stdgates-file (asdf:system-relative-pathname "cl-quil" "src/quil/stdgates.quil")))
"Produces a table of default gate definitions, mapping string name to a GATE-DEFINITION object."
(let* ((gate-defs
(remove-if-not (lambda (obj) (typep obj 'gate-definition))
(parse-quil-into-raw-program
(a:read-file-into-string stdgates-file))))
(parsed-program (make-instance 'parsed-program :executable-code #()
:memory-definitions '()
:circuit-definitions '()
:gate-definitions gate-defs)))

(resolve-objects parsed-program)
(validate-defgate-loops parsed-program)
(parsed-program-gate-definitions parsed-program)))

(unless (boundp' **default-gate-definitions**)
(eval-when (:compile-toplevel :load-toplevel :execute)
(let ((stdgates-file (asdf:system-relative-pathname
"cl-quil" "src/quil/stdgates.quil")))
(format t "~&; loading standard gates from ~A~%" stdgates-file)
(setf **default-gate-definitions**
(let ((table (make-hash-table :test 'equal)))
(dolist (gate-def (read-standard-gates-from-file stdgates-file) table)
(setf (gethash (gate-definition-name gate-def) table)
gate-def)))))))


0 comments on commit c5601f3

Please sign in to comment.