Skip to content

Commit

Permalink
Remove sample-rate from DEFWAVEFORM
Browse files Browse the repository at this point in the history
  • Loading branch information
macrologist committed Apr 12, 2024
1 parent cad60be commit 668b5f5
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 53 deletions.
9 changes: 5 additions & 4 deletions src/quilt/analysis/fill-delays.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@
(capture (capture-waveform instr)))))
(waveform-ref-name-resolution wf-ref)))

(defun waveform-active-duration (wf-or-wf-defn)
(defun waveform-active-duration (wf-or-wf-defn frame)
"Get the active duration of the waveform or waveform definition, in seconds.
If WF-OR-WF-DEFN is a waveform definition, SAMPLE-RATE (Hz) must be non-null. "
If WF-OR-WF-DEFN is a waveform definition, then FRAME's
SAMPLE-RATE (Hz) must be non-null. "
(etypecase wf-or-wf-defn
(standard-waveform (constant-value (waveform-duration wf-or-wf-defn)))
(waveform-definition
(/ (length (waveform-definition-entries wf-or-wf-defn))
(constant-value (waveform-definition-sample-rate wf-or-wf-defn))))))
(constant-value (frame-sample-rate frame))))))

(defparameter *quilt-seemingly-instantenous-duration* 0.0d0
"A numerical value representing the duration of seemingly instantenous operations, in seconds. This might be zero, and it might not be!")
Expand All @@ -48,7 +49,7 @@ If WF-OR-WF-DEFN is a waveform definition, SAMPLE-RATE (Hz) must be non-null. "
"Get the duration of the specified Quilt instruction INSTR if it is well defined, or NIL otherwise."
(typecase instr
((or pulse capture)
(waveform-active-duration (resolved-waveform instr)))
(waveform-active-duration (resolved-waveform instr) (pulse-op-frame instr)))
(delay
(constant-value (delay-duration instr)))
(raw-capture
Expand Down
21 changes: 11 additions & 10 deletions src/quilt/ast.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
hash
(qubit-index qubit))))))

(defun frame-sample-rate (frame)
(frame-definition-sample-rate (frame-name-resolution frame)))

(defmethod print-instruction-generic ((thing frame) (stream stream))
(format stream "~{~/cl-quil:instruction-fmt/ ~}\"~A\""
(frame-qubits thing)
Expand Down Expand Up @@ -214,6 +217,11 @@
(raw-capture-duration instr)
(raw-capture-memory-ref instr)))

(defgeneric pulse-op-frame (op)
(:method ((op raw-capture)) (raw-capture-frame op))
(:method ((op capture)) (capture-frame op))
(:method ((op pulse)) (pulse-frame op)))

;;; Timing Control and Synchronization

(defclass delay (instruction)
Expand Down Expand Up @@ -336,10 +344,6 @@
:reader waveform-definition-entries
:type list
:documentation "The raw IQ values of the waveform being defined.")
(sample-rate :initarg :sample-rate
:reader waveform-definition-sample-rate
:type constant
:documentation "The sample rate for which the waveform is applicable.")
(context :initarg :context
:type lexical-context
:accessor lexical-context
Expand All @@ -359,29 +363,26 @@
:documentation "A list of symbol parameter names."))
(:documentation "A waveform definition that has named parameters."))

