How to use the blueqat.quma.Quma function in blueqat

To help you get started, we’ve selected a few blueqat examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Blueqat / Blueqat / blueqat / quma.py View on Github external
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.")
github Blueqat / Blueqat / blueqat / quma.py View on Github external
def num(val):
        if val:
            return Quma({frozenset(): val})
        return Quma.zero()
github Blueqat / Blueqat / blueqat / quma.py View on Github external
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
github Blueqat / Blueqat / blueqat / quma.py View on Github external
def term(ns: Iterable[int], coeff: float = 1):
        return Quma({frozenset(ns): coeff})
github Blueqat / Blueqat / blueqat / quma.py View on Github external
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")
github Blueqat / Blueqat / blueqat / quma.py View on Github external
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
github Blueqat / Blueqat / blueqat / quma.py View on Github external
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.")