Skip to content

Commit 3ba4154

Browse files
committed
rework some of the standard gates init logic and expose sequence gates (for the qvm)
1 parent 31fbf5e commit 3ba4154

File tree

4 files changed

+53
-17
lines changed

4 files changed

+53
-17
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

+5-16
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@
253253
(mapcar #'constant parameters)
254254
(loop :for i :from (1- n) :downto 0 :collect (qubit i)))))))))
255255

256-
(defmethod gate-dimension ((gate sequence-gate-definition))
257-
(expt 2 (length (sequence-gate-definition-arguments gate))))
256+
(defmethod gate-dimension ((gate sequence-gate))
257+
(expt 2 (length (sequence-gate-definition-arguments (sequence-gate-gate-definition gate)))))
258258

259259
;;; END SEQUENCE GATE
260260

@@ -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

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

src/package.lisp

+3
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,9 @@
572572
#:exp-pauli-sum-gate-arguments ; READER
573573
#:exp-pauli-sum-gate-terms ; READER
574574

575+
#:sequence-gate ; CLASS
576+
#:sequence-gate-gate-definition ; READER
577+
575578
#:pauli-term ; STRUCT
576579
#:make-pauli-term ; FUNCTION
577580
#:pauli-term-pauli-word ; FUNCTION

0 commit comments

Comments
 (0)