density matrix for batched register
Roger-luo opened this issue · comments
Hi, I up Yao from v0.6.5 to v0.7.4 today and I found that the density_matrix cannot handle BatchedArrayReg now as you discussed above. I had to go back to v0.6.5 and everything works. Is this an unsolved issue?
Originally posted by @LWKJJONAK in #334 (comment)
Hi, I posted this issue yesterday and now I've found a way to fix it.
Generally, I need to use density_matrix such as
reg_ = focus!(copy(reg) |> circuit, (nbit)) |> cpu
rdm = density_matrix(reg_).state
Now some properties changed and I don't want to change all my code written before, so I did this:
import YaoAPI.density_matrix
function dens_m(M::DensityMatrix)
M.state
end
struct density_matrix_
state::Array{ComplexF64, 3}
end
function density_matrix(reg::BatchedArrayReg)
temp = density_matrix.(reg)
density_matrix_(cat(dens_m.(temp)...;dims = 3))
end
In this way, everything works as before, and it even gets faster, from
@btime reg_ = focus!(copy(cu_x_train_yao) |> circuit, (nbit)) |> cpu
@btime temp = (density_matrix(reg_)).state;
931.037 ms (1705 allocations: 9.40 MiB)
671.466 μs (806 allocations: 162.72 KiB)
to
@btime reg_ = focus!(copy(cu_x_train_yao) |> circuit, (nbit)) |> cpu
@btime temp = (density_matrix(reg_)).state;
724.986 ms (2433 allocations: 9.59 MiB)
406.429 μs (1453 allocations: 152.80 KiB)
yeah, the 0.7 version breaks some of the APIs, that's why it's a new minor version. But you can always pirate the API to add patches for old code. I'm closing this issue then