Skip to content

Commit c5601f3

Browse files
committed
rework some of the standard gates init logic
1 parent 31fbf5e commit c5601f3

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

cl-quil.asd

+2-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@
167167
(:file "fusion")
168168
(:file "simplify-arithmetic")
169169
(:file "validate-sequence-gate")
170-
(:file "simplification-grab-bag")))))
170+
(:file "simplification-grab-bag")))
171+
(:file "initialize-standard-gates")))
171172

172173
;;; Contribs
173174

src/gates.lisp

+3-14
Original file line numberDiff line numberDiff line change
@@ -382,20 +382,9 @@ The Pauli sum is recorded as a list of PAULI-TERM objects, stored in the TERMS s
382382

383383
;;;;;;;;;;;;;;;;;;;;;; Default Gate Definitions ;;;;;;;;;;;;;;;;;;;;;;
384384

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

400389
(defun standard-gate-names ()
401390
"Query for the list of standard Quil gate names."

src/initialize-standard-gates.lisp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
;;;; initialize-standard-gates.lisp
2+
;;;;
3+
;;;; Author: Parker Williams
4+
5+
(in-package #:cl-quil.frontend)
6+
7+
(defun read-standard-gates-from-file (&optional (stdgates-file (asdf:system-relative-pathname "cl-quil" "src/quil/stdgates.quil")))
8+
"Produces a table of default gate definitions, mapping string name to a GATE-DEFINITION object."
9+
(let* ((gate-defs
10+
(remove-if-not (lambda (obj) (typep obj 'gate-definition))
11+
(parse-quil-into-raw-program
12+
(a:read-file-into-string stdgates-file))))
13+
(parsed-program (make-instance 'parsed-program :executable-code #()
14+
:memory-definitions '()
15+
:circuit-definitions '()
16+
:gate-definitions gate-defs)))
17+
18+
(resolve-objects parsed-program)
19+
(validate-defgate-loops parsed-program)
20+
(parsed-program-gate-definitions parsed-program)))
21+
22+
(unless (boundp' **default-gate-definitions**)
23+
(eval-when (:compile-toplevel :load-toplevel :execute)
24+
(let ((stdgates-file (asdf:system-relative-pathname
25+
"cl-quil" "src/quil/stdgates.quil")))
26+
(format t "~&; loading standard gates from ~A~%" stdgates-file)
27+
(setf **default-gate-definitions**
28+
(let ((table (make-hash-table :test 'equal)))
29+
(dolist (gate-def (read-standard-gates-from-file stdgates-file) table)
30+
(setf (gethash (gate-definition-name gate-def) table)
31+
gate-def)))))))
32+
33+

0 commit comments

Comments
 (0)