Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor improvements to print_phase class var in gates.py. #136

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions pyzx/circuit/gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,14 @@ class Gate(object):
qc_name: ClassVar[str] = 'undefined'
qasm_name: ClassVar[str] = 'undefined'
qasm_name_adjoint: ClassVar[str] = 'undefined'
print_phase: ClassVar[bool] = False
index = 0
def __str__(self) -> str:
attribs = []
if hasattr(self, "control"): attribs.append(str(self.control)) # type: ignore # See issue #1424
if hasattr(self, "target"): attribs.append(str(self.target)) # type: ignore #https://github.com/python/mypy/issues/1424
if hasattr(self, "phase") and self.printphase: # type: ignore
attribs.append("phase={!s}".format(self.phase)) # type: ignore
if hasattr(self, "phase") and self.print_phase:
attribs.append("phase={!s}".format(self.phase))
return "{}{}({})".format(
self.name,
("*" if (hasattr(self,"adjoint") and self.adjoint) else ""), # type: ignore
Expand Down Expand Up @@ -197,9 +198,9 @@ def copy(self: Tvar) -> Tvar:

def to_adjoint(self: Tvar) -> Tvar:
g = self.copy()
if hasattr(g, "phase"):
g.phase = -g.phase # type: ignore
if hasattr(g, "adjoint"):
if hasattr(g, "phase"):
g.phase = -g.phase
if hasattr(g, "adjoint"):
g.adjoint = not g.adjoint # type: ignore
return g

Expand Down Expand Up @@ -246,8 +247,8 @@ def to_qasm(self) -> str:
for a in ["ctrl1","ctrl2", "control", "target"]:
if hasattr(self, a): args.append("q[{:d}]".format(getattr(self,a)))
param = ""
if hasattr(self, "printphase") and self.printphase: # type: ignore
param = "({}*pi)".format(float(self.phase)) # type: ignore
if hasattr(self, "phase") and self.print_phase:
param = "({}*pi)".format(float(self.phase))
return "{}{} {};".format(n, param, ", ".join(args))

def to_qc(self) -> str:
Expand All @@ -268,7 +269,7 @@ def to_qc(self) -> str:
for a in ["ctrl1","ctrl2", "control", "target"]:
if hasattr(self, a): args.append("q{:d}".format(getattr(self,a)))

# if hasattr(self, "printphase") and self.printphase:
# if hasattr(self, "phase") and self.print_phase:
# args.insert(0, phase_to_s(self.phase))
return "{} {}".format(n, " ".join(args))

Expand Down Expand Up @@ -297,8 +298,8 @@ def graph_add_node(self,

class ZPhase(Gate):
name = 'ZPhase'
printphase: ClassVar[bool] = True
qasm_name = 'rz'
print_phase = True
def __init__(self, target: int, phase: FractionLike) -> None:
self.target = target
self.phase = phase
Expand All @@ -308,7 +309,7 @@ def to_graph(self, g, q_mapper, _c_mapper):
q_mapper.advance_next_row(self.target)

def to_quipper(self):
if not self.printphase:
if not self.print_phase:
return super().to_quipper()
return 'QRot["exp(-i%Z)",{!s}]({!s})'.format(math.pi*self.phase/2,self.target)

Expand Down Expand Up @@ -352,7 +353,7 @@ class Z(ZPhase):
name = 'Z'
qasm_name = 'z'
qc_name = 'Z'
printphase = False
print_phase = False
def __init__(self, target: int) -> None:
super().__init__(target, Fraction(1,1))

Expand All @@ -361,7 +362,7 @@ class S(ZPhase):
qasm_name = 's'
qasm_name_adjoint = 'sdg'
qc_name = 'S'
printphase = False
print_phase = False
def __init__(self, target: int, adjoint:bool=False) -> None:
super().__init__(target, Fraction(1,2)*(-1 if adjoint else 1))
self.adjoint = adjoint
Expand All @@ -371,15 +372,15 @@ class T(ZPhase):
qasm_name = 't'
qasm_name_adjoint = 'tdg'
qc_name = 'T'
printphase = False
print_phase = False
def __init__(self, target: int, adjoint:bool=False) -> None:
super().__init__(target, Fraction(1,4)*(-1 if adjoint else 1))
self.adjoint = adjoint

class XPhase(Gate):
name = 'XPhase'
printphase: ClassVar[bool] = True
qasm_name = 'rx'
print_phase = True
def __init__(self, target: int, phase: FractionLike=0) -> None:
self.target = target
self.phase = phase
Expand All @@ -402,7 +403,7 @@ def to_emoji(self,strings: List[List[str]]) -> None:
strings[self.target].append(s)

def to_quipper(self):
if not self.printphase:
if not self.print_phase:
return super().to_quipper()
return 'QRot["exp(-i%X)",{!s}]({!s})'.format(math.pi*self.phase/2,self.target)

Expand Down Expand Up @@ -431,8 +432,8 @@ def split_phases(self) -> List[Gate]:

class YPhase(Gate):
name = 'YPhase'
printphase: ClassVar[bool] = True
qasm_name = 'ry'
print_phase = True
def __init__(self, target: int, phase: FractionLike=0) -> None:
self.target = target
self.phase = phase
Expand Down Expand Up @@ -462,7 +463,7 @@ class NOT(XPhase):
quippername = 'not'
qasm_name = 'x'
qc_name = 'X'
printphase = False
print_phase = False
def __init__(self, target: int) -> None:
super().__init__(target, phase = Fraction(1,1))

Expand Down Expand Up @@ -584,7 +585,7 @@ class CRZ(Gate):
name = 'CRZ'
qasm_name = 'crz'
quippername = 'undefined'
printphase: ClassVar[bool] = True
print_phase = True
def __init__(self, control: int, target: int, phase: FractionLike) -> None:
self.target = target
self.control = control
Expand Down Expand Up @@ -631,7 +632,7 @@ def to_graph(self, g, q_mapper, c_mapper):

class ParityPhase(Gate):
name = 'ParityPhase'
printphase: ClassVar[bool] = True
print_phase = True
def __init__(self, phase: FractionLike, *targets: int):
self.targets = targets
self.phase = phase
Expand Down Expand Up @@ -670,7 +671,7 @@ def tcount(self):
class FSim(Gate):
name = 'FSim'
qsim_name = 'fs'
printphase: ClassVar[bool] = True
print_phase = True
def __init__(self, theta:FractionLike, phi:FractionLike, control:int, target:int):
self.control = control
self.target = target
Expand Down
Loading