@@ -123,39 +123,37 @@ If POINT is generated by CHIP-SCHEDULE-RESOURCE-CARVING-POINT, then the resultin
123
123
#' <
124
124
:key (lambda (i) (gethash i (chip-schedule-times schedule)))))
125
125
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."
130
139
(let* ((lsched (chip-schedule-data schedule))
131
- (last-instrs (lscheduler-last-instrs lsched))
132
140
(earlier-instrs (lscheduler-earlier-instrs lsched))
133
141
(seen (make-hash-table ))
142
+ (leaves (lscheduler-last-instrs lsched))
134
143
(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
+ (loop :for earlier-instr :in (gethash instr earlier-instrs) :do
153
+ (push earlier-instr leaves)))
154
+ ((carving-point-p resource instr)
155
+ (mark-predecessors-seen instr earlier-instrs seen))))))
156
+ sched))
159
157
160
158
(defun chip-schedule-qubit-times (schedule)
161
159
" Find the first time a qubit is available, for each qubit in the schedule."
0 commit comments