-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from queue import PriorityQueue | ||
|
||
from qupsy.language import Pgm | ||
|
||
|
||
class Worklist: | ||
def __init__(self) -> None: | ||
self.current_set: PriorityQueue[tuple[int, int, Pgm]] = PriorityQueue() | ||
self.overall_set: list[Pgm] = [] | ||
|
||
def put(self, *pgms: Pgm) -> None: | ||
for pgm in pgms: | ||
if pgm not in self.overall_set: | ||
self.current_set.put((pgm.cost, pgm.depth, pgm)) | ||
self.overall_set.append(pgm) | ||
|
||
def get(self) -> Pgm: | ||
return self.current_set.get_nowait()[2] | ||
|
||
def show_set(self) -> None: | ||
print(self.overall_set) | ||
|
||
def show_pq(self) -> None: | ||
print(self.current_set.queue) | ||
|
||
def notEmpty(self) -> bool: | ||
return not self.current_set.empty() | ||
|
||
def num_pgm_left(self) -> int: | ||
return self.current_set.qsize() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from qupsy.language import GateCmd, H, HoleAexp, HoleCmd, HoleGate, Pgm, SeqCmd, X | ||
from qupsy.worklist import Worklist | ||
|
||
|
||
def test_worklist(): | ||
worklist = Worklist() | ||
assert not worklist.notEmpty() | ||
|
||
|
||
def test_add_same_pgm(): | ||
worklist = Worklist() | ||
pgm = Pgm(HoleCmd()) | ||
worklist.put(pgm, pgm) | ||
assert worklist.num_pgm_left() == 1 | ||
|
||
|
||
def test_get_pgm(): | ||
worklist = Worklist() | ||
pgm1 = Pgm(HoleCmd()) | ||
pgm2 = Pgm(GateCmd(HoleGate())) | ||
worklist.put(pgm1, pgm2) | ||
assert worklist.get() == pgm2 | ||
|
||
|
||
def test_add_same_cost(): | ||
worklist = Worklist() | ||
pgm1 = Pgm(SeqCmd(GateCmd(H(HoleAexp())), GateCmd(H(HoleAexp())))) | ||
pgm2 = Pgm(SeqCmd(GateCmd(X(HoleAexp())), GateCmd(X(HoleAexp())))) | ||
worklist.put(pgm1, pgm2) | ||
assert worklist.get() == pgm1 |