proarrow
Safe HaskellNone
LanguageGHC2024

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

Documentation

data TNET e Source Github #

Objects are lists of dimensions, one for each wire.

Constructors

TN [Nat] 

Instances

Instances details
Scalar e => Monoidal (TNET e) Source Github #

The wires side by side as the tensor: on matrices, the Kronecker product.

Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Associated Types

type Unit 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

type Unit = 'TN ('[] :: [Nat]) :: TNET e

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 # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Methods

swap :: forall (a :: TNET e) (b :: TNET e). (Ob a, Ob b) => (a ** b) ~> (b ** a) Source Github #

Scalar e => Closed (TNET e) Source Github # 
Instance details

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 # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Methods

distribDual :: forall (a :: TNET e) (b :: TNET e). (Ob a, Ob b) => Dual (a ** b) ~> (Dual a ** Dual b) Source Github #

dualityUnit :: forall (a :: TNET e). Ob a => (Unit :: TNET e) ~> (a ** Dual a) Source Github #

Scalar e => CopyDiscard (TNET e) Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Methods

copy :: forall (a :: TNET e). Ob a => a ~> (a ** a) Source Github #

discard :: forall (a :: TNET e). Ob a => a ~> (Unit :: 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.

Instance details

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.

Instance details

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 # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Scalar e => Sized (TNET e) Source Github #

The size of an object is the product of its dimensions.

Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Methods

sizeOf :: forall (a :: TNET e). Ob a => Int Source Github #

Scalar e => IsoMix (TNET e) Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Methods

dualUnit :: Dual (Unit :: TNET e) ~> (Unit :: TNET e) Source Github #

dualUnitInv :: (Unit :: TNET e) ~> Dual (Unit :: TNET e) Source Github #

dualityCounit :: forall (a :: TNET e). Ob a => (Dual a ** a) ~> (Unit :: TNET e) Source Github #

Scalar e => StarAutonomous (TNET e) Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Methods

dualInv :: forall (a :: TNET e) (b :: TNET e). (Ob a, Ob b) => (Dual a ~> Dual b) -> b ~> a Source Github #

doubleNeg :: forall (a :: TNET e). Ob a => Dual (Dual a) ~> a Source Github #

Scalar e => HasBinaryCoproducts (TNET e) Source Github #

The direct sum, as a single wire: the transpose of the product.

Instance details

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 # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Methods

sum :: forall (a :: TNET e) (b :: TNET e). (a ~> b) -> (a ~> b) -> a ~> b Source Github #

Scalar e => HasInitialObject (TNET e) Source Github #

A wire of dimension 0.

Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Associated Types

type InitialObject 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

type InitialObject = 'TN '[0] :: TNET e

Methods

initiate :: forall (a :: TNET e). Ob a => (InitialObject :: TNET e) ~> a Source Github #

Scalar e => CategoryOf (TNET e) Source Github #

Tensor networks with entries e, between lists of dimensions.

Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Associated Types

type (~>) 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

type (~>) = TensorNetwork :: TNET e -> TNET e -> Type
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.

Instance details

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.

Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Associated Types

type TerminalObject 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

type TerminalObject = 'TN '[0] :: TNET e

Methods

terminate :: forall (a :: TNET e). Ob a => a ~> (TerminalObject :: TNET e) Source Github #

Scalar e => DecidableIso (TNET e) Source Github #

Two objects are isomorphic when they have the same dimensions.

Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Methods

isoOf :: forall (c :: (TNET e +-> TNET e) -> Constraint) (a :: TNET e) (b :: TNET e). (Ob a, Ob b) => Maybe (Optic c a a b b) Source Github #

Scalar e => DaggerProfunctor (TensorNetwork :: TNET e -> TNET e -> Type) Source Github #

The conjugate transpose: the factors with their entries and conjugates swapped. dual is the transpose without conjugating, since the compact-closed structure is bilinear.

Instance details

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 # 
Instance details

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 # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Methods

coact :: forall (a :: TNET e) (x :: TNET e) (y :: TNET e). (Ob a, Ob x, Ob y) => TensorNetwork (Act t a x) (Act t a y) -> TensorNetwork x y Source Github #

Scalar e => MonoidalProfunctor (TensorNetwork :: TNET e -> TNET e -> Type) Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Methods

one :: TensorNetwork (Unit :: TNET e) (Unit :: TNET e) Source Github #

(**) :: forall (x1 :: TNET e) (x2 :: TNET e) (y1 :: TNET e) (y2 :: TNET e). TensorNetwork x1 x2 -> TensorNetwork y1 y2 -> TensorNetwork (x1 ** y1) (x2 ** y2) Source Github #

Scalar e => Profunctor (TensorNetwork :: TNET e -> TNET e -> Type) Source Github # 
Instance details

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 # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

(Scalar e, KnownListOf KnownNat ns) => CocommutativeComonoid ('TN ns :: TNET e) Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

(Scalar e, KnownListOf KnownNat ns) => CommutativeMonoid ('TN ns :: TNET e) Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

(Scalar e, KnownListOf KnownNat ns) => Comonoid ('TN ns :: TNET e) Source Github #

Copying each wire, and discarding.

Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Methods

counit :: ('TN ns :: TNET e) ~> (Unit :: TNET e) Source Github #

comult :: ('TN ns :: TNET e) ~> (('TN ns :: TNET e) ** ('TN ns :: TNET e)) Source Github #

(Scalar e, KnownListOf KnownNat ns) => Monoid ('TN ns :: TNET e) Source Github #

Merging each wire with its partner, and the unit.

Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Methods

mempty :: (Unit :: TNET e) ~> ('TN ns :: TNET e) Source Github #

mappend :: (('TN ns :: TNET e) ** ('TN ns :: TNET e)) ~> ('TN ns :: TNET e) Source Github #

type Unit Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

type Unit = 'TN ('[] :: [Nat]) :: TNET e
type InitialObject Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

type InitialObject = 'TN '[0] :: TNET e
type (~>) Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

type (~>) = TensorNetwork :: TNET e -> TNET e -> Type
type TerminalObject Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

type TerminalObject = 'TN '[0] :: TNET e
type Dual (a :: TNET e) Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

type Dual (a :: TNET e) = a
type Ob (a :: TNET e) Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

type Ob (a :: TNET e) = (Is ('TN :: [Nat] -> TNET e) a, KnownListOf KnownNat (UN ('TN :: [Nat] -> TNET e) a))
type (a :: TNET e) ** (b :: TNET e) Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

type (a :: TNET e) ** (b :: TNET e) = 'TN (UN ('TN :: [Nat] -> TNET e) a ++ UN ('TN :: [Nat] -> TNET e) b) :: TNET e
type (a :: TNET e) ~~> (b :: TNET e) Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

type (a :: TNET e) ~~> (b :: TNET e) = ExpSA a b
type (a :: TNET e) || (b :: TNET e) Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

type (a :: TNET e) || (b :: TNET e) = 'TN '[Size (UN ('TN :: [Nat] -> TNET e) a) + Size (UN ('TN :: [Nat] -> TNET e) b)] :: TNET e
type (a :: TNET e) && (b :: TNET e) Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

type (a :: TNET e) && (b :: TNET e) = 'TN '[Size (UN ('TN :: [Nat] -> TNET e) a) + Size (UN ('TN :: [Nat] -> TNET e) b)] :: TNET e

data TensorNetwork (a :: TNET e) (b :: TNET e) Source Github #

An arrow between two lists of dimensions.

Instances

Instances details
Scalar e => DaggerProfunctor (TensorNetwork :: TNET e -> TNET e -> Type) Source Github #

The conjugate transpose: the factors with their entries and conjugates swapped. dual is the transpose without conjugating, since the compact-closed structure is bilinear.

Instance details

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 # 
Instance details

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 # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Methods

coact :: forall (a :: TNET e) (x :: TNET e) (y :: TNET e). (Ob a, Ob x, Ob y) => TensorNetwork (Act t a x) (Act t a y) -> TensorNetwork x y Source Github #

Scalar e => MonoidalProfunctor (TensorNetwork :: TNET e -> TNET e -> Type) Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Methods

one :: TensorNetwork (Unit :: TNET e) (Unit :: TNET e) Source Github #

(**) :: forall (x1 :: TNET e) (x2 :: TNET e) (y1 :: TNET e) (y2 :: TNET e). TensorNetwork x1 x2 -> TensorNetwork y1 y2 -> TensorNetwork (x1 ** y1) (x2 ** y2) Source Github #

Scalar e => Profunctor (TensorNetwork :: TNET e -> TNET e -> Type) Source Github # 
Instance details

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 #

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.

Instances

Instances details
Scalar Double Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Methods

kernel :: [(Dim, Int, Int)] -> Vector Index -> Vector Index -> Vector Double -> Vector Double -> Vector Double

conj :: Maybe (Double -> Double)

gemm :: Maybe (Gemm Double)

Scalar Float Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Methods

kernel :: [(Dim, Int, Int)] -> Vector Index -> Vector Index -> Vector Float -> Vector Float -> Vector Float

conj :: Maybe (Float -> Float)

gemm :: Maybe (Gemm Float)

Scalar Int Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Methods

kernel :: [(Dim, Int, Int)] -> Vector Index -> Vector Index -> Vector Int -> Vector Int -> Vector Int

conj :: Maybe (Int -> Int)

gemm :: Maybe (Gemm Int)

Scalar (Complex Double) Source Github # 
Instance details

Defined in Proarrow.Category.Instance.TensorNetwork

Methods

kernel :: [(Dim, Int, Int)] -> Vector Index -> Vector Index -> Vector (Complex Double) -> Vector (Complex Double) -> Vector (Complex Double)

conj :: Maybe (Complex Double -> Complex Double)

gemm :: Maybe (Gemm (Complex Double))

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.

Equations

Size ('[] :: [Nat]) = 1 
Size (n ': ns) = n * Size ns 

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.