def __init__(self, *bitstring): """ >>> g = Bra(1, 1, 0) """ self.bitstring = bitstring Box.__init__(self, 'Bra({})'.format(', '.join(map(str, bitstring))), PRO(len(bitstring)), PRO(0)) Circuit.__init__(self, len(bitstring), 0, [self], [0])
def __init__(self, name, dom, cod, function=None, data=None): """ >>> assert COPY.dom == PRO(1) >>> assert COPY.cod == PRO(2) """ if function is not None: self._function = function rigid.Box.__init__(self, name, PRO(dom), PRO(cod), data=data) Diagram.__init__(self, dom, cod, [self], [0])
def __call__(self, diagram): """ >>> x = Ty('x') >>> F = CircuitFunctor({x: 1}, {}) >>> assert isinstance(F(Diagram.id(x)), Circuit) """ if isinstance(diagram, Ty): return PRO(len(super().__call__(diagram))) if isinstance(diagram, Ob) and not diagram.z: return PRO(self.ob[Ty(diagram.name)]) if isinstance(diagram, Diagram): return Circuit._upgrade(super().__call__(diagram)) return super().__call__(diagram)
def __init__(self, name, n_qubits, array=None, data=None, _dagger=False): """ >>> g = CX >>> assert g.dom == g.cod == PRO(2) """ if array is not None: self._array = np.array(array).reshape(2 * n_qubits * (2, ) or 1) Box.__init__(self, name, PRO(n_qubits), PRO(n_qubits), data=data, _dagger=_dagger) Circuit.__init__(self, n_qubits, n_qubits, [self], [0])
def __call__(self, *values): """ Call method implemented using PythonFunctors. >>> assert SWAP(1, 2) == (2, 1) >>> assert (COPY @ COPY >> Id(1) @ SWAP @ Id(1))(1, 2) == (1, 2, 1, 2) """ ob = Quiver(lambda t: PRO(len(t))) ar = Quiver(lambda f: Function(len(f.dom), len(f.cod), f.function)) return PythonFunctor(ob, ar)(self)(*values)
def __init__(self, dom=0): super().__init__(PRO(dom))
def swap(left, right): left = left if isinstance(left, PRO) else PRO(left) right = right if isinstance(right, PRO) else PRO(right) return monoidal.Diagram.swap( left, right, ar_factory=Diagram, swap_factory=Swap)
Diagram.__init__(self, dom, cod, [self], [0]) class Swap(rigid.Swap, Box): """ Swap in a ZX diagram. """ def __init__(self, left, right): rigid.Swap.__init__(self, left, right) Box.__init__(self, self.name, self.dom, self.cod) def __repr__(self): return "SWAP" __str__ = __repr__ SWAP = Swap(PRO(1), PRO(1)) class Spider(Box): """ Abstract spider box. """ def __init__(self, n_legs_in, n_legs_out, phase=0, name=None): dom, cod = PRO(n_legs_in), PRO(n_legs_out) super().__init__(name, dom, cod, data=phase) self.draw_as_spider, self.drawing_name = True, phase or "" self.tikzstyle_name = name @property def name(self): return "{}({}, {}{})".format( self._name, len(self.dom), len(self.cod), ", {}".format(format_number(self.phase)) if self.phase else "")
def __init__(self, dom, cod, function): self._function = function super().__init__(repr(function), PRO(dom), PRO(cod))
def __init__(self, left, right): dom, cod = PRO(left) @ PRO(right), PRO(right) @ PRO(left) boxes = [SWAP for i in range(left) for j in range(right)] offsets = [left + i - 1 - j for j in range(left) for i in range(right)] super().__init__(dom, cod, boxes, offsets)
def __init__(self): super().__init__('H', PRO(1), PRO(1)) self.draw_as_spider = True self.drawing_name = '' self.color, self.shape = "yellow", "s"
def __init__(self): super().__init__('H', PRO(1), PRO(1)) self.draw_as_spider = True self.drawing_name, self.tikzstyle_name, = '', 'H' self.color, self.shape = "yellow", "rectangle"
def __init__(self, dom, cod, boxes, offsets, layers=None): super().__init__(PRO(dom), PRO(cod), boxes, offsets, layers=layers)
def __init__(self, dom, cod, boxes, offsets, layers=None): """ >>> c = Circuit(2, 2, [CX, CX], [0, 0]) """ super().__init__(PRO(dom), PRO(cod), boxes, offsets, layers)
def remove_scalars(diagram): for i, box in enumerate(diagram.boxes): if box.dom == box.cod == PRO(): return diagram[:i] >> diagram[i + 1:], box.array[0] return diagram, None
def __init__(self, n_legs_in, n_legs_out, phase=0, name=None): dom, cod = PRO(n_legs_in), PRO(n_legs_out) super().__init__(name, dom, cod, data=phase) self.draw_as_spider, self.drawing_name = True, phase or "" self.tikzstyle_name = name
def permutation(perm, dom=None): dom = PRO(len(perm)) if dom is None else dom return monoidal.Diagram.permutation(perm, dom, ar_factory=Diagram)
def __init__(self, dom): """ >>> assert Diagram.id(42) == Id(42) == Diagram(42, 42, [], []) """ super().__init__(PRO(dom), PRO(dom), [], [], layers=None)
def __init__(self, data): super().__init__("scalar", PRO(0), PRO(0), data=data) self.drawing_name = format_number(data)
return False def __hash__(self): return hash(repr(self)) class DaggerFunction(Function): def __init__(self, dom, cod, function, dagger_function): self._dagger_function = dagger_function super().__init__(dom, cod, function) def dagger(self): return type(self)(self.cod, self.dom, self._dagger_function, self.function) def functionize(f): if isinstance(f, CallableDaggerBox): dagger_function = f.dagger().function return DaggerFunction(len(f.dom), len(f.cod), f.function, dagger_function) return Function(len(f.dom), len(f.cod), f.function) _PYTHON_FUNCTOR = Functor(ob=lambda t: PRO(len(t)), ar=functionize, ob_factory=PRO, ar_factory=Function) Diagram.__call__ = lambda self, *values: _PYTHON_FUNCTOR(self)(*values)