Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __mul__(self, other):
if isinstance(other, Quma):
return Quma(Quma.__mul_terms(self.terms, other.terms))
if isinstance(other, (int, float)):
if other:
return Quma({k: v * other for k, v in self.terms.items()})
return Quma.num(0)
raise ValueError(f"`Quma` * `{other.__class__.__name__}` is not supported.")
def num(val):
if val:
return Quma({frozenset(): val})
return Quma.zero()
while len(q) > 2:
q1 = q.popleft()
q2 = q.popleft()
if (q1, q2) in mapping:
q12 = mapping[q1, q2]
else:
q12 = mapping[q1, q2] = next(unused_qubits)
q.append(q12)
quad = frozenset(q)
assert quad not in newterms
newterms[quad] = val
constraints[frozenset({q12})] = 3
constraints[frozenset({q1, q2})] = 1
constraints[frozenset({q1, q12})] = -2
constraints[frozenset({q2, q12})] = -2
return Quma(newterms), Quma(constraints), mapping
def term(ns: Iterable[int], coeff: float = 1):
return Quma({frozenset(ns): coeff})
def zero():
return Quma({})
if isinstance(v, Quma):
# TODO: Impl
pass
if key <= k:
newkey = k - key
newval = v * value
if newkey in d:
d[newkey] += newval
else:
d[newkey] = newval
else:
if k in d:
d[k] += v
else:
d[k] = v
return Quma({k: v for k, v in d.items() if v})
if hasattr(key, '__iter__') and value is None:
expr = self
if isinstance(key, dict):
key = key.items()
for k, v in key:
expr = expr.subs(k, v)
return expr
raise ValueError("Quma.subs(Quma, val) or Quma.subs([(Quma, val)]) is supported")
def subs(self, key, value=None):
# This feature is unstable :(
if isinstance(value, (int, float, Quma)):
if isinstance(key, int):
key = frozenset((key,))
elif isinstance(key, Quma):
if not key.is_term():
raise ValueError("key shall be a term. (for example, q0 or q1*q2)")
key, = key.terms
if not isinstance(key, frozenset):
raise ValueError("key shall be single term or integer.")
d = {}
for k, v in self.terms.items():
if isinstance(v, Quma):
# TODO: Impl
pass
if key <= k:
newkey = k - key
def __add__(self, other):
if isinstance(other, Quma):
return Quma(Quma.__add_terms_inplace(self.terms.copy(), other.terms))
if isinstance(other, (int, float)):
return Quma(Quma.__add_terms_inplace(self.terms.copy(), Quma.num(other).terms))
raise ValueError(f"`Quma` + `{other.__class__.__name__}` is not supported.")