Skip to content

Commit

Permalink
poccompiler2 garbage collector
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Oct 10, 2023
1 parent 0c865e1 commit 9141b60
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions qlasskit/compiler/poccompiler2.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
class POCCompiler2(Compiler):
"""POC2 compiler translating an expression list to quantum circuit"""

def garbage_collect(self, qc):
uncomputed = qc.uncompute()

for k in self.mapped.keys():
if self.mapped[k] in uncomputed:
del self.mapped[k]

def compile(self, name, args: Args, ret_size: int, exprs: BoolExpList) -> QCircuit:
qc = QCircuit(name=name)

Expand All @@ -40,11 +47,8 @@ def compile(self, name, args: Args, ret_size: int, exprs: BoolExpList) -> QCircu
iret = self.compile_expr(qc, self._symplify_exp(exp))
# print("iret", iret)
qc.map_qubit(sym, iret, promote=True)
uncomputed = qc.uncompute()

for k in self.mapped.keys():
if self.mapped[k] in uncomputed:
del self.mapped[k]
self.garbage_collect(qc)

return qc

Expand All @@ -53,7 +57,7 @@ def compile_expr(self, qc: QCircuit, expr: Boolean) -> int:
return qc[expr.name]

elif expr in self.mapped:
print("!!cachehit!!", expr)
# print("!!cachehit!!", expr)
return self.mapped[expr]

elif isinstance(expr, Not):
Expand All @@ -65,9 +69,8 @@ def compile_expr(self, qc: QCircuit, expr: Boolean) -> int:
qc.cx(eret, fa)
qc.x(fa)

qc.uncompute()

qc.mark_ancilla(eret)
self.garbage_collect(qc)

self.mapped[expr] = fa

Expand All @@ -81,9 +84,10 @@ def compile_expr(self, qc: QCircuit, expr: Boolean) -> int:

qc.mcx(erets, fa)

qc.uncompute()

[qc.mark_ancilla(eret) for eret in erets]

self.garbage_collect(qc)

self.mapped[expr] = fa

return fa
Expand Down

0 comments on commit 9141b60

Please sign in to comment.