Skip to content

Commit

Permalink
Added tests of extern and call
Browse files Browse the repository at this point in the history
  • Loading branch information
macrologist committed Apr 17, 2024
1 parent 3e21544 commit 64f1be6
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cl-quil-tests.asd
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@
(:file "linear-reversible-circuit-tests")
(:file "permutation-tests")
(:file "sqisw-decomp-tests")
(:file "stub-tests")))
(:file "stub-tests")
(:file "extern-tests")))
76 changes: 76 additions & 0 deletions tests/extern-tests.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
;;;; extern-tests.lisp
;;;;
;;;; Author: Colin O'Keefe

(in-package #:cl-quil-tests)

(defun rando (realcell) (setf (aref realcell 0) (random 1.0)))

;; you can give functions a name for use in Quil programs
(cl-quil::register-classical-function "randomize" 'rando)

(deftest test-extern-and-call ()
(let (parsed
compiled
(quil
"
EXTERN randomize
DECLARE x REAL
RX(x) 0
CALL randomize x
CPHASE(pi*x/2) 0 1"))

;; we can parse programs like the above without error
(not-signals error
(setf parsed (parse-quil quil)))

;; an can compile such programs
(not-signals error
(setf compiled (cl-quil::compiler-hook parsed (cl-quil:build-8q-chip))))

;; we want to ensure that the call to randomize happens after the
;; first instruction to reference x and before any other
;; instruction that references x.
(flet ((is-dexpr (e) (cl-quil.frontend::delayed-expression-p e)))
(let* ((instrs
(parsed-program-executable-code compiled))
(pos-rx
(position-if (lambda (instr)
(and (typep instr 'application)
(find-if #'is-dexpr (application-parameters instr))))
instrs))
(pos-call
(position-if (lambda (instr) (typep instr 'cl-quil.frontend::call))
instrs))
(pos-ref-after-rx
(and pos-rx
(position-if (lambda (instr)
(and (typep instr 'application)
(find-if #'is-dexpr (application-parameters instr))))
instrs
:start (1+ pos-rx)))))
(is (and pos-rx pos-call pos-ref-after-rx
(< pos-rx pos-call pos-ref-after-rx)))))))

(defun add2 (x y) (+ x y))
(cl-quil::register-classical-function "add2" 'add2)

(deftest test-extern-in-expressions ()
(let (parsed)
;; parsing works
(not-signals error
(setf parsed (parse-quil "EXTERN add2;RX(add2(3,pi)/4) 0")))

;; because the expression involved no memory references, it is
;; actually evaluated
(is (is-constant
(elt (application-parameters (elt (parsed-program-executable-code parsed) 0)) 0)))

;; but this one will involve memory refs, and so will involve a delayed expression
(setf parsed (parse-quil "EXTERN add2;DECLARE x REAL;RX(add2(x,pi)/4) 0"))
(is (cl-quil.frontend::delayed-expression-p
(elt (application-parameters (elt (parsed-program-executable-code parsed) 0)) 0)))))




0 comments on commit 64f1be6

Please sign in to comment.