Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/notebooks/bose_hubbard.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "12d61180-27de-4d3a-bad6-73a0e94c8b16",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -925,9 +925,9 @@
}
],
"source": [
"from optyx import photonic, classical\n",
"from optyx import photonic\n",
"\n",
"circuit = photonic.Create(1, 1, 1) >> photonic.ansatz(3, 4) >> photonic.Id(2) @ classical.Select(1)\n",
"circuit = photonic.Create(1, 1, 1) >> photonic.ansatz(3, 4) >> photonic.Id(2) @ photonic.Select(1)\n",
"circuit.draw()"
]
},
Expand Down
6 changes: 3 additions & 3 deletions docs/notebooks/bosonic-vqe-2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "12d61180-27de-4d3a-bad6-73a0e94c8b16",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -893,9 +893,9 @@
}
],
"source": [
"from optyx import photonic, classical\n",
"from optyx import photonic\n",
"\n",
"circuit = photonic.Create(1, 1, 1) >> photonic.ansatz(3, 4) >> photonic.Id(2) @ classical.Select(1)\n",
"circuit = photonic.Create(1, 1, 1) >> photonic.ansatz(3, 4) >> photonic.Id(2) @ photonic.Select(1)\n",
"circuit.draw()"
]
},
Expand Down
4 changes: 2 additions & 2 deletions docs/notebooks/readme_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"id": "f1478f68",
"metadata": {},
"outputs": [
Expand All @@ -1052,7 +1052,7 @@
],
"source": [
"# an amplitude (raw result of tensor contraction)\n",
"from optyx.classical import Select\n",
"from optyx.photonic import Select\n",
"(\n",
" hong_ou_mandel >> Select(1, 1)\n",
").eval().tensor.array"
Expand Down
11 changes: 9 additions & 2 deletions optyx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
from optyx.core import zw
from optyx.core import zx
from optyx.core import path
from optyx.core import backends
from optyx.core import diagram
from optyx.core import control
from optyx.core.channel import (
Channel,
CQMap,
Discard,
Encode,
Measure,
Diagram,
Swap,
Spider,
Id,
Scalar,
mode,
qmode,
qubit,
bit,
Diagram
bit
)

from optyx._version import (
Expand Down
61 changes: 21 additions & 40 deletions optyx/classical.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
:nosignatures:
:toctree:

NotBit
XorBit
AndBit
OrBit
Not
Xor
And
Or
CopyBit
SwapBit
Z
Expand All @@ -40,10 +40,10 @@
:nosignatures:
:toctree:

AddN
SubN
MultiplyN
DivideN
Add
Sub
Multiply
Divide
Mod2
CopyN
SwapN
Expand Down Expand Up @@ -90,7 +90,7 @@
2. Using a :code:`BinaryMatrix` to define the transformation.
3. Using primitives such as :code:`XorBit`, :code:`AddN`, etc.

>>> xor_gate = XorBit(2)
>>> xor_gate = Xor(2)
>>>
>>> f = ClassicalFunction(lambda b: [b[0] ^ b[1]],
... bit**2, bit)
Expand Down Expand Up @@ -122,7 +122,7 @@
**3. Arithmetic on natural-number modes**

>>> num = Digit(3, 2)
>>> add = AddN(2) # (x,y,z) => x+y+z
>>> add = Add(2) # (x,y,z) => x+y+z
>>> parity = add >> Mod2() # outputs (x+y+z) mod 2 as a bit
>>> post = PostselectBit(1)
>>> assert np.allclose(
Expand All @@ -138,8 +138,7 @@
control,
zw,
zx,
diagram,
path
diagram
)
from optyx.core.channel import (
bit,
Expand Down Expand Up @@ -285,7 +284,7 @@ def __init__(self, value):
)


class AddN(ClassicalBox):
class Add(ClassicalBox):
"""
Classical addition of n natural numbers.
The domain of the map is n modes.
Expand All @@ -300,7 +299,7 @@ def __init__(self, n):
)


class SubN(ClassicalBox):
class Sub(ClassicalBox):
"""
Classical subtraction: subtract the first number from the second.
The domain of the map is 2 modes.
Expand All @@ -320,7 +319,7 @@ def __init__(self):
)


class MultiplyN(ClassicalBox):
class Multiply(ClassicalBox):
"""
Classical multiplication of 2 natural numbers.
The domain of the map is 2 modes.
Expand All @@ -335,7 +334,7 @@ def __init__(self):
)


class DivideN(ClassicalBox):
class Divide(ClassicalBox):
"""
Classical division: divide the first number by the second.
The domain of the map is 2 modes.
Expand Down Expand Up @@ -437,7 +436,7 @@ def __init__(self, *digits):
)


