Skip to content

Commit 034679b

Browse files
committed
Updated qdistrnd stuff. Threading is currently broken.
1 parent b4aeaf9 commit 034679b

File tree

2 files changed

+43
-44
lines changed

2 files changed

+43
-44
lines changed

src/Quantum/weight_dist.jl

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ function random_information_set_minimum_distance_bound!(::HasGauges, ::IsCSS, ::
661661
if which == :full
662662
verbose && println("Bounding the full dressed distance")
663663
stabs = _Flint_matrix_to_Julia_T_matrix(stabilizers(S), Int)
664-
_rref_no_col_swap!(stabs)
664+
_rref_no_col_swap_binary!(stabs)
665665
stabs = _remove_empty(stabs, :rows)
666666
gauges = _Flint_matrix_to_Julia_T_matrix(gauges_matrix(S), Int)
667667
logs = _Flint_matrix_to_Julia_T_matrix(logicals_matrix(S), Int)
@@ -672,15 +672,15 @@ function random_information_set_minimum_distance_bound!(::HasGauges, ::IsCSS, ::
672672

673673
# this is done in the constructor but the logical is not stored at the time
674674
# so must redo here
675-
mat = _rref_no_col_swap(operators_to_reduce)
675+
mat = _rref_no_col_swap_binary(operators_to_reduce)
676676
anti = mat * check_against
677677
curr_u_bound, index = findmin(row_wts_symplectic(mat[findall(!iszero(anti[i, :]) for i in axes(anti, 1)), :]))
678678
found = operators_to_reduce[index, :]
679679
verbose && println("Starting upper bound: $curr_u_bound")
680680
elseif which == :X
681681
verbose && println("Bounding the dressed X-distance")
682682
stabs = _Flint_matrix_to_Julia_T_matrix(stabilizers(S), Int)
683-
_rref_no_col_swap!(stabs)
683+
_rref_no_col_swap_binary!(stabs)
684684
stabs = _remove_empty(stabs, :rows)
685685
gauges = _Flint_matrix_to_Julia_T_matrix(reduce(vcat, [log[1][:, 1:n] for log in S.gauge_ops]), Int)
686686
logs = _Flint_matrix_to_Julia_T_matrix(reduce(vcat, [log[1][:, 1:n] for log in S.logicals]), Int)
@@ -691,15 +691,15 @@ function random_information_set_minimum_distance_bound!(::HasGauges, ::IsCSS, ::
691691

692692
# this is done in the constructor but the logical is not stored at the time
693693
# so must redo here
694-
mat = _rref_no_col_swap(operators_to_reduce)
694+
mat = _rref_no_col_swap_binary(operators_to_reduce)
695695
anti = mat * check_against
696696
curr_u_bound, index = findmin(row_wts_symplectic(mat[findall(!iszero(anti[i, :]) for i in axes(anti, 1)), :]))
697697
found = operators_to_reduce[index, :]
698698
verbose && println("Starting upper bound: $curr_u_bound")
699699
else
700700
verbose && println("Bounding the dressed Z-distance")
701701
stabs = _Flint_matrix_to_Julia_T_matrix(stabilizers(S), Int)
702-
_rref_no_col_swap!(stabs)
702+
_rref_no_col_swap_binary!(stabs)
703703
stabs = _remove_empty(stabs, :rows)
704704
gauges = _Flint_matrix_to_Julia_T_matrix(reduce(vcat, [log[2][:, n + 1:end] for log in S.gauge_ops]), Int)
705705
logs = _Flint_matrix_to_Julia_T_matrix(reduce(vcat, [log[2][:, n + 1:end] for log in S.logicals]), Int)
@@ -710,7 +710,7 @@ function random_information_set_minimum_distance_bound!(::HasGauges, ::IsCSS, ::
710710

711711
# this is done in the constructor but the logical is not stored at the time
712712
# so must redo here
713-
mat = _rref_no_col_swap(operators_to_reduce)
713+
mat = _rref_no_col_swap_binary(operators_to_reduce)
714714
anti = mat * check_against
715715
curr_u_bound, index = findmin(row_wts_symplectic(mat[findall(!iszero(anti[i, :]) for i in axes(anti, 1)), :]))
716716
found = operators_to_reduce[index, :]
@@ -720,7 +720,7 @@ function random_information_set_minimum_distance_bound!(::HasGauges, ::IsCSS, ::
720720
if which == :full
721721
verbose && println("Bounding the full bare distance")
722722
stabs = _Flint_matrix_to_Julia_T_matrix(stabilizers(S), Int)
723-
_rref_no_col_swap!(stabs)
723+
_rref_no_col_swap_binary!(stabs)
724724
stabs = _remove_empty(stabs, :rows)
725725
logs = _Flint_matrix_to_Julia_T_matrix(logicals_matrix(S), Int)
726726
operators_to_reduce = vcat(stabs, logs)
@@ -730,15 +730,15 @@ function random_information_set_minimum_distance_bound!(::HasGauges, ::IsCSS, ::
730730

731731
# this is done in the constructor but the logical is not stored at the time
732732
# so must redo here
733-
mat = _rref_no_col_swap(operators_to_reduce)
733+
mat = _rref_no_col_swap_binary(operators_to_reduce)
734734
anti = mat * check_against
735735
curr_u_bound, index = findmin(row_wts_symplectic(mat[findall(!iszero(anti[i, :]) for i in axes(anti, 1)), :]))
736736
found = operators_to_reduce[index, :]
737737
verbose && println("Starting upper bound: $curr_u_bound")
738738
elseif which == :X
739739
verbose && println("Bounding the bare X-distance")
740740
stabs = _Flint_matrix_to_Julia_T_matrix(stabilizers(S), Int)
741-
_rref_no_col_swap!(stabs)
741+
_rref_no_col_swap_binary!(stabs)
742742
stabs = _remove_empty(stabs, :rows)
743743
logs = _Flint_matrix_to_Julia_T_matrix(reduce(vcat, [log[1][:, 1:n] for log in S.logicals]), Int)
744744
operators_to_reduce = vcat(stabs, logs)
@@ -748,15 +748,15 @@ function random_information_set_minimum_distance_bound!(::HasGauges, ::IsCSS, ::
748748

749749
# this is done in the constructor but the logical is not stored at the time
750750
# so must redo here
751-
mat = _rref_no_col_swap(operators_to_reduce)
751+
mat = _rref_no_col_swap_binary(operators_to_reduce)
752752
anti = mat * check_against
753753
curr_u_bound, index = findmin(row_wts_symplectic(mat[findall(!iszero(anti[i, :]) for i in axes(anti, 1)), :]))
754754
found = operators_to_reduce[index, :]
755755
verbose && println("Starting upper bound: $curr_u_bound")
756756
else
757757
verbose && println("Bounding the bare Z-distance")
758758
stabs = _Flint_matrix_to_Julia_T_matrix(stabilizers(S), Int)
759-
_rref_no_col_swap!(stabs)
759+
_rref_no_col_swap_binary!(stabs)
760760
stabs = _remove_empty(stabs, :rows)
761761
logs = _Flint_matrix_to_Julia_T_matrix(reduce(vcat, [log[2][:, n + 1:end] for log in S.logicals]), Int)
762762
operators_to_reduce = vcat(stabs, logs)
@@ -766,7 +766,7 @@ function random_information_set_minimum_distance_bound!(::HasGauges, ::IsCSS, ::
766766

767767
# this is done in the constructor but the logical is not stored at the time
768768
# so must redo here
769-
mat = _rref_no_col_swap(operators_to_reduce)
769+
mat = _rref_no_col_swap_binary(operators_to_reduce)
770770
anti = mat * check_against
771771
curr_u_bound, index = findmin(row_wts_symplectic(mat[findall(!iszero(anti[i, :]) for i in axes(anti, 1)), :]))
772772
found = operators_to_reduce[index, :]
@@ -804,9 +804,9 @@ function random_information_set_minimum_distance_bound!(::HasNoGauges, ::IsCSS,
804804
n = S.n
805805
if which == :full
806806
verbose && println("Bounding the full distance")
807-
# println(_rref_no_col_swap!(_Flint_matrix_to_Julia_T_matrix(stabilizers(S), Bool)))
807+
# println(_rref_no_col_swap_binary!(_Flint_matrix_to_Julia_T_matrix(stabilizers(S), Bool)))
808808
stabs = _Flint_matrix_to_Julia_T_matrix(stabilizers(S), Int)
809-
_rref_no_col_swap!(stabs)
809+
_rref_no_col_swap_binary!(stabs)
810810
stabs = _remove_empty(stabs, :rows)
811811
logs = _Flint_matrix_to_Julia_T_matrix(logicals_matrix(S), Int)
812812
operators_to_reduce = vcat(stabs, logs)
@@ -816,7 +816,7 @@ function random_information_set_minimum_distance_bound!(::HasNoGauges, ::IsCSS,
816816

817817
# this is done in the constructor but the logical is not stored at the time
818818
# so must redo here
819-
mat = _rref_no_col_swap(operators_to_reduce)
819+
mat = _rref_no_col_swap_binary(operators_to_reduce)
820820
anti = mat * check_against .% order(field(S))
821821
log_locations = findall(!iszero(anti[i, :]) for i in axes(anti, 1))
822822
curr_u_bound, index = findmin(row_wts_symplectic(mat[log_locations, :]))
@@ -825,7 +825,7 @@ function random_information_set_minimum_distance_bound!(::HasNoGauges, ::IsCSS,
825825
elseif which == :X
826826
verbose && println("Bounding the X-distance")
827827
stabs = _Flint_matrix_to_Julia_T_matrix(stabilizers(S), Int)
828-
_rref_no_col_swap!(stabs)
828+
_rref_no_col_swap_binary!(stabs)
829829
stabs = _remove_empty(stabs, :rows)
830830
logs = _Flint_matrix_to_Julia_T_matrix(reduce(vcat, [log[1][:, 1:n] for log in S.logicals]), Int)
831831
operators_to_reduce = vcat(stabs, logs)
@@ -835,15 +835,15 @@ function random_information_set_minimum_distance_bound!(::HasNoGauges, ::IsCSS,
835835

836836
# this is done in the constructor but the logical is not stored at the time
837837
# so must redo here
838-
mat = _rref_no_col_swap(operators_to_reduce)
838+
mat = _rref_no_col_swap_binary(operators_to_reduce)
839839
anti = mat * check_against
840840
curr_u_bound, index = findmin(row_wts_symplectic(mat[findall(!iszero(anti[i, :]) for i in axes(anti, 1)), :]))
841841
found = operators_to_reduce[index, :]
842842
verbose && println("Starting upper bound: $curr_u_bound")
843843
else
844844
verbose && println("Bounding the Z-distance")
845845
stabs = _Flint_matrix_to_Julia_T_matrix(stabilizers(S), Int)
846-
_rref_no_col_swap!(stabs)
846+
_rref_no_col_swap_binary!(stabs)
847847
stabs = _remove_empty(stabs, :rows)
848848
logs = _Flint_matrix_to_Julia_T_matrix(reduce(vcat, [log[2][:, n + 1:end] for log in S.logicals]), Int)
849849
operators_to_reduce = vcat(stabs, logs)
@@ -853,7 +853,7 @@ function random_information_set_minimum_distance_bound!(::HasNoGauges, ::IsCSS,
853853

854854
# this is done in the constructor but the logical is not stored at the time
855855
# so must redo here
856-
mat = _rref_no_col_swap(operators_to_reduce)
856+
mat = _rref_no_col_swap_binary(operators_to_reduce)
857857
anti = mat * check_against
858858
curr_u_bound, index = findmin(row_wts_symplectic(mat[findall(!iszero(anti[i, :]) for i in axes(anti, 1)), :]))
859859
found = operators_to_reduce[index, :]
@@ -883,7 +883,7 @@ function random_information_set_minimum_distance_bound!(::HasGauges, ::IsNotCSS,
883883
if dressed
884884
verbose && println("Bounding the full dressed distance")
885885
stabs = _Flint_matrix_to_Julia_T_matrix(stabilizers(S), Int)
886-
_rref_no_col_swap!(stabs)
886+
_rref_no_col_swap_binary!(stabs)
887887
stabs = _remove_empty(stabs, :rows)
888888
gauges = _Flint_matrix_to_Julia_T_matrix(gauges_matrix(S), Int)
889889
logs = _Flint_matrix_to_Julia_T_matrix(logicals_matrix(S), Int)
@@ -894,15 +894,15 @@ function random_information_set_minimum_distance_bound!(::HasGauges, ::IsNotCSS,
894894

895895
# this is done in the constructor but the logical is not stored at the time
896896
# so must redo here
897-
mat = _rref_no_col_swap(operators_to_reduce)
897+
mat = _rref_no_col_swap_binary(operators_to_reduce)
898898
anti = mat * check_against
899899
curr_u_bound, index = findmin(row_wts_symplectic(mat[findall(!iszero(anti[i, :]) for i in axes(anti, 1)), :]))
900900
found = operators_to_reduce[index, :]
901901
verbose && println("Starting upper bound: $curr_u_bound")
902902
else
903903
verbose && println("Bounding the full bare distance")
904904
stabs = _Flint_matrix_to_Julia_T_matrix(stabilizers(S), Int)
905-
_rref_no_col_swap!(stabs)
905+
_rref_no_col_swap_binary!(stabs)
906906
stabs = _remove_empty(stabs, :rows)
907907
logs = _Flint_matrix_to_Julia_T_matrix(logicals_matrix(S), Int)
908908
operators_to_reduce = vcat(stabs, logs)
@@ -912,7 +912,7 @@ function random_information_set_minimum_distance_bound!(::HasGauges, ::IsNotCSS,
912912

913913
# this is done in the constructor but the logical is not stored at the time
914914
# so must redo here
915-
mat = _rref_no_col_swap(operators_to_reduce)
915+
mat = _rref_no_col_swap_binary(operators_to_reduce)
916916
anti = mat * check_against
917917
curr_u_bound, index = findmin(row_wts_symplectic(mat[findall(!iszero(anti[i, :]) for i in axes(anti, 1)), :]))
918918
found = operators_to_reduce[index, :]
@@ -939,7 +939,7 @@ function random_information_set_minimum_distance_bound!(::HasNoGauges, ::IsNotCS
939939
n = S.n
940940
verbose && println("Bounding the full distance")
941941
stabs = _Flint_matrix_to_Julia_T_matrix(stabilizers(S), Int)
942-
_rref_no_col_swap!(stabs)
942+
_rref_no_col_swap_binary!(stabs)
943943
stabs = _remove_empty(stabs, :rows)
944944
logs = _Flint_matrix_to_Julia_T_matrix(logicals_matrix(S), Int)
945945
operators_to_reduce = vcat(stabs, logs)
@@ -949,7 +949,7 @@ function random_information_set_minimum_distance_bound!(::HasNoGauges, ::IsNotCS
949949

950950
# this is done in the constructor but the logical is not stored at the time
951951
# so must redo here
952-
mat = _rref_no_col_swap(operators_to_reduce)
952+
mat = _rref_no_col_swap_binary(operators_to_reduce)
953953
anti = mat * check_against
954954
curr_u_bound, index = findmin(row_wts_symplectic(mat[findall(!iszero(anti[i, :]) for i in axes(anti, 1)), :]))
955955
found = operators_to_reduce[index, :]
@@ -977,33 +977,33 @@ function _RIS_bound_loop!(operators_to_reduce, check_against, curr_l_bound::Int,
977977
uppers = [curr_u_bound for _ in 1:num_thrds]
978978
founds = [found for _ in 1:num_thrds]
979979
thread_load = Int(floor(max_iters / num_thrds))
980-
Threads.@threads for t in 1:num_thrds
980+
# Threads.@threads for t in 1:num_thrds
981+
for t in 1:num_thrds
981982
log_test = zeros(Int, size(operators_to_reduce, 1), size(check_against, 2))
982983
for _ in 1:thread_load
983984
if flag[]
984985
perm = shuffle(1:n)
985986
perm2 = [perm; perm .+ n]
986987
perm_ops = operators_to_reduce[:, perm2]
987-
_rref_no_col_swap!(perm_ops)
988+
_rref_no_col_swap_binary!(perm_ops)
988989
ops = perm_ops[:, invperm(perm2)]
989990
LinearAlgebra.mul!(log_test, ops, check_against)
990-
log_test .%= 2
991991
for i in axes(log_test, 1)
992-
# then perm_ops[i, :] is a logical
993-
if any(!iszero, log_test[i, :])
992+
# then ops[i, :] is a logical
993+
if any(isodd, log_test[i, :])
994994
w = 0
995995
@inbounds for j in 1:n
996-
!iszero(ops[i, j] % 2) && !iszero(ops[i, j + n] % 2) || (w += 1;)
996+
(isodd(ops[i, j]) || isodd(ops[i, j + n])) && (w += 1;)
997997
end
998-
998+
999999
if uppers[t] > w
10001000
uppers[t] = w
10011001
# maybe use invpermute! here?
10021002
founds[t] .= ops[i, :]
10031003
verbose && println("Adjusting upper bound: $w")
10041004
if curr_l_bound == w
10051005
verbose && println("Found a logical that matched the lower bound of $curr_l_bound")
1006-
Threads.atomic_cas!(flag, true, false)
1006+
# Threads.atomic_cas!(flag, true, false)
10071007
break
10081008
end
10091009
end
@@ -1025,20 +1025,20 @@ function _RIS_bound_loop!(operators_to_reduce, check_against, curr_l_bound::Int,
10251025
perm = shuffle(1:n)
10261026
perm2 = [perm; perm .+ n]
10271027
perm_ops = operators_to_reduce[:, perm2]
1028-
_rref_no_col_swap!(perm_ops)
1028+
_rref_no_col_swap_binary!(perm_ops)
1029+
ops = perm_ops[:, invperm(perm2)]
10291030
LinearAlgebra.mul!(log_test, ops, check_against)
1030-
log_test .%= 2
10311031
for i in axes(log_test, 1)
1032-
# then perm_ops[i, :] is a logical
1033-
if any(!iszero, log_test[i, :])
1032+
# then ops[i, :] is a logical
1033+
if any(isodd, log_test[i, :])
10341034
w = 0
10351035
@inbounds for j in 1:n
1036-
!iszero(ops[i, j] % 2) && !iszero(ops[i, j + n] % 2) || (w += 1;)
1036+
(isodd(ops[i, j]) || isodd(ops[i, j + n])) && (w += 1;)
10371037
end
10381038

10391039
if upper_temp > w
10401040
upper_temp = w
1041-
found_temp = ops[i, :]
1041+
found_temp .= ops[i, :]
10421042
verbose && println("Adjusting upper bound: $w")
10431043
if curr_l_bound == w
10441044
verbose && println("Found a logical that matched the lower bound of $curr_l_bound")

src/utils.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,11 @@ end
433433
_rref_no_col_swap(M::CTMatrixTypes, row_range::Base.OneTo{Int}, col_range::Base.OneTo{Int}) = _rref_no_col_swap(M, 1:row_range.stop, 1:col_range.stop)
434434
_rref_no_col_swap(M::CTMatrixTypes) = _rref_no_col_swap(M, axes(M, 1), axes(M, 2))
435435

436-
function _rref_no_col_swap(A::Union{BitMatrix, Matrix{Bool}, Matrix{<: Integer}},
436+
function _rref_no_col_swap_binary(A::Union{BitMatrix, Matrix{Bool}, Matrix{<: Integer}},
437437
row_range::UnitRange{Int} = 1:size(A, 1), col_range::UnitRange{Int} = 1:size(A, 2))
438438

439439
B = copy(A)
440-
_rref_no_col_swap!(B, row_range, col_range)
440+
_rref_no_col_swap_binary!(B, row_range, col_range)
441441
return B
442442
end
443443

@@ -522,7 +522,7 @@ function _rref_no_col_swap!(A::CTMatrixTypes, row_range::UnitRange{Int}, col_ran
522522
return nothing
523523
end
524524

525-
function _rref_no_col_swap!(A::Union{BitMatrix, Matrix{Bool}, Matrix{<: Integer}},
525+
function _rref_no_col_swap_binary!(A::Union{BitMatrix, Matrix{Bool}, Matrix{<: Integer}},
526526
row_range::UnitRange{Int} = 1:size(A, 1), col_range::UnitRange{Int} = 1:size(A, 2))
527527

528528
isempty(row_range) && return nothing
@@ -553,8 +553,7 @@ function _rref_no_col_swap!(A::Union{BitMatrix, Matrix{Bool}, Matrix{<: Integer}
553553
if !iszero(A[k, j])
554554
# do a manual loop here to reduce allocations
555555
@simd for l in axes(A, 2)
556-
# A[k, l] ⊻= A[i, l]
557-
A[k, l] = (A[k, l] + A[i, l]) % 2
556+
A[k, l] ⊻= A[i, l]
558557
end
559558
end
560559
end

0 commit comments

Comments
 (0)