Skip to content

Commit

Permalink
refactor chip-contiguous-subschedule-from-last-instructions to non-re…
Browse files Browse the repository at this point in the history
…cursive
  • Loading branch information
erichulburd committed Jan 26, 2021
1 parent c5ffeee commit f73467f
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions src/addresser/outgoing-schedule.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ BEFORE-INST to make use of RESOURCE."
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.
;; change SORT'xs 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))
Expand All @@ -123,39 +123,46 @@ If POINT is generated by CHIP-SCHEDULE-RESOURCE-CARVING-POINT, then the resultin
#'<
:key (lambda (i) (gethash i (chip-schedule-times schedule)))))

;; TODO Make this non-recursive so we can avoid blowing the stack on
;; veeeerrrrry deep programs.
(defun chip-contiguous-subschedule-from-last-instructions (schedule resource)
"Returns an unordered contiguous subsequence of instructions from SCHEDULE where each instruction is in the dependency graph of the last instructions in SCHEDULE and touches only resources in RESOURCE."
(defun mark-predecessors-seen (instr earlier-instrs seen)
"mark-predecessors-seen performs a breadth 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 (first queue))
(predecessors (gethash v earlier-instrs))
)
(progn
(setq queue (cdr queue))
(loop for predecessor in predecessors
do (unless (gethash predecessor seen)
(setf (gethash predecessor seen) t)
(setq queue (append queue (list predecessor))))))
)))
)

(defun chip-contiguous-subschedule-from-last-instructions (schedule resource)
"chip-contiguous-subschedule-from-last-instruction 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."
(let* ((lsched (chip-schedule-data schedule))
(last-instrs (lscheduler-last-instrs lsched))
(earlier-instrs (lscheduler-earlier-instrs lsched))
(seen (make-hash-table))
(leaves (lscheduler-last-instrs lsched))
(sched nil))
(labels ((mark-predecessors-seen (instr)
(cond ((resource= (instruction-resources instr)
resource)
(setf (gethash instr seen) t))
((resource-subsetp (instruction-resources instr)
resource)
(map nil #'mark-predecessors-seen (gethash instr earlier-instrs)))))
(bottoms-up (instr)
;; Recursively explorer earlier instructions in the
;; schedule. Instructions are collected if their
;; resources are a subset of RESOURCE. If a carving
;; point is encountered, mark all earlier instructions
;; as seen.
(loop while leaves
do (let ((instr (first leaves)))
(setq leaves (cdr leaves))
(unless (gethash instr seen)
(setf (gethash instr seen) t)
(cond
((carving-point-p resource instr)
(map nil #'mark-predecessors-seen (reverse (gethash instr earlier-instrs))))
((and (resources-intersect-p resource (instruction-resources instr))
(not (carving-point-p resource instr)))
(resource-subsetp (instruction-resources instr) resource))
(push instr sched)
(map nil #'bottoms-up (reverse (gethash instr earlier-instrs))))))))
(map nil #'bottoms-up last-instrs)
sched)))
(setf (gethash instr seen) t)
(setq leaves (append leaves (reverse (gethash instr earlier-instrs)))))
((carving-point-p resource instr)
(mark-predecessors-seen instr earlier-instrs seen)
)
))))
sched))

(defun chip-schedule-qubit-times (schedule)
"Find the first time a qubit is available, for each qubit in the schedule."
Expand Down

0 comments on commit f73467f

Please sign in to comment.