@@ -399,6 +399,41 @@ def test_todense(num_charges, chargetype):
399399 np .testing .assert_allclose (dense [inds2 ], 0 )
400400
401401
402+ @pytest .mark .parametrize ('chargetype' , ["U1" , "Z2" , "mixed" ])
403+ @pytest .mark .parametrize ('num_charges' , [1 , 2 , 3 ])
404+ def test_fromdense (num_charges , chargetype ):
405+ np .random .seed (10 )
406+ Ds = [8 , 9 , 10 , 11 ]
407+ rank = 4
408+ flows = np .random .choice ([True , False ], size = rank , replace = True )
409+ charges = [get_charge (chargetype , num_charges , Ds [n ]) for n in range (rank )]
410+ fused = fuse_charges (charges , flows )
411+ mask = fused == np .zeros ((num_charges , 1 ))
412+ inds = np .nonzero (mask )[0 ]
413+ inds2 = np .nonzero (np .logical_not (mask ))[0 ]
414+ indices = [Index (charges [n ], flows [n ]) for n in range (rank )]
415+
416+ dense = np .random .random_sample (Ds )
417+ arr = BlockSparseTensor .fromdense (indices , dense )
418+ dense_arr = arr .todense ()
419+
420+ np .testing .assert_allclose (np .ravel (dense )[inds ], arr .data )
421+ np .testing .assert_allclose (np .ravel (dense_arr )[inds2 ], 0 )
422+
423+
424+ def test_fromdense_raises ():
425+ np .random .seed (10 )
426+ Ds = [8 , 9 , 10 , 11 ]
427+ rank = len (Ds )
428+ flows = np .random .choice ([True , False ], size = rank , replace = True )
429+ charges = [U1Charge .random (Ds [n ], - 5 , 5 ) for n in range (rank )]
430+ indices = [Index (charges [n ], flows [n ]) for n in range (rank )]
431+
432+ dense = np .random .random_sample ([8 , 9 , 9 , 11 ])
433+ with pytest .raises (ValueError ):
434+ _ = BlockSparseTensor .fromdense (indices , dense )
435+
436+
402437@pytest .mark .parametrize ('chargetype' , ["U1" , "Z2" , "mixed" ])
403438@pytest .mark .parametrize ('num_charges' , [1 , 2 , 3 ])
404439@pytest .mark .parametrize ('op' , [np .add , np .subtract ])
0 commit comments