Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.rhs = rhs
def eval(self):
return self.lhs.eval() - self.rhs.eval()
class QasmRealMul(QasmRealExpr):
def __init__(self, lhs: QasmExpr, rhs: QasmExpr):
self.lhs = lhs
self.rhs = rhs
def eval(self):
return self.lhs.eval() * self.rhs.eval()
class QasmRealDiv(QasmRealExpr):
def __init__(self, lhs: QasmExpr, rhs: QasmExpr):
self.lhs = lhs
self.rhs = rhs
def eval(self):
return self.lhs.eval() / self.rhs.eval()
class QasmRealValue(QasmRealExpr):
def __init__(self, value: float):
self.value = value
def eval(self):
return self.value
class QasmRealExpr(QasmExpr):
@abstractmethod
def eval(self) -> float:
pass
class QasmRealAdd(QasmRealExpr):
def __init__(self, lhs: QasmExpr, rhs: QasmExpr):
self.lhs = lhs
self.rhs = rhs
def eval(self):
return self.lhs.eval() + self.rhs.eval()
class QasmRealSub(QasmRealExpr):
def __init__(self, lhs: QasmExpr, rhs: QasmExpr):
self.lhs = lhs
self.rhs = rhs
def eval(self):
return self.lhs.eval() - self.rhs.eval()
class QasmRealMul(QasmRealExpr):
def __init__(self, lhs: QasmExpr, rhs: QasmExpr):
self.lhs = lhs
self.rhs = rhs
def eval(self):
return self.lhs.eval() * self.rhs.eval()
def eval(self):
pass
def __repr__(self):
if hasattr(self, 'value'):
return f'{self.__class__.__name__}({repr(self.value)})'
return f'{self.__class__.__name__}({repr(self.lhs)}, {repr(self.rhs)})'
class QasmRealExpr(QasmExpr):
@abstractmethod
def eval(self) -> float:
pass
class QasmRealAdd(QasmRealExpr):
def __init__(self, lhs: QasmExpr, rhs: QasmExpr):
self.lhs = lhs
self.rhs = rhs
def eval(self):
return self.lhs.eval() + self.rhs.eval()
class QasmRealSub(QasmRealExpr):
def __init__(self, lhs: QasmExpr, rhs: QasmExpr):
self.lhs = lhs
self.rhs = rhs
def eval(self):
return self.lhs.eval() - self.rhs.eval()
self.rhs = rhs
def eval(self):
return self.lhs.eval() + self.rhs.eval()
class QasmRealSub(QasmRealExpr):
def __init__(self, lhs: QasmExpr, rhs: QasmExpr):
self.lhs = lhs
self.rhs = rhs
def eval(self):
return self.lhs.eval() - self.rhs.eval()
class QasmRealMul(QasmRealExpr):
def __init__(self, lhs: QasmExpr, rhs: QasmExpr):
self.lhs = lhs
self.rhs = rhs
def eval(self):
return self.lhs.eval() * self.rhs.eval()
class QasmRealDiv(QasmRealExpr):
def __init__(self, lhs: QasmExpr, rhs: QasmExpr):
self.lhs = lhs
self.rhs = rhs
def eval(self):
return self.lhs.eval() / self.rhs.eval()
func = math.exp
elif self.func == QasmRealUnaryFunctions.Ln:
func = math.log
elif self.func == QasmRealUnaryFunctions.Sqrt:
func = math.sqrt
else:
raise ValueError('Unexpected Enum value.')
return func(self.arg.eval())
def __repr__(self):
return f'{self.__class__.__name__}({repr(self.func)}, {repr(self.arg)})'
QasmRealConstValues = Enum('QasmRealConstValues', 'Pi')
class QasmRealConst(QasmRealExpr):
def __init__(self, value: QasmRealConstValues):
self.value = value
def eval(self):
if self.value == QasmRealConstValues.Pi:
return math.pi
raise ValueError('Unexpected Enum value.')
class QasmGateDecl(QasmNode):
pass
class QasmApplyGate(QasmNode):
def __init__(self, gate, params, qregs):
self.rhs = rhs
def eval(self):
return self.lhs.eval() * self.rhs.eval()
class QasmRealDiv(QasmRealExpr):
def __init__(self, lhs: QasmExpr, rhs: QasmExpr):
self.lhs = lhs
self.rhs = rhs
def eval(self):
return self.lhs.eval() / self.rhs.eval()
class QasmRealValue(QasmRealExpr):
def __init__(self, value: float):
self.value = value
def eval(self):
return self.value
class QasmRealUnaryFunctions(Enum):
Sin = auto()
Cos = auto()
Tan = auto()
Exp = auto()
Ln = auto()
Sqrt = auto()
@staticmethod
def from_str(s: str) -> 'QasmRealUnaryFunctions':
if s == 'sin':
return QasmRealUnaryFunctions.Sin
if s == 'cos':
return QasmRealUnaryFunctions.Cos
if s == 'tan':
return QasmRealUnaryFunctions.Tan
if s == 'exp':
return QasmRealUnaryFunctions.Exp
if s == 'ln':
return QasmRealUnaryFunctions.Ln
if s == 'sqrt':
return QasmRealUnaryFunctions.Sqrt
raise ValueError('Unexpected value')
class QasmRealCall(QasmRealExpr):
def __init__(self, func: QasmRealUnaryFunctions, arg: QasmRealExpr):
self.func = func
self.arg = arg
def eval(self):
func = None
if self.func == QasmRealUnaryFunctions.Sin:
func = math.sin
elif self.func == QasmRealUnaryFunctions.Cos:
func = math.cos
elif self.func == QasmRealUnaryFunctions.Tan:
func = math.tan
elif self.func == QasmRealUnaryFunctions.Exp:
func = math.exp
elif self.func == QasmRealUnaryFunctions.Ln:
func = math.log