Constrained tensor product in F#

Let’s define a tensor product of two different types


in quantum mechanics there is this ket bra product also known as outer product

\left|\Psi\right> \left<\Phi\right|

in F# we can write a function

let tensor xvect yvect = 
    |> (fun x -> 
        |> (fun y -> 
           x, y)) 
    |> Seq.concat

Now we want to introduce a constraint such that each left or right vector appears only one time

Basically we have to

  1.  fold through the tensor
  2. check if a vector already exists in the accumulator
  3. if it is not the case, we append it into a new state

Now the problem is that we want also to do a compactification to reduce the unbalanced left and right matches to a minimum

Here it is our result

let cocycle chain =
    |> Seq.fold
        (fun cochain cycle->
            let (x: 'x, y: 'y) = cycle
            let foundx =
                |> Seq.exists (fun (x1: 'x option,y1: 'y option) -> (x1 = Some x) && (y1.IsSome) )
            let foundy =
                |> Seq.exists (fun (x1,y1) -> y1 = Some y && (x1.IsSome))
            match foundx, foundy with
            | true, true ->
            |false, false ->
                    (cochain |> Seq.except [(Some x, None); (None, Some y)] )
                    [(Some x, Some y)]
            |false, true ->
                    (cochain |> Seq.except [(Some x, None)] )
                    [(Some x, None)]
            |true, false ->
                    (cochain |> Seq.except [(None, Some y)] )
                    [(None, Some y)]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s