Skip to content

Commit 0d1084b

Browse files
committed
refactor chip-contiguous-subschedule-from-last-instructions to non-recursive
1 parent c5ffeee commit 0d1084b

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

src/addresser/outgoing-schedule.lisp

+28-29
Original file line numberDiff line numberDiff line change
@@ -123,39 +123,38 @@ If POINT is generated by CHIP-SCHEDULE-RESOURCE-CARVING-POINT, then the resultin
123123
#'<
124124
:key (lambda (i) (gethash i (chip-schedule-times schedule)))))
125125

126-
;; TODO Make this non-recursive so we can avoid blowing the stack on
127-
;; veeeerrrrry deep programs.
128-
(defun chip-contiguous-subschedule-from-last-instructions (schedule resource)
129-
"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."
126+
(defun mark-predecessors-seen (instr earlier-instrs seen)
127+
"performs a depth first search on INSTR as the root node and marks all of its descendants as true in hash-table SEEN."
128+
(let ((queue (list instr)))
129+
(loop :while queue :do
130+
(let* ((v (pop queue))
131+
(predecessors (gethash v earlier-instrs)))
132+
(loop :for predecessor :in predecessors :do
133+
(unless (gethash predecessor seen)
134+
(setf (gethash predecessor seen) t)
135+
(push predecessor queue)))))))
136+
137+
(defun chip-contiguous-subschedule-from-last-instructions (schedule resource)
138+
"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."
130139
(let* ((lsched (chip-schedule-data schedule))
131-
(last-instrs (lscheduler-last-instrs lsched))
132140
(earlier-instrs (lscheduler-earlier-instrs lsched))
133141
(seen (make-hash-table))
142+
(leaves (lscheduler-last-instrs lsched))
134143
(sched nil))
135-
(labels ((mark-predecessors-seen (instr)
136-
(cond ((resource= (instruction-resources instr)
137-
resource)
138-
(setf (gethash instr seen) t))
139-
((resource-subsetp (instruction-resources instr)
140-
resource)
141-
(map nil #'mark-predecessors-seen (gethash instr earlier-instrs)))))
142-
(bottoms-up (instr)
143-
;; Recursively explorer earlier instructions in the
144-
;; schedule. Instructions are collected if their
145-
;; resources are a subset of RESOURCE. If a carving
146-
;; point is encountered, mark all earlier instructions
147-
;; as seen.
148-
(unless (gethash instr seen)
149-
(setf (gethash instr seen) t)
150-
(cond
151-
((carving-point-p resource instr)
152-
(map nil #'mark-predecessors-seen (reverse (gethash instr earlier-instrs))))
153-
((and (resources-intersect-p resource (instruction-resources instr))
154-
(not (carving-point-p resource instr)))
155-
(push instr sched)
156-
(map nil #'bottoms-up (reverse (gethash instr earlier-instrs))))))))
157-
(map nil #'bottoms-up last-instrs)
158-
sched)))
144+
(loop :while leaves :do
145+
(let ((instr (pop leaves)))
146+
(unless (gethash instr seen)
147+
(setf (gethash instr seen) t)
148+
(cond
149+
((and (resources-intersect-p resource (instruction-resources instr))
150+
(resource-subsetp (instruction-resources instr) resource))
151+
(push instr sched)
152+
;; reverse loop across INSTR's EARLIER-INSTRS and prepend to LEAVES.
153+
(loop :for earlier-instr :in (gethash instr earlier-instrs) :do
154+
(push earlier-instr leaves)))
155+
((carving-point-p resource instr)
156+
(mark-predecessors-seen instr earlier-instrs seen))))))
157+
sched))
159158

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

0 commit comments

Comments
 (0)