-
Notifications
You must be signed in to change notification settings - Fork 22
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
8 changed files
with
99 additions
and
73 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
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,67 @@ | ||
from abc import ABC, abstractmethod | ||
import tempfile | ||
import uuid | ||
from .compile import compile | ||
import coreir | ||
from .logging import warning | ||
|
||
|
||
class CircuitDatabaseInterface(ABC): | ||
@abstractmethod | ||
def insert(self, circuit): | ||
pass | ||
|
||
@abstractmethod | ||
def clear(self): | ||
pass | ||
|
||
|
||
class ConservativeCircuitDatabase(CircuitDatabaseInterface): | ||
def insert(self, circuit): | ||
name = circuit.name | ||
new_name = name + "-" + str(uuid.uuid4()) | ||
type(circuit).rename(circuit, new_name) | ||
|
||
def clear(self): | ||
pass | ||
|
||
|
||
class CircuitDatabase(CircuitDatabaseInterface): | ||
class Entry: | ||
def __init__(self, name): | ||
self.name = name | ||
self.circuits = {} | ||
|
||
def hash(self, circuit): | ||
with tempfile.TemporaryDirectory() as tempdir: | ||
try: | ||
compile(tempdir + "/circuit", circuit, output="coreir", context=coreir.Context()) | ||
json_str = open(tempdir + "/circuit.json").read() | ||
except Exception as e: | ||
warning(f"Could not compile circuit: '{str(e)}'. Uniquifying anyway.") | ||
json_str = uuid.uuid4() | ||
return hash(json_str) | ||
|
||
def add_circuit(self, circuit): | ||
hash_ = self.hash(circuit) | ||
if hash_ in self.circuits: | ||
index = self.circuits[hash_][0] | ||
else: | ||
index = len(self.circuits) | ||
self.circuits[hash_] = (index, circuit) | ||
if index > 0: | ||
type(circuit).rename(circuit, circuit.name + "_unq" + str(index)) | ||
|
||
def __repr__(self): | ||
return repr(self.circuits) | ||
|
||
def __init__(self): | ||
self.entries = {} | ||
|
||
def insert(self, circuit): | ||
name = circuit.name | ||
entry = self.entries.setdefault(name, CircuitDatabase.Entry(name)) | ||
entry.add_circuit(circuit) | ||
|
||
def clear(self): | ||
self.entries = {} |
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
This file was deleted.
Oops, something went wrong.
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