circuit#

Implements classical-quantum circuits.

Objects are Ty generated by two basic types bit and qubit.

Arrows are diagrams generated by QuantumGate, ClassicalGate, Discard, Measure and Encode.

>>> from discopy.quantum.gates import Ket, CX, H, X, Rz, sqrt, Controlled
>>> circuit = Ket(0, 0) >> CX >> Controlled(Rz(0.25)) >> Measure() @ Discard()
>>> circuit.draw(
...     figsize=(3, 6),
...     path='docs/_static/imgs/quantum/circuit-example.png')
../_images/circuit-example.png
>>> from discopy.grammar.pregroup import Word
>>> from discopy.rigid import Ty, Cup, Id
>>> s, n = Ty('s'), Ty('n')
>>> Alice = Word('Alice', n)
>>> loves = Word('loves', n.r @ s @ n.l)
>>> Bob = Word('Bob', n)
>>> grammar = Cup(n, n.r) @ Id(s) @ Cup(n.l, n)
>>> sentence = grammar << Alice @ loves @ Bob
>>> ob = {s: 0, n: 1}
>>> ar = {Alice: Ket(0),
...       loves: CX << sqrt(2) @ H @ X << Ket(0, 0),
...       Bob: Ket(1)}
>>> F = Functor(ob, ar)
>>> assert abs(F(sentence).eval().array) ** 2
>>> from discopy import drawing
>>> drawing.equation(
...     sentence, F(sentence), symbol='$\\mapsto$',
...     figsize=(6, 3), nodesize=.5,
...     path='docs/_static/imgs/quantum/functor-example.png')
../_images/functor-example1.png