2929use crate :: graph:: * ;
3030use crate :: scalar:: * ;
3131use num:: traits:: Zero ;
32- use num:: Rational ;
32+ use num:: Rational64 ;
3333use rustc_hash:: FxHashSet ;
3434use std:: iter:: FromIterator ;
3535
@@ -293,7 +293,7 @@ pub fn local_comp_unchecked(g: &mut impl GraphLike, v: V) {
293293
294294 let x = ns. len ( ) as i32 ;
295295 g. scalar_mut ( ) . mul_sqrt2_pow ( ( ( x - 1 ) * ( x - 2 ) ) / 2 ) ;
296- g. scalar_mut ( ) . mul_phase ( Rational :: new ( * p. numer ( ) , 4 ) ) ;
296+ g. scalar_mut ( ) . mul_phase ( Rational64 :: new ( * p. numer ( ) , 4 ) ) ;
297297}
298298
299299checked_rule1 ! ( check_local_comp, local_comp_unchecked, local_comp) ;
@@ -350,7 +350,7 @@ pub fn pivot_unchecked(g: &mut impl GraphLike, v0: V, v1: V) {
350350 g. scalar_mut ( ) . mul_sqrt2_pow ( ( x - 2 ) * ( y - 2 ) ) ;
351351
352352 if !p0. is_zero ( ) && !p1. is_zero ( ) {
353- g. scalar_mut ( ) . mul_phase ( Rational :: new ( 1 , 1 ) ) ;
353+ g. scalar_mut ( ) . mul_phase ( Rational64 :: new ( 1 , 1 ) ) ;
354354 }
355355}
356356
@@ -366,7 +366,7 @@ fn unfuse_boundary(g: &mut impl GraphLike, v: V, b: V) {
366366 }
367367 let vd = VData {
368368 ty : VType :: Z ,
369- phase : Rational :: zero ( ) ,
369+ phase : Rational64 :: zero ( ) ,
370370 row : g. row ( v) ,
371371 qubit : g. qubit ( v) ,
372372 } ;
@@ -385,14 +385,14 @@ fn unfuse_gadget(g: &mut impl GraphLike, v: V) {
385385 }
386386 let vd = VData {
387387 ty : VType :: Z ,
388- phase : Rational :: zero ( ) ,
388+ phase : Rational64 :: zero ( ) ,
389389 row : g. row ( v) ,
390390 qubit : g. qubit ( v) ,
391391 } ;
392392 let v1 = g. add_vertex_with_data ( vd) ;
393393 let v2 = g. add_vertex_with_data ( vd) ;
394394 g. set_phase ( v2, g. phase ( v) ) ;
395- g. set_phase ( v, Rational :: zero ( ) ) ;
395+ g. set_phase ( v, Rational64 :: zero ( ) ) ;
396396 g. add_edge_with_type ( v, v1, EType :: H ) ;
397397 g. add_edge_with_type ( v1, v2, EType :: H ) ;
398398}
@@ -578,7 +578,7 @@ pub fn remove_pair_unchecked(g: &mut impl GraphLike, v0: V, v1: V) {
578578 * g. scalar_mut ( ) *= ScalarN :: one_plus_phase ( p0 + p1) ;
579579 // different colors
580580 } else {
581- let p2 = Rational :: one ( ) + & p0 + & p1;
581+ let p2 = Rational64 :: one ( ) + p0 + p1;
582582 * g. scalar_mut ( ) *= ScalarN :: one ( )
583583 + ScalarN :: from_phase ( p0)
584584 + ScalarN :: from_phase ( p1)
@@ -599,7 +599,7 @@ mod tests {
599599 use super :: * ;
600600 use crate :: tensor:: * ;
601601 use crate :: vec_graph:: Graph ;
602- use num:: Rational ;
602+ use num:: Rational64 ;
603603
604604 #[ test]
605605 fn spider_fusion_simple ( ) {
@@ -614,8 +614,8 @@ mod tests {
614614 g. add_vertex ( VType :: B ) ,
615615 ] ;
616616
617- g. set_phase ( vs[ 2 ] , Rational :: new ( 1 , 2 ) ) ;
618- g. set_phase ( vs[ 3 ] , Rational :: new ( 1 , 4 ) ) ;
617+ g. set_phase ( vs[ 2 ] , Rational64 :: new ( 1 , 2 ) ) ;
618+ g. set_phase ( vs[ 3 ] , Rational64 :: new ( 1 , 4 ) ) ;
619619
620620 g. add_edge ( vs[ 0 ] , vs[ 2 ] ) ;
621621 g. add_edge ( vs[ 1 ] , vs[ 2 ] ) ;
@@ -639,7 +639,7 @@ mod tests {
639639
640640 assert_eq ! ( g. to_tensor4( ) , h. to_tensor4( ) ) ;
641641
642- assert_eq ! ( g. phase( vs[ 2 ] ) , Rational :: new( 3 , 4 ) ) ;
642+ assert_eq ! ( g. phase( vs[ 2 ] ) , Rational64 :: new( 3 , 4 ) ) ;
643643 }
644644
645645 #[ test]
@@ -657,8 +657,8 @@ mod tests {
657657 g. add_vertex ( VType :: B ) ,
658658 ] ;
659659
660- g. set_phase ( vs[ 2 ] , Rational :: new ( 1 , 2 ) ) ;
661- g. set_phase ( vs[ 3 ] , Rational :: new ( 1 , 4 ) ) ;
660+ g. set_phase ( vs[ 2 ] , Rational64 :: new ( 1 , 2 ) ) ;
661+ g. set_phase ( vs[ 3 ] , Rational64 :: new ( 1 , 4 ) ) ;
662662
663663 g. add_edge ( vs[ 0 ] , vs[ 2 ] ) ;
664664 g. add_edge ( vs[ 1 ] , vs[ 2 ] ) ;
@@ -693,14 +693,14 @@ mod tests {
693693 println ! ( "\n \n th =\n {}" , th) ;
694694 assert_eq ! ( tg, th) ;
695695
696- assert_eq ! ( g. phase( vs[ 2 ] ) , Rational :: new( 3 , 4 ) ) ;
696+ assert_eq ! ( g. phase( vs[ 2 ] ) , Rational64 :: new( 3 , 4 ) ) ;
697697 }
698698
699699 #[ test]
700700 fn local_comp_1 ( ) {
701701 let mut g = Graph :: new ( ) ;
702702 g. add_vertex ( VType :: Z ) ;
703- g. set_phase ( 0 , Rational :: new ( 1 , 2 ) ) ;
703+ g. set_phase ( 0 , Rational64 :: new ( 1 , 2 ) ) ;
704704 g. add_vertex ( VType :: Z ) ;
705705 g. add_vertex ( VType :: Z ) ;
706706 g. add_vertex ( VType :: Z ) ;
@@ -738,12 +738,12 @@ mod tests {
738738 assert_eq ! ( tg, th) ;
739739
740740 for i in 1 ..5 {
741- assert_eq ! ( g. phase( i) , Rational :: new( -1 , 2 ) ) ;
741+ assert_eq ! ( g. phase( i) , Rational64 :: new( -1 , 2 ) ) ;
742742 }
743743
744744 assert_eq ! (
745745 * g. scalar( ) ,
746- Scalar :: sqrt2_pow( ( 4 - 1 ) * ( 4 - 2 ) / 2 ) * Scalar :: from_phase( Rational :: new( 1 , 4 ) )
746+ Scalar :: sqrt2_pow( ( 4 - 1 ) * ( 4 - 2 ) / 2 ) * Scalar :: from_phase( Rational64 :: new( 1 , 4 ) )
747747 ) ;
748748
749749 let h = g. clone ( ) ;
@@ -759,7 +759,7 @@ mod tests {
759759 for _ in 0 ..7 {
760760 g. add_vertex ( VType :: Z ) ;
761761 }
762- g. set_phase ( 3 , Rational :: new ( 1 , 1 ) ) ;
762+ g. set_phase ( 3 , Rational64 :: new ( 1 , 1 ) ) ;
763763 for i in 0 ..3 {
764764 g. add_edge_with_type ( i, 3 , EType :: H ) ;
765765 }
@@ -783,8 +783,8 @@ mod tests {
783783 assert_eq ! ( h. num_vertices( ) , 5 ) ;
784784 assert_eq ! ( h. num_edges( ) , 6 ) ;
785785
786- assert_eq ! ( h. phase( 0 ) , Rational :: new( 0 , 1 ) ) ;
787- assert_eq ! ( h. phase( 6 ) , Rational :: new( 1 , 1 ) ) ;
786+ assert_eq ! ( h. phase( 0 ) , Rational64 :: new( 0 , 1 ) ) ;
787+ assert_eq ! ( h. phase( 6 ) , Rational64 :: new( 1 , 1 ) ) ;
788788
789789 let mut inputs: Vec < usize > = Vec :: new ( ) ;
790790 let mut outputs: Vec < usize > = Vec :: new ( ) ;
@@ -816,8 +816,8 @@ mod tests {
816816 for _ in 0 ..7 {
817817 g. add_vertex ( VType :: Z ) ;
818818 }
819- g. set_phase ( 3 , Rational :: new ( 1 , 1 ) ) ;
820- g. set_phase ( 4 , Rational :: new ( 1 , 1 ) ) ;
819+ g. set_phase ( 3 , Rational64 :: new ( 1 , 1 ) ) ;
820+ g. set_phase ( 4 , Rational64 :: new ( 1 , 1 ) ) ;
821821 for i in 0 ..3 {
822822 g. add_edge_with_type ( i, 3 , EType :: H ) ;
823823 }
@@ -835,8 +835,8 @@ mod tests {
835835 assert_eq ! ( g. num_vertices( ) , 5 ) ;
836836 assert_eq ! ( g. num_edges( ) , 6 ) ;
837837
838- assert_eq ! ( g. phase( 0 ) , Rational :: new( 1 , 1 ) ) ;
839- assert_eq ! ( g. phase( 6 ) , Rational :: new( 1 , 1 ) ) ;
838+ assert_eq ! ( g. phase( 0 ) , Rational64 :: new( 1 , 1 ) ) ;
839+ assert_eq ! ( g. phase( 6 ) , Rational64 :: new( 1 , 1 ) ) ;
840840 }
841841
842842 #[ test]
@@ -847,8 +847,8 @@ mod tests {
847847 g. add_vertex ( VType :: Z ) ;
848848 }
849849 g. set_vertex_type ( 0 , VType :: B ) ;
850- // g.set_phase(3, Rational ::new(1,1));
851- // g.set_phase(4, Rational ::new(1,1));
850+ // g.set_phase(3, Rational64 ::new(1,1));
851+ // g.set_phase(4, Rational64 ::new(1,1));
852852 for i in 0 ..3 {
853853 g. add_edge_with_type ( i, 3 , EType :: H ) ;
854854 }
@@ -881,8 +881,8 @@ mod tests {
881881 for _ in 0 ..7 {
882882 g. add_vertex ( VType :: Z ) ;
883883 }
884- g. set_phase ( 3 , Rational :: new ( 1 , 1 ) ) ;
885- g. set_phase ( 4 , Rational :: new ( 1 , 4 ) ) ;
884+ g. set_phase ( 3 , Rational64 :: new ( 1 , 1 ) ) ;
885+ g. set_phase ( 4 , Rational64 :: new ( 1 , 4 ) ) ;
886886 for i in 0 ..3 {
887887 g. add_edge_with_type ( i, 3 , EType :: H ) ;
888888 }
@@ -911,51 +911,51 @@ mod tests {
911911 fn gadget_fusion_1 ( ) {
912912 // fuse gadgets of various sizes
913913 for n in 1 ..5 {
914- let mut g = Graph :: new ( ) ;
915- let bs: Vec < _ > = ( 0 ..n) . map ( |_| g . add_vertex ( VType :: B ) ) . collect ( ) ;
916- let vs: Vec < _ > = ( 0 ..n) . map ( |_| g . add_vertex ( VType :: Z ) ) . collect ( ) ;
917- let gs: Vec < _ > = ( 0 ..2 ) . map ( |_| g . add_vertex ( VType :: Z ) ) . collect ( ) ;
918- let ps: Vec < _ > = ( 0 ..2 ) . map ( |_| g . add_vertex ( VType :: Z ) ) . collect ( ) ;
919- g . set_inputs ( bs. clone ( ) ) ;
914+ let mut graph = Graph :: new ( ) ;
915+ let bs: Vec < _ > = ( 0 ..n) . map ( |_| graph . add_vertex ( VType :: B ) ) . collect ( ) ;
916+ let vs: Vec < _ > = ( 0 ..n) . map ( |_| graph . add_vertex ( VType :: Z ) ) . collect ( ) ;
917+ let gs: Vec < _ > = ( 0 ..2 ) . map ( |_| graph . add_vertex ( VType :: Z ) ) . collect ( ) ;
918+ let ps: Vec < _ > = ( 0 ..2 ) . map ( |_| graph . add_vertex ( VType :: Z ) ) . collect ( ) ;
919+ graph . set_inputs ( bs. clone ( ) ) ;
920920
921921 for i in 0 ..n {
922- g . add_edge ( bs[ i] , vs[ i] ) ;
922+ graph . add_edge ( bs[ i] , vs[ i] ) ;
923923 }
924- for j in 0 .. 2 {
925- g . add_edge_with_type ( gs [ j ] , ps [ j ] , EType :: H ) ;
926- for i in 0 ..n {
927- g . add_edge_with_type ( vs [ i ] , gs [ j ] , EType :: H ) ;
924+ for ( & g , & p ) in gs . iter ( ) . zip ( ps . iter ( ) ) {
925+ graph . add_edge_with_type ( g , p , EType :: H ) ;
926+ for & v in & vs {
927+ graph . add_edge_with_type ( v , g , EType :: H ) ;
928928 }
929929 }
930930
931- g . set_phase ( ps[ 0 ] , Rational :: new ( 1 , 4 ) ) ;
932- g . set_phase ( ps[ 1 ] , Rational :: new ( 1 , 2 ) ) ;
931+ graph . set_phase ( ps[ 0 ] , Rational64 :: new ( 1 , 4 ) ) ;
932+ graph . set_phase ( ps[ 1 ] , Rational64 :: new ( 1 , 2 ) ) ;
933933
934- let h = g . clone ( ) ;
934+ let h = graph . clone ( ) ;
935935
936- assert ! ( gadget_fusion( & mut g , gs[ 0 ] , gs[ 1 ] ) ) ;
937- assert ! ( g
938- . find_vertex( |v| g . phase( v) == Rational :: new( 3 , 4 ) )
936+ assert ! ( gadget_fusion( & mut graph , gs[ 0 ] , gs[ 1 ] ) ) ;
937+ assert ! ( graph
938+ . find_vertex( |v| graph . phase( v) == Rational64 :: new( 3 , 4 ) )
939939 . is_some( ) ) ;
940- assert ! ( g
941- . find_vertex( |v| g . phase( v) == Rational :: new( 1 , 4 ) )
940+ assert ! ( graph
941+ . find_vertex( |v| graph . phase( v) == Rational64 :: new( 1 , 4 ) )
942942 . is_none( ) ) ;
943- assert ! ( g
944- . find_vertex( |v| g . phase( v) == Rational :: new( 1 , 2 ) )
943+ assert ! ( graph
944+ . find_vertex( |v| graph . phase( v) == Rational64 :: new( 1 , 2 ) )
945945 . is_none( ) ) ;
946946 // println!("{}", g.to_tensor4());
947947 // println!("{}", h.to_tensor4());
948948 // println!("g = {} * \n {} \n\n", g.scalar(), g.to_dot());
949949 // println!("h = {} * \n {} \n\n", h.scalar(), h.to_dot());
950- assert_eq ! ( g . to_tensor4( ) , h. to_tensor4( ) ) ;
950+ assert_eq ! ( graph . to_tensor4( ) , h. to_tensor4( ) ) ;
951951 }
952952 }
953953
954954 #[ test]
955955 fn scalar_rules ( ) {
956956 for & t in & [ VType :: Z , VType :: X ] {
957957 let mut g = Graph :: new ( ) ;
958- g. add_vertex_with_phase ( t, Rational :: new ( 1 , 4 ) ) ;
958+ g. add_vertex_with_phase ( t, Rational64 :: new ( 1 , 4 ) ) ;
959959 let mut h = g. clone ( ) ;
960960 assert ! ( remove_single( & mut h, 0 ) ) ;
961961 assert_eq ! ( h. num_vertices( ) , 0 , "h still has vertices" ) ;
@@ -966,8 +966,8 @@ mod tests {
966966 for & t1 in & [ VType :: Z , VType :: X ] {
967967 for & et in & [ EType :: N , EType :: H ] {
968968 let mut g = Graph :: new ( ) ;
969- g. add_vertex_with_phase ( t0, Rational :: new ( 1 , 4 ) ) ;
970- g. add_vertex_with_phase ( t1, Rational :: new ( -1 , 2 ) ) ;
969+ g. add_vertex_with_phase ( t0, Rational64 :: new ( 1 , 4 ) ) ;
970+ g. add_vertex_with_phase ( t1, Rational64 :: new ( -1 , 2 ) ) ;
971971 g. add_edge_with_type ( 0 , 1 , et) ;
972972 let mut h = g. clone ( ) ;
973973 assert ! ( remove_pair( & mut h, 0 , 1 ) ) ;
0 commit comments