QuantumBFS / Yao.jl

Extensible, Efficient Quantum Algorithm Design for Humans.
https://yaoquantum.org
Other
934 stars 123 forks source link

Converting between KronBlock, Scale, and Add Objects #522

Closed p-luo closed 2 months ago

p-luo commented 2 months ago

I have defined a quantum circuit circuit using chain and want to run expect'(ham, Yao.zero_state(nq) => circuit). However, if I define my hamiltonian to be a KronBlock or a Scale object, by e.g.

using Yao
nq = 10
ham = kron(nq, 1 => Z)
ham |> typeof 

Output: KronBlock{2, 1, Tuple{ZGate}}

I cannot pass it through the desired function, and it seems like it only takes Add objects as input. I am able to bypass this issue but doing ham = ham + 0*kron(nq, 1 => Z), but am wondering if there is a more elegant way to convert between these objects.

exAClior commented 2 months ago

Hello, I cannot reproduce your issue with the following code. Did I miss-understood your mininmal working example? If this persists, could you please send the version of your Yao.jl?

nq = 10
qc = chain(nq,control(nq,4,3=>X))
ham = kron(nq, 1 => Z)
grad = expect'(ham, zero_state(nq)=>qc)
res = expect(ham, zero_state(nq)=>qc)
exAClior commented 2 months ago

Closing due to inactivity

p-luo commented 2 months ago

Hi,

Please see below for a MWE. Also, it's not an error per se but I am just not sure how to convert between different objects from the Yao package in a clean way. Thanks!

using Yao

nq = 10
layers = 2
circuit = chain(nq)
for _ in 1:layers
    for i in 1:nq-1
        push!(circuit, rot(kron(nq, i => X, i+1 => X), 0.))
    end
    for i in 1:nq-1
        push!(circuit, rot(kron(nq, i => X, i+1 => Y), 0.))
    end
    for i in 1:nq
        push!(circuit, put(nq, i => Rz(0.)))
    end
end

ham = kron(nq, 1 => Z)
ham |> typeof 
exAClior commented 2 months ago

Sorry for misunderstanding your question. To convert between Add blocks and other AbstractBlocks, you could simply do Add(blk) with blk = kron(nq,1=>X) for example