class NotBit(ClassicalBox):
class Not(ClassicalBox):
"""
Classical NOT gate.
The domain of the map is a bit.
Expand All @@ -453,7 +452,7 @@ def __init__(self):
)


class XorBit(ClassicalBox):
class Xor(ClassicalBox):
"""
Classical XOR gate.
The domain of the map is n bits.
Expand All @@ -462,14 +461,14 @@ class XorBit(ClassicalBox):
"""
def __init__(self, n=2):
super().__init__(
f"XorBit({n})",
f"Xor({n})",
zx.X(n, 1) @ diagram.Scalar(np.sqrt(n)),
bit**n,
bit
)


class AndBit(ClassicalBox):
class And(ClassicalBox):
"""
Classical AND gate.
The domain of the map is 2 bits.
Expand Down Expand Up @@ -517,7 +516,7 @@ def __init__(self):
)


class OrBit(ClassicalBox):
class Or(ClassicalBox):
"""
Classical OR gate.
The domain of the map is n bits.
Expand All @@ -526,7 +525,7 @@ class OrBit(ClassicalBox):
"""
def __init__(self, n=2):
super().__init__(
f"OrBit({n})",
f"Or({n})",
zx.Or(n),
bit**n,
bit
Expand Down Expand Up @@ -658,24 +657,6 @@ def __init__(self, matrix):
)


class Select(Channel):
"""
Post-select on an occupation number.
"""
def __init__(self, *photons: int):
self.photons = photons
super().__init__(
f"Select({photons})",
zw.Select(*photons)
)

def to_path(self, dtype=complex) -> path.Matrix:
array = np.eye(len(self.photons))
return path.Matrix[dtype](
array, len(self.photons), 0, selections=self.photons
)


class Digit(ClassicalBox):
"""
Create a classical state with
Expand Down
2 changes: 1 addition & 1 deletion optyx/core/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
from quimb.tensor import TensorNetwork
from optyx.core.channel import Diagram, Ty, mode, bit
from optyx.core.path import Matrix
from optyx.utils.utils import preprocess_quimb_tensors_safe
from optyx.utils.misc import preprocess_quimb_tensors_safe


@dataclass
Expand Down
9 changes: 9 additions & 0 deletions optyx/core/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,15 @@ class Hypergraph(hypergraph.Hypergraph): # pragma: no cover
category, functor = Category, Functor


Id = Diagram.id
Scalar = lambda s: Channel( # noqa: E731
name=f"Scalar({s})",
kraus=diagram.Scalar(s),
dom=Ty(),
cod=Ty()
)


Hypergraph.ty_factory = Ty
Diagram.spider_factory = Spider
Diagram.hypergraph_factory = Hypergraph
Expand Down
2 changes: 1 addition & 1 deletion optyx/core/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from discopy import tensor
from discopy.frobenius import Dim
import numpy as np
from optyx.utils.utils import BasisTransition, is_diagram_LO
from optyx.utils.misc import BasisTransition, is_diagram_LO

from optyx.core import diagram, zw

Expand Down
6 changes: 3 additions & 3 deletions optyx/core/diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
Let's check the branching law from [FC23]_.

>>> from optyx.core.zw import Create, W
>>> from optyx.utils.utils import compare_arrays_of_different_sizes
>>> from optyx.utils.misc import compare_arrays_of_different_sizes
>>> branching_l = Create(1) >> W(2)
>>> branching_r = Create(1) @ Create(0) + Create(0) @ Create(1)

Expand Down Expand Up @@ -207,7 +207,7 @@
from discopy.frobenius import Dim
from discopy.quantum.gates import format_number
from enum import Enum
from optyx.utils.utils import (
from optyx.utils.misc import (
BasisTransition,
calculate_right_offset,
get_max_dim_for_box
Expand Down Expand Up @@ -303,7 +303,7 @@ def to_tensor(
) -> tensor.Diagram:
"""Returns a :class:`tensor.Diagram` for evaluation"""
from optyx.core import zw
from optyx.utils.utils import is_identity
from optyx.utils.misc import is_identity

if input_dims is None:
input_dims = [2 for _ in range(len(self.dom))]
Expand Down
2 changes: 1 addition & 1 deletion optyx/core/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
from discopy.utils import unbiased
import discopy.matrix as underlying
from discopy.tensor import Tensor
from optyx.utils.utils import occupation_numbers, amplitudes_2_tensor
from optyx.utils.misc import occupation_numbers, amplitudes_2_tensor


def npperm(matrix):
Expand Down
10 changes: 3 additions & 7 deletions optyx/core/zw.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

**W commutativity**

>>> from optyx.utils.utils import compare_arrays_of_different_sizes
>>> from optyx.utils.misc import compare_arrays_of_different_sizes
>>> from discopy.drawing import Equation
>>> bSym_l = W(2)
>>> bSym_r = W(2) >> SWAP
Expand Down Expand Up @@ -150,19 +150,15 @@
from discopy.frobenius import Dim
from discopy import tensor
from optyx.core import diagram
from optyx.utils.utils import (
from optyx.utils.misc import (
occupation_numbers,
multinomial,
BasisTransition
)
from optyx.core.path import Matrix


class ZWDiagram(diagram.Diagram):
pass


class ZWBox(diagram.Box, ZWDiagram):
class ZWBox(diagram.Box, diagram.Diagram):
"""Box in a :class:`Diagram`"""

def __init__(self, name, dom, cod, **params):
Expand Down
Loading