| Safe Haskell | None |
|---|---|
| Language | GHC2024 |
Proarrow.Category.Instance.TensorNetwork
Description
Tensor networks as a hypergraph category: the same arrows as the matrices of Proarrow.Category.Instance.Mat, kept as a network instead of as one matrix. An object is a list of dimensions, one for each wire. An arrow has nodes, each with a dimension, a node for each of its input and output wires, and dense factors: flat vectors, each with a node for each of its axes. Its entry at given indices of the wires is the sum, over the indices of the nodes that agree with those of the wires, of the products of the entries of the factors.
So the structure of a hypergraph category costs nothing: identities, swaps, copying, merging, discarding, cups and caps are wirings without factors, and the tensor puts factors side by side without multiplying them. Composition glues the wirings and sums out the nodes that are no longer on a wire, multiplying only the factors that a summed node joins. With Proarrow.Tools.Einsum a network of tensors is contracted in the order its read-back chooses.
With the package's blas flag, a contraction of two factors that is a matrix product is handed
to the system's BLAS (Accelerate on macOS, OpenBLAS elsewhere) at entries of type Double,
Float and .Complex Double
The biproducts are direct sums, a single wire whose dimension is the sum of the sizes of the two objects. Their injections, projections and pairings are dense matrices, and so are the distributors.
Synopsis
- data TNET e = TN [Nat]
- data TensorNetwork (a :: TNET e) (b :: TNET e)
- class (Num e, Storable e) => Scalar e
- dimsOf :: forall (ns :: [Nat]). KnownListOf KnownNat ns => [Dim]
- type family Size (ns :: [Nat]) :: Nat where ...
- fromVector :: forall {e} (as :: [Nat]) (bs :: [Nat]). (Scalar e, KnownListOf KnownNat as, KnownListOf KnownNat bs) => Vector e -> TensorNetwork ('TN as :: TNET e) ('TN bs :: TNET e)
- toVector :: forall {e} (a :: TNET e) (b :: TNET e). Scalar e => TensorNetwork a b -> Vector e
- fromEntries :: forall {e} (as :: [Nat]) (bs :: [Nat]). (Scalar e, KnownListOf KnownNat as, KnownListOf KnownNat bs) => [[e]] -> TensorNetwork ('TN as :: TNET e) ('TN bs :: TNET e)
- entries :: forall {e} (a :: TNET e) (b :: TNET e). Scalar e => TensorNetwork a b -> [[e]]
- matMul :: Scalar e => Bool -> Bool -> Int -> Int -> Int -> Vector e -> Vector e -> Vector e
Documentation
Objects are lists of dimensions, one for each wire.
Instances
| Scalar e => Monoidal (TNET e) Source Github # | The wires side by side as the tensor: on matrices, the Kronecker product. | ||||
Defined in Proarrow.Category.Instance.TensorNetwork Associated Types
Methods withOb2 :: forall (a :: TNET e) (b :: TNET e) r. (Ob a, Ob b) => (Ob (a ** b) => r) -> r Source Github # leftUnitor :: forall (a :: TNET e). Ob a => ((Unit :: TNET e) ** a) ~> a Source Github # leftUnitorInv :: forall (a :: TNET e). Ob a => a ~> ((Unit :: TNET e) ** a) Source Github # rightUnitor :: forall (a :: TNET e). Ob a => (a ** (Unit :: TNET e)) ~> a Source Github # rightUnitorInv :: forall (a :: TNET e). Ob a => a ~> (a ** (Unit :: TNET e)) Source Github # associator :: forall (a :: TNET e) (b :: TNET e) (c :: TNET e). (Ob a, Ob b, Ob c) => ((a ** b) ** c) ~> (a ** (b ** c)) Source Github # associatorInv :: forall (a :: TNET e) (b :: TNET e) (c :: TNET e). (Ob a, Ob b, Ob c) => (a ** (b ** c)) ~> ((a ** b) ** c) Source Github # | |||||
| Scalar e => SymMonoidal (TNET e) Source Github # | |||||
| Scalar e => Closed (TNET e) Source Github # | |||||
Defined in Proarrow.Category.Instance.TensorNetwork Methods withObExp :: forall (a :: TNET e) (b :: TNET e) r. (Ob a, Ob b) => (Ob (a ~~> b) => r) -> r Source Github # curry :: forall (a :: TNET e) (b :: TNET e) (c :: TNET e). (Ob a, Ob b) => ((a ** b) ~> c) -> a ~> (b ~~> c) Source Github # apply :: forall (a :: TNET e) (b :: TNET e). (Ob a, Ob b) => ((a ~~> b) ** a) ~> b Source Github # (^^^) :: forall (a :: TNET e) (b :: TNET e) (x :: TNET e) (y :: TNET e). (b ~> y) -> (x ~> a) -> (a ~~> b) ~> (x ~~> y) Source Github # | |||||
| Scalar e => CompactClosed (TNET e) Source Github # | |||||
| Scalar e => CopyDiscard (TNET e) Source Github # | |||||
| Scalar e => Dialogue (TNET e) Source Github # | Every object is self-dual, with the transpose as the dual of an arrow: its wiring the other way round. | ||||
Defined in Proarrow.Category.Instance.TensorNetwork Methods withObDual :: forall (a :: TNET e) r. Ob a => (Ob (Dual a) => r) -> r Source Github # dual :: forall (a :: TNET e) (b :: TNET e). (a ~> b) -> Dual b ~> Dual a Source Github # linDist :: forall (a :: TNET e) (b :: TNET e) (c :: TNET e). (Ob a, Ob b, Ob c) => ((a ** b) ~> Dual c) -> a ~> Dual (b ** c) Source Github # linDistInv :: forall (a :: TNET e) (b :: TNET e) (c :: TNET e). (Ob a, Ob b, Ob c) => (a ~> Dual (b ** c)) -> (a ** b) ~> Dual c Source Github # doubleNegInv :: forall (a :: TNET e). Ob a => a ~> Dual (Dual a) Source Github # | |||||
| Scalar e => Distributive (TNET e) Source Github # | The distributors reorder the entries of the direct sums. | ||||
Defined in Proarrow.Category.Instance.TensorNetwork Methods distL :: forall (a :: TNET e) (b :: TNET e) (c :: TNET e). (Ob a, Ob b, Ob c) => (a ** (b || c)) ~> ((a ** b) || (a ** c)) Source Github # distR :: forall (a :: TNET e) (b :: TNET e) (c :: TNET e). (Ob a, Ob b, Ob c) => ((a || b) ** c) ~> ((a ** c) || (b ** c)) Source Github # absorbL :: forall (a :: TNET e). Ob a => (a ** (InitialObject :: TNET e)) ~> (InitialObject :: TNET e) Source Github # absorbR :: forall (a :: TNET e). Ob a => ((InitialObject :: TNET e) ** a) ~> (InitialObject :: TNET e) Source Github # | |||||
| Scalar e => Hypergraph (TNET e) Source Github # | |||||
Defined in Proarrow.Category.Instance.TensorNetwork | |||||
| Scalar e => Sized (TNET e) Source Github # | The size of an object is the product of its dimensions. | ||||
| Scalar e => IsoMix (TNET e) Source Github # | |||||
| Scalar e => StarAutonomous (TNET e) Source Github # | |||||
| Scalar e => HasBinaryCoproducts (TNET e) Source Github # | The direct sum, as a single wire: the transpose of the product. | ||||
Defined in Proarrow.Category.Instance.TensorNetwork Methods withObCoprod :: forall (a :: TNET e) (b :: TNET e) r. (Ob a, Ob b) => (Ob (a || b) => r) -> r Source Github # lft :: forall (a :: TNET e) (b :: TNET e). (Ob a, Ob b) => a ~> (a || b) Source Github # rgt :: forall (a :: TNET e) (b :: TNET e). (Ob a, Ob b) => b ~> (a || b) Source Github # (|||) :: forall (x :: TNET e) (a :: TNET e) (y :: TNET e). (x ~> a) -> (y ~> a) -> (x || y) ~> a Source Github # (+++) :: forall (a :: TNET e) (b :: TNET e) (x :: TNET e) (y :: TNET e). (a ~> x) -> (b ~> y) -> (a || b) ~> (x || y) Source Github # | |||||
| Scalar e => HasBiproducts (TNET e) Source Github # | |||||
| Scalar e => HasInitialObject (TNET e) Source Github # | A wire of dimension 0. | ||||
Defined in Proarrow.Category.Instance.TensorNetwork Associated Types
| |||||
| Scalar e => CategoryOf (TNET e) Source Github # | Tensor networks with entries | ||||
Defined in Proarrow.Category.Instance.TensorNetwork Associated Types
| |||||
| Scalar e => HasBinaryProducts (TNET e) Source Github # | The direct sum, as a single wire: the entries of the first arrow above those of the second. | ||||
Defined in Proarrow.Category.Instance.TensorNetwork Methods withObProd :: forall (a :: TNET e) (b :: TNET e) r. (Ob a, Ob b) => (Ob (a && b) => r) -> r Source Github # fst :: forall (a :: TNET e) (b :: TNET e). (Ob a, Ob b) => (a && b) ~> a Source Github # snd :: forall (a :: TNET e) (b :: TNET e). (Ob a, Ob b) => (a && b) ~> b Source Github # (&&&) :: forall (a :: TNET e) (x :: TNET e) (y :: TNET e). (a ~> x) -> (a ~> y) -> a ~> (x && y) Source Github # (***) :: forall (a :: TNET e) (b :: TNET e) (x :: TNET e) (y :: TNET e). (a ~> x) -> (b ~> y) -> (a && b) ~> (x && y) Source Github # | |||||
| Scalar e => HasTerminalObject (TNET e) Source Github # | A wire of dimension 0. | ||||
Defined in Proarrow.Category.Instance.TensorNetwork Associated Types
| |||||
| Scalar e => DecidableIso (TNET e) Source Github # | Two objects are isomorphic when they have the same dimensions. | ||||
| Scalar e => DaggerProfunctor (TensorNetwork :: TNET e -> TNET e -> Type) Source Github # | The conjugate transpose: the factors with their entries and conjugates swapped. | ||||
Defined in Proarrow.Category.Instance.TensorNetwork Methods dagger :: forall (a :: TNET e) (b :: TNET e). TensorNetwork a b -> TensorNetwork b a Source Github # | |||||
| Scalar e => Promonad (TensorNetwork :: TNET e -> TNET e -> Type) Source Github # | |||||
Defined in Proarrow.Category.Instance.TensorNetwork Methods id :: forall (a :: TNET e). Ob a => TensorNetwork a a Source Github # (.) :: forall (b :: TNET e) (c :: TNET e) (a :: TNET e). TensorNetwork b c -> TensorNetwork a b -> TensorNetwork a c Source Github # | |||||
| (Scalar e, MonoidalAction t) => Costrong (t :: (TNET e, TNET e) +-> TNET e) (TensorNetwork :: TNET e -> TNET e -> Type) Source Github # | |||||
Defined in Proarrow.Category.Instance.TensorNetwork | |||||
| Scalar e => MonoidalProfunctor (TensorNetwork :: TNET e -> TNET e -> Type) Source Github # | |||||
Defined in Proarrow.Category.Instance.TensorNetwork | |||||
| Scalar e => Profunctor (TensorNetwork :: TNET e -> TNET e -> Type) Source Github # | |||||
Defined in Proarrow.Category.Instance.TensorNetwork Methods dimap :: forall (c :: TNET e) (a :: TNET e) (b :: TNET e) (d :: TNET e). (c ~> a) -> (b ~> d) -> TensorNetwork a b -> TensorNetwork c d Source Github # lmap :: forall (c :: TNET e) (a :: TNET e) (b :: TNET e). (c ~> a) -> TensorNetwork a b -> TensorNetwork c b Source Github # rmap :: forall (b :: TNET e) (d :: TNET e) (a :: TNET e). (b ~> d) -> TensorNetwork a b -> TensorNetwork a d Source Github # (\\) :: forall (a :: TNET e) (b :: TNET e) r. ((Ob a, Ob b) => r) -> TensorNetwork a b -> r Source Github # | |||||
| (Scalar e, KnownListOf KnownNat ns) => Frobenius ('TN ns :: TNET e) Source Github # | |||||
Defined in Proarrow.Category.Instance.TensorNetwork | |||||
| (Scalar e, KnownListOf KnownNat ns) => CocommutativeComonoid ('TN ns :: TNET e) Source Github # | |||||
Defined in Proarrow.Category.Instance.TensorNetwork | |||||
| (Scalar e, KnownListOf KnownNat ns) => CommutativeMonoid ('TN ns :: TNET e) Source Github # | |||||
Defined in Proarrow.Category.Instance.TensorNetwork | |||||
| (Scalar e, KnownListOf KnownNat ns) => Comonoid ('TN ns :: TNET e) Source Github # | Copying each wire, and discarding. | ||||
| (Scalar e, KnownListOf KnownNat ns) => Monoid ('TN ns :: TNET e) Source Github # | Merging each wire with its partner, and the unit. | ||||
| type Unit Source Github # | |||||
Defined in Proarrow.Category.Instance.TensorNetwork | |||||
| type InitialObject Source Github # | |||||
Defined in Proarrow.Category.Instance.TensorNetwork | |||||
| type (~>) Source Github # | |||||
Defined in Proarrow.Category.Instance.TensorNetwork | |||||
| type TerminalObject Source Github # | |||||
Defined in Proarrow.Category.Instance.TensorNetwork | |||||
| type Dual (a :: TNET e) Source Github # | |||||
Defined in Proarrow.Category.Instance.TensorNetwork | |||||
| type Ob (a :: TNET e) Source Github # | |||||
| type (a :: TNET e) ** (b :: TNET e) Source Github # | |||||
| type (a :: TNET e) ~~> (b :: TNET e) Source Github # | |||||
Defined in Proarrow.Category.Instance.TensorNetwork | |||||
| type (a :: TNET e) || (b :: TNET e) Source Github # | |||||
| type (a :: TNET e) && (b :: TNET e) Source Github # | |||||
data TensorNetwork (a :: TNET e) (b :: TNET e) Source Github #
An arrow between two lists of dimensions.
Instances
class (Num e, Storable e) => Scalar e Source Github #
The entries of the factors: numbers that can be stored unboxed. An instance needs no methods: the loop that contracts factors is then compiled for its type.
dimsOf :: forall (ns :: [Nat]). KnownListOf KnownNat ns => [Dim] Source Github #
The dimensions of a list, as numbers.
type family Size (ns :: [Nat]) :: Nat where ... Source Github #
The size of a list of dimensions: their product.
fromVector :: forall {e} (as :: [Nat]) (bs :: [Nat]). (Scalar e, KnownListOf KnownNat as, KnownListOf KnownNat bs) => Vector e -> TensorNetwork ('TN as :: TNET e) ('TN bs :: TNET e) Source Github #
The arrow with the given entries, laid out as toVector gives them. The length of the vector
must be the product of all the dimensions.
toVector :: forall {e} (a :: TNET e) (b :: TNET e). Scalar e => TensorNetwork a b -> Vector e Source Github #
The entries, row by row: a row for each index of the output wires and a column for each index of the input wires, both with the last wire varying fastest. This relies on every node of the network being on a wire, which composition keeps so.
fromEntries :: forall {e} (as :: [Nat]) (bs :: [Nat]). (Scalar e, KnownListOf KnownNat as, KnownListOf KnownNat bs) => [[e]] -> TensorNetwork ('TN as :: TNET e) ('TN bs :: TNET e) Source Github #
The arrow with the given rows of entries, laid out as entries gives them.
entries :: forall {e} (a :: TNET e) (b :: TNET e). Scalar e => TensorNetwork a b -> [[e]] Source Github #
The entries as rows, as toVector lays them out.
matMul :: Scalar e => Bool -> Bool -> Int -> Int -> Int -> Vector e -> Vector e -> Vector e Source Github #
The product of two matrices given row by row, for products that are not part of a network:
whether the first and the second are stored transposed, the numbers of rows, columns and summed
indices, and the two matrices; the result row by row. It is the contraction of two factors on the
nodes of the rows, the columns and the summed index, so large products go to gemm.