10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-11-15 02:23:39 +01:00
QCaml/CI/Excitation.ml

178 lines
5.4 KiB
OCaml
Raw Normal View History

2019-02-18 15:39:26 +01:00
type single_exc =
{
hole : int ;
particle : int ;
spin : Spin.t ;
}
type t =
2019-02-18 19:45:41 +01:00
| Identity of Phase.t
| Single of Phase.t * single_exc
| Double of Phase.t * single_exc * single_exc
| Multiple of Phase.t * single_exc list
2019-02-18 15:39:26 +01:00
let single_of_spindet t t' =
assert (Spindeterminant.degree t t' = 1);
let d = Spindeterminant.bitstring t
and d' = Spindeterminant.bitstring t'
in
let tmp = Z.logxor d d' in
let hole_z = Z.logand (Spindeterminant.bitstring t ) tmp
and particle_z = Z.logand (Spindeterminant.bitstring t') tmp
in
let hole = 1 + Z.trailing_zeros hole_z
and particle = 1 + Z.trailing_zeros particle_z
in
(* Phase calculation *)
let low, high =
if particle > hole then hole, particle
else particle, hole
in
let mask =
let h = high-1 in
2019-02-18 19:45:41 +01:00
let l = low in
2019-02-18 15:39:26 +01:00
let mask_up = Z.(shift_left one h - one)
2019-02-18 19:45:41 +01:00
and mask_dn = Z.(lognot (shift_left one l) + one)
2019-02-18 15:39:26 +01:00
in Z.logand mask_up mask_dn
in
let phase =
2019-02-18 19:45:41 +01:00
Phase.add (Phase.add (Spindeterminant.phase t) (Spindeterminant.phase t'))
(Phase.of_nperm (Z.(popcount @@ logand d mask )))
2019-02-18 15:39:26 +01:00
in
(hole, particle, phase)
let single_of_det t t' =
2019-02-18 19:45:41 +01:00
assert Determinant.(beta t = beta t' || alfa t = alfa t');
2019-02-18 15:39:26 +01:00
if Determinant.(beta t = beta t') then
let hole, particle, phase =
single_of_spindet (Determinant.alfa t) (Determinant.alfa t')
in
2019-02-18 19:45:41 +01:00
Single (phase, { hole ; particle ; spin=Spin.Alfa })
2019-02-18 15:39:26 +01:00
else
let hole, particle, phase =
single_of_spindet (Determinant.beta t) (Determinant.beta t')
in
2019-02-18 19:45:41 +01:00
Single (phase, { hole ; particle ; spin=Spin.Beta })
2019-02-18 15:39:26 +01:00
2019-02-18 19:45:41 +01:00
let multiple_of_spindet t t' =
let holes = Spindeterminant.holes_of t t'
and particles = Spindeterminant.particles_of t t'
2019-02-18 15:39:26 +01:00
in
2019-02-18 19:45:41 +01:00
let t'' =
List.fold_left (fun accu h -> Spindeterminant.annihilation h accu) t holes
2019-02-18 15:39:26 +01:00
in
2019-02-18 19:45:41 +01:00
let t'' =
List.fold_left (fun accu p -> Spindeterminant.creation p accu) t'' particles
2019-02-18 15:39:26 +01:00
in
2019-02-18 19:45:41 +01:00
assert (t' = t'' || t' = Spindeterminant.negate_phase t'');
2019-02-18 15:39:26 +01:00
let phase =
2019-02-18 19:45:41 +01:00
if Spindeterminant.phase t' = Spindeterminant.phase t'' then
Phase.Pos
else
Phase.Neg
2019-02-18 15:39:26 +01:00
in
2019-02-18 19:45:41 +01:00
(phase, List.map2 (fun hole particle -> (hole, particle)) holes (List.rev particles) )
2019-02-18 15:39:26 +01:00
2019-02-22 00:18:32 +01:00
let double_of_spindet t t' =
match multiple_of_spindet t t' with
| (phase, (h1,p1)::(h2,p2)::[]) -> (h1, p1, h2, p2, phase)
| _ -> invalid_arg "t and t' are not doubly excited"
2019-02-18 19:45:41 +01:00
let multiple_of_det t t' =
let pa, a =
multiple_of_spindet (Determinant.alfa t) (Determinant.alfa t')
and pb, b =
multiple_of_spindet (Determinant.beta t) (Determinant.beta t')
in
let phase = Phase.add pa pb in
Multiple (phase, List.concat [
List.map (fun (hole, particle) -> { hole ; particle ; spin=Spin.Alfa }) a ;
List.map (fun (hole, particle) -> { hole ; particle ; spin=Spin.Beta }) b ])
let double_of_det t t' =
match multiple_of_det t t' with
| Multiple (phase, [e1 ; e2]) -> Double (phase, e1, e2)
| _ -> assert false
let pp_s_exc ppf t =
Format.fprintf ppf "@[T^{%s}_{%d->%d}@]"
2019-02-18 15:39:26 +01:00
(match t.spin with
| Spin.Alfa -> "\\alpha"
| Spin.Beta -> "\\beta " )
t.hole t.particle
2019-02-18 19:45:41 +01:00
let pp_exc ppf t =
let phase, l =
match t with
| Identity p -> p, []
| Single (p,x) -> p, x::[]
| Double (p,x,y) -> p, x::y::[]
| Multiple (p,l) -> p, l
in
Format.fprintf ppf "@[%c"
(match phase with
| Phase.Pos -> '+'
| Phase.Neg -> '-' );
List.iter (fun x -> Format.fprintf ppf "@[T^{%s}_{%d->%d}@]"
(match x.spin with
| Spin.Alfa -> "\\alpha"
| Spin.Beta -> "\\beta " )
x.hole x.particle) l;
Format.fprintf ppf "@]"
2019-02-18 15:39:26 +01:00
let test_case () =
2019-02-18 19:45:41 +01:00
(*
let test_id () =
let l_a = [ 1 ; 2 ; 3 ; 5 ; 64 ]
and l_b = [ 2 ; 3 ; 5 ; 65 ] in
let det1 = Determinant.of_lists l_a l_b in
let det2 = Determinant.negate_phase det1 in
in
*)
2019-02-18 15:39:26 +01:00
let test_single () =
let l_a = [ 1 ; 2 ; 3 ; 5 ; 64 ]
and l_b = [ 2 ; 3 ; 5 ; 65 ] in
let det1 = Determinant.of_lists l_a l_b in
let det2 = Determinant.single_excitation Spin.Alfa 3 7 det1 in
let t = single_of_det det1 det2 in
2019-02-18 19:45:41 +01:00
Alcotest.(check bool) "single 1" true (t = Single (Phase.Pos, { hole=3 ; particle=7 ; spin=Spin.Alfa}) );
let det2 =
Determinant.single_excitation Spin.Alfa 2 7 det1
|> Determinant.negate_phase
in
2019-02-18 15:39:26 +01:00
let t = single_of_det det1 det2 in
2019-02-18 19:45:41 +01:00
Alcotest.(check bool) "single 2" true (t = Single (Phase.Neg, { hole=2 ; particle=7 ; spin=Spin.Alfa }) );
2019-02-18 15:39:26 +01:00
let det2 = Determinant.single_excitation Spin.Beta 2 7 det1 in
let t = single_of_det det1 det2 in
2019-02-18 19:45:41 +01:00
Alcotest.(check bool) "single 3" true (t = Single (Phase.Pos, { hole=2 ; particle=7 ; spin=Spin.Beta}) );
2019-02-18 15:39:26 +01:00
let det2 = Determinant.single_excitation Spin.Beta 3 256 det1 in
let t = single_of_det det1 det2 in
2019-02-18 19:45:41 +01:00
Alcotest.(check bool) "single 4" true (t = Single (Phase.Pos, { hole=3 ; particle=256 ; spin=Spin.Beta}) );
in
let test_double () =
let l_a = [ 1 ; 2 ; 3 ; 5 ; 64 ]
and l_b = [ 2 ; 3 ; 5 ; 65 ] in
let det1 = Determinant.of_lists l_a l_b in
let det2 = Determinant.double_excitation Spin.Alfa 3 7 Spin.Alfa 2 6 det1 in
let t = double_of_det det1 det2 in
Alcotest.(check bool) "double 1" true
(t = Double (Phase.Neg,
{ hole=2 ; particle=7 ; spin=Spin.Alfa},
{ hole=3 ; particle=6 ; spin=Spin.Alfa}));
2019-02-18 15:39:26 +01:00
in
[
"Single", `Quick, test_single;
2019-02-18 19:45:41 +01:00
"Double", `Quick, test_double;
2019-02-18 15:39:26 +01:00
]