Skip to content

Commit

Permalink
add contiguous subschedule spec for callstack exhaustion
Browse files Browse the repository at this point in the history
  • Loading branch information
erichulburd committed Apr 12, 2021
1 parent b3f6148 commit a298cdf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
32 changes: 8 additions & 24 deletions src/addresser/outgoing-schedule.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -108,34 +108,18 @@ BEFORE-INST to make use of RESOURCE."
:when (carving-point-p resource instr)
:maximize (chip-schedule-end-time schedule instr)))

(defun chip-schedule-from-carving-point (schedule resource point)
"Returns the (time-ordered) subsequence of instructions from SCHEDULE which intersect RESOURCE and which start on or after the time POINT.
If POINT is generated by CHIP-SCHEDULE-RESOURCE-CARVING-POINT, then the resulting subsequence has a further \"information light-cone\" property: SCHEDULE will have no other instructions which can receive information from RESOURCE from after POINT."
;; NOTE: This could be written in such a way that GETHASH is called only once:
;; grab it inside of LOOP and attach it to INSTR by a CONS cell, then
;; change SORT's KEY to grab that value, then strip the cells away at end.
(sort
(loop :for instr :being :the :hash-keys :of (chip-schedule-times schedule)
:when (and (<= point (chip-schedule-start-time schedule instr))
(resources-intersect-p resource (instruction-resources instr)))
:collect instr)
#'<
:key (lambda (i) (gethash i (chip-schedule-times schedule)))))

(defun mark-predecessors-seen (instr earlier-instrs seen)
"performs a depth first search on INSTR as the root node and marks all of its descendants as true in hash-table SEEN."
(let ((queue (list instr)))
(loop :while queue :do
(let* ((v (pop queue))
(predecessors (gethash v earlier-instrs)))
(loop :for predecessor :in predecessors :do
"Performs a depth first search on INSTR as the root node and marks all of its descendants as true in hash-table SEEN."
(let ((stack (list instr)))
(loop :while stack :do
(let ((predecessors (gethash (pop stack) earlier-instrs)))
(dolist (predecessor predecessors)
(unless (gethash predecessor seen)
(setf (gethash predecessor seen) t)
(push predecessor queue)))))))
(push predecessor stack)))))))

(defun chip-contiguous-subschedule-from-last-instructions (schedule resource)
"loops over the last instructions in the SCHEDULE and returns an arbitrarily ordered list of instructions in the SCHEDULE that are contiguous and involve a subset of resources in RESOURCE."
"Returns an unordered list of instructions from SCHEDULE such that each instruction involves a subset of RESOURCE. Traversal of the schedule begins from the latest scheduled instructions and works backwards into earlier instructions until an instruction uses resources not in RESOURCE."
(let* ((lsched (chip-schedule-data schedule))
(earlier-instrs (lscheduler-earlier-instrs lsched))
(seen (make-hash-table))
Expand All @@ -149,7 +133,7 @@ If POINT is generated by CHIP-SCHEDULE-RESOURCE-CARVING-POINT, then the resultin
((and (resources-intersect-p resource (instruction-resources instr))
(resource-subsetp (instruction-resources instr) resource))
(push instr sched)
(loop :for earlier-instr :in (gethash instr earlier-instrs) :do
(dolist (earlier-instr (gethash instr earlier-instrs))
(push earlier-instr leaves)))
((carving-point-p resource instr)
(mark-predecessors-seen instr earlier-instrs seen))))))
Expand Down
23 changes: 23 additions & 0 deletions tests/addresser-tests.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,26 @@
(quil::chip-contiguous-subschedule-from-last-instructions
sched (quil::make-qubit-resource 0 2))
expected-subschedule))))))

(deftest test-fidelity-addresser-subschedule-on-deep-circuit ()
(let* ((chip (quil::build-nq-fully-connected-chip 3))
(sched (quil::make-chip-schedule chip))
(h0 (build-gate "H" nil 0))
(circuit-depth 25000))
(loop :for i :from 1 :to circuit-depth :do
(progn
;; these gates will exercise depth in the outer dfs, ie in
;; chip-contiguous-subschedule-from-last-instructions.
(quil::chip-schedule-append sched (build-gate "H" nil 0))
;; these gates will exercise depth in the inner dfs, ie in
;; mark-predecessors-seen.
(quil::chip-schedule-append sched (build-gate "H" nil 1))))
(flet ((is-h0 (gate)
(and (string= (quil::application-operator-root-name gate)
(quil::application-operator-root-name h0))
(equalp (quil::application-parameters gate)
(quil::application-parameters h0)))))
(let ((result (quil::chip-contiguous-subschedule-from-last-instructions sched (quil::make-qubit-resource 0))))
(is (= (length result) circuit-depth))
(is (every #'is-h0 result))))))

0 comments on commit a298cdf

Please sign in to comment.