(defun make-waveform-definition (name parameters entries sample-rate &key context)
(defun make-waveform-definition (name parameters entries &key context)
(check-type name string)
(check-type parameters symbol-list)
(if (not (endp parameters))
(make-instance 'parameterized-waveform-definition
:name name
:parameters parameters
:entries entries
:sample-rate sample-rate
:context context)
(make-instance 'static-waveform-definition
:name name
:entries entries
:sample-rate sample-rate
:context context)))

(defmethod print-instruction-generic ((defn waveform-definition) (stream stream))
(format stream "DEFWAVEFORM ~A~@[(~{%~A~^, ~})~] ~/cl-quil:instruction-fmt/:"
(format stream "DEFWAVEFORM ~A~@[(~{%~A~^, ~})~]:"
(waveform-definition-name defn)
(if (typep defn 'static-waveform-definition)
'()
(waveform-definition-parameters defn))
(waveform-definition-sample-rate defn))
(waveform-definition-parameters defn)))
(format stream "~% ~{~A~^, ~}"
(mapcar (lambda (z)
(with-output-to-string (s)
Expand Down
1 change: 0 additions & 1 deletion src/quilt/package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@
#:waveform-definition-name ; READER
#:waveform-definition-entries ; READER
#:waveform-definition-parameters ; READER
#:waveform-definition-sample-rate ; READER

#:calibration-definition ; ABSTRACT CLASS
#:gate-calibration-definition ; CLASS
Expand Down
13 changes: 7 additions & 6 deletions src/quilt/parser.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,7 @@
(quil-parse-error "EOF reached when waveform definition expected."))

;; Get the parameter and body lines
(let (name
sample-rate)
(let (name)
(destructuring-bind (parameter-line &rest body-lines) tok-lines
(destructuring-bind (op . params-args) parameter-line
;; Check that we are dealing with a DEFWAVEFORM.
Expand All @@ -414,15 +413,17 @@
(setf name (quil:token-payload (pop params-args)))

(multiple-value-bind (params rest-line) (parse-parameters params-args)

(setf sample-rate (parse-sample-rate (butlast rest-line)))


;; Check for colon and incise it.
(let ((maybe-colon (last rest-line)))
(when (or (null maybe-colon)
(not (eq ':COLON (quil:token-type (first maybe-colon)))))
(quil-parse-error "Expected a colon terminating the first line of DEFWAVEFORM.")))

(when (butlast rest-line)
(quil-parse-error "Unexpected tokens ~s before colon on first line of DEFWAVEFORM."
(butlast rest-line)))

(let ((*arithmetic-parameters* nil)
(*segment-encountered* nil))
(multiple-value-bind (parsed-entries rest-lines)
Expand All @@ -447,7 +448,7 @@
:collect (gensym (concatenate 'string (param-name p) "-UNUSED"))
:else
:collect (second found-p))))
(values (make-waveform-definition name param-symbols parsed-entries sample-rate :context op)
(values (make-waveform-definition name param-symbols parsed-entries :context op)
rest-lines)))))))))

;;; Calibration Definitions
Expand Down
7 changes: 4 additions & 3 deletions tests/quilt/analysis-tests.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ FOO(1.0) 0"
(deftest test-quilt-name-resolution ()
(let ((pp (parse-quilt "
DEFFRAME 0 \"xy\"
DEFWAVEFORM foo 1.0:
DEFWAVEFORM foo:
1.0, 1.0, 1.0
DEFCAL X q:
Expand All @@ -64,9 +64,10 @@ PULSE 0 \"xy\" foo")))

(deftest test-quilt-duration ()
(let ((pp (parse-quilt "
DEFFRAME 0 \"xy\"
DEFFRAME 0 \"xy\":
SAMPLE-RATE: 2.0
DEFWAVEFORM foo 2.0:
DEFWAVEFORM foo:
1.0, 1.0, 1.0, 1.0
PULSE 0 \"xy\" gaussian(duration: 1.0, fwhm: 0.5, t0: 0.5)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DEFWAVEFORM foo 1.0:
DEFWAVEFORM foo:
1, 1, 1

HALT
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
DEFFRAME 0 "xy"

DEFWAVEFORM my_custom_waveform 1.0:
DEFWAVEFORM my_custom_waveform:
1+2*i, 3+4*i, 5+6*i

DEFWAVEFORM my_custom_parameterized_waveform(%a) 1.0:
DEFWAVEFORM my_custom_parameterized_waveform(%a):
(1+2*i)*%a, (3+4*i)*%a, (5+6*i)*%a

PULSE 0 "xy" my_custom_waveform
Expand Down
35 changes: 9 additions & 26 deletions tests/quilt/parser-tests.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,6 @@
(signals quil::quil-parse-error
(parse-quil "DEFFRAME 0 \"foo\"")))

(deftest test-quilt-defwaveform-sample-rate ()
(signals quil-parse-error
(parse-quilt "
DEFWAVEFORM foo:
1.0, 1.0, 1.0, 1.0"))
(signals quil-parse-error
(parse-quilt "
DEFWAVEFORM foo 4+2*i:
1.0, 1.0, 1.0, 1.0"))
(let ((pp (parse-quilt "
DEFWAVEFORM foo 4.0:
1.0, 1.0, 1.0, 1.0")))
(is (= 4.0
(constant-value
(waveform-definition-sample-rate
(first (parsed-program-waveform-definitions pp))))))))

(defun prints-as (expected obj &key (accessor #'quil:parsed-program-executable-code))
"Checks whether OBJ prints as the EXPECTED string. If OBJ is a string, parses OBJ and then checks that the first instruction prints as EXPECTED."
(typecase obj
Expand Down Expand Up @@ -86,7 +69,7 @@ DEFFRAME 0 \"rf\"
DEFFRAME 0 \"zz\"
DEFFRAME 0 1 \"foo\"
DECLARE iq REAL[2]
DEFWAVEFORM wf 1.0:
DEFWAVEFORM wf:
1.0, 1.0, 1.0
")
(instrs (list
Expand Down Expand Up @@ -131,13 +114,13 @@ DEFWAVEFORM wf 1.0:
"DEFFRAME 0 1 \"xy\":~% HARDWARE-OBJECT: \"q0_q1_xy\"~%"
"DEFFRAME 0 1 \"xy\":~% SAMPLE-RATE: 1.0~% DIRECTION: \"tx\"~%"))
(waveform-defns (list
"DEFWAVEFORM foo 1.0:~% 1.0~%"
"DEFWAVEFORM foo 1.0:~% 1.0+1.0i, 1.0+1.0i~%"
"DEFWAVEFORM foo:~% 1.0~%"
"DEFWAVEFORM foo:~% 1.0+1.0i, 1.0+1.0i~%"
;; case sensitivity
"DEFWAVEFORM FOO 1.0:~% 1.0~%"
"DEFWAVEFORM FOO:~% 1.0~%"
;; parametric waveform def
;; this is a bit too dependent on how arithmetic expressions are printed...
"DEFWAVEFORM foo(%theta) 1.0:~% (%theta*(1.0))~%"
"DEFWAVEFORM foo(%theta):~% (%theta*(1.0))~%"
))
(calibration-defns (list ; just sticking delays in here to have something nontrivial to print
"DEFCAL FOO 0:~% DELAY 0 1.0~% NOP~%"
Expand Down Expand Up @@ -169,10 +152,10 @@ DEFWAVEFORM wf 1.0:
(signature "DEFFRAME 0 \"foo\"")))
(is (not (equalp (signature "DEFFRAME 0 \"foo\"")
(signature "DEFFRAME 0 \"Foo\""))))
(is (equalp (signature "DEFWAVEFORM foo 1.0:~% 1.0, 1.0")
(signature "DEFWAVEFORM foo 1.0:~% 1.0, 1.0")))
(is (not (equalp (signature "DEFWAVEFORM foo 1.0:~% 1.0, 1.0")
(signature "DEFWAVEFORM Foo 1.0:~% 1.0, 1.0"))))
(is (equalp (signature "DEFWAVEFORM foo:~% 1.0, 1.0")
(signature "DEFWAVEFORM foo:~% 1.0, 1.0")))
(is (not (equalp (signature "DEFWAVEFORM foo:~% 1.0, 1.0")
(signature "DEFWAVEFORM Foo:~% 1.0, 1.0"))))
(is (equalp (signature "DEFCAL RX(%theta) q:~% NOP")
(signature "DEFCAL RX(%theta) q:~% NOP")))
(is (equalp (signature "DEFCAL RX(0) 0:~% NOP")
Expand Down

0 comments on commit 668b5f5

Please sign in to comment.