Skip to content

Commit

Permalink
minor clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Jul 6, 2024
1 parent e0a10ef commit 6d40588
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
38 changes: 14 additions & 24 deletions qlasskit/ast2ast/astrewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,30 @@
# limitations under the License.
import ast
import copy
from dataclasses import dataclass
from typing import Any

from ..ast2logic import flatten


class IsNamePresent(ast.NodeTransformer):
@dataclass
class IsNamePresent(ast.NodeVisitor):
"""Check if a tree contains a specific name_id"""

def __init__(self, name_id):
self.name_id = name_id
self.present = False

@property
def is_present(self):
return self.present

def generic_visit(self, node):
return super().generic_visit(node)
name_id: str
present: bool = False

def visit_Name(self, node):
if node.id == self.name_id:
self.present = True

return node


@dataclass
class NameValReplacer(ast.NodeTransformer):
"""Replace all Name with name_id with the given val"""

def __init__(self, name_id, val):
self.name_id = name_id
self.val = val

def generic_visit(self, node):
return super().generic_visit(node)
name_id: str
val: Any

def visit_Name(self, node):
if node.id == self.name_id:
Expand Down Expand Up @@ -136,7 +126,7 @@ def __init__(self, env={}, ret=None):
def uniqd(self):
"""Return an unique identifier as str"""
self._uniqd += 1
return f"{hex(self._uniqd)[2:]}"
return f"{self._uniqd:x}"

def __unroll_arg(self, arg):
"""Argument unrolling for visit_call()"""
Expand Down Expand Up @@ -214,7 +204,7 @@ def visit_Subscript(self, node):

def visit_Name(self, node):
# __ prefix is reserved for internal use
if node.id[0:2] == "__":
if node.id.startswith("__"):
raise Exception("invalid name starting with __")

return node
Expand All @@ -241,7 +231,7 @@ def visit_If(self, node):

target_0id = b.targets[0].id

if target_0id[0:2] == "__" and target_0id not in self.env:
if target_0id.startswith("__") and target_0id not in self.env:
orelse_inner = ast.Name(id=target_0id[2:])
else:
orelse_inner = ast.Name(id=target_0id)
Expand All @@ -264,7 +254,7 @@ def visit_If(self, node):

target_0id = b.targets[0].id

if target_0id[0:2] == "__" and target_0id not in self.env:
if target_0id.startswith("__") and target_0id not in self.env:
orelse_inner = ast.Name(id=target_0id[2:])
elif target_0id[0 : len("_iftarg")] == "_iftarg":
if_l.append(b)
Expand Down Expand Up @@ -355,7 +345,7 @@ def visit_Assign(self, node):
ip.visit(node.value)

# Reassigning an already present variable (use a temp variable)
if ip.is_present and was_known and not isinstance(node.value, ast.Constant):
if ip.present and was_known and not isinstance(node.value, ast.Constant):
new_targ = ast.Name(id=f"__{target_0id}", ctx=ast.Load())

return [
Expand Down
2 changes: 1 addition & 1 deletion qlasskit/compiler/internalcompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def compile( # noqa: C901

# 2. Iterate over all expressions; iret contains qubit index for the current exp
for sym, exp in exprs:
is_temp = sym.name[0:2] == "__"
is_temp = sym.name.startswith("__")
symp_exp = self._symplify_exp(exp)

# 2.1 Compile the expression
Expand Down
2 changes: 1 addition & 1 deletion qlasskit/types/qtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self, got, excepted):


def bin_to_bool_list(b: str, bit_size=None) -> List[bool]:
if b[0:2] == "0b":
if b.startswith("0b"):
b = b[2:]

if bit_size is None:
Expand Down

0 comments on commit 6d40588

Please sign in to comment.